Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nhazp-0001Zd-2I for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 17:53:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nhazn-0007A0-Lo for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 17:53:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nhazn-00079r-3n for pgsql-hackers@lists.postgresql.org; Thu, 21 Apr 2022 17:53:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nhazf-0005yS-Uf for pgsql-hackers@postgresql.org; Thu, 21 Apr 2022 17:53:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 23LHrK0t3353762; Thu, 21 Apr 2022 13:53:20 -0400 From: Tom Lane To: Robert Haas cc: Shruthi Gowda , Julien Rouhaud , Sadhuprasad Patro , Stephen Frost , Bruce Momjian , Andres Freund , Alvaro Herrera , Masahiko Sawada , Tom Kincaid , Amit Kapila , Thomas Munro , PostgreSQL Development , Masahiko Sawada Subject: Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce) In-reply-to: References: <20220115054719.yeqoy2mdllbc7wq2@jrouhaud> <2935358.1650479692@sss.pgh.pa.us> <2996609.1650488182@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Wed, 20 Apr 2022 19:03:25 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3353727.1650563571.0@sss.pgh.pa.us> Date: Thu, 21 Apr 2022 13:53:20 -0400 Message-ID: <3353761.1650563600@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3353727.1650563571.1@sss.pgh.pa.us> Robert Haas writes: > On Wed, Apr 20, 2022 at 4:56 PM Tom Lane wrote: >> Having just had to bury my nose in renumber_oids.pl, I thought of a >> different approach we could take to expose these OIDs to Catalog.pm. >> That's to invent a new macro that Catalog.pm recognizes, and write >> something about like this in pg_database.h: >> DECLARE_OID_DEFINING_MACRO(Template0ObjectId, 4); >> DECLARE_OID_DEFINING_MACRO(PostgresObjectId, 5); > I like it! 0001 attached is a revised patch that does it that way. This seems like a clearly better answer. 0002 contains the perhaps-slightly-more-controversial changes of changing the macro names and explicitly pinning no databases. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="0001-fix-assignment-of-database-OIDs-2.patch"; charset="us-ascii" Content-ID: <3353727.1650563571.2@sss.pgh.pa.us> Content-Description: 0001-fix-assignment-of-database-OIDs-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.= pm index 0275795dea..ece0a934f0 100644 --- a/src/backend/catalog/Catalog.pm +++ b/src/backend/catalog/Catalog.pm @@ -44,6 +44,8 @@ sub ParseHeader $catalog{columns} =3D []; $catalog{toasting} =3D []; $catalog{indexing} =3D []; + $catalog{other_oids} =3D []; + $catalog{foreign_keys} =3D []; $catalog{client_code} =3D []; = open(my $ifh, '<', $input_file) || die "$input_file: $!"; @@ -118,6 +120,14 @@ sub ParseHeader index_decl =3D> $6 }; } + elsif (/^DECLARE_OID_DEFINING_MACRO\(\s*(\w+),\s*(\d+)\)/) + { + push @{ $catalog{other_oids} }, + { + other_name =3D> $1, + other_oid =3D> $2 + }; + } elsif ( /^DECLARE_(ARRAY_)?FOREIGN_KEY(_OPT)?\(\s*\(([^)]+)\),\s*(\w+),\s*\(([= ^)]+)\)\)/ ) @@ -572,6 +582,10 @@ sub FindAllOidsFromHeaders { push @oids, $index->{index_oid}; } + foreach my $other (@{ $catalog->{other_oids} }) + { + push @oids, $other->{other_oid}; + } } = return \@oids; diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c index 520f77971b..d7e5c02f95 100644 --- a/src/backend/catalog/catalog.c +++ b/src/backend/catalog/catalog.c @@ -339,9 +339,11 @@ IsPinnedObject(Oid classId, Oid objectId) * robustness. */ = - /* template1 is not pinned */ + /* template1, template0, postgres are not pinned */ if (classId =3D=3D DatabaseRelationId && - objectId =3D=3D TemplateDbOid) + (objectId =3D=3D TemplateDbOid || + objectId =3D=3D Template0ObjectId || + objectId =3D=3D PostgresObjectId)) return false; = /* the public namespace is not pinned */ diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl index 2d02b02267..f4ec6d6d40 100644 --- a/src/backend/catalog/genbki.pl +++ b/src/backend/catalog/genbki.pl @@ -472,7 +472,7 @@ EOM $catalog->{rowtype_oid_macro}, $catalog->{rowtype_oid} if $catalog->{rowtype_oid_macro}; = - # Likewise for macros for toast and index OIDs + # Likewise for macros for toast, index, and other OIDs foreach my $toast (@{ $catalog->{toasting} }) { printf $def "#define %s %s\n", @@ -488,6 +488,12 @@ EOM $index->{index_oid_macro}, $index->{index_oid} if $index->{index_oid_macro}; } + foreach my $other (@{ $catalog->{other_oids} }) + { + printf $def "#define %s %s\n", + $other->{other_name}, $other->{other_oid} + if $other->{other_name}; + } = print $def "\n"; = diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 1cb4a5b0d2..5e2eeefc4c 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -59,11 +59,11 @@ #include "sys/mman.h" #endif = -#include "access/transam.h" #include "access/xlog_internal.h" #include "catalog/pg_authid_d.h" #include "catalog/pg_class_d.h" /* pgrminclude ignore */ #include "catalog/pg_collation_d.h" +#include "catalog/pg_database_d.h" /* pgrminclude ignore */ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/logging.h" diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index d3588607e7..786d592e2b 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2901,10 +2901,11 @@ dumpDatabase(Archive *fout) qdatname =3D pg_strdup(fmtId(datname)); = /* - * Prepare the CREATE DATABASE command. We must specify encoding, local= e, - * and tablespace since those can't be altered later. Other DB properti= es - * are left to the DATABASE PROPERTIES entry, so that they can be applie= d - * after reconnecting to the target DB. + * Prepare the CREATE DATABASE command. We must specify OID (if we want + * to preserve that), as well as the encoding, locale, and tablespace + * since those can't be altered later. Other DB properties are left to + * the DATABASE PROPERTIES entry, so that they can be applied after + * reconnecting to the target DB. */ if (dopt->binary_upgrade) { diff --git a/src/include/access/transam.h b/src/include/access/transam.h index 9a2816de51..338dfca5a0 100644 --- a/src/include/access/transam.h +++ b/src/include/access/transam.h @@ -196,10 +196,6 @@ FullTransactionIdAdvance(FullTransactionId *dest) #define FirstUnpinnedObjectId 12000 #define FirstNormalObjectId 16384 = -/* OIDs of Template0 and Postgres database are fixed */ -#define Template0ObjectId 4 -#define PostgresObjectId 5 - /* * VariableCache is a data structure in shared memory that is used to tra= ck * OID and XID assignment state. For largely historical reasons, there i= s diff --git a/src/include/catalog/genbki.h b/src/include/catalog/genbki.h index 4ecd76f4be..992b784236 100644 --- a/src/include/catalog/genbki.h +++ b/src/include/catalog/genbki.h @@ -84,6 +84,14 @@ #define DECLARE_UNIQUE_INDEX(name,oid,oidmacro,decl) extern int no_such_v= ariable #define DECLARE_UNIQUE_INDEX_PKEY(name,oid,oidmacro,decl) extern int no_s= uch_variable = +/* + * These lines inform genbki.pl about manually-assigned OIDs that do not + * correspond to any entry in the catalog *.dat files, but should be subj= ect + * to uniqueness verification and renumber_oids.pl renumbering. A C macr= o + * to #define the given name is emitted into the corresponding *_d.h file= . + */ +#define DECLARE_OID_DEFINING_MACRO(name,oid) extern int no_such_variable + /* * These lines are processed by genbki.pl to create a table for use * by the pg_get_catalog_foreign_keys() function. We do not have any diff --git a/src/include/catalog/pg_database.h b/src/include/catalog/pg_da= tabase.h index e10e91c0af..96be9e9729 100644 --- a/src/include/catalog/pg_database.h +++ b/src/include/catalog/pg_database.h @@ -91,4 +91,13 @@ DECLARE_TOAST_WITH_MACRO(pg_database, 4177, 4178, PgDat= abaseToastTable, PgDataba DECLARE_UNIQUE_INDEX(pg_database_datname_index, 2671, DatabaseNameIndexId= , on pg_database using btree(datname name_ops)); DECLARE_UNIQUE_INDEX_PKEY(pg_database_oid_index, 2672, DatabaseOidIndexId= , on pg_database using btree(oid oid_ops)); = +/* + * pg_database.dat contains an entry for template1, but not for the templ= ate0 + * or postgres databases, because those are created later in initdb. + * However, we still want to manually assign the OIDs for template0 and + * postgres, so declare those here. + */ +DECLARE_OID_DEFINING_MACRO(Template0ObjectId, 4); +DECLARE_OID_DEFINING_MACRO(PostgresObjectId, 5); + #endif /* PG_DATABASE_H */ diff --git a/src/include/catalog/renumber_oids.pl b/src/include/catalog/re= number_oids.pl index 7de13da4bd..ba8c69c87e 100755 --- a/src/include/catalog/renumber_oids.pl +++ b/src/include/catalog/renumber_oids.pl @@ -170,6 +170,16 @@ foreach my $input_file (@header_files) $changed =3D 1; } } + elsif (/^(DECLARE_OID_DEFINING_MACRO\(\s*\w+,\s*)(\d+)\)/) + { + if (exists $maphash{$2}) + { + my $repl =3D $1 . $maphash{$2} . ")"; + $line =3D~ + s/^DECLARE_OID_DEFINING_MACRO\(\s*\w+,\s*\d+\)/$repl/; + $changed =3D 1; + } + } elsif ($line =3D~ m/^CATALOG\((\w+),(\d+),(\w+)\)/) { if (exists $maphash{$2}) diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_= oids index 61d41e7561..e55bc6fa3c 100755 --- a/src/include/catalog/unused_oids +++ b/src/include/catalog/unused_oids @@ -32,15 +32,6 @@ my @input_files =3D glob("pg_*.h"); = my $oids =3D Catalog::FindAllOidsFromHeaders(@input_files); = -# Push the template0 and postgres database OIDs. -my $Template0ObjectId =3D - Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId= '); -push @{$oids}, $Template0ObjectId; - -my $PostgresObjectId =3D - Catalog::FindDefinedSymbol('access/transam.h', '..', 'PostgresObjectId'= ); -push @{$oids}, $PostgresObjectId; - # Also push FirstGenbkiObjectId to serve as a terminator for the last gap= . my $FirstGenbkiObjectId =3D Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObject= Id'); ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="0002-rename-symbols-and-pin-no-DBs.patch"; charset="us-ascii" Content-ID: <3353727.1650563571.3@sss.pgh.pa.us> Content-Description: 0002-rename-symbols-and-pin-no-DBs.patch Content-Transfer-Encoding: quoted-printable diff --git a/doc/src/sgml/bki.sgml b/doc/src/sgml/bki.sgml index 33955494c6..20894baf18 100644 --- a/doc/src/sgml/bki.sgml +++ b/doc/src/sgml/bki.sgml @@ -180,12 +180,13 @@ [ = # A comment could appear here. -{ oid =3D> '1', oid_symbol =3D> 'TemplateDbOid', +{ oid =3D> '1', oid_symbol =3D> 'Template1DbOid', descr =3D> 'database\'s default template', - datname =3D> 'template1', encoding =3D> 'ENCODING', datistemplate =3D> = 't', + datname =3D> 'template1', encoding =3D> 'ENCODING', + datlocprovider =3D> 'LOCALE_PROVIDER', datistemplate =3D> 't', datallowconn =3D> 't', datconnlimit =3D> '-1', datfrozenxid =3D> '0', datminmxid =3D> '1', dattablespace =3D> 'pg_default', datcollate =3D> '= LC_COLLATE', - datctype =3D> 'LC_CTYPE', datacl =3D> '_null_' }, + datctype =3D> 'LC_CTYPE', daticulocale =3D> 'ICU_LOCALE', datacl =3D> '= _null_' }, = ] ]]> diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transa= m/xlog.c index 5eabd32cf6..61cda56c6f 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -4540,9 +4540,9 @@ BootStrapXLOG(void) checkPoint.nextMulti =3D FirstMultiXactId; checkPoint.nextMultiOffset =3D 0; checkPoint.oldestXid =3D FirstNormalTransactionId; - checkPoint.oldestXidDB =3D TemplateDbOid; + checkPoint.oldestXidDB =3D Template1DbOid; checkPoint.oldestMulti =3D FirstMultiXactId; - checkPoint.oldestMultiDB =3D TemplateDbOid; + checkPoint.oldestMultiDB =3D Template1DbOid; checkPoint.oldestCommitTsXid =3D InvalidTransactionId; checkPoint.newestCommitTsXid =3D InvalidTransactionId; checkPoint.time =3D (pg_time_t) time(NULL); diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c index d7e5c02f95..e784538aae 100644 --- a/src/backend/catalog/catalog.c +++ b/src/backend/catalog/catalog.c @@ -339,18 +339,20 @@ IsPinnedObject(Oid classId, Oid objectId) * robustness. */ = - /* template1, template0, postgres are not pinned */ - if (classId =3D=3D DatabaseRelationId && - (objectId =3D=3D TemplateDbOid || - objectId =3D=3D Template0ObjectId || - objectId =3D=3D PostgresObjectId)) - return false; - /* the public namespace is not pinned */ if (classId =3D=3D NamespaceRelationId && objectId =3D=3D PG_PUBLIC_NAMESPACE) return false; = + /* + * Databases are never pinned. It might seem that it'd be prudent to pi= n + * at least template0; but we do this intentionally so that template0 an= d + * template1 can be rebuilt from each other, thus letting them serve as + * mutual backups (as long as you've not modified template1, anyway). + */ + if (classId =3D=3D DatabaseRelationId) + return false; + /* * All other initdb-created objects are pinned. This is overkill (the * system doesn't really depend on having every last weird datatype, for diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/po= stinit.c index 9139fe895c..5dbc7379e3 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -908,7 +908,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const c= har *username, */ if (bootstrap) { - MyDatabaseId =3D TemplateDbOid; + MyDatabaseId =3D Template1DbOid; MyDatabaseTableSpace =3D DEFAULTTABLESPACE_OID; } else if (in_dbname !=3D NULL) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 5e2eeefc4c..fcef651c2f 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -1812,8 +1812,8 @@ make_template0(FILE *cmdfd) * be a little bit slower and make the new cluster a little bit bigger. */ static const char *const template0_setup[] =3D { - "CREATE DATABASE template0 IS_TEMPLATE =3D true ALLOW_CONNECTIONS =3D f= alse OID =3D " - CppAsString2(Template0ObjectId) + "CREATE DATABASE template0 IS_TEMPLATE =3D true ALLOW_CONNECTIONS =3D f= alse" + " OID =3D " CppAsString2(Template0DbOid) " STRATEGY =3D file_copy;\n\n", = /* @@ -1862,7 +1862,8 @@ make_postgres(FILE *cmdfd) * OID to postgres and select the file_copy strategy. */ static const char *const postgres_setup[] =3D { - "CREATE DATABASE postgres OID =3D " CppAsString2(PostgresObjectId) " ST= RATEGY =3D file_copy;\n\n", + "CREATE DATABASE postgres OID =3D " CppAsString2(PostgresDbOid) + " STRATEGY =3D file_copy;\n\n", "COMMENT ON DATABASE postgres IS 'default administrative connection dat= abase';\n\n", NULL }; diff --git a/src/include/catalog/pg_database.dat b/src/include/catalog/pg_= database.dat index 5feedff7bf..05873f74f6 100644 --- a/src/include/catalog/pg_database.dat +++ b/src/include/catalog/pg_database.dat @@ -12,7 +12,7 @@ = [ = -{ oid =3D> '1', oid_symbol =3D> 'TemplateDbOid', +{ oid =3D> '1', oid_symbol =3D> 'Template1DbOid', descr =3D> 'default template for new databases', datname =3D> 'template1', encoding =3D> 'ENCODING', datlocprovider =3D>= 'LOCALE_PROVIDER', datistemplate =3D> 't', datallowconn =3D> 't', datconnlimit =3D> '-1', datfrozenxid =3D> '0', diff --git a/src/include/catalog/pg_database.h b/src/include/catalog/pg_da= tabase.h index 96be9e9729..611c95656a 100644 --- a/src/include/catalog/pg_database.h +++ b/src/include/catalog/pg_database.h @@ -97,7 +97,7 @@ DECLARE_UNIQUE_INDEX_PKEY(pg_database_oid_index, 2672, D= atabaseOidIndexId, on pg * However, we still want to manually assign the OIDs for template0 and * postgres, so declare those here. */ -DECLARE_OID_DEFINING_MACRO(Template0ObjectId, 4); -DECLARE_OID_DEFINING_MACRO(PostgresObjectId, 5); +DECLARE_OID_DEFINING_MACRO(Template0DbOid, 4); +DECLARE_OID_DEFINING_MACRO(PostgresDbOid, 5); = #endif /* PG_DATABASE_H */ ------- =_aaaaaaaaaa0--