跳转到主要内容

pg_json_diff

JSONB 差异比较、JSON Patch 与 JSON Merge Patch 函数

pg_json_diff · JSONB 差异比较、JSON Patch 与 JSON Merge Patch 函数

概览

ID扩展软件包版本类别许可证编程语言
4160pg_json_diffpg_json_diff1.0UTILMITC++
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d-r否是否是是否
关联
相关扩展pgjq pgbson jsquery
Source requires PostgreSQL 12+ and C++17. Packaging removes the unsupported control-file author field; no extension dependency.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.019 18 17 16 15pg_json_diff-
RPMPIGSTY1.019 18 17 16 15pg_json_diff_$v-
DEBPIGSTY1.019 18 17 16 15postgresql-$v-pg-json-diff-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el8.aarch64N/APIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el9.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el9.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el10.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el10.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d12.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d12.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d13.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d13.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u22.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u22.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u24.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u24.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u26.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u26.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
软件包版本平台仓库大小下载
pg_json_diff_191.0el9.x86_64pigsty309.3 KiBpg_json_diff_19-1.0-1PGSTY.el9.x86_64.rpm
pg_json_diff_191.0el9.aarch64pigsty305.3 KiBpg_json_diff_19-1.0-1PGSTY.el9.aarch64.rpm
pg_json_diff_191.0el10.x86_64pigsty318.8 KiBpg_json_diff_19-1.0-1PGSTY.el10.x86_64.rpm
pg_json_diff_191.0el10.aarch64pigsty310.2 KiBpg_json_diff_19-1.0-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-json-diff1.0d12.x86_64pigsty280.0 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-json-diff1.0d12.aarch64pigsty273.6 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-json-diff1.0d13.x86_64pigsty288.7 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~trixie_amd64.deb
postgresql-19-pg-json-diff1.0d13.aarch64pigsty280.6 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~trixie_arm64.deb
postgresql-19-pg-json-diff1.0u22.x86_64pigsty295.1 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~jammy_amd64.deb
postgresql-19-pg-json-diff1.0u22.aarch64pigsty292.0 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~jammy_arm64.deb
postgresql-19-pg-json-diff1.0u24.x86_64pigsty298.8 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~noble_amd64.deb
postgresql-19-pg-json-diff1.0u24.aarch64pigsty294.8 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~noble_arm64.deb
postgresql-19-pg-json-diff1.0u26.x86_64pigsty300.2 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~resolute_amd64.deb
postgresql-19-pg-json-diff1.0u26.aarch64pigsty292.8 KiBpostgresql-19-pg-json-diff_1.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_json_diff_181.0el8.x86_64pigsty305.8 KiBpg_json_diff_18-1.0-1PGSTY.el8.x86_64.rpm
pg_json_diff_181.0el8.aarch64pigsty296.1 KiBpg_json_diff_18-1.0-1PGSTY.el8.aarch64.rpm
pg_json_diff_181.0el9.x86_64pigsty309.3 KiBpg_json_diff_18-1.0-1PGSTY.el9.x86_64.rpm
pg_json_diff_181.0el9.aarch64pigsty305.2 KiBpg_json_diff_18-1.0-1PGSTY.el9.aarch64.rpm
pg_json_diff_181.0el10.x86_64pigsty318.8 KiBpg_json_diff_18-1.0-1PGSTY.el10.x86_64.rpm
pg_json_diff_181.0el10.aarch64pigsty310.2 KiBpg_json_diff_18-1.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-json-diff1.0d12.x86_64pigsty280.0 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-json-diff1.0d12.aarch64pigsty273.5 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-json-diff1.0d13.x86_64pigsty288.7 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-json-diff1.0d13.aarch64pigsty280.9 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-json-diff1.0u22.x86_64pigsty295.1 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-json-diff1.0u22.aarch64pigsty292.0 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-json-diff1.0u24.x86_64pigsty298.9 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-json-diff1.0u24.aarch64pigsty294.8 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-json-diff1.0u26.x86_64pigsty300.2 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-json-diff1.0u26.aarch64pigsty292.8 KiBpostgresql-18-pg-json-diff_1.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_json_diff_171.0el8.x86_64pigsty305.7 KiBpg_json_diff_17-1.0-1PGSTY.el8.x86_64.rpm
pg_json_diff_171.0el8.aarch64pigsty296.0 KiBpg_json_diff_17-1.0-1PGSTY.el8.aarch64.rpm
pg_json_diff_171.0el9.x86_64pigsty309.2 KiBpg_json_diff_17-1.0-1PGSTY.el9.x86_64.rpm
pg_json_diff_171.0el9.aarch64pigsty305.1 KiBpg_json_diff_17-1.0-1PGSTY.el9.aarch64.rpm
pg_json_diff_171.0el10.x86_64pigsty318.4 KiBpg_json_diff_17-1.0-1PGSTY.el10.x86_64.rpm
pg_json_diff_171.0el10.aarch64pigsty309.8 KiBpg_json_diff_17-1.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-json-diff1.0d12.x86_64pigsty280.4 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-json-diff1.0d12.aarch64pigsty274.0 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-json-diff1.0d13.x86_64pigsty288.7 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-json-diff1.0d13.aarch64pigsty280.6 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-json-diff1.0u22.x86_64pigsty327.2 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-json-diff1.0u22.aarch64pigsty324.3 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-json-diff1.0u24.x86_64pigsty298.7 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-json-diff1.0u24.aarch64pigsty295.0 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-json-diff1.0u26.x86_64pigsty299.6 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-json-diff1.0u26.aarch64pigsty292.5 KiBpostgresql-17-pg-json-diff_1.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_json_diff_161.0el8.x86_64pigsty305.7 KiBpg_json_diff_16-1.0-1PGSTY.el8.x86_64.rpm
pg_json_diff_161.0el8.aarch64pigsty295.9 KiBpg_json_diff_16-1.0-1PGSTY.el8.aarch64.rpm
pg_json_diff_161.0el9.x86_64pigsty309.2 KiBpg_json_diff_16-1.0-1PGSTY.el9.x86_64.rpm
pg_json_diff_161.0el9.aarch64pigsty305.1 KiBpg_json_diff_16-1.0-1PGSTY.el9.aarch64.rpm
pg_json_diff_161.0el10.x86_64pigsty318.4 KiBpg_json_diff_16-1.0-1PGSTY.el10.x86_64.rpm
pg_json_diff_161.0el10.aarch64pigsty309.7 KiBpg_json_diff_16-1.0-1PGSTY.el10.aarch64.rpm
postgresql-16-pg-json-diff1.0d12.x86_64pigsty280.4 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~bookworm_amd64.deb
postgresql-16-pg-json-diff1.0d12.aarch64pigsty273.9 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~bookworm_arm64.deb
postgresql-16-pg-json-diff1.0d13.x86_64pigsty288.6 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~trixie_amd64.deb
postgresql-16-pg-json-diff1.0d13.aarch64pigsty280.5 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~trixie_arm64.deb
postgresql-16-pg-json-diff1.0u22.x86_64pigsty327.1 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~jammy_amd64.deb
postgresql-16-pg-json-diff1.0u22.aarch64pigsty324.3 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~jammy_arm64.deb
postgresql-16-pg-json-diff1.0u24.x86_64pigsty298.7 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~noble_amd64.deb
postgresql-16-pg-json-diff1.0u24.aarch64pigsty295.0 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~noble_arm64.deb
postgresql-16-pg-json-diff1.0u26.x86_64pigsty299.4 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~resolute_amd64.deb
postgresql-16-pg-json-diff1.0u26.aarch64pigsty292.5 KiBpostgresql-16-pg-json-diff_1.0-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
pg_json_diff_151.0el8.x86_64pigsty307.0 KiBpg_json_diff_15-1.0-1PGSTY.el8.x86_64.rpm
pg_json_diff_151.0el8.aarch64pigsty297.0 KiBpg_json_diff_15-1.0-1PGSTY.el8.aarch64.rpm
pg_json_diff_151.0el9.x86_64pigsty309.7 KiBpg_json_diff_15-1.0-1PGSTY.el9.x86_64.rpm
pg_json_diff_151.0el9.aarch64pigsty305.3 KiBpg_json_diff_15-1.0-1PGSTY.el9.aarch64.rpm
pg_json_diff_151.0el10.x86_64pigsty319.1 KiBpg_json_diff_15-1.0-1PGSTY.el10.x86_64.rpm
pg_json_diff_151.0el10.aarch64pigsty310.0 KiBpg_json_diff_15-1.0-1PGSTY.el10.aarch64.rpm
postgresql-15-pg-json-diff1.0d12.x86_64pigsty285.1 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~bookworm_amd64.deb
postgresql-15-pg-json-diff1.0d12.aarch64pigsty278.2 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~bookworm_arm64.deb
postgresql-15-pg-json-diff1.0d13.x86_64pigsty293.1 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~trixie_amd64.deb
postgresql-15-pg-json-diff1.0d13.aarch64pigsty284.1 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~trixie_arm64.deb
postgresql-15-pg-json-diff1.0u22.x86_64pigsty327.6 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~jammy_amd64.deb
postgresql-15-pg-json-diff1.0u22.aarch64pigsty324.6 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~jammy_arm64.deb
postgresql-15-pg-json-diff1.0u24.x86_64pigsty299.0 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~noble_amd64.deb
postgresql-15-pg-json-diff1.0u24.aarch64pigsty295.2 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~noble_arm64.deb
postgresql-15-pg-json-diff1.0u26.x86_64pigsty299.9 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~resolute_amd64.deb
postgresql-15-pg-json-diff1.0u26.aarch64pigsty292.6 KiBpostgresql-15-pg-json-diff_1.0-1PGSTY~resolute_arm64.deb

