Skip to content

Commit a561254

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/D9E5FC64-7A37-4F3D-B946-7E4FB468F88A@yesql.se
1 parent 1218abc commit a561254

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
@@ -10756,7 +10756,8 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1075610756
i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
1075710757
i_amname = PQfnumber(res, "amname");
1075810758

10759-
opcintype = PQgetvalue(res, 0, i_opcintype);
10759+
/* opcintype may still be needed after we PQclear res */
10760+
opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
1076010761
opckeytype = PQgetvalue(res, 0, i_opckeytype);
1076110762
opcdefault = PQgetvalue(res, 0, i_opcdefault);
1076210763
/* opcfamily will still be needed after we PQclear res */
@@ -10990,6 +10991,15 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1099010991

1099110992
PQclear(res);
1099210993

10994+
/*
10995+
* If needComma is still false it means we haven't added anything after
10996+
* the AS keyword. To avoid printing broken SQL, append a dummy STORAGE
10997+
* clause with the same datatype. This isn't sanctioned by the
10998+
* documentation, but actually DefineOpClass will treat it as a no-op.
10999+
*/
11000+
if (!needComma)
11001+
appendPQExpBuffer(q, "STORAGE %s", opcintype);
11002+
1099311003
appendPQExpBuffer(q, ";\n");
1099411004

1099511005
appendPQExpBuffer(labelq, "OPERATOR CLASS %s",
@@ -11015,6 +11025,8 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
1101511025
opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
1101611026
opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
1101711027

11028+
free(opcintype);
11029+
free(opcfamily);
1101811030
free(amname);
1101911031
destroyPQExpBuffer(query);
1102011032
destroyPQExpBuffer(q);

0 commit comments

Comments
 (0)