diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c index 7f9860f6b7b..fb2889e9c08 100644 --- a/src/bin/pg_dump/common.c +++ b/src/bin/pg_dump/common.c @@ -122,6 +122,9 @@ getSchemaData(int *numTablesPtr) tblinfo = getTables(&numTables); tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo)); + /* Do this after we've built tblinfoindex */ + getOwnedSeqs(tblinfo, numTables); + if (g_verbose) write_msg(NULL, "reading user-defined functions\n"); funinfo = getFuncs(&numFuncs); diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index afd4b184cdf..d4c82bea8be 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -3201,35 +3201,6 @@ getTables(int *numTables) PQclear(res); - /* - * Force sequences that are "owned" by table columns to be dumped whenever - * their owning table is being dumped. - */ - for (i = 0; i < ntups; i++) - { - TableInfo *seqinfo = &tblinfo[i]; - int j; - - if (!OidIsValid(seqinfo->owning_tab)) - continue; /* not an owned sequence */ - if (seqinfo->dobj.dump) - continue; /* no need to search */ - - /* can't use findTableByOid yet, unfortunately */ - for (j = 0; j < ntups; j++) - { - if (tblinfo[j].dobj.catId.oid == seqinfo->owning_tab) - { - if (tblinfo[j].dobj.dump) - { - seqinfo->interesting = true; - seqinfo->dobj.dump = true; - } - break; - } - } - } - destroyPQExpBuffer(query); destroyPQExpBuffer(delqry); destroyPQExpBuffer(lockquery); @@ -3237,6 +3208,40 @@ getTables(int *numTables) return tblinfo; } +/* + * getOwnedSeqs + * identify owned sequences and mark them as dumpable if owning table is + * + * We used to do this in getTables(), but it's better to do it after the + * index used by findTableByOid() has been set up. + */ +void +getOwnedSeqs(TableInfo tblinfo[], int numTables) +{ + int i; + + /* + * Force sequences that are "owned" by table columns to be dumped whenever + * their owning table is being dumped. + */ + for (i = 0; i < numTables; i++) + { + TableInfo *seqinfo = &tblinfo[i]; + TableInfo *owning_tab; + + if (!OidIsValid(seqinfo->owning_tab)) + continue; /* not an owned sequence */ + if (seqinfo->dobj.dump) + continue; /* no need to search */ + owning_tab = findTableByOid(seqinfo->owning_tab); + if (owning_tab && owning_tab->dobj.dump) + { + seqinfo->interesting = true; + seqinfo->dobj.dump = true; + } + } +} + /* * getInherits * read all the inheritance information diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 2351f30618f..3d213db0744 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -477,6 +477,7 @@ extern OpclassInfo *getOpclasses(int *numOpclasses); extern OpfamilyInfo *getOpfamilies(int *numOpfamilies); extern ConvInfo *getConversions(int *numConversions); extern TableInfo *getTables(int *numTables); +extern void getOwnedSeqs(TableInfo tblinfo[], int numTables); extern InhInfo *getInherits(int *numInherits); extern void getIndexes(TableInfo tblinfo[], int numTables); extern void getConstraints(TableInfo tblinfo[], int numTables);