源码

pg_json_diff-1.0-95cd668636ce.tar.gz

pig build pkg pg_json_diff;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install pg_json_diff;		# install by package name

pig install pg_json_diff -v 19;   # install for PG 19
pig install pg_json_diff -v 18;   # install for PG 18
pig install pg_json_diff -v 17;   # install for PG 17
pig install pg_json_diff -v 16;   # install for PG 16
pig install pg_json_diff -v 15;   # install for PG 15

在数据库中创建扩展。

CREATE EXTENSION pg_json_diff;

用法

来源:

pg_json_diff 1.0 提供三个基于 nlohmann/json 的不可变 JSONB 转换函数:生成 RFC 6902 JSON Patch、应用这种补丁,或应用 RFC 7386 合并补丁。它适合明确的文档转换,不是自动冲突解决器,也不是稳定的审计差异格式。

核心流程

先生成补丁,保存或检查它,再验证应用补丁后是否得到预期目标:

CREATE EXTENSION pg_json_diff;

WITH docs AS (
  SELECT '{"name":"Ada","age":30}'::jsonb AS old_doc,
         '{"name":"Ada","age":31,"city":"London"}'::jsonb AS new_doc
), patch AS (
  SELECT old_doc, new_doc, generate_json_diff(old_doc, new_doc) AS operations
  FROM docs
)
SELECT operations, apply_patch(old_doc, operations) = new_doc AS verified
FROM patch;

应用外部提供的补丁前应先验证,并在事务及应用级授权保护下执行转换。

函数索引

  • generate_json_diff(source, target) 返回 RFC 6902 操作数组。
  • apply_patch(document, patch) 支持添加、删除、替换、移动、复制与测试操作。
  • merge_patch(document, patch) 递归合并对象;空值成员删除键,数组则整体替换而不是合并。

三个函数都接受并返回 jsonb。

运维说明

每次调用都会把 PostgreSQL JSONB 序列化为文本,解析成 C++ JSON 值,执行操作,再次序列化并重新解析为 JSONB。因此,大型或深层嵌套文档会产生复制与解析开销。nlohmann/json 与 PostgreSQL JSONB 的数值模型不同,应测试超大整数和高精度小数的保真度。补丁错误会以通用内部错误返回;生成的操作顺序与路径也不应当作规范化的人类审计记录。

表格阅读器