Skip to content

Commit 34bf6bc

Browse files
committed
Fix pg_dump so pg_upgrade'ing an extension with simple opfamilies works.
As reported by Michael Feld, pg_upgrade'ing an installation having extensions with operator families that contain just a single operator class failed to reproduce the extension membership of those operator families. This caused no immediate ill effects, but would create problems when later trying to do a plain dump and restore, because the seemingly-not-part-of- the-extension operator families would appear separately in the pg_dump output, and then would conflict with the families created by loading the extension. This has been broken ever since extensions were introduced, and many of the standard contrib extensions are affected, so it's a bit astonishing nobody complained before. The cause of the problem is a perhaps-ill-considered decision to omit such operator families from pg_dump's output on the grounds that the CREATE OPERATOR CLASS commands could recreate them, and having explicit CREATE OPERATOR FAMILY commands would impede loading the dump script into pre-8.3 servers. Whatever the merits of that decision when 8.3 was being written, it looks like a poor tradeoff now. We can fix the pg_upgrade problem simply by removing that code, so that the operator families are dumped explicitly (and then will be properly made to be part of their extensions). Although this fixes the behavior of future pg_upgrade runs, it does nothing to clean up existing installations that may have improperly-linked operator families. Given the small number of complaints to date, maybe we don't need to worry about providing an automated solution for that; anyone who needs to clean it up can do so with manual "ALTER EXTENSION ADD OPERATOR FAMILY" commands, or even just ignore the duplicate-opfamily errors they get during a pg_restore. In any case we need this fix. Back-patch to all supported branches. Discussion: <20228.1460575691@sss.pgh.pa.us>
1 parent f6b8116 commit 34bf6bc

File tree

1 file changed

+1
-46
lines changed

1 file changed

+1
-46
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10715,9 +10715,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1071510715
appendPQExpBuffer(q, "FOR TYPE %s USING %s",
1071610716
opcintype,
1071710717
fmtId(amname));
10718-
if (strlen(opcfamilyname) > 0 &&
10719-
(strcmp(opcfamilyname, opcinfo->dobj.name) != 0 ||
10720-
strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0))
10718+
if (strlen(opcfamilyname) > 0)
1072110719
{
1072210720
appendPQExpBuffer(q, " FAMILY ");
1072310721
if (strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
@@ -11000,15 +10998,6 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
1100010998
if (!opfinfo->dobj.dump || dataOnly)
1100110999
return;
1100211000

11003-
/*
11004-
* We want to dump the opfamily only if (1) it contains "loose" operators
11005-
* or functions, or (2) it contains an opclass with a different name or
11006-
* owner. Otherwise it's sufficient to let it be created during creation
11007-
* of the contained opclass, and not dumping it improves portability of
11008-
* the dump. Since we have to fetch the loose operators/funcs anyway, do
11009-
* that first.
11010-
*/
11011-
1101211001
query = createPQExpBuffer();
1101311002
q = createPQExpBuffer();
1101411003
delq = createPQExpBuffer();
@@ -11091,40 +11080,6 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
1109111080

1109211081
res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1109311082

11094-
if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
11095-
{
11096-
/* No loose members, so check contained opclasses */
11097-
resetPQExpBuffer(query);
11098-
11099-
appendPQExpBuffer(query, "SELECT 1 "
11100-
"FROM pg_catalog.pg_opclass c, pg_catalog.pg_opfamily f, pg_catalog.pg_depend "
11101-
"WHERE f.oid = '%u'::pg_catalog.oid "
11102-
"AND refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
11103-
"AND refobjid = f.oid "
11104-
"AND classid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
11105-
"AND objid = c.oid "
11106-
"AND (opcname != opfname OR opcnamespace != opfnamespace OR opcowner != opfowner) "
11107-
"LIMIT 1",
11108-
opfinfo->dobj.catId.oid);
11109-
11110-
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11111-
11112-
if (PQntuples(res) == 0)
11113-
{
11114-
/* no need to dump it, so bail out */
11115-
PQclear(res);
11116-
PQclear(res_ops);
11117-
PQclear(res_procs);
11118-
destroyPQExpBuffer(query);
11119-
destroyPQExpBuffer(q);
11120-
destroyPQExpBuffer(delq);
11121-
destroyPQExpBuffer(labelq);
11122-
return;
11123-
}
11124-
11125-
PQclear(res);
11126-
}
11127-
1112811083
/* Get additional fields from the pg_opfamily row */
1112911084
resetPQExpBuffer(query);
1113011085

0 commit comments

Comments
 (0)