diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 6c0e5e4c69520ad4d6156b607f06753914fbf045..dab29c52d40f11b86bd88658e8cbec9d9a56e292 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -7953,7 +7953,8 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) i_opcfamilynsp = PQfnumber(res, "opcfamilynsp"); i_amname = PQfnumber(res, "amname"); - opcintype = PQgetvalue(res, 0, i_opcintype); + /* opcintype may still be needed after we PQclear res */ + opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype)); opckeytype = PQgetvalue(res, 0, i_opckeytype); opcdefault = PQgetvalue(res, 0, i_opcdefault); opcfamily = PQgetvalue(res, 0, i_opcfamily); @@ -8115,6 +8116,15 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) PQclear(res); + /* + * If needComma is still false it means we haven't added anything after + * the AS keyword. To avoid printing broken SQL, append a dummy STORAGE + * clause with the same datatype. This isn't sanctioned by the + * documentation, but actually DefineOpClass will treat it as a no-op. + */ + if (!needComma) + appendPQExpBuffer(q, "STORAGE %s", opcintype); + appendPQExpBuffer(q, ";\n"); ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId, @@ -8136,6 +8146,7 @@ dumpOpclass(Archive *fout, OpclassInfo *opcinfo) NULL, opcinfo->rolname, opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId); + free(opcintype); free(amname); destroyPQExpBuffer(query); destroyPQExpBuffer(q);