Skip to content

Commit fb0f639

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 079013f commit fb0f639

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
@@ -11522,9 +11522,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1152211522
appendPQExpBuffer(q, "FOR TYPE %s USING %s",
1152311523
opcintype,
1152411524
fmtId(amname));
11525-
if (strlen(opcfamilyname) > 0 &&
11526-
(strcmp(opcfamilyname, opcinfo->dobj.name) != 0 ||
11527-
strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0))
11525+
if (strlen(opcfamilyname) > 0)
1152811526
{
1152911527
appendPQExpBufferStr(q, " FAMILY ");
1153011528
if (strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
@@ -11808,15 +11806,6 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
1180811806
if (!opfinfo->dobj.dump || dopt->dataOnly)
1180911807
return;
1181011808

11811-
/*
11812-
* We want to dump the opfamily only if (1) it contains "loose" operators
11813-
* or functions, or (2) it contains an opclass with a different name or
11814-
* owner. Otherwise it's sufficient to let it be created during creation
11815-
* of the contained opclass, and not dumping it improves portability of
11816-
* the dump. Since we have to fetch the loose operators/funcs anyway, do
11817-
* that first.
11818-
*/
11819-
1182011809
query = createPQExpBuffer();
1182111810
q = createPQExpBuffer();
1182211811
delq = createPQExpBuffer();
@@ -11899,40 +11888,6 @@ dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
1189911888

1190011889
res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1190111890

11902-
if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
11903-
{
11904-
/* No loose members, so check contained opclasses */
11905-
resetPQExpBuffer(query);
11906-
11907-
appendPQExpBuffer(query, "SELECT 1 "
11908-
"FROM pg_catalog.pg_opclass c, pg_catalog.pg_opfamily f, pg_catalog.pg_depend "
11909-
"WHERE f.oid = '%u'::pg_catalog.oid "
11910-
"AND refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
11911-
"AND refobjid = f.oid "
11912-
"AND classid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
11913-
"AND objid = c.oid "
11914-
"AND (opcname != opfname OR opcnamespace != opfnamespace OR opcowner != opfowner) "
11915-
"LIMIT 1",
11916-
opfinfo->dobj.catId.oid);
11917-
11918-
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11919-
11920-
if (PQntuples(res) == 0)
11921-
{
11922-
/* no need to dump it, so bail out */
11923-
PQclear(res);
11924-
PQclear(res_ops);
11925-
PQclear(res_procs);
11926-
destroyPQExpBuffer(query);
11927-
destroyPQExpBuffer(q);
11928-
destroyPQExpBuffer(delq);
11929-
destroyPQExpBuffer(labelq);
11930-
return;
11931-
}
11932-
11933-
PQclear(res);
11934-
}
11935-
1193611891
/* Get additional fields from the pg_opfamily row */
1193711892
resetPQExpBuffer(query);
1193811893

0 commit comments

Comments
 (0)