From: Craig Ringer Date: Tue, 26 May 2015 02:18:10 +0000 (+0800) Subject: Only dump non-default sequence access methods X-Git-Tag: bdr-pg/REL9_4_2-1~1 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=1592812131d84de56ba258c333f936e5e19647e2;p=2ndquadrant_bdr.git Only dump non-default sequence access methods To prevent issues with UDR and with restoring BDR dumps to non-BDR databases, don't emit a USING clause unless the pg_seqam catalog is present and the dumped sequence uses a non-default sequence access method. The dump should be restored with default_seqam = 'local' to ensure that local sequences aren't transformed into 'bdr' sequences during restore. --- diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 8d07387c20..01154754e1 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -14202,6 +14202,46 @@ findLastBuiltinOid_V70(Archive *fout) return last_oid; } +/* + * If pg_seqam exists, return the sequence AM name for the specified + * sequence. If there's no pg_seqam, return null. + * + * The string returned, if any, must be free'd by the caller. + */ +static char* +find_sequence_seqam(Archive *fout, Oid seq_oid) +{ + PGresult *res; + const char *amname = NULL; + PQExpBuffer query = createPQExpBuffer(); + + res = ExecuteSqlQuery(fout, "SELECT EXISTS(SELECT 1 " + "FROM pg_catalog.pg_class c, " + "pg_catalog.pg_namespace n " + "WHERE n.oid = c.relnamespace " + "AND c.relname = 'pg_seqam' " + "AND c.relkind = 'r');", + PGRES_TUPLES_OK); + if (strcmp(PQgetvalue(res, 0, 0), "t") == 0) + { + PQclear(res); + + printfPQExpBuffer(query, "SELECT a.seqamname\n" + "FROM pg_catalog.pg_seqam a, pg_catalog.pg_class c\n" + "WHERE c.relam = a.oid AND c.oid = %u", + seq_oid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + amname = pg_strdup(PQgetvalue(res, 0, 0)); + } + + PQclear(res); + + destroyPQExpBuffer(query); + return amname; +} + /* * dumpSequence * write the declaration (not data) of one user-defined sequence @@ -14215,7 +14255,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo) *maxv = NULL, *minv = NULL, *cache, - *amname = "local"; + *amname; char bufm[100], bufx[100]; bool cycled; @@ -14296,28 +14336,7 @@ dumpSequence(Archive *fout, TableInfo *tbinfo) PQclear(res); - res = ExecuteSqlQuery(fout, "SELECT EXISTS(SELECT 1 " - "FROM pg_catalog.pg_class c, " - "pg_catalog.pg_namespace n " - "WHERE n.oid = c.relnamespace " - "AND c.relname = 'pg_seqam' " - "AND c.relkind = 'r');", - PGRES_TUPLES_OK); - if (strcmp(PQgetvalue(res, 0, 0), "t") == 0) - { - PQclear(res); - - printfPQExpBuffer(query, "SELECT a.seqamname\n" - "FROM pg_catalog.pg_seqam a, pg_catalog.pg_class c\n" - "WHERE c.relam = a.oid AND c.oid = %u", - tbinfo->dobj.catId.oid); - - res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); - - amname = pg_strdup(PQgetvalue(res, 0, 0)); - } - - PQclear(res); + amname = find_sequence_seqam(fout, tbinfo->dobj.catId.oid); /* * DROP must be fully qualified in case same name appears in pg_catalog @@ -14360,11 +14379,24 @@ dumpSequence(Archive *fout, TableInfo *tbinfo) " CACHE %s%s", cache, (cycled ? "\n CYCLE" : "")); - appendPQExpBuffer(query, "\n USING %s", fmtId(amname)); + /* + * Only dump the sequence access method if it's not a PostgreSQL + * built-in sequence. + * + * FIXME: For 'bdr' sequences we should really delay them + * until the data from the 'bdr' extension schema has been + * restored. + */ + if (amname != NULL && strcmp(amname, "local") != 0) + appendPQExpBuffer(query, "\n USING %s", fmtId(amname)); + + free(amname); + appendPQExpBufferStr(query, ";\n"); appendPQExpBuffer(labelq, "SEQUENCE %s", fmtId(tbinfo->dobj.name)); + /* binary_upgrade: no need to clear TOAST table oid */ if (binary_upgrade)