diff options
author | Noah Misch <noah@leadboat.com> | 2025-07-31 06:37:56 -0700 |
---|---|---|
committer | Noah Misch <noah@leadboat.com> | 2025-07-31 06:38:03 -0700 |
commit | 04bc2c42f765f16e1df4d1504b871d638d6b47e8 (patch) | |
tree | 99a22f6a0cfffcdf37a421e056ccf5dcd7e5432f /src/bin/pg_dump/pg_dump.c | |
parent | cc9a62c51a1894a9c838becf558abb056c904fb1 (diff) |
Sort dump objects independent of OIDs, for the 7 holdout object types.
pg_dump sorts objects by their logical names, e.g. (nspname, relname,
tgname), before dependency-driven reordering. That removes one source
of logically-identical databases differing in their schema-only dumps.
In other words, it helps with schema diffing. The logical name sort
ignored essential sort keys for constraints, operators, PUBLICATION
... FOR TABLE, PUBLICATION ... FOR TABLES IN SCHEMA, operator classes,
and operator families. pg_dump's sort then depended on object OID,
yielding spurious schema diffs. After this change, OIDs affect dump
order only in the event of catalog corruption. While pg_dump also
wrongly ignored pg_collation.collencoding, CREATE COLLATION restrictions
have been keeping that imperceptible in practical use.
Use techniques like we use for object types already having full sort key
coverage. Where the pertinent queries weren't fetching the ignored sort
keys, this adds columns to those queries and stores those keys in memory
for the long term.
The ignorance of sort keys became more problematic when commit
172259afb563d35001410dc6daad78b250924038 added a schema diff test
sensitive to it. Buildfarm member hippopotamus witnessed that.
However, dump order stability isn't a new goal, and this might avoid
other dump comparison failures. Hence, back-patch to v13 (all supported
versions).
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Discussion: https://postgr.es/m/20250707192654.9e.nmisch@google.com
Backpatch-through: 13
Diffstat (limited to 'src/bin/pg_dump/pg_dump.c')
-rw-r--r-- | src/bin/pg_dump/pg_dump.c | 79 |
1 files changed, 61 insertions, 18 deletions
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 4b13669f450..29d37158a38 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -1767,6 +1767,13 @@ selectDumpableProcLang(ProcLangInfo *plang, Archive *fout) static void selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout) { + /* see getAccessMethods() comment about v9.6. */ + if (fout->remoteVersion < 90600) + { + method->dobj.dump = DUMP_COMPONENT_NONE; + return; + } + if (checkExtensionMembership(&method->dobj, fout)) return; /* extension membership overrides all else */ @@ -5325,6 +5332,8 @@ getOperators(Archive *fout, int *numOprs) int i_oprnamespace; int i_rolname; int i_oprkind; + int i_oprleft; + int i_oprright; int i_oprcode; /* @@ -5336,6 +5345,8 @@ getOperators(Archive *fout, int *numOprs) "oprnamespace, " "(%s oprowner) AS rolname, " "oprkind, " + "oprleft, " + "oprright, " "oprcode::oid AS oprcode " "FROM pg_operator", username_subquery); @@ -5353,6 +5364,8 @@ getOperators(Archive *fout, int *numOprs) i_oprnamespace = PQfnumber(res, "oprnamespace"); i_rolname = PQfnumber(res, "rolname"); i_oprkind = PQfnumber(res, "oprkind"); + i_oprleft = PQfnumber(res, "oprleft"); + i_oprright = PQfnumber(res, "oprright"); i_oprcode = PQfnumber(res, "oprcode"); for (i = 0; i < ntups; i++) @@ -5367,6 +5380,8 @@ getOperators(Archive *fout, int *numOprs) atooid(PQgetvalue(res, i, i_oprnamespace))); oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname)); oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0]; + oprinfo[i].oprleft = atooid(PQgetvalue(res, i, i_oprleft)); + oprinfo[i].oprright = atooid(PQgetvalue(res, i, i_oprright)); oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode)); /* Decide whether we want to dump it */ @@ -5407,6 +5422,7 @@ getCollations(Archive *fout, int *numCollations) int i_collname; int i_collnamespace; int i_rolname; + int i_collencoding; /* Collations didn't exist pre-9.1 */ if (fout->remoteVersion < 90100) @@ -5424,7 +5440,8 @@ getCollations(Archive *fout, int *numCollations) appendPQExpBuffer(query, "SELECT tableoid, oid, collname, " "collnamespace, " - "(%s collowner) AS rolname " + "(%s collowner) AS rolname, " + "collencoding " "FROM pg_collation", username_subquery); @@ -5440,6 +5457,7 @@ getCollations(Archive *fout, int *numCollations) i_collname = PQfnumber(res, "collname"); i_collnamespace = PQfnumber(res, "collnamespace"); i_rolname = PQfnumber(res, "rolname"); + i_collencoding = PQfnumber(res, "collencoding"); for (i = 0; i < ntups; i++) { @@ -5452,6 +5470,7 @@ getCollations(Archive *fout, int *numCollations) findNamespace(fout, atooid(PQgetvalue(res, i, i_collnamespace))); collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname)); + collinfo[i].collencoding = atoi(PQgetvalue(res, i, i_collencoding)); /* Decide whether we want to dump it */ selectDumpableObject(&(collinfo[i].dobj), fout); @@ -5561,19 +5580,28 @@ getAccessMethods(Archive *fout, int *numAccessMethods) int i_amhandler; int i_amtype; - /* Before 9.6, there are no user-defined access methods */ - if (fout->remoteVersion < 90600) - { - *numAccessMethods = 0; - return NULL; - } - query = createPQExpBuffer(); - /* Select all access methods from pg_am table */ - appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, amtype, " - "amhandler::pg_catalog.regproc AS amhandler " - "FROM pg_am"); + /* + * Select all access methods from pg_am table. v9.6 introduced CREATE + * ACCESS METHOD, so earlier versions usually have only built-in access + * methods. v9.6 also changed the access method API, replacing dozens of + * pg_am columns with amhandler. Even if a user created an access method + * by "INSERT INTO pg_am", we have no way to translate pre-v9.6 pg_am + * columns to a v9.6+ CREATE ACCESS METHOD. Hence, before v9.6, read + * pg_am just to facilitate findAccessMethodByOid() providing the + * OID-to-name mapping. + */ + appendPQExpBufferStr(query, "SELECT tableoid, oid, amname, "); + if (fout->remoteVersion >= 90600) + appendPQExpBufferStr(query, + "amtype, " + "amhandler::pg_catalog.regproc AS amhandler "); + else + appendPQExpBufferStr(query, + "'i'::pg_catalog.\"char\" AS amtype, " + "'-'::pg_catalog.regproc AS amhandler "); + appendPQExpBufferStr(query, "FROM pg_am"); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5631,6 +5659,7 @@ getOpclasses(Archive *fout, int *numOpclasses) OpclassInfo *opcinfo; int i_tableoid; int i_oid; + int i_opcmethod; int i_opcname; int i_opcnamespace; int i_rolname; @@ -5640,11 +5669,20 @@ getOpclasses(Archive *fout, int *numOpclasses) * system-defined opclasses at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, " - "opcnamespace, " - "(%s opcowner) AS rolname " - "FROM pg_opclass", - username_subquery); + if (fout->remoteVersion >= 80300) + appendPQExpBuffer(query, "SELECT tableoid, oid, " + "opcmethod, opcname, " + "opcnamespace, " + "(%s opcowner) AS rolname " + "FROM pg_opclass", + username_subquery); + else + appendPQExpBuffer(query, "SELECT tableoid, oid, " + "opcamid AS opcmethod, opcname, " + "opcnamespace, " + "(%s opcowner) AS rolname " + "FROM pg_opclass", + username_subquery); res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); @@ -5655,6 +5693,7 @@ getOpclasses(Archive *fout, int *numOpclasses) i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); + i_opcmethod = PQfnumber(res, "opcmethod"); i_opcname = PQfnumber(res, "opcname"); i_opcnamespace = PQfnumber(res, "opcnamespace"); i_rolname = PQfnumber(res, "rolname"); @@ -5669,6 +5708,7 @@ getOpclasses(Archive *fout, int *numOpclasses) opcinfo[i].dobj.namespace = findNamespace(fout, atooid(PQgetvalue(res, i, i_opcnamespace))); + opcinfo[i].opcmethod = atooid(PQgetvalue(res, i, i_opcmethod)); opcinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname)); /* Decide whether we want to dump it */ @@ -5706,6 +5746,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies) OpfamilyInfo *opfinfo; int i_tableoid; int i_oid; + int i_opfmethod; int i_opfname; int i_opfnamespace; int i_rolname; @@ -5724,7 +5765,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies) * system-defined opfamilies at dump-out time. */ - appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, " + appendPQExpBuffer(query, "SELECT tableoid, oid, opfmethod, opfname, " "opfnamespace, " "(%s opfowner) AS rolname " "FROM pg_opfamily", @@ -5740,6 +5781,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies) i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); i_opfname = PQfnumber(res, "opfname"); + i_opfmethod = PQfnumber(res, "opfmethod"); i_opfnamespace = PQfnumber(res, "opfnamespace"); i_rolname = PQfnumber(res, "rolname"); @@ -5753,6 +5795,7 @@ getOpfamilies(Archive *fout, int *numOpfamilies) opfinfo[i].dobj.namespace = findNamespace(fout, atooid(PQgetvalue(res, i, i_opfnamespace))); + opfinfo[i].opfmethod = atooid(PQgetvalue(res, i, i_opfmethod)); opfinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname)); /* Decide whether we want to dump it */ |