Skip to content

Commit 5886c7d

Browse files
committed
Fix pg_dump to not emit invalid SQL for an empty operator class.
If an operator class has no operators or functions, and doesn't need a STORAGE clause, we emitted "CREATE OPERATOR CLASS ... AS ;" which is syntactically invalid. Fix by forcing a STORAGE clause to be emitted anyway in this case. (At some point we might consider changing the grammar to allow CREATE OPERATOR CLASS without an opclass_item_list. But probably we'd want to omit the AS in that case, so that wouldn't fix this pg_dump issue anyway.) It's been like this all along, so back-patch to all supported branches. Daniel Gustafsson, tweaked by me to avoid a dangling-pointer bug Discussion: https://postgr.es/m/[email protected]
1 parent 50d9476 commit 5886c7d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12771,7 +12771,8 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1277112771
i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
1277212772
i_amname = PQfnumber(res, "amname");
1277312773

12774-
opcintype = PQgetvalue(res, 0, i_opcintype);
12774+
/* opcintype may still be needed after we PQclear res */
12775+
opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
1277512776
opckeytype = PQgetvalue(res, 0, i_opckeytype);
1277612777
opcdefault = PQgetvalue(res, 0, i_opcdefault);
1277712778
/* opcfamily will still be needed after we PQclear res */
@@ -13005,6 +13006,15 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1300513006

1300613007
PQclear(res);
1300713008

13009+
/*
13010+
* If needComma is still false it means we haven't added anything after
13011+
* the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
13012+
* clause with the same datatype. This isn't sanctioned by the
13013+
* documentation, but actually DefineOpClass will treat it as a no-op.
13014+
*/
13015+
if (!needComma)
13016+
appendPQExpBuffer(q, "STORAGE %s", opcintype);
13017+
1300813018
appendPQExpBufferStr(q, ";\n");
1300913019

1301013020
appendPQExpBuffer(labelq, "OPERATOR CLASS %s",
@@ -13032,6 +13042,8 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1303213042
opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
1303313043
opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
1303413044

13045+
free(opcintype);
13046+
free(opcfamily);
1303513047
free(amname);
1303613048
destroyPQExpBuffer(query);
1303713049
destroyPQExpBuffer(q);

0 commit comments

Comments
 (0)