@@ -96,6 +96,12 @@ bool g_verbose; /* User wants verbose narration of our
9696/* subquery used to convert user ID (eg, datdba) to user name */
9797static const char *username_subquery;
9898
99+ /*
100+ * For 8.0 and earlier servers, pulled from pg_database, for 8.1+ we use
101+ * FirstNormalObjectId - 1.
102+ */
103+ static Oid g_last_builtin_oid; /* value of the last builtin oid */
104+
99105/* The specified names/patterns should to match at least one entity */
100106static int strict_names = 0;
101107
@@ -233,6 +239,7 @@ static char *convertRegProcReference(Archive *fout,
233239 const char *proc);
234240static char *convertOperatorReference(Archive *fout, const char *opr);
235241static char *convertTSFunction(Archive *fout, Oid funcOid);
242+ static Oid findLastBuiltinOid_V71(Archive *fout, const char *);
236243static void selectSourceSchema(Archive *fout, const char *schemaName);
237244static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
238245static void getBlobs(Archive *fout);
@@ -684,6 +691,20 @@ main(int argc, char **argv)
684691 exit_horribly(NULL,
685692 "Exported snapshots are not supported by this server version.\n");
686693
694+ /*
695+ * Find the last built-in OID, if needed (prior to 8.1)
696+ *
697+ * With 8.1 and above, we can just use FirstNormalObjectId - 1.
698+ */
699+ if (fout->remoteVersion < 80100)
700+ g_last_builtin_oid = findLastBuiltinOid_V71(fout,
701+ PQdb(GetConnection(fout)));
702+ else
703+ g_last_builtin_oid = FirstNormalObjectId - 1;
704+
705+ if (g_verbose)
706+ write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
707+
687708 /* Expand schema selection patterns into OID lists */
688709 if (schema_include_patterns.head != NULL)
689710 {
@@ -1494,7 +1515,7 @@ selectDumpableCast(CastInfo *cast, Archive *fout)
14941515 * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
14951516 * support ACLs currently.
14961517 */
1497- if (cast->dobj.catId.oid < (Oid) FirstNormalObjectId )
1518+ if (cast->dobj.catId.oid <= (Oid) g_last_builtin_oid )
14981519 cast->dobj.dump = DUMP_COMPONENT_NONE;
14991520 else
15001521 cast->dobj.dump = fout->dopt->include_everything ?
@@ -1526,7 +1547,7 @@ selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
15261547 plang->dobj.dump = DUMP_COMPONENT_NONE;
15271548 else
15281549 {
1529- if (plang->dobj.catId.oid < (Oid) FirstNormalObjectId )
1550+ if (plang->dobj.catId.oid <= (Oid) g_last_builtin_oid )
15301551 plang->dobj.dump = fout->remoteVersion < 90600 ?
15311552 DUMP_COMPONENT_NONE : DUMP_COMPONENT_ACL;
15321553 else
@@ -1552,7 +1573,7 @@ selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
15521573 * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
15531574 * they do not support ACLs currently.
15541575 */
1555- if (method->dobj.catId.oid < (Oid) FirstNormalObjectId )
1576+ if (method->dobj.catId.oid <= (Oid) g_last_builtin_oid )
15561577 method->dobj.dump = DUMP_COMPONENT_NONE;
15571578 else
15581579 method->dobj.dump = fout->dopt->include_everything ?
@@ -1577,7 +1598,7 @@ selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
15771598 * change permissions on those objects, if they wish to, and have those
15781599 * changes preserved.
15791600 */
1580- if (dopt->binary_upgrade && extinfo->dobj.catId.oid < (Oid) FirstNormalObjectId )
1601+ if (dopt->binary_upgrade && extinfo->dobj.catId.oid <= (Oid) g_last_builtin_oid )
15811602 extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
15821603 else
15831604 extinfo->dobj.dump = extinfo->dobj.dump_contains =
@@ -8820,8 +8841,8 @@ dumpExtension(Archive *fout, ExtensionInfo *extinfo)
88208841 /*
88218842 * We unconditionally create the extension, so we must drop it if it
88228843 * exists. This could happen if the user deleted 'plpgsql' and then
8823- * readded it, causing its oid to be greater than FirstNormalObjectId .
8824- * The FirstNormalObjectId test was kept to avoid repeatedly dropping
8844+ * readded it, causing its oid to be greater than g_last_builtin_oid .
8845+ * The g_last_builtin_oid test was kept to avoid repeatedly dropping
88258846 * and recreating extensions like 'plpgsql'.
88268847 */
88278848 appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
@@ -15324,6 +15345,33 @@ dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
1532415345 destroyPQExpBuffer(labelq);
1532515346}
1532615347
15348+ /*
15349+ * findLastBuiltinOid_V71 -
15350+ *
15351+ * find the last built in oid
15352+ *
15353+ * For 7.1 through 8.0, we do this by retrieving datlastsysoid from the
15354+ * pg_database entry for the current database.
15355+ */
15356+ static Oid
15357+ findLastBuiltinOid_V71(Archive *fout, const char *dbname)
15358+ {
15359+ PGresult *res;
15360+ Oid last_oid;
15361+ PQExpBuffer query = createPQExpBuffer();
15362+
15363+ resetPQExpBuffer(query);
15364+ appendPQExpBufferStr(query, "SELECT datlastsysoid from pg_database where datname = ");
15365+ appendStringLiteralAH(query, dbname, fout);
15366+
15367+ res = ExecuteSqlQueryForSingleRow(fout, query->data);
15368+ last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
15369+ PQclear(res);
15370+ destroyPQExpBuffer(query);
15371+
15372+ return last_oid;
15373+ }
15374+
1532715375/*
1532815376 * dumpSequence
1532915377 * write the declaration (not data) of one user-defined sequence
0 commit comments