Skip to content

pg_json_diff

JSONB diff, JSON Patch, and JSON Merge Patch functions

pg_json_diff · JSONB diff, JSON Patch, and JSON Merge Patch functions

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4160pg_json_diffpg_json_diff1.0UTILMITC++
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
Relationships
See Alsopgjq pgbson jsquery
Source requires PostgreSQL 12+ and C++17. Packaging removes the unsupported control-file author field; no extension dependency.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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
PackageVersionOSORGSIZEFile URL
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

Source

pig build pkg pg_json_diff;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with 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 this extension with:

CREATE EXTENSION pg_json_diff;

Usage

Sources:

pg_json_diff 1.0 provides three immutable JSONB transformation functions backed by nlohmann/json: generate an RFC 6902 JSON Patch, apply such a patch, or apply an RFC 7386 merge patch. Use it for explicit document transformations, not as automatic conflict resolution or a stable audit-diff format.

Core Workflow

Generate a patch, store or inspect it, and verify that applying it produces the expected target:

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;

Validate externally supplied patches before applying them and run the transformation in a transaction with application-level authorization.

Function Index

  • generate_json_diff(source, target) returns an RFC 6902 operation array.
  • apply_patch(document, patch) supports add, remove, replace, move, copy, and test operations.
  • merge_patch(document, patch) recursively merges objects; a null member removes a key and arrays are replaced rather than merged.

All three functions accept and return jsonb.

Operational Notes

Each call serializes PostgreSQL JSONB to text, parses it into a C++ JSON value, performs the operation, serializes again, and reparses the result as JSONB. Large or deeply nested documents therefore incur copying and parser cost. nlohmann/json and PostgreSQL JSONB have different numeric models, so test very large integers and high-precision decimals for fidelity. Patch errors are surfaced as general internal errors, and generated operation order or paths should not be treated as a canonical human audit record.

Table reader