pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedpreserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
90+ messages / 10 participants
[nested] [flat]
Commits from this thread:
2022-04-21 2cb1272445 Rethink method for assigning OIDs to the template0 and postgres DBs.
2022-01-24 aa01051418 pg_upgrade: Preserve database OIDs.
2022-01-17 9a974cbcba pg_upgrade: Preserve relfilenodes and tablespace OIDs.
* preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 15:56 Robert Haas <robertmhaas@gmail.com>
0 siblings, 3 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-17 15:56 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Wed, Aug 11, 2021 at 3:41 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> I have fixed all the issues and now the patch is working as expected.
Hi,
I'm changing the subject line since the patch does something which was
discussed on that thread but isn't really related to the old email
subject. In general, I think this patch is uncontroversial and in
reasonably good shape. However, there's one part that I'm not too sure
about. If Tom Lane happens to be paying attention to this thread, I
think his feedback would be particularly useful, since he knows a lot
about the inner workings of pg_dump. Opinions from anybody else would
be great, too. Anyway, here's the hunk that worries me:
+
+ /*
+ * Need a separate entry, otherwise the command will
be run in the
+ * same transaction as the CREATE DATABASE command, which is not
+ * allowed.
+ */
+ ArchiveEntry(fout,
+ dbCatId, /* catalog ID */
+ dbDumpId, /* dump ID */
+ ARCHIVE_OPTS(.tag = datname,
+ .owner = dba,
+
.description = "SET_DB_OID",
+
.section = SECTION_PRE_DATA,
+
.createStmt = setDBIdQry->data,
+
.dropStmt = NULL));
+
To me, adding a separate TOC entry for a thing that is not really a
separate object seems like a scary hack that might come back to bite
us. Unfortunately, I don't know enough about pg_dump to say exactly
how it might come back to bite us, which leaves wide open the
possibility that I am completely wrong.... I just think it's the
intention that archive entries correspond to actual objects in the
database, not commands that we want executed in some particular order.
If that criticism is indeed correct, then my proposal would be to
instead add a WITH OID = nnn option to CREATE DATABASE and allow it to
be used only in binary upgrade mode. That has the disadvantage of
being inconsistent with the way that we preserve OIDs everywhere else,
but the only other alternatives are (1) do something like the above,
(2) remove the requirement that CREATE DATABASE run in its own
transaction, and (3) give up. (2) sounds hard and (3) is unappealing.
The rest of this email will be detailed review comments on the patch
as presented, and thus probably only interesting to someone actually
working on the patch. Feel free to skip if that's not you.
- I suggest splitting the patch into one portion that deals with
database OID and another portion that deals with tablespace OID and
relfilenode OID, or maybe splitting it all the way into three separate
patches, one for each. This could allow the uncontroversial parts to
get committed first while we're wondering what to do about the problem
described above.
- There are two places in the patch, one in dumpDatabase() and one in
generate_old_dump() where blank lines are removed with no other
changes. Please avoid whitespace-only hunks.
- If possible, please try to pgindent the new code. It's pretty good
what you did, but e.g. the declaration of
binary_upgrade_next_pg_tablespace_oid probably has less whitespace
than pgindent is going to want.
- The comments in dumpDatabase() claim that "postgres" and "template1"
are handled specially in some way, but there seems to be no code that
matches those comments.
- heap_create()'s logic around setting create_storage looks slightly
redundant. I'm not positive what would be better, but ... suppose you
just took the part that's currently gated by if (!IsBinaryUpgrade) and
did it unconditionally. Then put if (IsBinaryUpgrade) around the else
clause, but delete the last bit from there that sets create_storage.
Maybe we'd still want a comment saying that it's intentional that
create_storage = true even though it will be overwritten later, but
then, I think, we wouldn't need to set create_storage in two different
places. Maybe I'm wrong.
- If we're not going to do that, then I think you should swap the if
and else clauses and reverse the sense of the test. In createdb(),
CreateTableSpace(), and a bunch of existing places, we do if
(IsBinaryUpgrade) { ... } else { ... } so I don't think it makes sense
for this one to instead do if (!IsBinaryUpgrade) { ... } else { ... }.
- I'm not sure that I'd bother renaming
binary_upgrade_set_pg_class_oids_and_relfilenodes(). It's such a long
name, and a relfilenode is kind of an OID, so the current name isn't
even really wrong. I'd probably drop the header comment too, since it
seems rather obvious. But both of these things are judgement calls.
- Inside that function, there is a comment that says "Indexes cannot
have toast tables, so we need not make this probe in the index code
path." However, you have moved the code from someplace where it didn't
happen for indexes to someplace where it happens for both tables and
indexes. Therefore the comment, which was true when the code was where
it was before, is now false. So you need to update it.
- It is not clear to me why pg_upgrade's Makefile needs to be changed
to include -DFRONTEND in CPPFLAGS. All of the .c files in this
directory include postgres_fe.h rather than postgres.h, and that file
has #define FRONTEND 1. Moreover, there are no actual code changes in
this directory, so why should the Makefile need any change?
- A couple of comment changes - and the commit message - mention data
encryption, but that's not a feature that this patch implements, nor
are we committed to adding it in the immediate future (or ever,
really). So I think those places should be revised to say that we do
this because we want the filenames to match between the old and new
clusters, and leave the reasons why that might be a good thing up to
the reader's imagination.
Thanks,
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 16:12 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Tom Lane @ 2021-08-17 16:12 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> To me, adding a separate TOC entry for a thing that is not really a
> separate object seems like a scary hack that might come back to bite
> us. Unfortunately, I don't know enough about pg_dump to say exactly
> how it might come back to bite us, which leaves wide open the
> possibility that I am completely wrong.... I just think it's the
> intention that archive entries correspond to actual objects in the
> database, not commands that we want executed in some particular order.
I agree, this seems like a moderately bad idea. It could get broken
either by executing only one of the TOC entries during restore, or
by executing them in the wrong order. The latter possibility could
be forestalled by adding a dependency, which I do not see this hunk
doing, which is clearly a bug. The former possibility would require
user intervention, so maybe it's in the category of "if you break
this you get to keep both pieces". Still, it's ugly.
> If that criticism is indeed correct, then my proposal would be to
> instead add a WITH OID = nnn option to CREATE DATABASE and allow it to
> be used only in binary upgrade mode.
If it's not too complicated to implement, that seems like an OK idea
from here. I don't have any great love for the way we handle OID
preservation in binary upgrade mode, so not doing it exactly the same
way for databases doesn't seem like a disadvantage.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 16:33 Stephen Frost <sfrost@snowman.net>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-17 16:33 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Tom Lane (tgl@sss.pgh.pa.us) wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
> > To me, adding a separate TOC entry for a thing that is not really a
> > separate object seems like a scary hack that might come back to bite
> > us. Unfortunately, I don't know enough about pg_dump to say exactly
> > how it might come back to bite us, which leaves wide open the
> > possibility that I am completely wrong.... I just think it's the
> > intention that archive entries correspond to actual objects in the
> > database, not commands that we want executed in some particular order.
>
> I agree, this seems like a moderately bad idea. It could get broken
> either by executing only one of the TOC entries during restore, or
> by executing them in the wrong order. The latter possibility could
> be forestalled by adding a dependency, which I do not see this hunk
> doing, which is clearly a bug. The former possibility would require
> user intervention, so maybe it's in the category of "if you break
> this you get to keep both pieces". Still, it's ugly.
Yeah, agreed.
> > If that criticism is indeed correct, then my proposal would be to
> > instead add a WITH OID = nnn option to CREATE DATABASE and allow it to
> > be used only in binary upgrade mode.
>
> If it's not too complicated to implement, that seems like an OK idea
> from here. I don't have any great love for the way we handle OID
> preservation in binary upgrade mode, so not doing it exactly the same
> way for databases doesn't seem like a disadvantage.
Also agreed on this, though I wonder- do we actually need to explicitly
make CREATE DATABASE q WITH OID = 1234; only work during binary upgrade
mode in the backend? That strikes me as perhaps doing more work than we
really need to while also preventing something that users might actually
like to do.
Either way, we'll need to check that the OID given to us can be used for
the database, I'd think.
Having pg_dump only include it in binary upgrade mode is fine though.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 16:42 Tom Lane <tgl@sss.pgh.pa.us>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 1 reply; 90+ messages in thread
From: Tom Lane @ 2021-08-17 16:42 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Robert Haas <robertmhaas@gmail.com>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Stephen Frost <sfrost@snowman.net> writes:
> Also agreed on this, though I wonder- do we actually need to explicitly
> make CREATE DATABASE q WITH OID = 1234; only work during binary upgrade
> mode in the backend? That strikes me as perhaps doing more work than we
> really need to while also preventing something that users might actually
> like to do.
There should be adequate defenses against a duplicate OID already,
so +1 --- no reason to insist this only be used during binary upgrade.
Actually though ... I've not read the patch, but what does it do about
the fact that the postgres and template0 DBs do not have stable OIDs?
I cannot imagine any way to force those to match across PG versions
that would not be an unsustainable crock.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 17:37 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-17 17:37 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 17, 2021 at 12:42 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Actually though ... I've not read the patch, but what does it do about
> the fact that the postgres and template0 DBs do not have stable OIDs?
> I cannot imagine any way to force those to match across PG versions
> that would not be an unsustainable crock.
Well, it's interesting that you mention that, because there's a
comment in the patch that probably has to do with this:
+ /*
+ * Make sure that pg_upgrade does not change database OID. Don't care
+ * about "postgres" database, backend will assign it fixed OID anyway.
+ * ("template1" has fixed OID too but the value 1 should not collide with
+ * any other OID so backend pays no attention to it.)
+ */
I wasn't able to properly understand that comment, and to be honest
I'm not sure I precisely understand your concern either. I don't quite
see why the template0 database matters. I think that database isn't
going to be dumped, or restored, so as far as pg_upgrade is concerned
it might as well not exist in either cluster, and I don't see why
pg_upgrade can't therefore just ignore it completely. But template1
and postgres are another matter. If I understand correctly, those
databases are going to be created in the new cluster by initdb, but
then pg_upgrade is going to populate them with data - including
relation files - from the old cluster. And, yeah, I don't see how we
could make those database OIDs match, which is not great.
To be honest, what I'd be inclined to do about that is just nail down
those OIDs for future releases. In fact, I'd probably go so far as to
hardcode that in such a way that even if you drop those databases and
recreate them, they get recreated with the same hard-coded OID. Now
that doesn't do anything to create stability when people upgrade from
an old release to a current one, but I don't really see that as an
enormous problem. The only hard requirement for this feature is if we
use the database OID for some kind of encryption or integrity checking
or checksum type feature. Then, you want to avoid having the database
OID change when you upgrade, so that the encryption or integrity check
or checksum in question does not have to be recomputed for every page
as part of pg_upgrade. But, that only matters if you're going between
two releases that support that feature, which will not be the case if
you're upgrading from some old release. Apart from that kind of
feature, it still seems like a nice-to-have to keep database OIDs the
same, but if those cases end up as exceptions, oh well.
Does that seem reasonable, or am I missing something big?
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 17:54 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 2 replies; 90+ messages in thread
From: Tom Lane @ 2021-08-17 17:54 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> I wasn't able to properly understand that comment, and to be honest
> I'm not sure I precisely understand your concern either. I don't quite
> see why the template0 database matters. I think that database isn't
> going to be dumped, or restored, so as far as pg_upgrade is concerned
> it might as well not exist in either cluster, and I don't see why
> pg_upgrade can't therefore just ignore it completely. But template1
> and postgres are another matter. If I understand correctly, those
> databases are going to be created in the new cluster by initdb, but
> then pg_upgrade is going to populate them with data - including
> relation files - from the old cluster.
Right. If pg_upgrade explicitly ignores template0 then its OID
need not be stable ... at least, not unless there's a chance it
could conflict with some other database OID, which would become
a live possibility if we let users get at "WITH OID = n".
(Having said that, I'm not sure that pg_upgrade special-cases
template0, or that it should do so.)
> To be honest, what I'd be inclined to do about that is just nail down
> those OIDs for future releases.
Yeah, I was thinking along similar lines.
> In fact, I'd probably go so far as to
> hardcode that in such a way that even if you drop those databases and
> recreate them, they get recreated with the same hard-coded OID.
Less sure that this is a good idea, though. In particular, I do not
think that you can make it work in the face of
alter database template1 rename to oops;
create database template1;
> The only hard requirement for this feature is if we
> use the database OID for some kind of encryption or integrity checking
> or checksum type feature.
It's fairly unclear to me why that is so important as to justify the
amount of klugery that this line of thought seems to be bringing.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 17:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 90+ messages in thread
From: Tom Lane @ 2021-08-17 17:57 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
I wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
>> The only hard requirement for this feature is if we
>> use the database OID for some kind of encryption or integrity checking
>> or checksum type feature.
> It's fairly unclear to me why that is so important as to justify the
> amount of klugery that this line of thought seems to be bringing.
And, not to put too fine a point on it, how will you possibly do
that without entirely breaking CREATE DATABASE?
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 18:07 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 0 replies; 90+ messages in thread
From: Shruthi Gowda @ 2021-08-17 18:07 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 17, 2021 at 11:07 PM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Tue, Aug 17, 2021 at 12:42 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > Actually though ... I've not read the patch, but what does it do about
> > the fact that the postgres and template0 DBs do not have stable OIDs?
> > I cannot imagine any way to force those to match across PG versions
> > that would not be an unsustainable crock.
>
> Well, it's interesting that you mention that, because there's a
> comment in the patch that probably has to do with this:
>
> + /*
> + * Make sure that pg_upgrade does not change database OID. Don't care
> + * about "postgres" database, backend will assign it fixed OID anyway.
> + * ("template1" has fixed OID too but the value 1 should not collide with
> + * any other OID so backend pays no attention to it.)
> + */
>
In the original patch the author intended to avoid dumping the
postgres DB OID like below:
+ if (dopt->binary_upgrade && dbCatId.oid != PostgresDbOid)
Since postgres OID is not hardcoded/fixed I removed the check.
My bad I missed updating the comment section. Sorry for the confusion.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 18:32 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-17 18:32 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 17, 2021 at 11:56:30AM -0400, Robert Haas wrote:
> On Wed, Aug 11, 2021 at 3:41 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > I have fixed all the issues and now the patch is working as expected.
>
> Hi,
>
> I'm changing the subject line since the patch does something which was
> discussed on that thread but isn't really related to the old email
> subject. In general, I think this patch is uncontroversial and in
> reasonably good shape. However, there's one part that I'm not too sure
> about. If Tom Lane happens to be paying attention to this thread, I
> think his feedback would be particularly useful, since he knows a lot
> about the inner workings of pg_dump. Opinions from anybody else would
> be great, too. Anyway, here's the hunk that worries me:
What is the value of preserving db/ts/relfilenode OIDs?
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-17 18:50 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-17 18:50 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 17, 2021 at 1:54 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Right. If pg_upgrade explicitly ignores template0 then its OID
> need not be stable ... at least, not unless there's a chance it
> could conflict with some other database OID, which would become
> a live possibility if we let users get at "WITH OID = n".
Well, that might be a good reason not to let them do that, then, at
least for n<64k.
> > In fact, I'd probably go so far as to
> > hardcode that in such a way that even if you drop those databases and
> > recreate them, they get recreated with the same hard-coded OID.
>
> Less sure that this is a good idea, though. In particular, I do not
> think that you can make it work in the face of
> alter database template1 rename to oops;
> create database template1;
That is a really good point. If we can't categorically force the OID
of those databases to have a particular, fixed value, and based on
this example that seems to be impossible, then there's always a
possibility that we might find a value in the old cluster that doesn't
happen to match what is present in the new cluster. Seen from that
angle, the problem is really with databases that are pre-existent in
the new cluster but whose contents still need to be dumped. Maybe we
could (optionally? conditionally?) drop those databases from the new
cluster and then recreate them with the OID that we want them to have.
> > The only hard requirement for this feature is if we
> > use the database OID for some kind of encryption or integrity checking
> > or checksum type feature.
>
> It's fairly unclear to me why that is so important as to justify the
> amount of klugery that this line of thought seems to be bringing.
Well, I think it would make sense to figure out how small we can make
the kludge first, and then decide whether it's larger than we can
tolerate. From my point of view, I completely understand why people to
whom those kinds of features are important want to include all the
fields that make up a buffer tag in the checksum or other integrity
check. Right now, if somebody copies a page from one place to another,
or if the operating system fumbles things and switches some pages
around, we have no direct way of detecting that anything bad has
happened. This is not the only problem that would need to be solved in
order to fix that, but it's one of them, and I don't particularly see
why it's not a valid goal. It's not as if a 16-bit checksum that is
computed in exactly the same way for every page in the cluster is such
state-of-the-art technology that only fools question its surpassing
excellence.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-20 17:36 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-08-20 17:36 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
> The rest of this email will be detailed review comments on the patch
> as presented, and thus probably only interesting to someone actually
> working on the patch. Feel free to skip if that's not you.
>
> - I suggest splitting the patch into one portion that deals with
> database OID and another portion that deals with tablespace OID and
> relfilenode OID, or maybe splitting it all the way into three separate
> patches, one for each. This could allow the uncontroversial parts to
> get committed first while we're wondering what to do about the problem
> described above.
Thanks Robert for your comments.
I have split the patch into two portions. One that handles DB OID and
the other that
handles tablespace OID and relfilenode OID.
> - There are two places in the patch, one in dumpDatabase() and one in
> generate_old_dump() where blank lines are removed with no other
> changes. Please avoid whitespace-only hunks.
These changes are avoided.
> - If possible, please try to pgindent the new code. It's pretty good
> what you did, but e.g. the declaration of
> binary_upgrade_next_pg_tablespace_oid probably has less whitespace
> than pgindent is going to want.
Taken care in latest patches
> - The comments in dumpDatabase() claim that "postgres" and "template1"
> are handled specially in some way, but there seems to be no code that
> matches those comments.
The comment is removed.
> - heap_create()'s logic around setting create_storage looks slightly
> redundant. I'm not positive what would be better, but ... suppose you
> just took the part that's currently gated by if (!IsBinaryUpgrade) and
> did it unconditionally. Then put if (IsBinaryUpgrade) around the else
> clause, but delete the last bit from there that sets create_storage.
> Maybe we'd still want a comment saying that it's intentional that
> create_storage = true even though it will be overwritten later, but
> then, I think, we wouldn't need to set create_storage in two different
> places. Maybe I'm wrong.
>
> - If we're not going to do that, then I think you should swap the if
> and else clauses and reverse the sense of the test. In createdb(),
> CreateTableSpace(), and a bunch of existing places, we do if
> (IsBinaryUpgrade) { ... } else { ... } so I don't think it makes sense
> for this one to instead do if (!IsBinaryUpgrade) { ... } else { ... }.
I have avoided the redundant code and removed the comment as it does
not make sense now that we are setting the create_storage conditionally.
(In the original patch, create_storage was set to TRUE by default for binary
upgrade case which was wrong and was hitting assert in the following flow).
> - I'm not sure that I'd bother renaming
> binary_upgrade_set_pg_class_oids_and_relfilenodes(). It's such a long
> name, and a relfilenode is kind of an OID, so the current name isn't
> even really wrong. I'd probably drop the header comment too, since it
> seems rather obvious. But both of these things are judgement calls.
I agree. I have retained the old function name.
> - Inside that function, there is a comment that says "Indexes cannot
> have toast tables, so we need not make this probe in the index code
> path." However, you have moved the code from someplace where it didn't
> happen for indexes to someplace where it happens for both tables and
> indexes. Therefore the comment, which was true when the code was where
> it was before, is now false. So you need to update it.
The comment is updated.
> - It is not clear to me why pg_upgrade's Makefile needs to be changed
> to include -DFRONTEND in CPPFLAGS. All of the .c files in this
> directory include postgres_fe.h rather than postgres.h, and that file
> has #define FRONTEND 1. Moreover, there are no actual code changes in
> this directory, so why should the Makefile need any change?
Makefile change is removed.
> - A couple of comment changes - and the commit message - mention data
> encryption, but that's not a feature that this patch implements, nor
> are we committed to adding it in the immediate future (or ever,
> really). So I think those places should be revised to say that we do
> this because we want the filenames to match between the old and new
> clusters, and leave the reasons why that might be a good thing up to
> the reader's imagination.
Taken care.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v2-0001-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 8.6K) download
From 6af195ee95bbde8e75fd6bc81350ede33256d866 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Fri, 20 Aug 2021 22:07:42 +0530
Subject: [PATCH v2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs during binary upgrade
so that the database OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/commands/dbcommands.c | 22 +++++++++++++---
src/backend/utils/adt/pg_upgrade_support.c | 11 ++++++++
src/bin/pg_dump/pg_dump.c | 30 ++++++++++++++++++++++
src/bin/pg_upgrade/pg_upgrade.c | 3 +++
src/include/catalog/binary_upgrade.h | 2 ++
src/include/catalog/pg_proc.dat | 4 +++
.../spgist_name_ops/expected/spgist_name_ops.out | 6 +++--
7 files changed, 72 insertions(+), 6 deletions(-)
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..a1e4a83 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -92,6 +92,7 @@ static void remove_dbtablespaces(Oid db_id);
static bool check_db_file_conflict(Oid db_id);
static int errdetail_busy_db(int notherbackends, int npreparedxacts);
+Oid binary_upgrade_next_pg_database_oid = InvalidOid;
/*
* CREATE DATABASE
@@ -504,11 +505,24 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ if (IsBinaryUpgrade)
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ if (!OidIsValid(binary_upgrade_next_pg_database_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_database OID value not set when in binary upgrade mode")));
+
+ dboid = binary_upgrade_next_pg_database_oid;
+ binary_upgrade_next_pg_database_oid = InvalidOid;
+ }
+ else
+ {
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..c499434 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_database_oid(PG_FUNCTION_ARGS)
+{
+ Oid dboid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_database_oid = dboid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 90ac445..0a938a2 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -47,6 +47,7 @@
#include "catalog/pg_authid_d.h"
#include "catalog/pg_cast_d.h"
#include "catalog/pg_class_d.h"
+#include "catalog/pg_database.h"
#include "catalog/pg_default_acl_d.h"
#include "catalog/pg_largeobject_d.h"
#include "catalog/pg_largeobject_metadata_d.h"
@@ -2781,6 +2782,7 @@ dumpDatabase(Archive *fout)
PQExpBuffer dbQry = createPQExpBuffer();
PQExpBuffer delQry = createPQExpBuffer();
PQExpBuffer creaQry = createPQExpBuffer();
+ PQExpBuffer setDBIdQry = createPQExpBuffer();
PQExpBuffer labelq = createPQExpBuffer();
PGconn *conn = GetConnection(fout);
PGresult *res;
@@ -2950,6 +2952,34 @@ dumpDatabase(Archive *fout)
qdatname = pg_strdup(fmtId(datname));
+ /* Make sure that pg_upgrade does not change database OID. */
+ if (dopt->binary_upgrade)
+ {
+ appendPQExpBufferStr(setDBIdQry, "\n-- For binary upgrade, must preserve pg_database oid\n");
+ appendPQExpBuffer(setDBIdQry,
+ "SELECT pg_catalog.binary_upgrade_set_next_pg_database_oid('%u'::pg_catalog.oid);\n",
+ dbCatId.oid);
+
+ dbDumpId = createDumpId();
+
+ /*
+ * Need a separate entry, otherwise the command will be run in the
+ * same transaction as the CREATE DATABASE command, which is not
+ * allowed.
+ */
+ ArchiveEntry(fout,
+ dbCatId, /* catalog ID */
+ dbDumpId, /* dump ID */
+ ARCHIVE_OPTS(.tag = datname,
+ .owner = dba,
+ .description = "SET_DB_OID",
+ .section = SECTION_PRE_DATA,
+ .createStmt = setDBIdQry->data,
+ .dropStmt = NULL));
+
+ destroyPQExpBuffer(setDBIdQry);
+ }
+
/*
* Prepare the CREATE DATABASE command. We must specify encoding, locale,
* and tablespace since those can't be altered later. Other DB properties
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..ad25710 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -22,6 +22,9 @@
* this, old/new pg_class.relfilenode values will not match if CLUSTER,
* REINDEX, or VACUUM FULL have been performed in the old cluster.
*
+ * We control assignment of pg_database.oid because we want the oid to match
+ * between the old and new cluster.
+ *
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
*
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..3809521 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,6 +14,8 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_database_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b603700..1d7ae19 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11036,6 +11036,10 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4548', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_database_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_database_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1dcdad4 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -56,10 +56,11 @@ select * from t
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
+ binary_upgrade_set_next_pg_database_oid | | binary_upgrade_set_next_pg_database_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+(10 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -101,10 +102,11 @@ select * from t
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
+ binary_upgrade_set_next_pg_database_oid | | binary_upgrade_set_next_pg_database_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+(10 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
v2-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg.patch (application/octet-stream, 20.6K) download
From 216bafaf9b0d081bdc670d64d162de75d5b9155c Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Fri, 20 Aug 2021 19:26:12 +0530
Subject: [PATCH v2] Preserve relfilenode and tablespace OID in pg_upgrade
The patch aims to preserve the OIDs of relfilenode and tablespace during binary
upgrade so that the OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/catalog/heap.c | 36 +++++++++
src/backend/catalog/index.c | 1 +
src/backend/commands/tablespace.c | 14 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 ++++++++++
src/bin/pg_dump/pg_dump.c | 94 ++++++++++++++--------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/include/catalog/binary_upgrade.h | 5 ++
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
10 files changed, 195 insertions(+), 43 deletions(-)
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 83746d3..f3c99f6 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -379,6 +381,40 @@ heap_create(const char *relname,
relfilenode = relid;
}
+ if (IsBinaryUpgrade)
+ {
+ /* Override relfilenode? */
+ if (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
+ relkind == RELKIND_MATVIEW)
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+ }
+ else if (relkind == RELKIND_TOASTVALUE)
+ {
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
+ }
+ else if (relkind == RELKIND_INDEX)
+ {
+ if (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_class index relfilenode value not set when in binary upgrade mode")));
+ relfilenode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
+ }
+ }
+
/*
* Never allow a pg_class entry to explicitly specify the database's
* default tablespace in reltablespace; force it to zero instead. This
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 26bfa74..a124617 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -86,6 +86,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index a54239a..34e5746 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,7 +336,18 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ if (IsBinaryUpgrade)
+ {
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..d4dc284 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_pg_class_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_pg_class_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_pg_class_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 90ac445..6ced774 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4700,48 +4700,63 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid pg_class_relfilenode;
+ Oid pg_class_reltoastrelid;
+ Oid pg_class_relfilenode_toast;
+ char pg_class_relkind;
+ Oid pg_index_indexrelid;
+ Oid pg_class_relfilenode_toast_idx;
+
+ /*
+ * Preserve the OIDs of the table's toast table and index, if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relfilenode, c.relkind, c.reltoastrelid, ct.relfilenode AS relfilenode_toast, i.indexrelid, cti.relfilenode AS relfilenode_toast_index "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ pg_class_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ pg_class_relfilenode_toast = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode_toast")));
+ pg_class_relkind = *PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relkind"));
+ pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ pg_class_relfilenode_toast_idx = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode_toast_index")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
- /*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
- */
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ /* Not every relation has storage. */
+ if (OidIsValid(pg_class_relfilenode))
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_relfilenode('%u'::pg_catalog.oid);\n",
+ pg_class_relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
@@ -4753,20 +4768,31 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_reltoastrelid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_relfilenode('%u'::pg_catalog.oid);\n",
+ pg_class_relfilenode_toast);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_index_indexrelid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_relfilenode('%u'::pg_catalog.oid);\n",
+ pg_class_relfilenode_toast_idx);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_relfilenode('%u'::pg_catalog.oid);\n",
+ pg_class_relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index c291017..618187c 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1265,6 +1265,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..acaf343 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..4ba5748 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index b603700..0626525 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11036,6 +11036,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4544', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_pg_class_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_pg_class_relfilenode' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_pg_class_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_pg_class_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_pg_class_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_pg_class_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..8541b4a 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_pg_class_relfilenode | | binary_upgrade_set_next_heap_pg_class_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_pg_class_relfilenode | 1 | binary_upgrade_set_next_index_pg_class_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_pg_class_relfilenode | 1 | binary_upgrade_set_next_toast_pg_class_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_pg_class_relfilenode | | binary_upgrade_set_next_heap_pg_class_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_pg_class_relfilenode | 1 | binary_upgrade_set_next_index_pg_class_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_pg_class_relfilenode | 1 | binary_upgrade_set_next_toast_pg_class_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-23 20:57 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 3 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-23 20:57 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Fri, Aug 20, 2021 at 1:36 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> Thanks Robert for your comments.
> I have split the patch into two portions. One that handles DB OID and
> the other that
> handles tablespace OID and relfilenode OID.
It's pretty clear from the discussion, I think, that the database OID
one is going to need rework to be considered.
Regarding the other one:
- The comment in binary_upgrade_set_pg_class_oids() is still not
accurate. You removed the sentence which says "Indexes cannot have
toast tables, so we need not make this probe in the index code path"
but the immediately preceding sentence is still inaccurate in at least
two ways. First, it only talks about tables, but the code now applies
to indexes. Second, it only talks about OIDs, but now also deals with
refilenodes. It's really important to fully update every comment that
might be affected by your changes!
- The SQL query in that function isn't completely correct. There is a
left join from pg_class to pg_index whose ON clause includes
"c.reltoastrelid = i.indrelid AND i.indisvalid." The reason it's
likely that is because it is possible, in corner cases, for a TOAST
table to have multiple TOAST indexes. I forget exactly how that
happens, but I think it might be like if a REINDEX CONCURRENTLY on the
TOAST table fails midway through, or something of that sort. Now if
that happens, the LEFT JOIN you added is going to cause the output to
contain multiple rows, because you didn't replicate the i.indisvalid
condition into that ON clause. And then it will fail. Apparently we
don't have a pg_upgrade test case for this scenario; we probably
should. Actually what I think would be even better than putting
i.indisvalid into that ON clause would be to join off of i.indrelid
rather than c.reltoastrelid.
- The code that decodes the various columns of this query does so in a
slightly different order than the query itself. It would be better to
make it match. Perhaps put relkind first in both cases. I might also
think about trying to make the column naming a bit more consistent,
e.g. relkind, relfilenode, toast_oid, toast_relfilenode,
toast_index_oid, toast_index_relfilenode.
- In heap_create(), the wording of the error messages is not quite
consistent. You have "relfilenode value not set when in binary upgrade
mode", "toast relfilenode value not set when in binary upgrade mode",
and "pg_class index relfilenode value not set when in binary upgrade
mode". Why does the last one mention pg_class when the other two
don't?
- The code in heap_create() now has no comments whatsoever, which is a
shame, because it's actually kind of a tricky bit of logic. Someone
might wonder why we override the relfilenode inside that function
instead of doing it at the same places where we absorb
binary_upgrade_next_{heap,index,toast}_pg_class_oid and the passing
down the relfilenode. I think the answer is that passing down the
relfilenode from the caller would result in storage not actually being
created, whereas in this case we want it to be created but just with
the value we specify, and the reason we want that is because we need
later DDL that happens after these statements but before the old
cluster's relations are moved over to execute successfully, which it
won't if the storage is altogether absent.
However, that raises the question of whether this patch has even got
the basic design right. Maybe we ought to actually be absorbing the
relfilenode setting at the same places where we're doing so for the
OID, and then passing an additional parameter to heap_create() like
bool suppress_storage or something like that. Maybe, taking it even
further, we ought to be changing the signatures of
binary_upgrade_next_heap_pg_class_oid and friends to be two-argument
functions, and pass down the OID and the relfilenode in the same call,
rather than calling two separate functions. I'm not so much concerned
about the cost of calling two functions as the potential for
confusion. I'm not honestly sure that either of these changes are the
right thing to do, but I am pretty strongly inclined to do at least
the first part - trying to absorb reloid and relfilenode in the same
places. If we're not going to do that we certainly need to explain why
we're doing it the way we are in the comments.
It's not really this patch's fault, but it would sure be nice if we
had some better testing for this area. Suppose this patch somehow
changed nothing from the present behavior. How would we know? Or
suppose it managed to somehow set all the relfilenodes in the new
cluster to random values rather than the intended one? There's no
automated testing that would catch any of that, and it's not obvious
how it could be added to test.sh. I suppose what we really need to do
at some point is rewrite that as a TAP test, but that seems like a
separate project from this patch.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-23 21:12 Stephen Frost <sfrost@snowman.net>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-23 21:12 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
Greetings,
* Robert Haas (robertmhaas@gmail.com) wrote:
> On Fri, Aug 20, 2021 at 1:36 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Thanks Robert for your comments.
> > I have split the patch into two portions. One that handles DB OID and
> > the other that
> > handles tablespace OID and relfilenode OID.
>
> It's pretty clear from the discussion, I think, that the database OID
> one is going to need rework to be considered.
Regarding that ... I have to wonder just what promises we feel we've
made when it comes to what a user is expected to be able to do with the
new cluster *before* pg_upgrade is run on it. For my part, I sure feel
like it's "nothing", in which case it seems like we can do things that
we can't do with a running system, like literally just DROP and recreate
with the correct OID of any databases we need to, or even push that back
to the user to do that at initdb time with some kind of error thrown by
pg_upgrade during the --check phase. "Initial databases have
non-standard OIDs, recreate destination cluster with initdb
--with-oid=12341" or something along those lines.
Also open to the idea of simply forcing 'template1' to always being
OID=1 even if it's dropped/recreated and then just dropping/recreating
the template0 and postgres databases if they've got different OIDs than
what the old cluster did- after all, they should be getting entirely
re-populated as part of the pg_upgrade process itself.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 00:29 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 2 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 00:29 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Aug 23, 2021 at 04:57:31PM -0400, Robert Haas wrote:
> On Fri, Aug 20, 2021 at 1:36 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Thanks Robert for your comments.
> > I have split the patch into two portions. One that handles DB OID and
> > the other that
> > handles tablespace OID and relfilenode OID.
>
> It's pretty clear from the discussion, I think, that the database OID
> one is going to need rework to be considered.
I assume this patch is not going to be applied until there is an actual
use case for preserving these values.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 09:10 Shruthi Gowda <gowdashru@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 0 replies; 90+ messages in thread
From: Shruthi Gowda @ 2021-08-24 09:10 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 5:59 AM Bruce Momjian <bruce@momjian.us> wrote:
>
> On Mon, Aug 23, 2021 at 04:57:31PM -0400, Robert Haas wrote:
> > On Fri, Aug 20, 2021 at 1:36 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > > Thanks Robert for your comments.
> > > I have split the patch into two portions. One that handles DB OID and
> > > the other that
> > > handles tablespace OID and relfilenode OID.
> >
> > It's pretty clear from the discussion, I think, that the database OID
> > one is going to need rework to be considered.
>
> I assume this patch is not going to be applied until there is an actual
> use case for preserving these values.
JFI, I added an entry into commit fest for this patch.
link: https://commitfest.postgresql.org/34/3296/
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 15:24 Robert Haas <robertmhaas@gmail.com>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 2 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-24 15:24 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Aug 23, 2021 at 5:12 PM Stephen Frost <sfrost@snowman.net> wrote:
> Regarding that ... I have to wonder just what promises we feel we've
> made when it comes to what a user is expected to be able to do with the
> new cluster *before* pg_upgrade is run on it. For my part, I sure feel
> like it's "nothing", in which case it seems like we can do things that
> we can't do with a running system, like literally just DROP and recreate
> with the correct OID of any databases we need to, or even push that back
> to the user to do that at initdb time with some kind of error thrown by
> pg_upgrade during the --check phase. "Initial databases have
> non-standard OIDs, recreate destination cluster with initdb
> --with-oid=12341" or something along those lines.
Yeah, possibly. Honestly, I find it weird that pg_upgrade expects the
new cluster to already exist. It seems like it would be more sensible
if it created the cluster itself. That's not entirely trivial, because
for example you have to create it with the correct locale settings and
stuff. But if you require the cluster to exist already, then you run
into the kinds of questions that you're asking here, and whether the
answer is "nothing" as you propose here or something more than that,
it's clearly not "whatever you want" nor anything close to that.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 15:28 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 2 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-24 15:28 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Aug 23, 2021 at 8:29 PM Bruce Momjian <bruce@momjian.us> wrote:
> I assume this patch is not going to be applied until there is an actual
> use case for preserving these values.
My interpretation of the preceding discussion was that several people
thought this change was a good idea regardless of whether anything
ever happens with TDE, so I wasn't seeing a reason to wait.
Personally, I've always thought that it was quite odd that pg_upgrade
didn't preserve the relfilenode values, so I'm in favor of the change.
I bet we could even make some simplifications to that code if we got
all of this sorted out, which seems like it would be nice.
I think it was also mentioned that this might be nice for pgBackRest,
which apparently permits incremental backups across major version
upgrades but likes filenames to match.
That being said, if you or somebody else thinks that this is a bad
idea or that the reasons offered up until now are insufficient, feel
free to make that argument. I just work here...
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 16:04 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 2 replies; 90+ messages in thread
From: Tom Lane @ 2021-08-24 16:04 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Bruce Momjian <bruce@momjian.us>; Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> On Mon, Aug 23, 2021 at 8:29 PM Bruce Momjian <bruce@momjian.us> wrote:
>> I assume this patch is not going to be applied until there is an actual
>> use case for preserving these values.
> ...
> That being said, if you or somebody else thinks that this is a bad
> idea or that the reasons offered up until now are insufficient, feel
> free to make that argument. I just work here...
Per upthread discussion, it seems impractical to fully guarantee
that database OIDs match, which seems to mean that the whole premise
collapses. Like Bruce, I want to see a plausible use case justifying
any partial-guarantee scenario before we add more complication (= bugs)
to pg_upgrade.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 16:39 Bruce Momjian <bruce@momjian.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 16:39 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 24, 2021 at 12:04:00PM -0400, Tom Lane wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
> > On Mon, Aug 23, 2021 at 8:29 PM Bruce Momjian <bruce@momjian.us> wrote:
> >> I assume this patch is not going to be applied until there is an actual
> >> use case for preserving these values.
>
> > ...
>
> > That being said, if you or somebody else thinks that this is a bad
> > idea or that the reasons offered up until now are insufficient, feel
> > free to make that argument. I just work here...
>
> Per upthread discussion, it seems impractical to fully guarantee
> that database OIDs match, which seems to mean that the whole premise
> collapses. Like Bruce, I want to see a plausible use case justifying
> any partial-guarantee scenario before we add more complication (= bugs)
> to pg_upgrade.
Yes, pg_upgrade is already complex enough, so why add more complexity
for some cosmetic value. (I think "cosmetic" flew out the window with
pg_upgrade long ago. ;-) )
I know that pgBackRest has asked for stable relfilenodes to make
incremental file system backups after pg_upgrade smaller, but if we want
to make relfilenodes stable, we had better understand that is _why_ we
are adding this complexity.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 16:40 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 16:40 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 11:28:37AM -0400, Robert Haas wrote:
> On Mon, Aug 23, 2021 at 8:29 PM Bruce Momjian <bruce@momjian.us> wrote:
> > I assume this patch is not going to be applied until there is an actual
> > use case for preserving these values.
>
> My interpretation of the preceding discussion was that several people
> thought this change was a good idea regardless of whether anything
> ever happens with TDE, so I wasn't seeing a reason to wait.
> Personally, I've always thought that it was quite odd that pg_upgrade
> didn't preserve the relfilenode values, so I'm in favor of the change.
> I bet we could even make some simplifications to that code if we got
> all of this sorted out, which seems like it would be nice.
Yes, if this ends up being a cleanup with no added complexity, that
would be nice, but I had not seen how that was possible in the psat.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 16:43 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 2 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 16:43 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 11:24:21AM -0400, Robert Haas wrote:
> On Mon, Aug 23, 2021 at 5:12 PM Stephen Frost <sfrost@snowman.net> wrote:
> > Regarding that ... I have to wonder just what promises we feel we've
> > made when it comes to what a user is expected to be able to do with the
> > new cluster *before* pg_upgrade is run on it. For my part, I sure feel
> > like it's "nothing", in which case it seems like we can do things that
> > we can't do with a running system, like literally just DROP and recreate
> > with the correct OID of any databases we need to, or even push that back
> > to the user to do that at initdb time with some kind of error thrown by
> > pg_upgrade during the --check phase. "Initial databases have
> > non-standard OIDs, recreate destination cluster with initdb
> > --with-oid=12341" or something along those lines.
>
> Yeah, possibly. Honestly, I find it weird that pg_upgrade expects the
> new cluster to already exist. It seems like it would be more sensible
> if it created the cluster itself. That's not entirely trivial, because
> for example you have to create it with the correct locale settings and
> stuff. But if you require the cluster to exist already, then you run
> into the kinds of questions that you're asking here, and whether the
> answer is "nothing" as you propose here or something more than that,
> it's clearly not "whatever you want" nor anything close to that.
Yes, it is a trade-off. If we had pg_upgrade create the new cluster,
the pg_upgrade instructions would be simpler, but pg_upgrade would be
more complex since it has to adjust _everything_ properly so pg_upgrade
works --- I never got to that point, but I am willing to explore what
would be required.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 17:23 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-24 17:23 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Bruce Momjian <bruce@momjian.us>; Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 24, 2021 at 12:04 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Per upthread discussion, it seems impractical to fully guarantee
> that database OIDs match, which seems to mean that the whole premise
> collapses. Like Bruce, I want to see a plausible use case justifying
> any partial-guarantee scenario before we add more complication (= bugs)
> to pg_upgrade.
I think you might be overlooking the emails from Stephen and I where
we suggested how that could be made to work?
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 17:25 Stephen Frost <sfrost@snowman.net>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 0 replies; 90+ messages in thread
From: Stephen Frost @ 2021-08-24 17:25 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
Greetings,
* Robert Haas (robertmhaas@gmail.com) wrote:
> On Mon, Aug 23, 2021 at 5:12 PM Stephen Frost <sfrost@snowman.net> wrote:
> > Regarding that ... I have to wonder just what promises we feel we've
> > made when it comes to what a user is expected to be able to do with the
> > new cluster *before* pg_upgrade is run on it. For my part, I sure feel
> > like it's "nothing", in which case it seems like we can do things that
> > we can't do with a running system, like literally just DROP and recreate
> > with the correct OID of any databases we need to, or even push that back
> > to the user to do that at initdb time with some kind of error thrown by
> > pg_upgrade during the --check phase. "Initial databases have
> > non-standard OIDs, recreate destination cluster with initdb
> > --with-oid=12341" or something along those lines.
>
> Yeah, possibly. Honestly, I find it weird that pg_upgrade expects the
> new cluster to already exist. It seems like it would be more sensible
> if it created the cluster itself. That's not entirely trivial, because
> for example you have to create it with the correct locale settings and
> stuff. But if you require the cluster to exist already, then you run
> into the kinds of questions that you're asking here, and whether the
> answer is "nothing" as you propose here or something more than that,
> it's clearly not "whatever you want" nor anything close to that.
Yeah, I'd had a similar thought and also tend to agree that it'd make
more sense for pg_upgrade to set up the new cluster too, and doing so in
a way that makes sure that it matches the old cluster as that's rather
important. Having the user do it also implies that there is some
freedom for the user to mess around with the new cluster before running
pg_upgrade, it seems to me anyway, and that's certainly not something
that we've built anything into pg_upgrade to deal with cleanly..
It isn't like initdb takes all *that* long to run either, and reducing
the number of steps that the user has to take to perform an upgrade sure
seems like a good thing to do. Anyhow, just wanted to throw that out
there as another way we might approach this.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 17:26 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-24 17:26 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 12:43 PM Bruce Momjian <bruce@momjian.us> wrote:
> Yes, it is a trade-off. If we had pg_upgrade create the new cluster,
> the pg_upgrade instructions would be simpler, but pg_upgrade would be
> more complex since it has to adjust _everything_ properly so pg_upgrade
> works --- I never got to that point, but I am willing to explore what
> would be required.
It's probably a topic for another thread, rather than this one, but I
think that would be very cool.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 17:46 Stephen Frost <sfrost@snowman.net>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Stephen Frost @ 2021-08-24 17:46 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Bruce Momjian <bruce@momjian.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
Greetings,
* Robert Haas (robertmhaas@gmail.com) wrote:
> On Tue, Aug 24, 2021 at 12:43 PM Bruce Momjian <bruce@momjian.us> wrote:
> > Yes, it is a trade-off. If we had pg_upgrade create the new cluster,
> > the pg_upgrade instructions would be simpler, but pg_upgrade would be
> > more complex since it has to adjust _everything_ properly so pg_upgrade
> > works --- I never got to that point, but I am willing to explore what
> > would be required.
>
> It's probably a topic for another thread, rather than this one, but I
> think that would be very cool.
Yes, definite +1 on this.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 18:16 Bruce Momjian <bruce@momjian.us>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 18:16 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 12:43:20PM -0400, Bruce Momjian wrote:
> Yes, it is a trade-off. If we had pg_upgrade create the new cluster,
> the pg_upgrade instructions would be simpler, but pg_upgrade would be
> more complex since it has to adjust _everything_ properly so pg_upgrade
> works --- I never got to that point, but I am willing to explore what
> would be required.
One other issue --- the more that pg_upgrade preserves, the more likely
pg_upgrade will break when some internal changes happen in Postgres.
Therefore, if you want pg_upgrade to preserve something, you have to
have a good reason --- even code simplicity might not be a sufficient
reason.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 18:34 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-24 18:34 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 2:16 PM Bruce Momjian <bruce@momjian.us> wrote:
> One other issue --- the more that pg_upgrade preserves, the more likely
> pg_upgrade will break when some internal changes happen in Postgres.
> Therefore, if you want pg_upgrade to preserve something, you have to
> have a good reason --- even code simplicity might not be a sufficient
> reason.
While I accept that as a general principle, I don't think it's really
applicable in this case. pg_upgrade already knows all about
relfilenodes; it has a source file called relfilenode.c. I don't see
that a pg_upgrade that preserves relfilenodes is any more or less
likely to break in the future than a pg_upgrade that renumbers all the
files so that the relation OID and the relfilenode are equal. You've
got about the same amount of reliance on the on-disk layout either
way.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-24 18:49 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-24 18:49 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 02:34:26PM -0400, Robert Haas wrote:
> On Tue, Aug 24, 2021 at 2:16 PM Bruce Momjian <bruce@momjian.us> wrote:
> > One other issue --- the more that pg_upgrade preserves, the more likely
> > pg_upgrade will break when some internal changes happen in Postgres.
> > Therefore, if you want pg_upgrade to preserve something, you have to
> > have a good reason --- even code simplicity might not be a sufficient
> > reason.
>
> While I accept that as a general principle, I don't think it's really
> applicable in this case. pg_upgrade already knows all about
> relfilenodes; it has a source file called relfilenode.c. I don't see
> that a pg_upgrade that preserves relfilenodes is any more or less
> likely to break in the future than a pg_upgrade that renumbers all the
> files so that the relation OID and the relfilenode are equal. You've
> got about the same amount of reliance on the on-disk layout either
> way.
I was making more of a general statement that preservation can be
problematic and its impact must be researched.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:00 Robert Haas <robertmhaas@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 2 replies; 90+ messages in thread
From: Robert Haas @ 2021-08-26 15:00 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Tue, Aug 17, 2021 at 2:50 PM Robert Haas <robertmhaas@gmail.com> wrote:
> > Less sure that this is a good idea, though. In particular, I do not
> > think that you can make it work in the face of
> > alter database template1 rename to oops;
> > create database template1;
>
> That is a really good point. If we can't categorically force the OID
> of those databases to have a particular, fixed value, and based on
> this example that seems to be impossible, then there's always a
> possibility that we might find a value in the old cluster that doesn't
> happen to match what is present in the new cluster. Seen from that
> angle, the problem is really with databases that are pre-existent in
> the new cluster but whose contents still need to be dumped. Maybe we
> could (optionally? conditionally?) drop those databases from the new
> cluster and then recreate them with the OID that we want them to have.
Actually, we do that already. create_new_objects() runs pg_restore
with --create for most databases, but with --clean --create for
template1 and postgres. This means that template1 and postgres will
always be recreated in the new cluster, and other databases are
assumed not to exist in the new cluster and the upgrade will fail if
they unexpectedly do. And the reason why pg_upgrade does that is that
it wants to "propagate [the] database-level properties" of postgres
and template1. So suppose we just make the database OID one of the
database-level properties that we want to propagate. That should
mostly just work, but where can things go wrong?
The only real failure mode is we try to create a database in the new
cluster and find out that the OID is already in use. If the new OID
that collides >64k, then the user has messed with the new cluster
before doing that. And since pg_upgrade is pretty clearly already
assuming that you shouldn't do that, it's fine to also make that
assumption in this case. We can disregard such cases as user error.
If the new OID that collides is <64k, then it must be colliding with
template0, template1, or postgres in the new cluster, because those
are the only databases that can have such OIDs since, currently, we
don't allow users to specify an OID for a new database. And the
problem cannot be with template1, because we hard-code its OID to 1.
If there is a database with OID 1 in either cluster, it must be
template1, and if there is a database with OID 1 in both clusters, it
must be template1 in both cases, and we'll just drop and recreate it
with OID 1 and everything is fine. So we need only consider template0
and postgres, which are created with system-generated OIDs. And, it
would be no issue if either of those databases had the same OID in the
old and new cluster, so the only possible OID collision is one where
the same system-generated OID was assigned to one of those databases
in the old cluster and to the other in the new cluster.
First consider the case where template0 has OID, say, 13000, in the
old cluster, and postgres has that OID in the new cluster. No problem
occurs, because template0 isn't transferred anyway. The reverse
direction is a problem, though. If postgres had been assigned OID
13000 in the old cluster and, by sheer chance, template0 had that OID
in the new cluster, then the upgrade would fail, because it wouldn't
be able to recreate the postgres database with the correct OID.
But that doesn't seem very difficult to fix. I think all we need to do
is have initdb assign a fixed OID to template0 at creation time. Then,
in any new release to which someone might be trying to upgrade, the
system-generated OID assigned to postgres in the old release can't
match the fixed OID assigned to template0 in the new release, so the
one problem case is ruled out. We do need, however, to make sure that
the assign-my-database-a-fixed-OID syntax is either entirely
restricted to initdb & pg_upgrade or at least that OIDS < 64k can only
be assigned in one of those modes. Otherwise, some creative person
could manufacture new problem cases by setting up the source database
so that the OID of one of their databases matches the fixed OID we
gave to template0 or template1, or the system-generated OID for
postgres in the new cluster.
In short, as far as I can see, all we need to do to preserve database
OIDs across pg_upgrade is:
1. Add a new syntax for creating a database with a given OID, and use
it in pg_dump --binary-upgrade.
2. Don't let users use it at least for OIDs <64k, or maybe just don't
let them use it at all.
3. But let initdb use it, and have initdb set the initial OID for
template0 to a fixed value < 10000. If the user changes it later, no
problem; the cluster into which they are upgrading won't contain any
databases with high-numbered OIDs.
Anyone see a flaw in that analysis?
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:24 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 2 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 15:24 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> Anyone see a flaw in that analysis?
I am still waiting to hear the purpose of this preservation. As long as
you don't apply the patch, I guess I will just stop asking.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:35 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-26 15:35 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:24 AM Bruce Momjian <bruce@momjian.us> wrote:
> On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > Anyone see a flaw in that analysis?
>
> I am still waiting to hear the purpose of this preservation. As long as
> you don't apply the patch, I guess I will just stop asking.
You make it sound like I didn't answer that question the last time you
asked it, but I did.[1] I went back to the previous thread and found
that, in fact, there's at least one email *from you* appearing to
endorse that concept for reasons unrelated to TDE[2] and another where
you appear to agree that it would be useful for TDE to do it.[3]
Stephen Frost also wrote up his discussion during the Unconference and
some of his reasons for liking the idea.[4]
If you've changed your mind about this being a good idea, or if you no
longer think it's useful without TDE, that's fine. Everyone is
entitled to change their opinion. But then please say that straight
out. It baffles me why you're now acting as if it hasn't been
discussed when it clearly has been, and both you and I were
participants in that discussion.
[1] https://www.postgresql.org/message-id/CA+Tgmob7msyh3VRaY87USr22UakvvSyy4zBaQw2AO2CfoUD3rA@mail.gmail...
[2] https://www.postgresql.org/message-id/20210601140949.GC22012@momjian.us
[3] https://www.postgresql.org/message-id/20210527210023.GJ5646@momjian.us
[4] https://www.postgresql.org/message-id/20210531201652.GY20766@tamriel.snowman.net
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:36 Stephen Frost <sfrost@snowman.net>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 15:36 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Bruce Momjian (bruce@momjian.us) wrote:
> On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > Anyone see a flaw in that analysis?
>
> I am still waiting to hear the purpose of this preservation. As long as
> you don't apply the patch, I guess I will just stop asking.
I'm a bit confused why this question keeps coming up as we've discussed
multiple reasons (incremental backups, possible use for TDE which would
make this required, general improved sanity when working with pg_upgrade
is frankly a benefit in its own right too...). If the additional code
was a huge burden or even a moderate one then that might be an argument
against, but it hardly sounds like it will be given Robert's thorough
analysis so far and the (admittedly not complete, but not that far from
it based on the DB OID review) proposed patch.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:39 Stephen Frost <sfrost@snowman.net>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 15:39 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Robert Haas (robertmhaas@gmail.com) wrote:
> On Tue, Aug 17, 2021 at 2:50 PM Robert Haas <robertmhaas@gmail.com> wrote:
> > > Less sure that this is a good idea, though. In particular, I do not
> > > think that you can make it work in the face of
> > > alter database template1 rename to oops;
> > > create database template1;
> >
> > That is a really good point. If we can't categorically force the OID
> > of those databases to have a particular, fixed value, and based on
> > this example that seems to be impossible, then there's always a
> > possibility that we might find a value in the old cluster that doesn't
> > happen to match what is present in the new cluster. Seen from that
> > angle, the problem is really with databases that are pre-existent in
> > the new cluster but whose contents still need to be dumped. Maybe we
> > could (optionally? conditionally?) drop those databases from the new
> > cluster and then recreate them with the OID that we want them to have.
>
> Actually, we do that already. create_new_objects() runs pg_restore
> with --create for most databases, but with --clean --create for
> template1 and postgres. This means that template1 and postgres will
> always be recreated in the new cluster, and other databases are
> assumed not to exist in the new cluster and the upgrade will fail if
> they unexpectedly do. And the reason why pg_upgrade does that is that
> it wants to "propagate [the] database-level properties" of postgres
> and template1. So suppose we just make the database OID one of the
> database-level properties that we want to propagate. That should
> mostly just work, but where can things go wrong?
>
> The only real failure mode is we try to create a database in the new
> cluster and find out that the OID is already in use. If the new OID
> that collides >64k, then the user has messed with the new cluster
> before doing that. And since pg_upgrade is pretty clearly already
> assuming that you shouldn't do that, it's fine to also make that
> assumption in this case. We can disregard such cases as user error.
>
> If the new OID that collides is <64k, then it must be colliding with
> template0, template1, or postgres in the new cluster, because those
> are the only databases that can have such OIDs since, currently, we
> don't allow users to specify an OID for a new database. And the
> problem cannot be with template1, because we hard-code its OID to 1.
> If there is a database with OID 1 in either cluster, it must be
> template1, and if there is a database with OID 1 in both clusters, it
> must be template1 in both cases, and we'll just drop and recreate it
> with OID 1 and everything is fine. So we need only consider template0
> and postgres, which are created with system-generated OIDs. And, it
> would be no issue if either of those databases had the same OID in the
> old and new cluster, so the only possible OID collision is one where
> the same system-generated OID was assigned to one of those databases
> in the old cluster and to the other in the new cluster.
>
> First consider the case where template0 has OID, say, 13000, in the
> old cluster, and postgres has that OID in the new cluster. No problem
> occurs, because template0 isn't transferred anyway. The reverse
> direction is a problem, though. If postgres had been assigned OID
> 13000 in the old cluster and, by sheer chance, template0 had that OID
> in the new cluster, then the upgrade would fail, because it wouldn't
> be able to recreate the postgres database with the correct OID.
>
> But that doesn't seem very difficult to fix. I think all we need to do
> is have initdb assign a fixed OID to template0 at creation time. Then,
> in any new release to which someone might be trying to upgrade, the
> system-generated OID assigned to postgres in the old release can't
> match the fixed OID assigned to template0 in the new release, so the
> one problem case is ruled out. We do need, however, to make sure that
> the assign-my-database-a-fixed-OID syntax is either entirely
> restricted to initdb & pg_upgrade or at least that OIDS < 64k can only
> be assigned in one of those modes. Otherwise, some creative person
> could manufacture new problem cases by setting up the source database
> so that the OID of one of their databases matches the fixed OID we
> gave to template0 or template1, or the system-generated OID for
> postgres in the new cluster.
>
> In short, as far as I can see, all we need to do to preserve database
> OIDs across pg_upgrade is:
>
> 1. Add a new syntax for creating a database with a given OID, and use
> it in pg_dump --binary-upgrade.
> 2. Don't let users use it at least for OIDs <64k, or maybe just don't
> let them use it at all.
> 3. But let initdb use it, and have initdb set the initial OID for
> template0 to a fixed value < 10000. If the user changes it later, no
> problem; the cluster into which they are upgrading won't contain any
> databases with high-numbered OIDs.
>
> Anyone see a flaw in that analysis?
This looks like a pretty good analysis to me. As it relates to the
question about allowing users to specify an OID, I'd be inclined to
allow it but only for OIDs >64k. We've certainly reserved things in the
past and I don't see any issue with having that reservation here, but if
we're going to build the capability to specify the OID into CREATE
DATABASE then it seems a bit odd to disallow users from using it, as
long as we're preventing them from causing problems with it.
Are there issues that you see with allowing users to specify the OID
even with the >64k restriction..? I can't think of one offhand but
perhaps I'm missing something.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:44 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 15:44 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Stephen Frost <sfrost@snowman.net>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:35:01AM -0400, Robert Haas wrote:
> On Thu, Aug 26, 2021 at 11:24 AM Bruce Momjian <bruce@momjian.us> wrote:
> > On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > > Anyone see a flaw in that analysis?
> >
> > I am still waiting to hear the purpose of this preservation. As long as
> > you don't apply the patch, I guess I will just stop asking.
>
> You make it sound like I didn't answer that question the last time you
> asked it, but I did.[1] I went back to the previous thread and found
> that, in fact, there's at least one email *from you* appearing to
> endorse that concept for reasons unrelated to TDE[2] and another where
> you appear to agree that it would be useful for TDE to do it.[3]
> Stephen Frost also wrote up his discussion during the Unconference and
> some of his reasons for liking the idea.[4]
>
> If you've changed your mind about this being a good idea, or if you no
> longer think it's useful without TDE, that's fine. Everyone is
> entitled to change their opinion. But then please say that straight
> out. It baffles me why you're now acting as if it hasn't been
> discussed when it clearly has been, and both you and I were
> participants in that discussion.
>
> [1] https://www.postgresql.org/message-id/CA+Tgmob7msyh3VRaY87USr22UakvvSyy4zBaQw2AO2CfoUD3rA@mail.gmail...
> [2] https://www.postgresql.org/message-id/20210601140949.GC22012@momjian.us
> [3] https://www.postgresql.org/message-id/20210527210023.GJ5646@momjian.us
> [4] https://www.postgresql.org/message-id/20210531201652.GY20766@tamriel.snowman.net
Yes, it would help incremental backup of pgBackRest, as reported by the
developers. However, I have seen no discussion if this is useful enough
reason to add the complexity to preserve this. The TODO list shows
"Desirability" as the first item to be discussed, so I expected that to
be discussed first. Also, with TDE not progressing (and my approach not
even needing this), I have not seen a full discussion if this item is
desirable based on its complexity.
What I did see is this patch appear with no context of why it is useful
given our current plans, except for pgBackRest, which I think I
mentioned.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:48 Bruce Momjian <bruce@momjian.us>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 2 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 15:48 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:36:51AM -0400, Stephen Frost wrote:
> Greetings,
>
> * Bruce Momjian (bruce@momjian.us) wrote:
> > On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > > Anyone see a flaw in that analysis?
> >
> > I am still waiting to hear the purpose of this preservation. As long as
> > you don't apply the patch, I guess I will just stop asking.
>
> I'm a bit confused why this question keeps coming up as we've discussed
> multiple reasons (incremental backups, possible use for TDE which would
I have not seen much explaination on pgBackRest, except me mentioning
it. Is this something really useful?
As far as TDE, I haven't seen any concrete plan for that, so why add
this code for that reason?
> make this required, general improved sanity when working with pg_upgrade
> is frankly a benefit in its own right too...). If the additional code
How? I am not aware of any advantage except cosmetic.
> was a huge burden or even a moderate one then that might be an argument
> against, but it hardly sounds like it will be given Robert's thorough
> analysis so far and the (admittedly not complete, but not that far from
> it based on the DB OID review) proposed patch.
I am find to add it if it is minor, but I want to see the calculus of
its value vs complexity, which I have not seen spelled out.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 15:52 Robert Haas <robertmhaas@gmail.com>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-26 15:52 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:39 AM Stephen Frost <sfrost@snowman.net> wrote:
> This looks like a pretty good analysis to me. As it relates to the
> question about allowing users to specify an OID, I'd be inclined to
> allow it but only for OIDs >64k. We've certainly reserved things in the
> past and I don't see any issue with having that reservation here, but if
> we're going to build the capability to specify the OID into CREATE
> DATABASE then it seems a bit odd to disallow users from using it, as
> long as we're preventing them from causing problems with it.
>
> Are there issues that you see with allowing users to specify the OID
> even with the >64k restriction..? I can't think of one offhand but
> perhaps I'm missing something.
So I actually should have said 16k here, not 64k, as somebody already
pointed out to me off-list. Whee!
I don't know of a reason not to let people do that, other than that it
seems like an attractive nuisance. People will do it and it will fail
because they chose a duplicate OID, or they'll complain that a regular
dump and restore didn't preserve their database OIDs, or maybe they'll
expect that they can copy a database from one cluster to another
because they gave it the same OID! That said, I don't see a great harm
in it. It just seems to me like exposing knobs to users that don't
seem to have any legitimate use may be borrowing trouble.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 16:34 Stephen Frost <sfrost@snowman.net>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 16:34 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Bruce Momjian (bruce@momjian.us) wrote:
> On Thu, Aug 26, 2021 at 11:36:51AM -0400, Stephen Frost wrote:
> > * Bruce Momjian (bruce@momjian.us) wrote:
> > > On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > > > Anyone see a flaw in that analysis?
> > >
> > > I am still waiting to hear the purpose of this preservation. As long as
> > > you don't apply the patch, I guess I will just stop asking.
> >
> > I'm a bit confused why this question keeps coming up as we've discussed
> > multiple reasons (incremental backups, possible use for TDE which would
>
> I have not seen much explaination on pgBackRest, except me mentioning
> it. Is this something really useful?
Being able to quickly perform a backup on a newly upgraded cluster would
certainly be valuable and that's definitely not possible today due to
all of the filenames changing.
> As far as TDE, I haven't seen any concrete plan for that, so why add
> this code for that reason?
That this would help with TDE (of which there seems little doubt...) is
an additional benefit to this. Specifically, taking the existing work
that's already been done to allow block-by-block encryption and
adjusting it for AES-XTS and then using the db-dir+relfileno+block
number as the IV, just like many disk encryption systems do, avoids the
concerns that were brought up about using LSN for the IV with CTR and
it's certainly not difficult to do, but it does depend on this change.
This was all discussed previously and it sure looks like a sensible
approach to use that mirrors what many other systems already do
successfully.
> > make this required, general improved sanity when working with pg_upgrade
> > is frankly a benefit in its own right too...). If the additional code
>
> How? I am not aware of any advantage except cosmetic.
Having to resort to matching up inode numbers between the two clusters
after a pg_upgrade to figure out what files are actually the same
underneath is a pain that goes beyond just cosmetics imv. Removing that
additional level that admins, and developers for that matter, have to go
through would be a nice improvement on its own.
> > was a huge burden or even a moderate one then that might be an argument
> > against, but it hardly sounds like it will be given Robert's thorough
> > analysis so far and the (admittedly not complete, but not that far from
> > it based on the DB OID review) proposed patch.
>
> I am find to add it if it is minor, but I want to see the calculus of
> its value vs complexity, which I have not seen spelled out.
I feel that this, along with the prior discussions, spells it out
sufficiently given the patch's complexity looks to be reasonably minor
and very similar to the existing things that pg_upgrade already does.
Had pg_upgrade done this in the first place, I don't think there would
have been nearly this amount of discussion about it.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 16:37 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
1 sibling, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-26 16:37 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 11:48 AM Bruce Momjian <bruce@momjian.us> wrote:
> I am find to add it if it is minor, but I want to see the calculus of
> its value vs complexity, which I have not seen spelled out.
I don't think it's going to be all that complicated, but we're going
to have to wait until we have something closer to a final patch before
we can really evaluate that. I am honestly a little puzzled about why
you think complexity is such a big issue for this patch in particular.
I feel we do probably several hundred things every release cycle that
are more complicated than this, so it doesn't seem like this is
particularly extraordinary or needs a lot of extra scrutiny. I do
think there is some risk that there are messy cases we can't handle
cleanly, but if that becomes an issue then I'll abandon the effort
until a solution can be found. I'm not trying to relentlessly drive
something through that is a bad idea on principle.
I agree with all Stephen's comments, too.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 16:42 Stephen Frost <sfrost@snowman.net>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 16:42 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Robert Haas (robertmhaas@gmail.com) wrote:
> On Thu, Aug 26, 2021 at 11:39 AM Stephen Frost <sfrost@snowman.net> wrote:
> > This looks like a pretty good analysis to me. As it relates to the
> > question about allowing users to specify an OID, I'd be inclined to
> > allow it but only for OIDs >64k. We've certainly reserved things in the
> > past and I don't see any issue with having that reservation here, but if
> > we're going to build the capability to specify the OID into CREATE
> > DATABASE then it seems a bit odd to disallow users from using it, as
> > long as we're preventing them from causing problems with it.
> >
> > Are there issues that you see with allowing users to specify the OID
> > even with the >64k restriction..? I can't think of one offhand but
> > perhaps I'm missing something.
>
> So I actually should have said 16k here, not 64k, as somebody already
> pointed out to me off-list. Whee!
Hah, yes, of course.
> I don't know of a reason not to let people do that, other than that it
> seems like an attractive nuisance. People will do it and it will fail
> because they chose a duplicate OID, or they'll complain that a regular
> dump and restore didn't preserve their database OIDs, or maybe they'll
> expect that they can copy a database from one cluster to another
> because they gave it the same OID! That said, I don't see a great harm
> in it. It just seems to me like exposing knobs to users that don't
> seem to have any legitimate use may be borrowing trouble.
We're going to have to gate this somehow to allow the OIDs under 16k to
be used, so it seems like what you're suggesting is that we have that
gate in place but then allow any OID to be used if you've crossed that
gate?
That is, if we do something like:
SELECT pg_catalog.binary_upgrade_allow_setting_db_oid();
CREATE DATABASE blah WITH OID 1234;
for pg_upgrade, well, users who are interested may well figure out how
to do that themselves if they decide they want to set the OID, whereas
if it 'just works' provided they don't try to use an OID too low then
maybe they won't try to bypass the restriction against using system
OIDs..?
Ok, I'll give you that this is a stretch and I'm on the fence about if
it's worthwhile or not to include and document and if, as you say, it's
inviting trouble to allow users to set it. Users do seem to have a
knack for finding things even when they aren't documented and then we
get to deal with those complaints too. :)
Perhaps others have some stronger feelings one way or another.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 16:49 Bruce Momjian <bruce@momjian.us>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 1 reply; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 16:49 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 12:34:56PM -0400, Stephen Frost wrote:
> * Bruce Momjian (bruce@momjian.us) wrote:
> > On Thu, Aug 26, 2021 at 11:36:51AM -0400, Stephen Frost wrote:
> > > * Bruce Momjian (bruce@momjian.us) wrote:
> > > > On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > > > > Anyone see a flaw in that analysis?
> > > >
> > > > I am still waiting to hear the purpose of this preservation. As long as
> > > > you don't apply the patch, I guess I will just stop asking.
> > >
> > > I'm a bit confused why this question keeps coming up as we've discussed
> > > multiple reasons (incremental backups, possible use for TDE which would
> >
> > I have not seen much explaination on pgBackRest, except me mentioning
> > it. Is this something really useful?
>
> Being able to quickly perform a backup on a newly upgraded cluster would
> certainly be valuable and that's definitely not possible today due to
> all of the filenames changing.
You mean incremental backup, right? I was told this by the pgBackRest
developers during PGCon, but I have not heard that stated publicly, so I
hate to go just on what I heard rather than seeing that stated publicly.
> > As far as TDE, I haven't seen any concrete plan for that, so why add
> > this code for that reason?
>
> That this would help with TDE (of which there seems little doubt...) is
> an additional benefit to this. Specifically, taking the existing work
> that's already been done to allow block-by-block encryption and
> adjusting it for AES-XTS and then using the db-dir+relfileno+block
> number as the IV, just like many disk encryption systems do, avoids the
> concerns that were brought up about using LSN for the IV with CTR and
> it's certainly not difficult to do, but it does depend on this change.
> This was all discussed previously and it sure looks like a sensible
> approach to use that mirrors what many other systems already do
> successfully.
Well, I would think we would not add this for TDE until we were sure
someone was working on adding TDE.
> > > make this required, general improved sanity when working with pg_upgrade
> > > is frankly a benefit in its own right too...). If the additional code
> >
> > How? I am not aware of any advantage except cosmetic.
>
> Having to resort to matching up inode numbers between the two clusters
> after a pg_upgrade to figure out what files are actually the same
> underneath is a pain that goes beyond just cosmetics imv. Removing that
> additional level that admins, and developers for that matter, have to go
> through would be a nice improvement on its own.
OK, I was just not aware anyone did that, since I have never hard anyone
complain about it before.
> > > was a huge burden or even a moderate one then that might be an argument
> > > against, but it hardly sounds like it will be given Robert's thorough
> > > analysis so far and the (admittedly not complete, but not that far from
> > > it based on the DB OID review) proposed patch.
> >
> > I am find to add it if it is minor, but I want to see the calculus of
> > its value vs complexity, which I have not seen spelled out.
>
> I feel that this, along with the prior discussions, spells it out
> sufficiently given the patch's complexity looks to be reasonably minor
> and very similar to the existing things that pg_upgrade already does.
> Had pg_upgrade done this in the first place, I don't think there would
> have been nearly this amount of discussion about it.
Well, there is a reason pg_upgrade didn't initially do this --- because
it adds complexity, and potentially makes future changes to pg_upgrade
necessary if the server behavior changes.
I am not saying this change is wrong, but I think the reasons need to be
stated in this thread, rather than just moving forward.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 16:51 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 16:51 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 12:37:19PM -0400, Robert Haas wrote:
> On Thu, Aug 26, 2021 at 11:48 AM Bruce Momjian <bruce@momjian.us> wrote:
> > I am find to add it if it is minor, but I want to see the calculus of
> > its value vs complexity, which I have not seen spelled out.
>
> I don't think it's going to be all that complicated, but we're going
> to have to wait until we have something closer to a final patch before
> we can really evaluate that. I am honestly a little puzzled about why
> you think complexity is such a big issue for this patch in particular.
> I feel we do probably several hundred things every release cycle that
> are more complicated than this, so it doesn't seem like this is
> particularly extraordinary or needs a lot of extra scrutiny. I do
> think there is some risk that there are messy cases we can't handle
> cleanly, but if that becomes an issue then I'll abandon the effort
> until a solution can be found. I'm not trying to relentlessly drive
> something through that is a bad idea on principle.
>
> I agree with all Stephen's comments, too.
I just don't want to add requirements/complexity to pg_upgrade without
clearly stated reasons because future database changes will need to
honor this new preservation behavior.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:03 Stephen Frost <sfrost@snowman.net>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 17:03 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Bruce Momjian (bruce@momjian.us) wrote:
> On Thu, Aug 26, 2021 at 12:34:56PM -0400, Stephen Frost wrote:
> > * Bruce Momjian (bruce@momjian.us) wrote:
> > > On Thu, Aug 26, 2021 at 11:36:51AM -0400, Stephen Frost wrote:
> > > > * Bruce Momjian (bruce@momjian.us) wrote:
> > > > > On Thu, Aug 26, 2021 at 11:00:47AM -0400, Robert Haas wrote:
> > > > > > Anyone see a flaw in that analysis?
> > > > >
> > > > > I am still waiting to hear the purpose of this preservation. As long as
> > > > > you don't apply the patch, I guess I will just stop asking.
> > > >
> > > > I'm a bit confused why this question keeps coming up as we've discussed
> > > > multiple reasons (incremental backups, possible use for TDE which would
> > >
> > > I have not seen much explaination on pgBackRest, except me mentioning
> > > it. Is this something really useful?
> >
> > Being able to quickly perform a backup on a newly upgraded cluster would
> > certainly be valuable and that's definitely not possible today due to
> > all of the filenames changing.
>
> You mean incremental backup, right? I was told this by the pgBackRest
> developers during PGCon, but I have not heard that stated publicly, so I
> hate to go just on what I heard rather than seeing that stated publicly.
Yes, we're talking about either incremental (or perhaps differential)
backup where only the files which are actually different would be backed
up. Just like with PG, I can't provide any complete guarantees that
we'd be able to actually make this possible after a major version with
pgBackRest with this change, but it definitely isn't possible *without*
this change. I can't see any reason why we wouldn't be able to do a
checksum-based incremental backup though (which would be *much* faster
than a regular backup) once this change is made and have that be a
reliable and trustworthy backup. I'd want to think about it more and
discuss it with David in some detail before saying if we could maybe
perform a timestamp-based incremental backup (without checksum'ing the
files, as we do in normal situations), but that would really just be a
bonus.
> > > As far as TDE, I haven't seen any concrete plan for that, so why add
> > > this code for that reason?
> >
> > That this would help with TDE (of which there seems little doubt...) is
> > an additional benefit to this. Specifically, taking the existing work
> > that's already been done to allow block-by-block encryption and
> > adjusting it for AES-XTS and then using the db-dir+relfileno+block
> > number as the IV, just like many disk encryption systems do, avoids the
> > concerns that were brought up about using LSN for the IV with CTR and
> > it's certainly not difficult to do, but it does depend on this change.
> > This was all discussed previously and it sure looks like a sensible
> > approach to use that mirrors what many other systems already do
> > successfully.
>
> Well, I would think we would not add this for TDE until we were sure
> someone was working on adding TDE.
That this would help with TDE is what I'd consider an added bonus.
> > > > make this required, general improved sanity when working with pg_upgrade
> > > > is frankly a benefit in its own right too...). If the additional code
> > >
> > > How? I am not aware of any advantage except cosmetic.
> >
> > Having to resort to matching up inode numbers between the two clusters
> > after a pg_upgrade to figure out what files are actually the same
> > underneath is a pain that goes beyond just cosmetics imv. Removing that
> > additional level that admins, and developers for that matter, have to go
> > through would be a nice improvement on its own.
>
> OK, I was just not aware anyone did that, since I have never hard anyone
> complain about it before.
I've certainly done it and I'd be kind of surprised if others haven't,
but I've also played a lot with pg_dump in various modes, so perhaps
that's not a great representation. I've definitely had to explain to
clients why there's a whole different set of filenames after a
pg_upgrade and why that is the case for an 'in place' upgrade before
too.
> > > > was a huge burden or even a moderate one then that might be an argument
> > > > against, but it hardly sounds like it will be given Robert's thorough
> > > > analysis so far and the (admittedly not complete, but not that far from
> > > > it based on the DB OID review) proposed patch.
> > >
> > > I am find to add it if it is minor, but I want to see the calculus of
> > > its value vs complexity, which I have not seen spelled out.
> >
> > I feel that this, along with the prior discussions, spells it out
> > sufficiently given the patch's complexity looks to be reasonably minor
> > and very similar to the existing things that pg_upgrade already does.
> > Had pg_upgrade done this in the first place, I don't think there would
> > have been nearly this amount of discussion about it.
>
> Well, there is a reason pg_upgrade didn't initially do this --- because
> it adds complexity, and potentially makes future changes to pg_upgrade
> necessary if the server behavior changes.
I have a very hard time seeing what changes might happen in the server
in this space that wouldn't have an impact on pg_upgrade, with or
without this.
> I am not saying this change is wrong, but I think the reasons need to be
> stated in this thread, rather than just moving forward.
Ok, they've been stated and it seems to at least Robert and myself that
this is worthwhile to at least continue through to a concluded patch,
after which we can contemplate that patch's complexity against these
reasons.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:16 Bruce Momjian <bruce@momjian.us>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 1 reply; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 17:16 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 01:03:54PM -0400, Stephen Frost wrote:
> Yes, we're talking about either incremental (or perhaps differential)
> backup where only the files which are actually different would be backed
> up. Just like with PG, I can't provide any complete guarantees that
> we'd be able to actually make this possible after a major version with
> pgBackRest with this change, but it definitely isn't possible *without*
> this change. I can't see any reason why we wouldn't be able to do a
> checksum-based incremental backup though (which would be *much* faster
> than a regular backup) once this change is made and have that be a
> reliable and trustworthy backup. I'd want to think about it more and
> discuss it with David in some detail before saying if we could maybe
> perform a timestamp-based incremental backup (without checksum'ing the
> files, as we do in normal situations), but that would really just be a
> bonus.
Well, it would be nice to know exactly how it would help pgBackRest if
that is one of the reasons we are adding this feature.
> > > > As far as TDE, I haven't seen any concrete plan for that, so why add
> > > > this code for that reason?
> > >
> > > That this would help with TDE (of which there seems little doubt...) is
> > > an additional benefit to this. Specifically, taking the existing work
> > > that's already been done to allow block-by-block encryption and
> > > adjusting it for AES-XTS and then using the db-dir+relfileno+block
> > > number as the IV, just like many disk encryption systems do, avoids the
> > > concerns that were brought up about using LSN for the IV with CTR and
> > > it's certainly not difficult to do, but it does depend on this change.
> > > This was all discussed previously and it sure looks like a sensible
> > > approach to use that mirrors what many other systems already do
> > > successfully.
> >
> > Well, I would think we would not add this for TDE until we were sure
> > someone was working on adding TDE.
>
> That this would help with TDE is what I'd consider an added bonus.
Not if we have no plans to implement TDE, which was my point. Why not
wait to see if we are actually going to implement TDE rather than adding
it now. It is just so obvious, why do I have to state this?
> > > > > make this required, general improved sanity when working with pg_upgrade
> > > > > is frankly a benefit in its own right too...). If the additional code
> > > >
> > > > How? I am not aware of any advantage except cosmetic.
> > >
> > > Having to resort to matching up inode numbers between the two clusters
> > > after a pg_upgrade to figure out what files are actually the same
> > > underneath is a pain that goes beyond just cosmetics imv. Removing that
> > > additional level that admins, and developers for that matter, have to go
> > > through would be a nice improvement on its own.
> >
> > OK, I was just not aware anyone did that, since I have never hard anyone
> > complain about it before.
>
> I've certainly done it and I'd be kind of surprised if others haven't,
> but I've also played a lot with pg_dump in various modes, so perhaps
> that's not a great representation. I've definitely had to explain to
> clients why there's a whole different set of filenames after a
> pg_upgrade and why that is the case for an 'in place' upgrade before
> too.
Uh, so I guess I am right that few people have mentioned this in the
past. Why were users caring about the file names?
> > > > > was a huge burden or even a moderate one then that might be an argument
> > > > > against, but it hardly sounds like it will be given Robert's thorough
> > > > > analysis so far and the (admittedly not complete, but not that far from
> > > > > it based on the DB OID review) proposed patch.
> > > >
> > > > I am find to add it if it is minor, but I want to see the calculus of
> > > > its value vs complexity, which I have not seen spelled out.
> > >
> > > I feel that this, along with the prior discussions, spells it out
> > > sufficiently given the patch's complexity looks to be reasonably minor
> > > and very similar to the existing things that pg_upgrade already does.
> > > Had pg_upgrade done this in the first place, I don't think there would
> > > have been nearly this amount of discussion about it.
> >
> > Well, there is a reason pg_upgrade didn't initially do this --- because
> > it adds complexity, and potentially makes future changes to pg_upgrade
> > necessary if the server behavior changes.
>
> I have a very hard time seeing what changes might happen in the server
> in this space that wouldn't have an impact on pg_upgrade, with or
> without this.
I don't know, but I have to ask since I can't know the future, so any
"preseration" has to be studied.
> > I am not saying this change is wrong, but I think the reasons need to be
> > stated in this thread, rather than just moving forward.
>
> Ok, they've been stated and it seems to at least Robert and myself that
> this is worthwhile to at least continue through to a concluded patch,
> after which we can contemplate that patch's complexity against these
> reasons.
OK, that works for me. What bothers me is that the Desirability of this
changes has not be clearly stated in this thread.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:20 Robert Haas <robertmhaas@gmail.com>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-08-26 17:20 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Stephen Frost <sfrost@snowman.net>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 12:51 PM Bruce Momjian <bruce@momjian.us> wrote:
> I just don't want to add requirements/complexity to pg_upgrade without
> clearly stated reasons because future database changes will need to
> honor this new preservation behavior.
Well, I agree that it's good to have reasons clearly stated and I hope
that at this point you agree that they have been. Whether you agree
with them is another question, but I hope you at least agree that they
have been stated.
As far as the other part of your concern, what I think makes this
change pretty safe is that we are preserving more things rather than
fewer. I can imagine some server behavior depending on something being
the same between the old and the new clusters, but it is harder to
imagine a dependency on something not being preserved. For example, we
know that the OIDs of pg_type rows have to be the same in the old and
new cluster because arrays are stored on disk with the type OIDs
included. Therefore those need to be preserved. If in the future we
changed things so that arrays - and other container types - did not
include the type OIDs in the on-disk representation, then perhaps it
would no longer be necessary to preserve the OIDs of pg_type rows
across a pg_upgrade. However, it would not be harmful to do so. It
just might not be required.
So I think this proposed change is in the safe direction. If
relfilenodes were currently preserved and we wanted to make them not
be preserved, then I think you would be quite right to say "whoa,
whoa, that could be a problem." Indeed it could. If anyone then in the
future wanted to introduce a dependency on them staying the same, they
would have a problem. However, nothing in the server itself can care
about relfilenodes - or anything else - being *different* across a
pg_upgrade. The whole point of pg_upgrade is to make it feel like you
have the same database after you run it as you did before you ran it,
even though under the hood a lot of surgery has been done. Barring
bugs, you can never be sad about there being too LITTLE difference
between the post-upgrade database and the pre-upgrade database.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:24 Stephen Frost <sfrost@snowman.net>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 90+ messages in thread
From: Stephen Frost @ 2021-08-26 17:24 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Greetings,
* Bruce Momjian (bruce@momjian.us) wrote:
> On Thu, Aug 26, 2021 at 01:03:54PM -0400, Stephen Frost wrote:
> > Yes, we're talking about either incremental (or perhaps differential)
> > backup where only the files which are actually different would be backed
> > up. Just like with PG, I can't provide any complete guarantees that
> > we'd be able to actually make this possible after a major version with
> > pgBackRest with this change, but it definitely isn't possible *without*
> > this change. I can't see any reason why we wouldn't be able to do a
> > checksum-based incremental backup though (which would be *much* faster
> > than a regular backup) once this change is made and have that be a
> > reliable and trustworthy backup. I'd want to think about it more and
> > discuss it with David in some detail before saying if we could maybe
> > perform a timestamp-based incremental backup (without checksum'ing the
> > files, as we do in normal situations), but that would really just be a
> > bonus.
>
> Well, it would be nice to know exactly how it would help pgBackRest if
> that is one of the reasons we are adding this feature.
pgBackRest keeps a manifest for every file in the PG data directory that
is backed up and we identify that file by the filename. Further, we
calculate a checksum for every file. If the filenames didn't change
then we'd be able to compare the file in the new cluster against the
file and checksum in the manifest in order to be able to perform the
incremental/differential backup. We don't store the inodes in the
manifest though, and we don't have any concept of looking at multiple
data directories at the same time or anything like that (which would
also mean that the old data directory would have to be kept around for
that to even work, which seems like a good bit of additional
complication and risk that someone might start up the old cluster by
accident..).
That's how it'd be very helpful to pgBackRest for the filenames to be
preserved across pg_upgrade's.
> > > > > As far as TDE, I haven't seen any concrete plan for that, so why add
> > > > > this code for that reason?
> > > >
> > > > That this would help with TDE (of which there seems little doubt...) is
> > > > an additional benefit to this. Specifically, taking the existing work
> > > > that's already been done to allow block-by-block encryption and
> > > > adjusting it for AES-XTS and then using the db-dir+relfileno+block
> > > > number as the IV, just like many disk encryption systems do, avoids the
> > > > concerns that were brought up about using LSN for the IV with CTR and
> > > > it's certainly not difficult to do, but it does depend on this change.
> > > > This was all discussed previously and it sure looks like a sensible
> > > > approach to use that mirrors what many other systems already do
> > > > successfully.
> > >
> > > Well, I would think we would not add this for TDE until we were sure
> > > someone was working on adding TDE.
> >
> > That this would help with TDE is what I'd consider an added bonus.
>
> Not if we have no plans to implement TDE, which was my point. Why not
> wait to see if we are actually going to implement TDE rather than adding
> it now. It is just so obvious, why do I have to state this?
There's been multiple years of effort put into implementing TDE and I'm
sure hopeful that it continues as I'm trying to put effort into moving
it forward myself. I'm a bit baffled by the idea that we're just
suddenly going to stop putting effort into TDE as it is brought up time
and time again by clients that I've talked to as one of the few reasons
they haven't moved to PG yet- I can't believe that hasn't been
experienced by folks at other organizations too, I mean, there's people
maintaining forks of PG specifically for TDE ...
Seems like maybe we were both seeing something as obvious to the other
that wasn't actually the case.
> > > > > > make this required, general improved sanity when working with pg_upgrade
> > > > > > is frankly a benefit in its own right too...). If the additional code
> > > > >
> > > > > How? I am not aware of any advantage except cosmetic.
> > > >
> > > > Having to resort to matching up inode numbers between the two clusters
> > > > after a pg_upgrade to figure out what files are actually the same
> > > > underneath is a pain that goes beyond just cosmetics imv. Removing that
> > > > additional level that admins, and developers for that matter, have to go
> > > > through would be a nice improvement on its own.
> > >
> > > OK, I was just not aware anyone did that, since I have never hard anyone
> > > complain about it before.
> >
> > I've certainly done it and I'd be kind of surprised if others haven't,
> > but I've also played a lot with pg_dump in various modes, so perhaps
> > that's not a great representation. I've definitely had to explain to
> > clients why there's a whole different set of filenames after a
> > pg_upgrade and why that is the case for an 'in place' upgrade before
> > too.
>
> Uh, so I guess I am right that few people have mentioned this in the
> past. Why were users caring about the file names?
This is a bit baffling to me. Users and admins certainly care about
what files their data is stored in and knowing how to find them.
Covering the data directory structure is a commonly asked for part of
the training that I regularly do for clients.
> > > > > > was a huge burden or even a moderate one then that might be an argument
> > > > > > against, but it hardly sounds like it will be given Robert's thorough
> > > > > > analysis so far and the (admittedly not complete, but not that far from
> > > > > > it based on the DB OID review) proposed patch.
> > > > >
> > > > > I am find to add it if it is minor, but I want to see the calculus of
> > > > > its value vs complexity, which I have not seen spelled out.
> > > >
> > > > I feel that this, along with the prior discussions, spells it out
> > > > sufficiently given the patch's complexity looks to be reasonably minor
> > > > and very similar to the existing things that pg_upgrade already does.
> > > > Had pg_upgrade done this in the first place, I don't think there would
> > > > have been nearly this amount of discussion about it.
> > >
> > > Well, there is a reason pg_upgrade didn't initially do this --- because
> > > it adds complexity, and potentially makes future changes to pg_upgrade
> > > necessary if the server behavior changes.
> >
> > I have a very hard time seeing what changes might happen in the server
> > in this space that wouldn't have an impact on pg_upgrade, with or
> > without this.
>
> I don't know, but I have to ask since I can't know the future, so any
> "preseration" has to be studied.
We can gain, perhaps, some insight looking into the past and that seems
to indicate that this is certainly a very stable part of the server code
in the first place, which would imply that it's unlikely that there'll
be much need to adjust this code in the future in the first place.
> > > I am not saying this change is wrong, but I think the reasons need to be
> > > stated in this thread, rather than just moving forward.
> >
> > Ok, they've been stated and it seems to at least Robert and myself that
> > this is worthwhile to at least continue through to a concluded patch,
> > after which we can contemplate that patch's complexity against these
> > reasons.
>
> OK, that works for me. What bothers me is that the Desirability of this
> changes has not be clearly stated in this thread.
I hope that this email and the many many prior ones have gotten across
the desirability of the change.
Thanks,
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:34 Bruce Momjian <bruce@momjian.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 17:34 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 01:20:38PM -0400, Robert Haas wrote:
> So I think this proposed change is in the safe direction. If
> relfilenodes were currently preserved and we wanted to make them not
> be preserved, then I think you would be quite right to say "whoa,
> whoa, that could be a problem." Indeed it could. If anyone then in the
> future wanted to introduce a dependency on them staying the same, they
> would have a problem. However, nothing in the server itself can care
> about relfilenodes - or anything else - being *different* across a
> pg_upgrade. The whole point of pg_upgrade is to make it feel like you
> have the same database after you run it as you did before you ran it,
> even though under the hood a lot of surgery has been done. Barring
> bugs, you can never be sad about there being too LITTLE difference
> between the post-upgrade database and the pre-upgrade database.
Yes, this makes sense, and it is good we have stated the possible
benefits now:
* pgBackRest
* pg_upgrade diagnostics
* TDE (maybe)
We can eventually evaluate the value of this based on those items.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-08-26 17:37 Bruce Momjian <bruce@momjian.us>
parent: Stephen Frost <sfrost@snowman.net>
0 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2021-08-26 17:37 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: Robert Haas <robertmhaas@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Shruthi Gowda <gowdashru@gmail.com>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Aug 26, 2021 at 01:24:46PM -0400, Stephen Frost wrote:
> Greetings,
>
> * Bruce Momjian (bruce@momjian.us) wrote:
> > On Thu, Aug 26, 2021 at 01:03:54PM -0400, Stephen Frost wrote:
> > > Yes, we're talking about either incremental (or perhaps differential)
> > > backup where only the files which are actually different would be backed
> > > up. Just like with PG, I can't provide any complete guarantees that
> > > we'd be able to actually make this possible after a major version with
> > > pgBackRest with this change, but it definitely isn't possible *without*
> > > this change. I can't see any reason why we wouldn't be able to do a
> > > checksum-based incremental backup though (which would be *much* faster
> > > than a regular backup) once this change is made and have that be a
> > > reliable and trustworthy backup. I'd want to think about it more and
> > > discuss it with David in some detail before saying if we could maybe
> > > perform a timestamp-based incremental backup (without checksum'ing the
> > > files, as we do in normal situations), but that would really just be a
> > > bonus.
> >
> > Well, it would be nice to know exactly how it would help pgBackRest if
> > that is one of the reasons we are adding this feature.
>
> pgBackRest keeps a manifest for every file in the PG data directory that
> is backed up and we identify that file by the filename. Further, we
> calculate a checksum for every file. If the filenames didn't change
> then we'd be able to compare the file in the new cluster against the
> file and checksum in the manifest in order to be able to perform the
> incremental/differential backup. We don't store the inodes in the
> manifest though, and we don't have any concept of looking at multiple
> data directories at the same time or anything like that (which would
> also mean that the old data directory would have to be kept around for
> that to even work, which seems like a good bit of additional
> complication and risk that someone might start up the old cluster by
> accident..).
>
> That's how it'd be very helpful to pgBackRest for the filenames to be
> preserved across pg_upgrade's.
OK, that is clear.
> > > > > > As far as TDE, I haven't seen any concrete plan for that, so why add
> > > > > > this code for that reason?
> > > > >
> > > > > That this would help with TDE (of which there seems little doubt...) is
> > > > > an additional benefit to this. Specifically, taking the existing work
> > > > > that's already been done to allow block-by-block encryption and
> > > > > adjusting it for AES-XTS and then using the db-dir+relfileno+block
> > > > > number as the IV, just like many disk encryption systems do, avoids the
> > > > > concerns that were brought up about using LSN for the IV with CTR and
> > > > > it's certainly not difficult to do, but it does depend on this change.
> > > > > This was all discussed previously and it sure looks like a sensible
> > > > > approach to use that mirrors what many other systems already do
> > > > > successfully.
> > > >
> > > > Well, I would think we would not add this for TDE until we were sure
> > > > someone was working on adding TDE.
> > >
> > > That this would help with TDE is what I'd consider an added bonus.
> >
> > Not if we have no plans to implement TDE, which was my point. Why not
> > wait to see if we are actually going to implement TDE rather than adding
> > it now. It is just so obvious, why do I have to state this?
>
> There's been multiple years of effort put into implementing TDE and I'm
> sure hopeful that it continues as I'm trying to put effort into moving
> it forward myself. I'm a bit baffled by the idea that we're just
Well, this is the first time I am hearing this publicly.
> suddenly going to stop putting effort into TDE as it is brought up time
> and time again by clients that I've talked to as one of the few reasons
> they haven't moved to PG yet- I can't believe that hasn't been
> experienced by folks at other organizations too, I mean, there's people
> maintaining forks of PG specifically for TDE ...
Agreed.
> > > I've certainly done it and I'd be kind of surprised if others haven't,
> > > but I've also played a lot with pg_dump in various modes, so perhaps
> > > that's not a great representation. I've definitely had to explain to
> > > clients why there's a whole different set of filenames after a
> > > pg_upgrade and why that is the case for an 'in place' upgrade before
> > > too.
> >
> > Uh, so I guess I am right that few people have mentioned this in the
> > past. Why were users caring about the file names?
>
> This is a bit baffling to me. Users and admins certainly care about
> what files their data is stored in and knowing how to find them.
> Covering the data directory structure is a commonly asked for part of
> the training that I regularly do for clients.
I just never thought people cared about the file names, since I have
never heard a complaint about how pg_upgrade works all these years.
> > > I have a very hard time seeing what changes might happen in the server
> > > in this space that wouldn't have an impact on pg_upgrade, with or
> > > without this.
> >
> > I don't know, but I have to ask since I can't know the future, so any
> > "preseration" has to be studied.
>
> We can gain, perhaps, some insight looking into the past and that seems
> to indicate that this is certainly a very stable part of the server code
> in the first place, which would imply that it's unlikely that there'll
> be much need to adjust this code in the future in the first place.
Good, it have to ask.
> > > > I am not saying this change is wrong, but I think the reasons need to be
> > > > stated in this thread, rather than just moving forward.
> > >
> > > Ok, they've been stated and it seems to at least Robert and myself that
> > > this is worthwhile to at least continue through to a concluded patch,
> > > after which we can contemplate that patch's complexity against these
> > > reasons.
> >
> > OK, that works for me. What bothers me is that the Desirability of this
> > changes has not be clearly stated in this thread.
>
> I hope that this email and the many many prior ones have gotten across
> the desirability of the change.
Yes, I think we are in a better position now to evaluate this.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-09-22 19:06 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-09-22 19:06 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Aug 24, 2021 at 2:27 AM Robert Haas <robertmhaas@gmail.com> wrote:
> It's pretty clear from the discussion, I think, that the database OID
> one is going to need rework to be considered.
>
> Regarding the other one:
>
> - The comment in binary_upgrade_set_pg_class_oids() is still not
> accurate. You removed the sentence which says "Indexes cannot have
> toast tables, so we need not make this probe in the index code path"
> but the immediately preceding sentence is still inaccurate in at least
> two ways. First, it only talks about tables, but the code now applies
> to indexes. Second, it only talks about OIDs, but now also deals with
> refilenodes. It's really important to fully update every comment that
> might be affected by your changes!
The comment is updated.
> - The SQL query in that function isn't completely correct. There is a
> left join from pg_class to pg_index whose ON clause includes
> "c.reltoastrelid = i.indrelid AND i.indisvalid." The reason it's
> likely that is because it is possible, in corner cases, for a TOAST
> table to have multiple TOAST indexes. I forget exactly how that
> happens, but I think it might be like if a REINDEX CONCURRENTLY on the
> TOAST table fails midway through, or something of that sort. Now if
> that happens, the LEFT JOIN you added is going to cause the output to
> contain multiple rows, because you didn't replicate the i.indisvalid
> condition into that ON clause. And then it will fail. Apparently we
> don't have a pg_upgrade test case for this scenario; we probably
> should. Actually what I think would be even better than putting
> i.indisvalid into that ON clause would be to join off of i.indrelid
> rather than c.reltoastrelid.
The SQL query will not result in duplicate rows because the first join
filters the duplicate rows if any with the on clause ' i.indisvalid'
on it. The result of the first join is further left joined with pg_class and
pg_class will not have duplicate rows for a given oid.
> - The code that decodes the various columns of this query does so in a
> slightly different order than the query itself. It would be better to
> make it match. Perhaps put relkind first in both cases. I might also
> think about trying to make the column naming a bit more consistent,
> e.g. relkind, relfilenode, toast_oid, toast_relfilenode,
> toast_index_oid, toast_index_relfilenode.
Fixed.
> - In heap_create(), the wording of the error messages is not quite
> consistent. You have "relfilenode value not set when in binary upgrade
> mode", "toast relfilenode value not set when in binary upgrade mode",
> and "pg_class index relfilenode value not set when in binary upgrade
> mode". Why does the last one mention pg_class when the other two
> don't?
The error message is made consistent. This code chuck is moved to a different
place as a part of another review comment fix.
> - The code in heap_create() now has no comments whatsoever, which is a
> shame, because it's actually kind of a tricky bit of logic. Someone
> might wonder why we override the relfilenode inside that function
> instead of doing it at the same places where we absorb
> binary_upgrade_next_{heap,index,toast}_pg_class_oid and the passing
> down the relfilenode. I think the answer is that passing down the
> relfilenode from the caller would result in storage not actually being
> created, whereas in this case we want it to be created but just with
> the value we specify, and the reason we want that is because we need
> later DDL that happens after these statements but before the old
> cluster's relations are moved over to execute successfully, which it
> won't if the storage is altogether absent.
> However, that raises the question of whether this patch has even got
> the basic design right. Maybe we ought to actually be absorbing the
> relfilenode setting at the same places where we're doing so for the
> OID, and then passing an additional parameter to heap_create() like
> bool suppress_storage or something like that. Maybe, taking it even
> further, we ought to be changing the signatures of
> binary_upgrade_next_heap_pg_class_oid and friends to be two-argument
> functions, and pass down the OID and the relfilenode in the same call,
> rather than calling two separate functions. I'm not so much concerned
> about the cost of calling two functions as the potential for
> confusion. I'm not honestly sure that either of these changes are the
> right thing to do, but I am pretty strongly inclined to do at least
> the first part - trying to absorb reloid and relfilenode in the same
> places. If we're not going to do that we certainly need to explain why
> we're doing it the way we are in the comments.
As per your suggestion, reloid and relfilenode are absorbed in the same place.
An additional parameter called 'suppress_storage' is passed to heap_create()
which indicates whether or not to create the storage when the caller
passed a valid relfilenode.
I did not make the changes to set the oid and relfilenode in the same call.
I feel the uniformity w.r.t the other function signatures in
pg_upgrade_support.c will be lost because currently each function sets
only one attribute.
Also, renaming the applicable function names to represent that they
set both oid and relfilenode will make the function name even longer.
We may opt to not include the relfilenode in the function name instead
use a generic name like binary_upgrade_set_next_xxx_pg_class_oid() but
then
we will end up with some functions that set two attributes and some
functions that set one attribute.
> It's not really this patch's fault, but it would sure be nice if we
> had some better testing for this area. Suppose this patch somehow
> changed nothing from the present behavior. How would we know? Or
> suppose it managed to somehow set all the relfilenodes in the new
> cluster to random values rather than the intended one? There's no
> automated testing that would catch any of that, and it's not obvious
> how it could be added to test.sh. I suppose what we really need to do
> at some point is rewrite that as a TAP test, but that seems like a
> separate project from this patch.
I have verified the table, index, toast table and toast index
relfilenode and DB oid in old and new cluster manually and it is
working as expected.
I have also attached the patch to preserve the DB oid. As discussed,
template0 will be created with a fixed oid during initdb. I am using
OID 2 for template0. Even though oid 2 is already in use for the
'pg_am' catalog I see no harm in using it for template0 DB because oid
doesn’t have to be unique across the database - it has to be unique
for the particular catalog table. Kindly let me know if I am missing
something?
Apparently, if we did decide to pick an unused oid for template0 then
I see a challenge in removing that oid from the unused oid list. I
could not come up with a feasible solution for handling it.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v3-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg.patch (application/octet-stream, 26.9K) download
From 2c00a37c4a3a00bb765232fea64e2bc02bfc8129 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Wed, 22 Sep 2021 23:33:41 +0530
Subject: [PATCH v3 1/2] Preserve relfilenode and tablespace OID in pg_upgrade
The patch aims to preserve the OIDs of relfilenode and tablespace during binary
upgrade so that the OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 72 ++++++++++++--
src/backend/catalog/index.c | 22 ++++-
src/backend/commands/tablespace.c | 17 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 +++++++++
src/bin/pg_dump/pg_dump.c | 104 +++++++++++++--------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
12 files changed, 252 insertions(+), 62 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 5fcd004..6560b19 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 83746d3..851a4e2 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -286,7 +288,10 @@ SystemAttributeByName(const char *attname)
*
* Note API change: the caller must now always provide the OID
* to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * should) be left unspecified unless explicitly set in binary-upgrade mode.
+ *
+ * suppress_storage indicates whether or not to create the storage if the
+ * caller passed a valid relfilenode.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,7 +311,8 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool suppress_storage)
{
bool create_storage;
Relation rel;
@@ -367,16 +373,22 @@ heap_create(const char *relname,
}
/*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
+ * Decide whether to create storage. If suppress_storage is true and a
+ * valid relfilenode is passed, storage is already created, so don't do it
+ * here. If suppress_storage is false and relfilenode is valid, create the
+ * storage with the specified relfilenode. If relfilenode is unspecified
+ * by the caller then create the storage with oid same as relid. Also,
+ * don't create it for relkinds without physical storage.
*/
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ if (!RELKIND_HAS_STORAGE(relkind) || (OidIsValid(relfilenode) && suppress_storage))
create_storage = false;
else
{
create_storage = true;
- relfilenode = relid;
+
+ /* Initialize relfilenode to relid */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1168,8 +1180,12 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool suppress_storage = true;
pg_class_desc = table_open(RelationRelationId, RowExclusiveLock);
@@ -1230,7 +1246,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade &&
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
@@ -1244,7 +1260,27 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ /*
+ * Override the relfilenode and set the suppress_storage flag to
+ * false so that the storage gets created with the specified
+ * relfilenode during heap_create().
+ */
+ if (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
+ relkind == RELKIND_MATVIEW)
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+
+ suppress_storage = false;
+ }
}
+
/* There might be no TOAST table, so we have to test for it. */
else if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
@@ -1252,6 +1288,21 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ /*
+ * Override the toast relfilenode and set the suppress_storage
+ * flag to false so that the storage gets created with the
+ * specified relfilenode during heap_create().
+ */
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
+
+ suppress_storage = false;
}
else
relid = GetNewRelFileNode(reltablespace, pg_class_desc,
@@ -1294,7 +1345,7 @@ heap_create_with_catalog(const char *relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1303,7 +1354,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ suppress_storage);
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 26bfa74..56a4ef9 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -86,6 +86,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -732,6 +733,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool suppress_storage = true;
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -903,7 +905,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -913,6 +915,21 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /*
+ * Overide the relfilenode and set the suppress_storage to false
+ * so that the storage gets created with the specified relfilenode
+ * during heap_create().
+ */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
+
+ suppress_storage = false;
}
else
{
@@ -939,7 +956,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ suppress_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 4b96eec..51ffa97 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,8 +336,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..135c382 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index a485fb2..b148919 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4693,73 +4693,101 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
- /*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
- */
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ /* Not every relation has storage. */
+ if (OidIsValid(relfilenode))
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index c291017..618187c 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1265,6 +1265,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..acaf343 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..4ba5748 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 6ce480b..2008758 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool suppress_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index d068d65..cd0a20d 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11036,6 +11036,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4544', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1ee65ed 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
v3-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 7.9K) download
From dc0a50b5513c38bfa0d1356c370475edec0b49cf Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Wed, 22 Sep 2021 23:36:41 +0530
Subject: [PATCH v3 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs during binary upgrade
so that the database OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 ++++++++++++++-
src/backend/commands/dbcommands.c | 35 ++++++++++++++++++++++++++++++-----
src/backend/parser/gram.y | 3 ++-
src/bin/initdb/initdb.c | 21 +++++++++++++--------
src/bin/pg_dump/pg_dump.c | 16 ++++++++++++++--
src/bin/psql/tab-complete.c | 2 +-
6 files changed, 74 insertions(+), 18 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..3865a96 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">db_oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The oid value should be greater than 16383. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..0251157 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("Consider using a value greater than %u.",
+ (FirstNormalObjectId - 1)));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /* Select an OID for the new database if is not explicitly configured. */
+ if (!OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index e3068a3..b265959 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -687,7 +687,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10254,6 +10254,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1ed4808..a5d88f1 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1836,15 +1836,12 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Create template0 database with fixed OID 2
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID 2;\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1877,6 +1874,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b148919..47c657e 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2957,8 +2957,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propogate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 5cd5838..396b4c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2516,7 +2516,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-09-23 19:13 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-09-23 19:13 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Wed, Sep 22, 2021 at 3:07 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > - The comment in binary_upgrade_set_pg_class_oids() is still not
> > accurate. You removed the sentence which says "Indexes cannot have
> > toast tables, so we need not make this probe in the index code path"
> > but the immediately preceding sentence is still inaccurate in at least
> > two ways. First, it only talks about tables, but the code now applies
> > to indexes. Second, it only talks about OIDs, but now also deals with
> > refilenodes. It's really important to fully update every comment that
> > might be affected by your changes!
>
> The comment is updated.
Looks good.
> The SQL query will not result in duplicate rows because the first join
> filters the duplicate rows if any with the on clause ' i.indisvalid'
> on it. The result of the first join is further left joined with pg_class and
> pg_class will not have duplicate rows for a given oid.
Oh, you're right. My mistake.
> As per your suggestion, reloid and relfilenode are absorbed in the same place.
> An additional parameter called 'suppress_storage' is passed to heap_create()
> which indicates whether or not to create the storage when the caller
> passed a valid relfilenode.
I find it confusing to have both suppress_storage and create_storage
with one basically as the negation of the other. To avoid that sort of
thing I generally have a policy that variables and options should say
whether something should happen, rather than whether it should be
prevented from happening. Sometimes there are good reasons - such as
strong existing precedent - to deviate from this practice but I think
it's good to follow when possible. So my proposal is to always have
create_storage and never suppress_storage, and if some function needs
to adjust the value of create_storage that was passed to it then OK.
> I did not make the changes to set the oid and relfilenode in the same call.
> I feel the uniformity w.r.t the other function signatures in
> pg_upgrade_support.c will be lost because currently each function sets
> only one attribute.
> Also, renaming the applicable function names to represent that they
> set both oid and relfilenode will make the function name even longer.
> We may opt to not include the relfilenode in the function name instead
> use a generic name like binary_upgrade_set_next_xxx_pg_class_oid() but
> then
> we will end up with some functions that set two attributes and some
> functions that set one attribute.
OK.
> I have also attached the patch to preserve the DB oid. As discussed,
> template0 will be created with a fixed oid during initdb. I am using
> OID 2 for template0. Even though oid 2 is already in use for the
> 'pg_am' catalog I see no harm in using it for template0 DB because oid
> doesn’t have to be unique across the database - it has to be unique
> for the particular catalog table. Kindly let me know if I am missing
> something?
> Apparently, if we did decide to pick an unused oid for template0 then
> I see a challenge in removing that oid from the unused oid list. I
> could not come up with a feasible solution for handling it.
You are correct that there is no intrinsic reason why the same OID
can't be used in various different catalogs. We have a policy of not
doing that, though; I'm not clear on the reason. Maybe it'd be OK to
deviate from that policy here, but another option would be to simply
change the unused_oids script (and maybe some of the others). It
already has:
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
push @{$oids}, $FirstGenbkiObjectId;
Presumably it could be easily adapted to push the value of some other
defined symbol into @{$oids} also, thus making that OID in effect
used.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-10-04 16:44 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-10-04 16:44 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Fri, Sep 24, 2021 at 12:44 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Wed, Sep 22, 2021 at 3:07 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > > - The comment in binary_upgrade_set_pg_class_oids() is still not
> > > accurate. You removed the sentence which says "Indexes cannot have
> > > toast tables, so we need not make this probe in the index code path"
> > > but the immediately preceding sentence is still inaccurate in at least
> > > two ways. First, it only talks about tables, but the code now applies
> > > to indexes. Second, it only talks about OIDs, but now also deals with
> > > refilenodes. It's really important to fully update every comment that
> > > might be affected by your changes!
> >
> > The comment is updated.
>
> Looks good.
>
> > The SQL query will not result in duplicate rows because the first join
> > filters the duplicate rows if any with the on clause ' i.indisvalid'
> > on it. The result of the first join is further left joined with pg_class and
> > pg_class will not have duplicate rows for a given oid.
>
> Oh, you're right. My mistake.
>
> > As per your suggestion, reloid and relfilenode are absorbed in the same place.
> > An additional parameter called 'suppress_storage' is passed to heap_create()
> > which indicates whether or not to create the storage when the caller
> > passed a valid relfilenode.
>
> I find it confusing to have both suppress_storage and create_storage
> with one basically as the negation of the other. To avoid that sort of
> thing I generally have a policy that variables and options should say
> whether something should happen, rather than whether it should be
> prevented from happening. Sometimes there are good reasons - such as
> strong existing precedent - to deviate from this practice but I think
> it's good to follow when possible. So my proposal is to always have
> create_storage and never suppress_storage, and if some function needs
> to adjust the value of create_storage that was passed to it then OK.
Sure, I agree. In the latest patch, only 'create_storage' is used.
> > I did not make the changes to set the oid and relfilenode in the same call.
> > I feel the uniformity w.r.t the other function signatures in
> > pg_upgrade_support.c will be lost because currently each function sets
> > only one attribute.
> > Also, renaming the applicable function names to represent that they
> > set both oid and relfilenode will make the function name even longer.
> > We may opt to not include the relfilenode in the function name instead
> > use a generic name like binary_upgrade_set_next_xxx_pg_class_oid() but
> > then
> > we will end up with some functions that set two attributes and some
> > functions that set one attribute.
>
> OK.
>
> > I have also attached the patch to preserve the DB oid. As discussed,
> > template0 will be created with a fixed oid during initdb. I am using
> > OID 2 for template0. Even though oid 2 is already in use for the
> > 'pg_am' catalog I see no harm in using it for template0 DB because oid
> > doesn’t have to be unique across the database - it has to be unique
> > for the particular catalog table. Kindly let me know if I am missing
> > something?
> > Apparently, if we did decide to pick an unused oid for template0 then
> > I see a challenge in removing that oid from the unused oid list. I
> > could not come up with a feasible solution for handling it.
>
> You are correct that there is no intrinsic reason why the same OID
> can't be used in various different catalogs. We have a policy of not
> doing that, though; I'm not clear on the reason. Maybe it'd be OK to
> deviate from that policy here, but another option would be to simply
> change the unused_oids script (and maybe some of the others). It
> already has:
>
> my $FirstGenbkiObjectId =
> Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
> push @{$oids}, $FirstGenbkiObjectId;
>
> Presumably it could be easily adapted to push the value of some other
> defined symbol into @{$oids} also, thus making that OID in effect
> used.
Thanks for the inputs, Robert. In the v4 patch, an unused OID (i.e, 4)
is fixed for the template0 and the same is removed from unused oid
list.
In addition to the review comment fixes, I have removed some code that
is no longer needed/doesn't make sense since we preserve the OIDs.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v4-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg.patch (application/octet-stream, 30.6K) download
From 7dd616333bd650c01c078885f3a8078c12fcd94c Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Mon, 4 Oct 2021 21:06:41 +0530
Subject: [PATCH v4 1/2] Preserve relfilenode and tablespace OID in pg_upgrade
The patch aims to preserve the tablespace, relfilenode OIDs during binary
upgrade so that the OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 76 ++++++++++++---
src/backend/catalog/index.c | 22 ++++-
src/backend/commands/tablespace.c | 17 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 +++++++++
src/bin/pg_dump/pg_dump.c | 104 +++++++++++++--------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/info.c | 31 +-----
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/bin/pg_upgrade/pg_upgrade.h | 10 +-
src/bin/pg_upgrade/relfilenode.c | 6 +-
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
15 files changed, 260 insertions(+), 105 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 5fcd004..58b994c 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ false);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 83746d3..e45c61d 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -286,7 +288,10 @@ SystemAttributeByName(const char *attname)
*
* Note API change: the caller must now always provide the OID
* to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * should) be left unspecified unless explicitly set in binary-upgrade mode.
+ *
+ * create_storage indicates whether or not to create the storage if the
+ * caller passed a valid relfilenode.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,9 +311,9 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool create_storage)
{
- bool create_storage;
Relation rel;
/* The caller must have provided an OID for the relation. */
@@ -367,16 +372,25 @@ heap_create(const char *relname,
}
/*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
+ * Decide whether to create storage. If relfilenode is valid and
+ * create_storage is false, storage is already created, so don't do it
+ * here. If relfilenode is valid and create_storage is true, create the
+ * storage with the specified relfilenode. If relfilenode is unspecified
+ * by the caller then create the storage with oid same as relid. Also,
+ * don't create it for relkinds without physical storage.
*/
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ if (!RELKIND_HAS_STORAGE(relkind) || (OidIsValid(relfilenode) && !create_storage))
create_storage = false;
else
{
create_storage = true;
- relfilenode = relid;
+
+ /*
+ * Create the storage with oid same as relid if relfilenode is
+ * unspecified by the caller
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1168,8 +1182,12 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage = false;
pg_class_desc = table_open(RelationRelationId, RowExclusiveLock);
@@ -1230,7 +1248,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade &&
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
@@ -1244,7 +1262,27 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ /*
+ * Override the relfilenode and set the create_storage flag to
+ * true so that the storage gets created with the specified
+ * relfilenode during heap_create().
+ */
+ if (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
+ relkind == RELKIND_MATVIEW)
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+
+ create_storage = true;
+ }
}
+
/* There might be no TOAST table, so we have to test for it. */
else if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
@@ -1252,6 +1290,21 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ /*
+ * Override the toast relfilenode and set the create_storage flag
+ * to true so that the storage gets created with the specified
+ * relfilenode during heap_create().
+ */
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
+
+ create_storage = true;
}
else
relid = GetNewRelFileNode(reltablespace, pg_class_desc,
@@ -1294,7 +1347,7 @@ heap_create_with_catalog(const char *relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1303,7 +1356,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 26bfa74..f167b10 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -86,6 +86,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -732,6 +733,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage = false;
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -903,7 +905,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -913,6 +915,21 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /*
+ * Overide the relfilenode and set the create_storage to true so
+ * that the storage gets created with the specified relfilenode
+ * during heap_create().
+ */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
+
+ create_storage = true;
}
else
{
@@ -939,7 +956,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 4b96eec..51ffa97 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,8 +336,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..135c382 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index a485fb2..b148919 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4693,73 +4693,101 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
- /*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
- */
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ /* Not every relation has storage. */
+ if (OidIsValid(relfilenode))
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index c291017..618187c 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1265,6 +1265,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 5d9a26c..775564e 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -199,14 +199,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->old_db_oid = old_db->db_oid;
map->new_db_oid = new_db->db_oid;
- /*
- * old_relfilenode might differ from pg_class.oid (and hence
- * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
- */
- map->old_relfilenode = old_rel->relfilenode;
-
- /* new_relfilenode will match old and new pg_class.oid */
- map->new_relfilenode = new_rel->relfilenode;
+ /* relfilenode is preserved across old and new cluster */
+ map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
map->nspname = old_rel->nspname;
@@ -278,27 +272,6 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
reloid, db->db_name, reldesc);
}
-
-void
-print_maps(FileNameMap *maps, int n_maps, const char *db_name)
-{
- if (log_opts.verbose)
- {
- int mapnum;
-
- pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
-
- for (mapnum = 0; mapnum < n_maps; mapnum++)
- pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
- maps[mapnum].nspname, maps[mapnum].relname,
- maps[mapnum].old_relfilenode,
- maps[mapnum].new_relfilenode);
-
- pg_log(PG_VERBOSE, "\n\n");
- }
-}
-
-
/*
* get_db_and_rel_infos()
*
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..acaf343 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index ca0795f..c79ddfc 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -159,13 +159,7 @@ typedef struct
const char *new_tablespace_suffix;
Oid old_db_oid;
Oid new_db_oid;
-
- /*
- * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
- * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
- */
- Oid old_relfilenode;
- Oid new_relfilenode;
+ Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
char *relname;
@@ -390,8 +384,6 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
-void print_maps(FileNameMap *maps, int n,
- const char *db_name);
/* option.c */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 4deae7d..3bc5b44 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -119,8 +119,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
new_pgdata);
if (n_maps)
{
- print_maps(mappings, n_maps, new_db->db_name);
-
transfer_single_new_db(mappings, n_maps, old_tablespace);
}
/* We allocate something even for n_maps == 0 */
@@ -206,14 +204,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
map->old_tablespace,
map->old_tablespace_suffix,
map->old_db_oid,
- map->old_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
map->new_db_oid,
- map->new_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..4ba5748 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 6ce480b..7cf5402 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool create_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index d068d65..cd0a20d 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11036,6 +11036,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4544', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1ee65ed 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
v4-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 12.4K) download
From c2d00b36558957782f5f4f75b6613a4c971d8ef0 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Mon, 4 Oct 2021 21:38:33 +0530
Subject: [PATCH v4 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs across binary upgrade
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 ++++++++++++++-
src/backend/commands/dbcommands.c | 35 ++++++++++++++++++++++++++++++-----
src/backend/parser/gram.y | 3 ++-
src/bin/initdb/initdb.c | 21 +++++++++++++--------
src/bin/pg_dump/pg_dump.c | 16 ++++++++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 +++------
src/bin/pg_upgrade/pg_upgrade.h | 3 +--
src/bin/pg_upgrade/relfilenode.c | 4 ++--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 +++
src/include/catalog/unused_oids | 5 +++++
12 files changed, 89 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..3865a96 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">db_oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The oid value should be greater than 16383. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..0251157 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("Consider using a value greater than %u.",
+ (FirstNormalObjectId - 1)));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /* Select an OID for the new database if is not explicitly configured. */
+ if (!OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 08f1bf1..a8cdd43 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -687,7 +687,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10254,6 +10254,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1ed4808..3ce3c59 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1836,15 +1836,12 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Create template0 database with oid Template0ObjectId i.e, 4
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID 4;\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1877,6 +1874,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b148919..47c657e 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2957,8 +2957,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propogate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 775564e..fe1357d 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -196,10 +196,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -330,8 +328,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2",
+ "ORDER BY 1",
/* 9.2 removed the spclocation column */
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index c79ddfc..937c45b 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -157,8 +157,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 3bc5b44..a4fc298 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -203,14 +203,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 5cd5838..396b4c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2516,7 +2516,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index d22de19..716a319 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Templete0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index 5b7ce5f..2750913 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-10-06 20:35 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-10-06 20:35 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Oct 4, 2021 at 12:44 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> Thanks for the inputs, Robert. In the v4 patch, an unused OID (i.e, 4)
> is fixed for the template0 and the same is removed from unused oid
> list.
>
> In addition to the review comment fixes, I have removed some code that
> is no longer needed/doesn't make sense since we preserve the OIDs.
This is not a full review, but I'm wondering about this bit of code:
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ if (!RELKIND_HAS_STORAGE(relkind) || (OidIsValid(relfilenode)
&& !create_storage))
create_storage = false;
else
{
create_storage = true;
- relfilenode = relid;
+
+ /*
+ * Create the storage with oid same as relid if relfilenode is
+ * unspecified by the caller
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
This seems hard to understand, and I wonder if perhaps it can be
simplified. If !RELKIND_HAS_STORAGE(relkind), then we're going to set
create_storage to false if it was previously true, and otherwise just
do nothing. Otherwise, if !create_storage, we'll enter the
create_storage = false branch which effectively does nothing.
Otherwise, if !OidIsValid(relfilenode), we'll set relfilenode = relid.
So couldn't we express that like this?
if (!RELKIND_HAS_STORAGE(relkind))
create_storage = false;
else if (create_storage && !OidIsValid(relfilenode))
relfilenode = relid;
If so, that seems more clear.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-10-07 07:24 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-10-07 07:24 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Oct 7, 2021 at 2:05 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Mon, Oct 4, 2021 at 12:44 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Thanks for the inputs, Robert. In the v4 patch, an unused OID (i.e, 4)
> > is fixed for the template0 and the same is removed from unused oid
> > list.
> >
> > In addition to the review comment fixes, I have removed some code that
> > is no longer needed/doesn't make sense since we preserve the OIDs.
>
> This is not a full review, but I'm wondering about this bit of code:
>
> - if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
> + if (!RELKIND_HAS_STORAGE(relkind) || (OidIsValid(relfilenode)
> && !create_storage))
> create_storage = false;
> else
> {
> create_storage = true;
> - relfilenode = relid;
> +
> + /*
> + * Create the storage with oid same as relid if relfilenode is
> + * unspecified by the caller
> + */
> + if (!OidIsValid(relfilenode))
> + relfilenode = relid;
> }
>
> This seems hard to understand, and I wonder if perhaps it can be
> simplified. If !RELKIND_HAS_STORAGE(relkind), then we're going to set
> create_storage to false if it was previously true, and otherwise just
> do nothing. Otherwise, if !create_storage, we'll enter the
> create_storage = false branch which effectively does nothing.
> Otherwise, if !OidIsValid(relfilenode), we'll set relfilenode = relid.
> So couldn't we express that like this?
>
> if (!RELKIND_HAS_STORAGE(relkind))
> create_storage = false;
> else if (create_storage && !OidIsValid(relfilenode))
> relfilenode = relid;
>
> If so, that seems more clear.
'create_storage' flag says whether or not to create the storage when a
valid relfilenode is passed. 'create_storage' flag alone cannot make
the storage creation decision in heap_create().
Only binary upgrade flow sets the 'create_storage' flag to true and
expects storage gets created with specified relfilenode. Every other
caller/flow passes false for 'create_storage' and we still need to
create storage in heap_create() if relkind has storage. That's why I
have explicitly set 'create_storage = true' in the else flow and
initialize relfilenode on need basis.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-10-07 14:02 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-10-07 14:02 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Oct 7, 2021 at 3:24 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> Every other
> caller/flow passes false for 'create_storage' and we still need to
> create storage in heap_create() if relkind has storage.
That seems surprising.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-10-26 13:24 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-10-26 13:24 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Oct 7, 2021 at 7:33 PM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Thu, Oct 7, 2021 at 3:24 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Every other
> > caller/flow passes false for 'create_storage' and we still need to
> > create storage in heap_create() if relkind has storage.
>
> That seems surprising.
I have revised the patch w.r.t the way 'create_storage' is interpreted
in heap_create() along with some minor changes to preserve the DBOID
patch.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v5-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg.patch (application/octet-stream, 29.9K) download
From da9a1c117cb299543e4468a082518f778ec52dff Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Tue, 26 Oct 2021 18:16:00 +0530
Subject: [PATCH v5 1/2] Preserve relfilenode and tablespace OID in pg_upgrade
The patch aims to preserve the tablespace, relfilenode OIDs during binary
upgrade so that the OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 59 +++++++++---
src/backend/catalog/index.c | 19 +++-
src/backend/commands/tablespace.c | 17 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 +++++++++
src/bin/pg_dump/pg_dump.c | 104 +++++++++++++--------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/info.c | 31 +-----
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/bin/pg_upgrade/pg_upgrade.h | 10 +-
src/bin/pg_upgrade/relfilenode.c | 6 +-
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
15 files changed, 237 insertions(+), 108 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 5fcd004..6560b19 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 81cc39f..ebd8454 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -286,7 +288,9 @@ SystemAttributeByName(const char *attname)
*
* Note API change: the caller must now always provide the OID
* to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * should) be left unspecified unless explicitly set in binary-upgrade mode.
+ *
+ * create_storage indicates whether or not to create the storage.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,9 +310,9 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool create_storage)
{
- bool create_storage;
Relation rel;
/* The caller must have provided an OID for the relation. */
@@ -366,17 +370,17 @@ heap_create(const char *relname,
break;
}
- /*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
- */
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ /* Don't create storage for relkinds without physical storage. */
+ if (!RELKIND_HAS_STORAGE(relkind))
create_storage = false;
else
{
- create_storage = true;
- relfilenode = relid;
+ /*
+ * If relfilenode is unspecified by the caller then create storage
+ * with oid same as relid.
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1171,6 +1175,9 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
@@ -1233,7 +1240,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade &&
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
@@ -1247,7 +1254,21 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ /* Override the relfilenode */
+ if (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
+ relkind == RELKIND_MATVIEW)
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+ }
}
+
/* There might be no TOAST table, so we have to test for it. */
else if (IsBinaryUpgrade &&
OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
@@ -1255,6 +1276,15 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ /* Override the toast relfilenode */
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
}
else
relid = GetNewRelFileNode(reltablespace, pg_class_desc,
@@ -1297,7 +1327,7 @@ heap_create_with_catalog(const char *relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1306,7 +1336,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ RELKIND_HAS_STORAGE(relkind));
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 26bfa74..88c82f9 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -86,6 +86,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -732,6 +733,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage;
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -739,6 +741,9 @@ index_create(Relation heapRelation,
/* partitioned indexes must never be "built" by themselves */
Assert(!partitioned || (flags & INDEX_CREATE_SKIP_BUILD));
+ /* Set the create_storage flag */
+ create_storage = !partitioned && !OidIsValid(relFileNode);
+
relkind = partitioned ? RELKIND_PARTITIONED_INDEX : RELKIND_INDEX;
is_exclusion = (indexInfo->ii_ExclusionOps != NULL);
@@ -903,7 +908,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -913,6 +918,15 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /* Overide the index relfilenode */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
}
else
{
@@ -939,7 +953,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 4b96eec..51ffa97 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,8 +336,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..135c382 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index e9f68e8..13213ff 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4693,73 +4693,101 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
- /*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
- */
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ /* Not every relation has storage. */
+ if (OidIsValid(relfilenode))
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index c291017..618187c 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1265,6 +1265,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 5d9a26c..775564e 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -199,14 +199,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->old_db_oid = old_db->db_oid;
map->new_db_oid = new_db->db_oid;
- /*
- * old_relfilenode might differ from pg_class.oid (and hence
- * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
- */
- map->old_relfilenode = old_rel->relfilenode;
-
- /* new_relfilenode will match old and new pg_class.oid */
- map->new_relfilenode = new_rel->relfilenode;
+ /* relfilenode is preserved across old and new cluster */
+ map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
map->nspname = old_rel->nspname;
@@ -278,27 +272,6 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
reloid, db->db_name, reldesc);
}
-
-void
-print_maps(FileNameMap *maps, int n_maps, const char *db_name)
-{
- if (log_opts.verbose)
- {
- int mapnum;
-
- pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
-
- for (mapnum = 0; mapnum < n_maps; mapnum++)
- pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
- maps[mapnum].nspname, maps[mapnum].relname,
- maps[mapnum].old_relfilenode,
- maps[mapnum].new_relfilenode);
-
- pg_log(PG_VERBOSE, "\n\n");
- }
-}
-
-
/*
* get_db_and_rel_infos()
*
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..acaf343 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index ca0795f..c79ddfc 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -159,13 +159,7 @@ typedef struct
const char *new_tablespace_suffix;
Oid old_db_oid;
Oid new_db_oid;
-
- /*
- * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
- * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
- */
- Oid old_relfilenode;
- Oid new_relfilenode;
+ Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
char *relname;
@@ -390,8 +384,6 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
-void print_maps(FileNameMap *maps, int n,
- const char *db_name);
/* option.c */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 4deae7d..3bc5b44 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -119,8 +119,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
new_pgdata);
if (n_maps)
{
- print_maps(mappings, n_maps, new_db->db_name);
-
transfer_single_new_db(mappings, n_maps, old_tablespace);
}
/* We allocate something even for n_maps == 0 */
@@ -206,14 +204,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
map->old_tablespace,
map->old_tablespace_suffix,
map->old_db_oid,
- map->old_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
map->new_db_oid,
- map->new_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..4ba5748 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 6ce480b..7cf5402 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool create_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index d068d65..cd0a20d 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11036,6 +11036,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4544', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1ee65ed 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
v5-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 12.7K) download
From 554d92617025bb33d97a68b60697009c2ad2c030 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Tue, 26 Oct 2021 18:40:51 +0530
Subject: [PATCH v5 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs across binary upgrade
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 ++++++++++++++-
src/backend/commands/dbcommands.c | 35 ++++++++++++++++++++++++++++++-----
src/backend/parser/gram.y | 3 ++-
src/bin/initdb/initdb.c | 23 +++++++++++++++--------
src/bin/pg_dump/pg_dump.c | 16 ++++++++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 +++------
src/bin/pg_upgrade/pg_upgrade.h | 3 +--
src/bin/pg_upgrade/relfilenode.c | 4 ++--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 +++
src/include/catalog/unused_oids | 5 +++++
12 files changed, 91 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..fc108a8 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">db_oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The OID range for user objects starts from 16384. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..e8cdb7f 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /* Select an OID for the new database if is not explicitly configured. */
+ if (!OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 08f1bf1..a8cdd43 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -687,7 +687,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10254,6 +10254,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1ed4808..61cac6e 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1836,15 +1837,13 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Create template0 database with oid Template0ObjectId i.e, 4
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1877,6 +1876,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 13213ff..ce37bf4 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2957,8 +2957,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propogate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 775564e..fe1357d 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -196,10 +196,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -330,8 +328,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2",
+ "ORDER BY 1",
/* 9.2 removed the spclocation column */
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index c79ddfc..937c45b 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -157,8 +157,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 3bc5b44..a4fc298 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -203,14 +203,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index ecae9df..b812f61 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2516,7 +2516,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index d22de19..716a319 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Templete0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index 5b7ce5f..2750913 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-06 04:44 Sadhuprasad Patro <b.sadhu@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 2 replies; 90+ messages in thread
From: Sadhuprasad Patro @ 2021-12-06 04:44 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Oct 26, 2021 at 6:55 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
>
>
> I have revised the patch w.r.t the way 'create_storage' is interpreted
> in heap_create() along with some minor changes to preserve the DBOID
> patch.
>
Hi Shruthi,
I am reviewing the attached patches and providing a few comments here
below for patch "v5-0002-Preserve-database-OIDs-in-pg_upgrade.patch"
1.
inline patch 1: doc/src/sgml/ref/create_database.sgml (+0/-0 lines) download inline-1.patch
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable
class="parameter">name</replaceable>
- [ IS_TEMPLATE [=] <replaceable
class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable
class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable
class="parameter">db_oid</replaceable> ] ]
Replace "db_oid" with 'oid'. Below in the listitem, we have mentioned 'oid'.
2.
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user
objects %u.",
+ dboid, FirstNormalObjectId));
+ }
Are we sure that 'IsBinaryUpgrade' will be set properly, before the
createdb function is called? Can we recheck once ?
3.
@@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /* Select an OID for the new database if is not explicitly configured. */
+ if (!OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
I think we need to do 'check_db_file_conflict' for the USER given OID
also.. right? It may already be present.
4.
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
/*
+ * Create template0 database with oid Template0ObjectId i.e, 4
+ */
+
Better to mention here, why OID 4 is reserved for template0 database?.
5.
+ /*
+ * Create template0 database with oid Template0ObjectId i.e, 4
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID "
+ CppAsString2(Template0ObjectId) ";\n\n",
Can we write something like, 'OID = CppAsString2(Template0ObjectId)'?
mention "=".
6.
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
Make the above comment a single line comment.
7.
There are some spelling mistakes in the comments as below, please
correct the same
+ /*
+ * Make sure that binary upgrade propogate the database OID to the
new =====> correct spelling
+ * cluster
+ */
+/* OID 4 is reserved for Templete0 database */
====> Correct spelling
+#define Template0ObjectId 4
I am reviewing another patch
"v5-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg" as well
and will provide the comments soon if any...
Thanks & Regards
SadhuPrasad
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-06 17:55 Robert Haas <robertmhaas@gmail.com>
parent: Sadhuprasad Patro <b.sadhu@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Robert Haas @ 2021-12-06 17:55 UTC (permalink / raw)
To: Sadhuprasad Patro <b.sadhu@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Sun, Dec 5, 2021 at 11:44 PM Sadhuprasad Patro <b.sadhu@gmail.com> wrote:
> 1.
> --- a/doc/src/sgml/ref/create_database.sgml
> +++ b/doc/src/sgml/ref/create_database.sgml
> @@ -31,7 +31,8 @@ CREATE DATABASE <replaceable
> class="parameter">name</replaceable>
> - [ IS_TEMPLATE [=] <replaceable
> class="parameter">istemplate</replaceable> ] ]
> + [ IS_TEMPLATE [=] <replaceable
> class="parameter">istemplate</replaceable> ]
> + [ OID [=] <replaceable
> class="parameter">db_oid</replaceable> ] ]
>
> Replace "db_oid" with 'oid'. Below in the listitem, we have mentioned 'oid'.
I agree that the listitem and the synopsis need to be consistent, but
it could be made consistent either by changing that one to db_oid or
this one to oid.
> 2.
> --- a/src/backend/commands/dbcommands.c
> +++ b/src/backend/commands/dbcommands.c
> + if ((dboid < FirstNormalObjectId) &&
> + (strcmp(dbname, "template0") != 0) &&
> + (!IsBinaryUpgrade))
> + ereport(ERROR,
> + (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
> + errmsg("Invalid value for option \"%s\"", defel->defname),
> + errhint("The specified OID %u is less than the minimum OID for user
> objects %u.",
> + dboid, FirstNormalObjectId));
> + }
>
> Are we sure that 'IsBinaryUpgrade' will be set properly, before the
> createdb function is called? Can we recheck once ?
How could it be set incorrectly, and how could we recheck this?
> 3.
> @@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
> */
> pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
>
> - do
> + /* Select an OID for the new database if is not explicitly configured. */
> + if (!OidIsValid(dboid))
> {
> - dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
> - Anum_pg_database_oid);
> - } while (check_db_file_conflict(dboid));
>
> I think we need to do 'check_db_file_conflict' for the USER given OID
> also.. right? It may already be present.
Hopefully, if that happens, we straight up fail later on.
> 4.
> --- a/src/bin/initdb/initdb.c
> +++ b/src/bin/initdb/initdb.c
>
> /*
> + * Create template0 database with oid Template0ObjectId i.e, 4
> + */
> +
>
> Better to mention here, why OID 4 is reserved for template0 database?.
I'm not sure how we would give a reason for selecting an arbitrary
constant? We could perhaps explain why we use a fixed OID. But there's
no reason it has to be 4, I think.
> 5.
> + /*
> + * Create template0 database with oid Template0ObjectId i.e, 4
> + */
> + static const char *const template0_setup[] = {
> + "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID "
> + CppAsString2(Template0ObjectId) ";\n\n",
>
> Can we write something like, 'OID = CppAsString2(Template0ObjectId)'?
> mention "=".
That seems like a good idea, because it would be more consistent.
> 6.
> +
> + /*
> + * We use the OID of postgres to determine datlastsysoid
> + */
> + "UPDATE pg_database SET datlastsysoid = "
> + " (SELECT oid FROM pg_database "
> + " WHERE datname = 'postgres');\n\n",
> +
>
> Make the above comment a single line comment.
I think what Shruthi did is more correct. It doesn't have to be done
as a single-line comment just because it can fit on one line. And
Shruthi didn't write this comment anyway, it's only moved slightly
from where it was before.
> 7.
> There are some spelling mistakes in the comments as below, please
> correct the same
> + /*
> + * Make sure that binary upgrade propogate the database OID to the
> new =====> correct spelling
> + * cluster
> + */
>
> +/* OID 4 is reserved for Templete0 database */
> ====> Correct spelling
> +#define Template0ObjectId 4
Yes, those would be good to fix.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-13 14:40 Shruthi Gowda <gowdashru@gmail.com>
parent: Sadhuprasad Patro <b.sadhu@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-12-13 14:40 UTC (permalink / raw)
To: Sadhuprasad Patro <b.sadhu@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Dec 6, 2021 at 10:14 AM Sadhuprasad Patro <b.sadhu@gmail.com> wrote:
>
> On Tue, Oct 26, 2021 at 6:55 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> >
> >
> > I have revised the patch w.r.t the way 'create_storage' is interpreted
> > in heap_create() along with some minor changes to preserve the DBOID
> > patch.
> >
>
> Hi Shruthi,
>
> I am reviewing the attached patches and providing a few comments here
> below for patch "v5-0002-Preserve-database-OIDs-in-pg_upgrade.patch"
>
> 1.
> --- a/doc/src/sgml/ref/create_database.sgml
> +++ b/doc/src/sgml/ref/create_database.sgml
> @@ -31,7 +31,8 @@ CREATE DATABASE <replaceable
> class="parameter">name</replaceable>
> - [ IS_TEMPLATE [=] <replaceable
> class="parameter">istemplate</replaceable> ] ]
> + [ IS_TEMPLATE [=] <replaceable
> class="parameter">istemplate</replaceable> ]
> + [ OID [=] <replaceable
> class="parameter">db_oid</replaceable> ] ]
>
> Replace "db_oid" with 'oid'. Below in the listitem, we have mentioned 'oid'.
Replaced "db_oid" with "oid"
>
> 2.
> --- a/src/backend/commands/dbcommands.c
> +++ b/src/backend/commands/dbcommands.c
> + if ((dboid < FirstNormalObjectId) &&
> + (strcmp(dbname, "template0") != 0) &&
> + (!IsBinaryUpgrade))
> + ereport(ERROR,
> + (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
> + errmsg("Invalid value for option \"%s\"", defel->defname),
> + errhint("The specified OID %u is less than the minimum OID for user
> objects %u.",
> + dboid, FirstNormalObjectId));
> + }
>
> Are we sure that 'IsBinaryUpgrade' will be set properly, before the
> createdb function is called? Can we recheck once ?
I believe 'IsBinaryUpgrade' will be set to true when pg_upgrade is invoked.
pg_ugrade internally does pg_dump and pg_restore for every database in
the cluster.
> 3.
> @@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
> */
> pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
>
> - do
> + /* Select an OID for the new database if is not explicitly configured. */
> + if (!OidIsValid(dboid))
> {
> - dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
> - Anum_pg_database_oid);
> - } while (check_db_file_conflict(dboid));
>
> I think we need to do 'check_db_file_conflict' for the USER given OID
> also.. right? It may already be present.
If a datafile with user-specified OID exists, the create database
fails with the below error.
postgres=# create database d2 oid 16452;
ERROR: could not create directory "base/16452": File exists
> 4.
> --- a/src/bin/initdb/initdb.c
> +++ b/src/bin/initdb/initdb.c
>
> /*
> + * Create template0 database with oid Template0ObjectId i.e, 4
> + */
> +
>
> Better to mention here, why OID 4 is reserved for template0 database?.
The comment is updated to explain why template0 oid is fixed.
> 5.
> + /*
> + * Create template0 database with oid Template0ObjectId i.e, 4
> + */
> + static const char *const template0_setup[] = {
> + "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID "
> + CppAsString2(Template0ObjectId) ";\n\n",
>
> Can we write something like, 'OID = CppAsString2(Template0ObjectId)'?
> mention "=".
Fixed
> 6.
> +
> + /*
> + * We use the OID of postgres to determine datlastsysoid
> + */
> + "UPDATE pg_database SET datlastsysoid = "
> + " (SELECT oid FROM pg_database "
> + " WHERE datname = 'postgres');\n\n",
> +
>
> Make the above comment a single line comment.
As Robert confirmed, this part of the code is moved from a different place.
> 7.
> There are some spelling mistakes in the comments as below, please
> correct the same
> + /*
> + * Make sure that binary upgrade propogate the database OID to the
> new =====> correct spelling
> + * cluster
> + */
>
> +/* OID 4 is reserved for Templete0 database */
> ====> Correct spelling
> +#define Template0ObjectId 4
>
Fixed.
> I am reviewing another patch
> "v5-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg" as well
> and will provide the comments soon if any...
Thanks. I have rebased relfilenode oid preserve patch. You may use the
rebased patch for review.
Thanks & Regards
Shruthi K C
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v6-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg.patch (application/octet-stream, 29.7K) download
From 9bbddded62ab81499bd8dabf351258cff32fe733 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Sun, 12 Dec 2021 23:26:31 +0530
Subject: [PATCH v6 1/2] Preserve relfilenode and tablespace OID in pg_upgrade
The patch aims to preserve the tablespace, relfilenode OIDs during binary
upgrade so that the OIDs are same across old and new cluster.
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 58 +++++++++---
src/backend/catalog/index.c | 19 +++-
src/backend/commands/tablespace.c | 17 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 +++++++++
src/bin/pg_dump/pg_dump.c | 104 +++++++++++++--------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/info.c | 31 +-----
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/bin/pg_upgrade/pg_upgrade.h | 10 +-
src/bin/pg_upgrade/relfilenode.c | 6 +-
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
15 files changed, 236 insertions(+), 108 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 5fcd004..6560b19 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 6780ec5..6c1abd4 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -286,7 +288,9 @@ SystemAttributeByName(const char *attname)
*
* Note API change: the caller must now always provide the OID
* to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * should) be left unspecified unless explicitly set in binary-upgrade mode.
+ *
+ * create_storage indicates whether or not to create the storage.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,9 +310,9 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool create_storage)
{
- bool create_storage;
Relation rel;
/* The caller must have provided an OID for the relation. */
@@ -343,17 +347,17 @@ heap_create(const char *relname,
if (!RELKIND_HAS_TABLESPACE(relkind))
reltablespace = InvalidOid;
- /*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
- */
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ /* Don't create storage for relkinds without physical storage. */
+ if (!RELKIND_HAS_STORAGE(relkind))
create_storage = false;
else
{
- create_storage = true;
- relfilenode = relid;
+ /*
+ * If relfilenode is unspecified by the caller then create storage
+ * with oid same as relid.
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1121,6 +1125,9 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
@@ -1183,7 +1190,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
/*
@@ -1200,6 +1207,15 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ /* Override the toast relfilenode */
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
}
}
else
@@ -1211,6 +1227,19 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ /* Override the relfilenode */
+ if (relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
+ relkind == RELKIND_MATVIEW)
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+ }
}
}
@@ -1255,7 +1284,7 @@ heap_create_with_catalog(const char *relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1264,7 +1293,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ RELKIND_HAS_STORAGE(relkind));
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 1757cd3..a4d1f9c 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -87,6 +87,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -733,6 +734,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage;
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -740,6 +742,9 @@ index_create(Relation heapRelation,
/* partitioned indexes must never be "built" by themselves */
Assert(!partitioned || (flags & INDEX_CREATE_SKIP_BUILD));
+ /* Set the create_storage flag */
+ create_storage = !partitioned && !OidIsValid(relFileNode);
+
relkind = partitioned ? RELKIND_PARTITIONED_INDEX : RELKIND_INDEX;
is_exclusion = (indexInfo->ii_ExclusionOps != NULL);
@@ -904,7 +909,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -914,6 +919,15 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /* Overide the index relfilenode */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
}
else
{
@@ -940,7 +954,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 4b96eec..51ffa97 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,8 +336,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7..135c382 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 10a86f9..b41d86b 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4836,73 +4836,101 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
- /*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
- */
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ /* Not every relation has storage. */
+ if (OidIsValid(relfilenode))
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 27b9573..3f42316 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1222,6 +1222,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 5d9a26c..775564e 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -199,14 +199,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->old_db_oid = old_db->db_oid;
map->new_db_oid = new_db->db_oid;
- /*
- * old_relfilenode might differ from pg_class.oid (and hence
- * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
- */
- map->old_relfilenode = old_rel->relfilenode;
-
- /* new_relfilenode will match old and new pg_class.oid */
- map->new_relfilenode = new_rel->relfilenode;
+ /* relfilenode is preserved across old and new cluster */
+ map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
map->nspname = old_rel->nspname;
@@ -278,27 +272,6 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
reloid, db->db_name, reldesc);
}
-
-void
-print_maps(FileNameMap *maps, int n_maps, const char *db_name)
-{
- if (log_opts.verbose)
- {
- int mapnum;
-
- pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
-
- for (mapnum = 0; mapnum < n_maps; mapnum++)
- pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
- maps[mapnum].nspname, maps[mapnum].relname,
- maps[mapnum].old_relfilenode,
- maps[mapnum].new_relfilenode);
-
- pg_log(PG_VERBOSE, "\n\n");
- }
-}
-
-
/*
* get_db_and_rel_infos()
*
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd7..acaf343 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index ca0795f..c79ddfc 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -159,13 +159,7 @@ typedef struct
const char *new_tablespace_suffix;
Oid old_db_oid;
Oid new_db_oid;
-
- /*
- * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
- * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
- */
- Oid old_relfilenode;
- Oid new_relfilenode;
+ Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
char *relname;
@@ -390,8 +384,6 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
-void print_maps(FileNameMap *maps, int n,
- const char *db_name);
/* option.c */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 4deae7d..3bc5b44 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -119,8 +119,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
new_pgdata);
if (n_maps)
{
- print_maps(mappings, n_maps, new_db->db_name);
-
transfer_single_new_db(mappings, n_maps, old_tablespace);
}
/* We allocate something even for n_maps == 0 */
@@ -206,14 +204,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
map->old_tablespace,
map->old_tablespace_suffix,
map->old_db_oid,
- map->old_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
map->new_db_oid,
- map->new_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7..4ba5748 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 6ce480b..7cf5402 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool create_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 79d787c..92727d8 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11058,6 +11058,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4548', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1ee65ed 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
v6-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 12.7K) download
From b0694cc31feb241eb82fda6f83897df784c66cfd Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Mon, 13 Dec 2021 19:30:28 +0530
Subject: [PATCH v6 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs across binary upgrade
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 ++++++++++++++-
src/backend/commands/dbcommands.c | 35 ++++++++++++++++++++++++++++++-----
src/backend/parser/gram.y | 3 ++-
src/bin/initdb/initdb.c | 24 ++++++++++++++++--------
src/bin/pg_dump/pg_dump.c | 16 ++++++++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 +++------
src/bin/pg_upgrade/pg_upgrade.h | 3 +--
src/bin/pg_upgrade/relfilenode.c | 4 ++--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 +++
src/include/catalog/unused_oids | 5 +++++
12 files changed, 92 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..94d4cfb 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The OID range for user objects starts from 16384. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..e8cdb7f 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /* Select an OID for the new database if is not explicitly configured. */
+ if (!OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 3d4dd43..9572f9a 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -711,7 +711,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10416,6 +10416,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 403adb0..31aa21a 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,15 +1839,14 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Template0 oid is fixed during initdb to avoid oid conflict across versions
+ * during binary upgrade.
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1879,6 +1879,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b41d86b..62e418c 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -3018,8 +3018,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 775564e..fe1357d 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -196,10 +196,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -330,8 +328,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2",
+ "ORDER BY 1",
/* 9.2 removed the spclocation column */
(GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
"t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index c79ddfc..937c45b 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -157,8 +157,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 3bc5b44..a4fc298 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -203,14 +203,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 2f412ca..414d709 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2587,7 +2587,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index d22de19..d06e3a9 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Template0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index 5b7ce5f..2750913 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-13 15:13 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-12-13 15:13 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Dec 6, 2021 at 11:25 PM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Sun, Dec 5, 2021 at 11:44 PM Sadhuprasad Patro <b.sadhu@gmail.com> wrote:
> > 3.
> > @@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
> > */
> > pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
> >
> > - do
> > + /* Select an OID for the new database if is not explicitly configured. */
> > + if (!OidIsValid(dboid))
> > {
> > - dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
> > - Anum_pg_database_oid);
> > - } while (check_db_file_conflict(dboid));
> >
> > I think we need to do 'check_db_file_conflict' for the USER given OID
> > also.. right? It may already be present.
>
> Hopefully, if that happens, we straight up fail later on.
That's right. If a database with user-specified OID exists, the
createdb fails with a "duplicate key value" error.
If just a data directory with user-specified OID exists,
MakePGDirectory() fails to create the directory and the cleanup
callback createdb_failure_callback() removes the directory that was
not created by 'createdb()' function.
The subsequent create database call with the same OID will succeed.
Should we handle the case where a data directory exists and the
corresponding DB with that oid does not exist? I presume this
situation doesn't arise unless the user tries to create directories in
the data path. Any thoughts?
Thanks & Regards
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-13 21:05 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 2 replies; 90+ messages in thread
From: Robert Haas @ 2021-12-13 21:05 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Dec 13, 2021 at 9:40 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > I am reviewing another patch
> > "v5-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg" as well
> > and will provide the comments soon if any...
I spent much of today reviewing 0001. Here's an updated version, so
far only lightly tested. Please check whether I've broken anything.
Here are the changes:
- I adjusted the function header comment for heap_create. Your
proposed comment seemed like it was pretty detailed but not 100%
correct. It also made one of the lines kind of long because you didn't
wrap the text in the surrounding style. I decided to make it simpler
and shorter instead of longer still and 100% correct.
- I removed a one-line comment that said /* Override the toast
relfilenode */ because it preceded an error check, not a line of code
that would have done what the comment claimed.
- I removed a one-line comment that said /* Override the relfilenode
*/ because the following code would only sometimes override the
relfilenode. The code didn't seem complex enough to justify a a longer
and more accurate comment, so I just took it out.
- I changed a test for (relkind == RELKIND_RELATION || relkind ==
RELKIND_SEQUENCE || relkind == RELKIND_MATVIEW) to use
RELKIND_HAS_STORAGE(). It's true that not all of the storage types
that RELKIND_HAS_STORAGE() tests are possible here, but that's not a
reason to avoiding using the macro. If somebody adds a new relkind
with storage in the future, they might miss the need to manually
update this place, but they will not likely miss the need to update
RELKIND_HAS_STORAGE() since, if they did, their code probably wouldn't
work at all.
- I changed the way that you were passing create_storage down to
heap_create. I think I said before that you should EITHER fix it so
that we set create_storage = true only when the relation actually has
storage OR ELSE have heap_create() itself override the value to false
when there is no storage. You did both. There are times when it's
reasonable to ensure the same thing in multiple places, but this
doesn't seem to be one of them. So I took that out. I chose to retain
the code in heap_create() that overrides the value to false, added a
comment explaining that it does that, and then adjusted the callers to
ignore the storage type. I then added comments, and in one place an
assertion, to make it clearer what is happening.
- In pg_dump.c, I adjusted the comment that says "Not every relation
has storage." and the test that immediately follows, to ignore the
relfilenode when relkind says it's a partitioned table. Really,
partitioned tables should never have had relfilenodes, but as it turns
out, they used to have them.
Let me know your thoughts.
--
Robert Haas
EDB: http://www.enterprisedb.com
Attachments:
v7-0001-pg_upgrade-Preserve-relfilenodes-and-tablespace-O.patch (application/octet-stream, 30.7K) download
From e24c58382415ceefe247b6961cd6995b3e413214 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Mon, 13 Dec 2021 15:40:13 -0500
Subject: [PATCH v7] pg_upgrade: Preserve relfilenodes and tablespace OIDs.
The fact that relfilenodes and tablespace OIDs change when a cluster
is upgraded can be confusing for people trying to troubleshoot
problems with pg_upgrade. It also isn't much fun if you're trying
to use 'rsync' or similar to update a standby after upgrading the
master. And if in the future we ever encrypt blocks, we might want
to use a salt or nonce that depends on the relfilenode and
tablespace OID. For all of these reasons, arrange for pg_dump to
preserve them as it already does for relation OIDs.
Database OIDs have a similar issue, but there are some tricky points
in that case that do not apply to these cases, so that problem is left
for another patch.
Shruthi KC, based on an earlier patch from Antonin Houska, reviewed
and with some adjustments by me.
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 63 ++++++++---
src/backend/catalog/index.c | 23 +++-
src/backend/commands/tablespace.c | 17 ++-
src/backend/utils/adt/pg_upgrade_support.c | 44 ++++++++
src/bin/pg_dump/pg_dump.c | 104 ++++++++++++------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/info.c | 31 +-----
src/bin/pg_upgrade/pg_upgrade.c | 13 ++-
src/bin/pg_upgrade/pg_upgrade.h | 10 +-
src/bin/pg_upgrade/relfilenode.c | 6 +-
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 +++
.../expected/spgist_name_ops.out | 12 +-
15 files changed, 246 insertions(+), 107 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index 5fcd004e1b..6560b19e14 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 6780ec53b7..11bbb9a768 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -285,8 +287,12 @@ SystemAttributeByName(const char *attname)
* heap_create - Create an uncataloged heap relation
*
* Note API change: the caller must now always provide the OID
- * to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * to use for the relation. The relfilenode may be (and in
+ * the simplest cases is) left unspecified.
+ *
+ * create_storage indicates whether or not to create the storage.
+ * However, even if create_storage is true, no storage will be
+ * created if the relkind is one that doesn't have storage.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,9 +312,9 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool create_storage)
{
- bool create_storage;
Relation rel;
/* The caller must have provided an OID for the relation. */
@@ -343,17 +349,17 @@ heap_create(const char *relname,
if (!RELKIND_HAS_TABLESPACE(relkind))
reltablespace = InvalidOid;
- /*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
- */
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ /* Don't create storage for relkinds without physical storage. */
+ if (!RELKIND_HAS_STORAGE(relkind))
create_storage = false;
else
{
- create_storage = true;
- relfilenode = relid;
+ /*
+ * If relfilenode is unspecified by the caller then create storage
+ * with oid same as relid.
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1121,6 +1127,9 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
@@ -1183,7 +1192,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
/*
@@ -1200,6 +1209,14 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
}
}
else
@@ -1211,6 +1228,17 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ if (RELKIND_HAS_STORAGE(relkind))
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+ }
}
}
@@ -1250,12 +1278,16 @@ heap_create_with_catalog(const char *relname,
* Create the relcache entry (mostly dummy at this point) and the physical
* disk file. (If we fail further down, it's the smgr's responsibility to
* remove the disk file again.)
+ *
+ * NB: Note that passing create_storage = true is correct even for binary
+ * upgrade. The storage we create here will be replaced later, but we need
+ * to have something on disk in the meanwhile.
*/
new_rel_desc = heap_create(relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1264,7 +1296,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 1757cd3446..88f2d60f01 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -87,6 +87,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -733,6 +734,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage = !OidIsValid(relFileNode);
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -904,7 +906,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -914,6 +916,22 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /* Overide the index relfilenode */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
+
+ /*
+ * Note that we want create_storage = true for binary upgrade.
+ * The storage we create here will be replaced later, but we need
+ * to have something on disk in the meanwhile.
+ */
+ Assert(create_storage);
}
else
{
@@ -940,7 +958,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 4b96eec9df..51ffa97576 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -88,6 +88,7 @@
char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -335,8 +336,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index b5b46d7231..135c382371 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -29,6 +29,17 @@ do { \
errmsg("function can only be called when server is in binary upgrade mode"))); \
} while (0)
+Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
@@ -84,6 +95,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
+Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
@@ -95,6 +117,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
+Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
@@ -106,6 +139,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
PG_RETURN_VOID();
}
+Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 10a86f9810..8a8ea596bf 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4836,73 +4836,105 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
/*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
+ * Not every relation has storage. Also, in a pre-v12 database,
+ * partitioned tables have a relfilenode, which should not be preserved
+ * when upgrading.
*/
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ if (OidIsValid(relfilenode) && relkind != RELKIND_PARTITIONED_TABLE)
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 27b95732c8..3f42316a84 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1222,6 +1222,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 5d9a26cf82..775564e580 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -199,14 +199,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->old_db_oid = old_db->db_oid;
map->new_db_oid = new_db->db_oid;
- /*
- * old_relfilenode might differ from pg_class.oid (and hence
- * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
- */
- map->old_relfilenode = old_rel->relfilenode;
-
- /* new_relfilenode will match old and new pg_class.oid */
- map->new_relfilenode = new_rel->relfilenode;
+ /* relfilenode is preserved across old and new cluster */
+ map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
map->nspname = old_rel->nspname;
@@ -278,27 +272,6 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
reloid, db->db_name, reldesc);
}
-
-void
-print_maps(FileNameMap *maps, int n_maps, const char *db_name)
-{
- if (log_opts.verbose)
- {
- int mapnum;
-
- pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
-
- for (mapnum = 0; mapnum < n_maps; mapnum++)
- pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
- maps[mapnum].nspname, maps[mapnum].relname,
- maps[mapnum].old_relfilenode,
- maps[mapnum].new_relfilenode);
-
- pg_log(PG_VERBOSE, "\n\n");
- }
-}
-
-
/*
* get_db_and_rel_infos()
*
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 3628bd74a7..acaf343fb9 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index ca0795f68f..c79ddfc6e4 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -159,13 +159,7 @@ typedef struct
const char *new_tablespace_suffix;
Oid old_db_oid;
Oid new_db_oid;
-
- /*
- * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
- * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
- */
- Oid old_relfilenode;
- Oid new_relfilenode;
+ Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
char *relname;
@@ -390,8 +384,6 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
-void print_maps(FileNameMap *maps, int n,
- const char *db_name);
/* option.c */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 4deae7d985..3bc5b44199 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -119,8 +119,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
new_pgdata);
if (n_maps)
{
- print_maps(mappings, n_maps, new_db->db_name);
-
transfer_single_new_db(mappings, n_maps, old_tablespace);
}
/* We allocate something even for n_maps == 0 */
@@ -206,14 +204,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
map->old_tablespace,
map->old_tablespace_suffix,
map->old_db_oid,
- map->old_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
map->new_db_oid,
- map->new_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index f6e82e7ac5..4ba5748e0a 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 6ce480b49c..7cf5402e5a 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool create_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 79d787cd26..92727d893f 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11058,6 +11058,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4548', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddcecea..1ee65ede24 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
2.24.3 (Apple Git-128)
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-14 18:21 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-12-14 18:21 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Dec 14, 2021 at 2:35 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Mon, Dec 13, 2021 at 9:40 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > > I am reviewing another patch
> > > "v5-0001-Preserve-relfilenode-and-tablespace-OID-in-pg_upg" as well
> > > and will provide the comments soon if any...
>
> I spent much of today reviewing 0001. Here's an updated version, so
> far only lightly tested. Please check whether I've broken anything.
> Here are the changes:
Thanks, Robert for the updated version. I reviewed the changes and it
looks fine.
I also tested the patch. The patch works as expected.
> - I adjusted the function header comment for heap_create. Your
> proposed comment seemed like it was pretty detailed but not 100%
> correct. It also made one of the lines kind of long because you didn't
> wrap the text in the surrounding style. I decided to make it simpler
> and shorter instead of longer still and 100% correct.
The comment update looks fine. However, I still feel it would be good to
mention on which (rare) circumstance a valid relfilenode can get passed.
> - I removed a one-line comment that said /* Override the toast
> relfilenode */ because it preceded an error check, not a line of code
> that would have done what the comment claimed.
>
> - I removed a one-line comment that said /* Override the relfilenode
> */ because the following code would only sometimes override the
> relfilenode. The code didn't seem complex enough to justify a a longer
> and more accurate comment, so I just took it out.
Fine
> - I changed a test for (relkind == RELKIND_RELATION || relkind ==
> RELKIND_SEQUENCE || relkind == RELKIND_MATVIEW) to use
> RELKIND_HAS_STORAGE(). It's true that not all of the storage types
> that RELKIND_HAS_STORAGE() tests are possible here, but that's not a
> reason to avoiding using the macro. If somebody adds a new relkind
> with storage in the future, they might miss the need to manually
> update this place, but they will not likely miss the need to update
> RELKIND_HAS_STORAGE() since, if they did, their code probably wouldn't
> work at all.
I agree.
> - I changed the way that you were passing create_storage down to
> heap_create. I think I said before that you should EITHER fix it so
> that we set create_storage = true only when the relation actually has
> storage OR ELSE have heap_create() itself override the value to false
> when there is no storage. You did both. There are times when it's
> reasonable to ensure the same thing in multiple places, but this
> doesn't seem to be one of them. So I took that out. I chose to retain
> the code in heap_create() that overrides the value to false, added a
> comment explaining that it does that, and then adjusted the callers to
> ignore the storage type. I then added comments, and in one place an
> assertion, to make it clearer what is happening.
The changes are fine. Thanks for the fine-tuning.
> - In pg_dump.c, I adjusted the comment that says "Not every relation
> has storage." and the test that immediately follows, to ignore the
> relfilenode when relkind says it's a partitioned table. Really,
> partitioned tables should never have had relfilenodes, but as it turns
> out, they used to have them.
>
Fine. Understood.
Thanks & Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-14 18:39 tushar <tushar.ahuja@enterprisedb.com>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: tushar @ 2021-12-14 18:39 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; Shruthi Gowda <gowdashru@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On 12/14/21 2:35 AM, Robert Haas wrote:
> I spent much of today reviewing 0001. Here's an updated version, so
> far only lightly tested. Please check whether I've broken anything.
Thanks Robert, I tested from v96/12/13/v14 -> v15( with patch)
things are working fine i.e table /index relfilenode is preserved,
not changing after pg_upgrade.
--
regards,tushar
EnterpriseDB https://www.enterprisedb.com/
The Enterprise PostgreSQL Company
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-15 17:03 tushar <tushar.ahuja@enterprisedb.com>
parent: tushar <tushar.ahuja@enterprisedb.com>
0 siblings, 0 replies; 90+ messages in thread
From: tushar @ 2021-12-15 17:03 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; Shruthi Gowda <gowdashru@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On 12/15/21 12:09 AM, tushar wrote:
>> I spent much of today reviewing 0001. Here's an updated version, so
>> far only lightly tested. Please check whether I've broken anything.
> Thanks Robert, I tested from v96/12/13/v14 -> v15( with patch)
> things are working fine i.e table /index relfilenode is preserved,
> not changing after pg_upgrade.
I covered tablespace OIDs testing scenarios and that is also preserved
after pg_upgrade.
--
regards,tushar
EnterpriseDB https://www.enterprisedb.com/
The Enterprise PostgreSQL Company
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2021-12-17 07:33 Shruthi Gowda <gowdashru@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2021-12-17 07:33 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Dec 13, 2021 at 8:43 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
>
> On Mon, Dec 6, 2021 at 11:25 PM Robert Haas <robertmhaas@gmail.com> wrote:
> >
> > On Sun, Dec 5, 2021 at 11:44 PM Sadhuprasad Patro <b.sadhu@gmail.com> wrote:
> > > 3.
> > > @@ -504,11 +525,15 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
> > > */
> > > pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
> > >
> > > - do
> > > + /* Select an OID for the new database if is not explicitly configured. */
> > > + if (!OidIsValid(dboid))
> > > {
> > > - dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
> > > - Anum_pg_database_oid);
> > > - } while (check_db_file_conflict(dboid));
> > >
> > > I think we need to do 'check_db_file_conflict' for the USER given OID
> > > also.. right? It may already be present.
> >
> > Hopefully, if that happens, we straight up fail later on.
>
> That's right. If a database with user-specified OID exists, the
> createdb fails with a "duplicate key value" error.
> If just a data directory with user-specified OID exists,
> MakePGDirectory() fails to create the directory and the cleanup
> callback createdb_failure_callback() removes the directory that was
> not created by 'createdb()' function.
> The subsequent create database call with the same OID will succeed.
> Should we handle the case where a data directory exists and the
> corresponding DB with that oid does not exist? I presume this
> situation doesn't arise unless the user tries to create directories in
> the data path. Any thoughts?
I have updated the DBOID preserve patch to handle this case and
generated the latest patch on top of your v7-001-preserve-relfilenode
patch.
Thanks & Regards
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments:
v7-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 13.1K) download
From 84cbe8cc7202fbb643d5ee9204cddab6d7ef856c Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Fri, 17 Dec 2021 12:55:27 +0530
Subject: [PATCH v7 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs across binary upgrade
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 +++++++++-
src/backend/commands/dbcommands.c | 54 +++++++++++++++++++++++++++++++----
src/backend/parser/gram.y | 3 +-
src/bin/initdb/initdb.c | 24 ++++++++++------
src/bin/pg_dump/pg_dump.c | 16 +++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 ++
src/include/catalog/unused_oids | 5 ++++
12 files changed, 111 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..94d4cfb 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The OID range for user objects starts from 16384. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 029fab4..2852cfd 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
+ {
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database oid %u is already used by database %s",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory exists for database oid %u", dboid));
+ }
+ else
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 3d4dd43..9572f9a 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -711,7 +711,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10416,6 +10416,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 03b80f9..34abb30 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,15 +1839,14 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Template0 oid is fixed during initdb to avoid oid conflict across versions
+ * during binary upgrade.
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1879,6 +1879,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index c3609af..97193cf 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2840,8 +2840,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index ded8cbe..e74ee12 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index b59252b..26ef6fc 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index ea3f3d4..cfb2347 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index b524dc8..9c8b0b0 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2587,7 +2587,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index d22de19..d06e3a9 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Template0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index 5b7ce5f..2750913 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-15 05:47 Julien Rouhaud <rjuju123@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Julien Rouhaud @ 2022-01-15 05:47 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
Hi,
On Fri, Dec 17, 2021 at 01:03:06PM +0530, Shruthi Gowda wrote:
>
> I have updated the DBOID preserve patch to handle this case and
> generated the latest patch on top of your v7-001-preserve-relfilenode
> patch.
The cfbot reports that the patch doesn't apply anymore:
http://cfbot.cputube.org/patch_36_3296.log
=== Applying patches on top of PostgreSQL commit ID 5513dc6a304d8bda114004a3b906cc6fde5d6274 ===
=== applying patch ./v7-0002-Preserve-database-OIDs-in-pg_upgrade.patch
[...]
patching file src/bin/pg_upgrade/info.c
Hunk #1 FAILED at 190.
Hunk #2 succeeded at 351 (offset 27 lines).
1 out of 2 hunks FAILED -- saving rejects to file src/bin/pg_upgrade/info.c.rej
patching file src/bin/pg_upgrade/pg_upgrade.h
Hunk #1 FAILED at 145.
1 out of 1 hunk FAILED -- saving rejects to file src/bin/pg_upgrade/pg_upgrade.h.rej
patching file src/bin/pg_upgrade/relfilenode.c
Hunk #1 FAILED at 193.
1 out of 1 hunk FAILED -- saving rejects to file src/bin/pg_upgrade/relfilenode.c.rej
Could you send a rebased version? In the meantime I willl switch the cf entry
to Waiting on Author.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-17 14:57 Shruthi Gowda <gowdashru@gmail.com>
parent: Julien Rouhaud <rjuju123@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-17 14:57 UTC (permalink / raw)
To: Julien Rouhaud <rjuju123@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Sat, Jan 15, 2022 at 11:17 AM Julien Rouhaud <rjuju123@gmail.com> wrote:
>
> Hi,
>
> On Fri, Dec 17, 2021 at 01:03:06PM +0530, Shruthi Gowda wrote:
> >
> > I have updated the DBOID preserve patch to handle this case and
> > generated the latest patch on top of your v7-001-preserve-relfilenode
> > patch.
>
> The cfbot reports that the patch doesn't apply anymore:
> http://cfbot.cputube.org/patch_36_3296.log
> === Applying patches on top of PostgreSQL commit ID 5513dc6a304d8bda114004a3b906cc6fde5d6274 ===
> === applying patch ./v7-0002-Preserve-database-OIDs-in-pg_upgrade.patch
> [...]
> patching file src/bin/pg_upgrade/info.c
> Hunk #1 FAILED at 190.
> Hunk #2 succeeded at 351 (offset 27 lines).
> 1 out of 2 hunks FAILED -- saving rejects to file src/bin/pg_upgrade/info.c.rej
> patching file src/bin/pg_upgrade/pg_upgrade.h
> Hunk #1 FAILED at 145.
> 1 out of 1 hunk FAILED -- saving rejects to file src/bin/pg_upgrade/pg_upgrade.h.rej
> patching file src/bin/pg_upgrade/relfilenode.c
> Hunk #1 FAILED at 193.
> 1 out of 1 hunk FAILED -- saving rejects to file src/bin/pg_upgrade/relfilenode.c.rej
>
> Could you send a rebased version? In the meantime I willl switch the cf entry
> to Waiting on Author.
I have rebased and generated the patches on top of PostgreSQL commit
ID cf925936ecc031355cd56fbd392ec3180517a110.
Kindly apply v8-0001-pg_upgrade-Preserve-relfilenodes-and-tablespace-O.patch
first and then v8-0002-Preserve-database-OIDs-in-pg_upgrade.patch.
Thanks & Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments: download all (2 files, .tar.gz)
v8-0002-Preserve-database-OIDs-in-pg_upgrade.patch (application/octet-stream, 13.1K) download
From 07a5f08ab00cf88c2e21ec04b688f31625f720a4 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Mon, 17 Jan 2022 19:43:30 +0530
Subject: [PATCH v8 2/2] Preserve database OIDs in pg_upgrade
The patch aims to preserve the database OIDs across binary upgrade
Author: Shruthi KC, based on an earlier patch from Antonin Houska
Discussion: https://www.postgresql.org/message-id/7082.1562337694@localhost
---
doc/src/sgml/ref/create_database.sgml | 15 +++++++++-
src/backend/commands/dbcommands.c | 54 +++++++++++++++++++++++++++++++----
src/backend/parser/gram.y | 3 +-
src/bin/initdb/initdb.c | 24 ++++++++++------
src/bin/pg_dump/pg_dump.c | 16 +++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 ++
src/include/catalog/unused_oids | 5 ++++
12 files changed, 111 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..94d4cfb 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,18 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier with which the database gets created.
+ The OID range for user objects starts from 16384. CREATE DATABASE fails if a
+ database with specified oid already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 509d1a3..878f366 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,27 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * Throw an error if the user specified oid < FirstNormalObjectId for
+ * creating the database. However, we need to allow creating database
+ * with oid < FirstNormalObjectId for below cases:
+ * 1. creating template0 with fixed oid during initdb
+ * 2. creating databases with oids from the old cluster during binary
+ * upgrade.
+ */
+ if ((dboid < FirstNormalObjectId) &&
+ (strcmp(dbname, "template0") != 0) &&
+ (!IsBinaryUpgrade))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +525,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
+ {
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database oid %u is already used by database %s",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory exists for database oid %u", dboid));
+ }
+ else
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index bb015a8..416eeee 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -711,7 +711,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF
NULLS_P NUMERIC
- OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
+ OBJECT_P OF OFF OFFSET OID OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
ORDER ORDINALITY OTHERS OUT_P OUTER_P
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
@@ -10414,6 +10414,7 @@ createdb_opt_name:
| OWNER { $$ = pstrdup($1); }
| TABLESPACE { $$ = pstrdup($1); }
| TEMPLATE { $$ = pstrdup($1); }
+ | OID { $$ = pstrdup($1); }
;
/*
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f964a00..f798457 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,15 +1839,14 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * Template0 oid is fixed during initdb to avoid oid conflict across versions
+ * during binary upgrade.
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1879,6 +1879,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index f251b75..332f966 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2835,8 +2835,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa082..69ef231 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d..1db8e3f 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253c..2f4deb3 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 71f1a5c..db3962c 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2598,7 +2598,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca..fe76c36 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Template0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index e55bc6f..314208b 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
v8-0001-pg_upgrade-Preserve-relfilenodes-and-tablespace-O.patch (application/octet-stream, 30.7K) download
From 4ab741fb3d234f5ba45fe3c3396bae2424e91df2 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Mon, 17 Jan 2022 19:38:12 +0530
Subject: [PATCH v8 1/2] pg_upgrade: Preserve relfilenodes and tablespace OIDs.
The fact that relfilenodes and tablespace OIDs change when a cluster
is upgraded can be confusing for people trying to troubleshoot
problems with pg_upgrade. It also isn't much fun if you're trying
to use 'rsync' or similar to update a standby after upgrading the
master. And if in the future we ever encrypt blocks, we might want
to use a salt or nonce that depends on the relfilenode and
tablespace OID. For all of these reasons, arrange for pg_dump to
preserve them as it already does for relation OIDs.
Database OIDs have a similar issue, but there are some tricky points
in that case that do not apply to these cases, so that problem is left
for another patch.
Shruthi KC, based on an earlier patch from Antonin Houska, reviewed
and with some adjustments by me.
---
src/backend/bootstrap/bootparse.y | 3 +-
src/backend/catalog/heap.c | 63 ++++++++++---
src/backend/catalog/index.c | 23 ++++-
src/backend/commands/tablespace.c | 17 +++-
src/backend/utils/adt/pg_upgrade_support.c | 44 +++++++++
src/bin/pg_dump/pg_dump.c | 104 ++++++++++++++-------
src/bin/pg_dump/pg_dumpall.c | 3 +
src/bin/pg_upgrade/info.c | 31 +-----
src/bin/pg_upgrade/pg_upgrade.c | 13 +--
src/bin/pg_upgrade/pg_upgrade.h | 10 +-
src/bin/pg_upgrade/relfilenode.c | 6 +-
src/include/catalog/binary_upgrade.h | 5 +
src/include/catalog/heap.h | 3 +-
src/include/catalog/pg_proc.dat | 16 ++++
.../spgist_name_ops/expected/spgist_name_ops.out | 12 ++-
15 files changed, 246 insertions(+), 107 deletions(-)
diff --git a/src/backend/bootstrap/bootparse.y b/src/backend/bootstrap/bootparse.y
index f1dd95c..142433f 100644
--- a/src/backend/bootstrap/bootparse.y
+++ b/src/backend/bootstrap/bootparse.y
@@ -212,7 +212,8 @@ Boot_CreateStmt:
mapped_relation,
true,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
elog(DEBUG4, "bootstrap relation created");
}
else
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 5621549..7e99de8 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -91,7 +91,9 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
Oid binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
static void AddNewRelationTuple(Relation pg_class_desc,
Relation new_rel_desc,
@@ -285,8 +287,12 @@ SystemAttributeByName(const char *attname)
* heap_create - Create an uncataloged heap relation
*
* Note API change: the caller must now always provide the OID
- * to use for the relation. The relfilenode may (and, normally,
- * should) be left unspecified.
+ * to use for the relation. The relfilenode may be (and in
+ * the simplest cases is) left unspecified.
+ *
+ * create_storage indicates whether or not to create the storage.
+ * However, even if create_storage is true, no storage will be
+ * created if the relkind is one that doesn't have storage.
*
* rel->rd_rel is initialized by RelationBuildLocalRelation,
* and is mostly zeroes at return.
@@ -306,9 +312,9 @@ heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid)
+ MultiXactId *relminmxid,
+ bool create_storage)
{
- bool create_storage;
Relation rel;
/* The caller must have provided an OID for the relation. */
@@ -343,17 +349,17 @@ heap_create(const char *relname,
if (!RELKIND_HAS_TABLESPACE(relkind))
reltablespace = InvalidOid;
- /*
- * Decide whether to create storage. If caller passed a valid relfilenode,
- * storage is already created, so don't do it here. Also don't create it
- * for relkinds without physical storage.
- */
- if (!RELKIND_HAS_STORAGE(relkind) || OidIsValid(relfilenode))
+ /* Don't create storage for relkinds without physical storage. */
+ if (!RELKIND_HAS_STORAGE(relkind))
create_storage = false;
else
{
- create_storage = true;
- relfilenode = relid;
+ /*
+ * If relfilenode is unspecified by the caller then create storage
+ * with oid same as relid.
+ */
+ if (!OidIsValid(relfilenode))
+ relfilenode = relid;
}
/*
@@ -1121,6 +1127,9 @@ heap_create_with_catalog(const char *relname,
Oid existing_relid;
Oid old_type_oid;
Oid new_type_oid;
+
+ /* By default set to InvalidOid unless overridden by binary-upgrade */
+ Oid relfilenode = InvalidOid;
TransactionId relfrozenxid;
MultiXactId relminmxid;
@@ -1183,7 +1192,7 @@ heap_create_with_catalog(const char *relname,
*/
if (!OidIsValid(relid))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
/*
@@ -1200,6 +1209,14 @@ heap_create_with_catalog(const char *relname,
{
relid = binary_upgrade_next_toast_pg_class_oid;
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
+
+ if (!OidIsValid(binary_upgrade_next_toast_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("toast relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_toast_pg_class_relfilenode;
+ binary_upgrade_next_toast_pg_class_relfilenode = InvalidOid;
}
}
else
@@ -1211,6 +1228,17 @@ heap_create_with_catalog(const char *relname,
relid = binary_upgrade_next_heap_pg_class_oid;
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
+
+ if (RELKIND_HAS_STORAGE(relkind))
+ {
+ if (!OidIsValid(binary_upgrade_next_heap_pg_class_relfilenode))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("relfilenode value not set when in binary upgrade mode")));
+
+ relfilenode = binary_upgrade_next_heap_pg_class_relfilenode;
+ binary_upgrade_next_heap_pg_class_relfilenode = InvalidOid;
+ }
}
}
@@ -1250,12 +1278,16 @@ heap_create_with_catalog(const char *relname,
* Create the relcache entry (mostly dummy at this point) and the physical
* disk file. (If we fail further down, it's the smgr's responsibility to
* remove the disk file again.)
+ *
+ * NB: Note that passing create_storage = true is correct even for binary
+ * upgrade. The storage we create here will be replaced later, but we need
+ * to have something on disk in the meanwhile.
*/
new_rel_desc = heap_create(relname,
relnamespace,
reltablespace,
relid,
- InvalidOid,
+ relfilenode,
accessmtd,
tupdesc,
relkind,
@@ -1264,7 +1296,8 @@ heap_create_with_catalog(const char *relname,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ true);
Assert(relid == RelationGetRelid(new_rel_desc));
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 4506cbc..e420381 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -87,6 +87,7 @@
/* Potentially set by pg_upgrade_support functions */
Oid binary_upgrade_next_index_pg_class_oid = InvalidOid;
+Oid binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
/*
* Pointer-free representation of variables used when reindexing system
@@ -733,6 +734,7 @@ index_create(Relation heapRelation,
char relkind;
TransactionId relfrozenxid;
MultiXactId relminmxid;
+ bool create_storage = !OidIsValid(relFileNode);
/* constraint flags can only be set when a constraint is requested */
Assert((constr_flags == 0) ||
@@ -904,7 +906,7 @@ index_create(Relation heapRelation,
*/
if (!OidIsValid(indexRelationId))
{
- /* Use binary-upgrade override for pg_class.oid/relfilenode? */
+ /* Use binary-upgrade override for pg_class.oid and relfilenode */
if (IsBinaryUpgrade)
{
if (!OidIsValid(binary_upgrade_next_index_pg_class_oid))
@@ -914,6 +916,22 @@ index_create(Relation heapRelation,
indexRelationId = binary_upgrade_next_index_pg_class_oid;
binary_upgrade_next_index_pg_class_oid = InvalidOid;
+
+ /* Overide the index relfilenode */
+ if ((relkind == RELKIND_INDEX) &&
+ (!OidIsValid(binary_upgrade_next_index_pg_class_relfilenode)))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("index relfilenode value not set when in binary upgrade mode")));
+ relFileNode = binary_upgrade_next_index_pg_class_relfilenode;
+ binary_upgrade_next_index_pg_class_relfilenode = InvalidOid;
+
+ /*
+ * Note that we want create_storage = true for binary upgrade.
+ * The storage we create here will be replaced later, but we need
+ * to have something on disk in the meanwhile.
+ */
+ Assert(create_storage);
}
else
{
@@ -940,7 +958,8 @@ index_create(Relation heapRelation,
mapped_relation,
allow_system_table_mods,
&relfrozenxid,
- &relminmxid);
+ &relminmxid,
+ create_storage);
Assert(relfrozenxid == InvalidTransactionId);
Assert(relminmxid == InvalidMultiXactId);
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index 7123b40..b2ccf5e 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -89,6 +89,7 @@ char *default_tablespace = NULL;
char *temp_tablespaces = NULL;
bool allow_in_place_tablespaces = false;
+Oid binary_upgrade_next_pg_tablespace_oid = InvalidOid;
static void create_tablespace_directories(const char *location,
const Oid tablespaceoid);
@@ -340,8 +341,20 @@ CreateTableSpace(CreateTableSpaceStmt *stmt)
MemSet(nulls, false, sizeof(nulls));
- tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
- Anum_pg_tablespace_oid);
+ if (IsBinaryUpgrade)
+ {
+ /* Use binary-upgrade override for tablespace oid */
+ if (!OidIsValid(binary_upgrade_next_pg_tablespace_oid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("pg_tablespace OID value not set when in binary upgrade mode")));
+
+ tablespaceoid = binary_upgrade_next_pg_tablespace_oid;
+ binary_upgrade_next_pg_tablespace_oid = InvalidOid;
+ }
+ else
+ tablespaceoid = GetNewOidWithIndex(rel, TablespaceOidIndexId,
+ Anum_pg_tablespace_oid);
values[Anum_pg_tablespace_oid - 1] = ObjectIdGetDatum(tablespaceoid);
values[Anum_pg_tablespace_spcname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(stmt->tablespacename));
diff --git a/src/backend/utils/adt/pg_upgrade_support.c b/src/backend/utils/adt/pg_upgrade_support.c
index c651f0e..67b9675e 100644
--- a/src/backend/utils/adt/pg_upgrade_support.c
+++ b/src/backend/utils/adt/pg_upgrade_support.c
@@ -30,6 +30,17 @@ do { \
} while (0)
Datum
+binary_upgrade_set_next_pg_tablespace_oid(PG_FUNCTION_ARGS)
+{
+ Oid tbspoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_pg_tablespace_oid = tbspoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_type_oid(PG_FUNCTION_ARGS)
{
Oid typoid = PG_GETARG_OID(0);
@@ -85,6 +96,17 @@ binary_upgrade_set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_heap_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_heap_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -96,6 +118,17 @@ binary_upgrade_set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_index_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_index_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
@@ -107,6 +140,17 @@ binary_upgrade_set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
}
Datum
+binary_upgrade_set_next_toast_relfilenode(PG_FUNCTION_ARGS)
+{
+ Oid nodeoid = PG_GETARG_OID(0);
+
+ CHECK_IS_BINARY_UPGRADE;
+ binary_upgrade_next_toast_pg_class_relfilenode = nodeoid;
+
+ PG_RETURN_VOID();
+}
+
+Datum
binary_upgrade_set_next_pg_enum_oid(PG_FUNCTION_ARGS)
{
Oid enumoid = PG_GETARG_OID(0);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 92ab957..f251b75 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -4624,73 +4624,105 @@ binary_upgrade_set_pg_class_oids(Archive *fout,
PQExpBuffer upgrade_buffer, Oid pg_class_oid,
bool is_index)
{
+ PQExpBuffer upgrade_query = createPQExpBuffer();
+ PGresult *upgrade_res;
+ Oid relfilenode;
+ Oid toast_oid;
+ Oid toast_relfilenode;
+ char relkind;
+ Oid toast_index_oid;
+ Oid toast_index_relfilenode;
+
+ /*
+ * Preserve the OID and relfilenode of the table, table's index, table's
+ * toast table and toast table's index if any.
+ *
+ * One complexity is that the current table definition might not require
+ * the creation of a TOAST table, but the old database might have a TOAST
+ * table that was created earlier, before some wide columns were dropped.
+ * By setting the TOAST oid we force creation of the TOAST heap and index
+ * by the new backend, so we can copy the files during binary upgrade
+ * without worrying about this case.
+ */
+ appendPQExpBuffer(upgrade_query,
+ "SELECT c.relkind, c.relfilenode, c.reltoastrelid, ct.relfilenode AS toast_relfilenode, i.indexrelid, cti.relfilenode AS toast_index_relfilenode "
+ "FROM pg_catalog.pg_class c LEFT JOIN "
+ "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
+ "LEFT JOIN pg_catalog.pg_class ct ON (c.reltoastrelid = ct.oid) "
+ "LEFT JOIN pg_catalog.pg_class AS cti ON (i.indexrelid = cti.oid) "
+ "WHERE c.oid = '%u'::pg_catalog.oid;",
+ pg_class_oid);
+
+ upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
+
+ relkind = *PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "relkind"));
+
+ relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "relfilenode")));
+ toast_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "reltoastrelid")));
+ toast_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_relfilenode")));
+ toast_index_oid = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "indexrelid")));
+ toast_index_relfilenode = atooid(PQgetvalue(upgrade_res, 0,
+ PQfnumber(upgrade_res, "toast_index_relfilenode")));
+
appendPQExpBufferStr(upgrade_buffer,
- "\n-- For binary upgrade, must preserve pg_class oids\n");
+ "\n-- For binary upgrade, must preserve pg_class oids and relfilenodes\n");
if (!is_index)
{
- PQExpBuffer upgrade_query = createPQExpBuffer();
- PGresult *upgrade_res;
- Oid pg_class_reltoastrelid;
- char pg_class_relkind;
- Oid pg_index_indexrelid;
-
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
/*
- * Preserve the OIDs of the table's toast table and index, if any.
- * Indexes cannot have toast tables, so we need not make this probe in
- * the index code path.
- *
- * One complexity is that the current table definition might not
- * require the creation of a TOAST table, but the old database might
- * have a TOAST table that was created earlier, before some wide
- * columns were dropped. By setting the TOAST oid we force creation
- * of the TOAST heap and index by the new backend, so we can copy the
- * files during binary upgrade without worrying about this case.
+ * Not every relation has storage. Also, in a pre-v12 database,
+ * partitioned tables have a relfilenode, which should not be preserved
+ * when upgrading.
*/
- appendPQExpBuffer(upgrade_query,
- "SELECT c.reltoastrelid, c.relkind, i.indexrelid "
- "FROM pg_catalog.pg_class c LEFT JOIN "
- "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
- "WHERE c.oid = '%u'::pg_catalog.oid;",
- pg_class_oid);
-
- upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
-
- pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "reltoastrelid")));
- pg_class_relkind = *PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "relkind"));
- pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0,
- PQfnumber(upgrade_res, "indexrelid")));
+ if (OidIsValid(relfilenode) && relkind != RELKIND_PARTITIONED_TABLE)
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_heap_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
/*
* In a pre-v12 database, partitioned tables might be marked as having
* toast tables, but we should ignore them if so.
*/
- if (OidIsValid(pg_class_reltoastrelid) &&
- pg_class_relkind != RELKIND_PARTITIONED_TABLE)
+ if (OidIsValid(toast_oid) &&
+ relkind != RELKIND_PARTITIONED_TABLE)
{
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_class_reltoastrelid);
+ toast_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_toast_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_relfilenode);
/* every toast table has an index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
- pg_index_indexrelid);
+ toast_index_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ toast_index_relfilenode);
}
PQclear(upgrade_res);
destroyPQExpBuffer(upgrade_query);
}
else
+ {
+ /* Preserve the OID and relfilenode of the index */
appendPQExpBuffer(upgrade_buffer,
"SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
pg_class_oid);
+ appendPQExpBuffer(upgrade_buffer,
+ "SELECT pg_catalog.binary_upgrade_set_next_index_relfilenode('%u'::pg_catalog.oid);\n",
+ relfilenode);
+ }
appendPQExpBufferChar(upgrade_buffer, '\n');
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 868f15e..5d5a243 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1061,6 +1061,9 @@ dumpTablespaces(PGconn *conn)
/* needed for buildACLCommands() */
fspcname = pg_strdup(fmtId(spcname));
+ appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
+ appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
+
appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index 60e9cc5..f7fa082 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -193,14 +193,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->old_db_oid = old_db->db_oid;
map->new_db_oid = new_db->db_oid;
- /*
- * old_relfilenode might differ from pg_class.oid (and hence
- * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
- */
- map->old_relfilenode = old_rel->relfilenode;
-
- /* new_relfilenode will match old and new pg_class.oid */
- map->new_relfilenode = new_rel->relfilenode;
+ /* relfilenode is preserved across old and new cluster */
+ map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
map->nspname = old_rel->nspname;
@@ -272,27 +266,6 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
reloid, db->db_name, reldesc);
}
-
-void
-print_maps(FileNameMap *maps, int n_maps, const char *db_name)
-{
- if (log_opts.verbose)
- {
- int mapnum;
-
- pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
-
- for (mapnum = 0; mapnum < n_maps; mapnum++)
- pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
- maps[mapnum].nspname, maps[mapnum].relname,
- maps[mapnum].old_relfilenode,
- maps[mapnum].new_relfilenode);
-
- pg_log(PG_VERBOSE, "\n\n");
- }
-}
-
-
/*
* get_db_and_rel_infos()
*
diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c
index 68b336e..07defac 100644
--- a/src/bin/pg_upgrade/pg_upgrade.c
+++ b/src/bin/pg_upgrade/pg_upgrade.c
@@ -15,12 +15,13 @@
* oids are the same between old and new clusters. This is important
* because toast oids are stored as toast pointers in user tables.
*
- * While pg_class.oid and pg_class.relfilenode are initially the same
- * in a cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM
- * FULL. In the new cluster, pg_class.oid and pg_class.relfilenode will
- * be the same and will match the old pg_class.oid value. Because of
- * this, old/new pg_class.relfilenode values will not match if CLUSTER,
- * REINDEX, or VACUUM FULL have been performed in the old cluster.
+ * While pg_class.oid and pg_class.relfilenode are initially the same in a
+ * cluster, they can diverge due to CLUSTER, REINDEX, or VACUUM FULL. We
+ * control assignments of pg_class.relfilenode because we want the filenames
+ * to match between the old and new cluster.
+ *
+ * We control assignment of pg_tablespace.oid because we want the oid to match
+ * between the old and new cluster.
*
* We control all assignments of pg_type.oid because these oids are stored
* in user composite type values.
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index dd4a543..da6770d 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -147,13 +147,7 @@ typedef struct
const char *new_tablespace_suffix;
Oid old_db_oid;
Oid new_db_oid;
-
- /*
- * old/new relfilenodes might differ for pg_largeobject(_metadata) indexes
- * due to VACUUM FULL or REINDEX. Other relfilenodes are preserved.
- */
- Oid old_relfilenode;
- Oid new_relfilenode;
+ Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
char *relname;
@@ -379,8 +373,6 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
DbInfo *new_db, int *nmaps, const char *old_pgdata,
const char *new_pgdata);
void get_db_and_rel_infos(ClusterInfo *cluster);
-void print_maps(FileNameMap *maps, int n,
- const char *db_name);
/* option.c */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 302ee2b..6e0253c 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -119,8 +119,6 @@ transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
new_pgdata);
if (n_maps)
{
- print_maps(mappings, n_maps, new_db->db_name);
-
transfer_single_new_db(mappings, n_maps, old_tablespace);
}
/* We allocate something even for n_maps == 0 */
@@ -196,14 +194,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
map->old_tablespace,
map->old_tablespace_suffix,
map->old_db_oid,
- map->old_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
map->new_db_oid,
- map->new_relfilenode,
+ map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
index cc16a26..0b6944b 100644
--- a/src/include/catalog/binary_upgrade.h
+++ b/src/include/catalog/binary_upgrade.h
@@ -14,14 +14,19 @@
#ifndef BINARY_UPGRADE_H
#define BINARY_UPGRADE_H
+extern PGDLLIMPORT Oid binary_upgrade_next_pg_tablespace_oid;
+
extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_mrng_array_pg_type_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
+extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_relfilenode;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h
index 3b96006..c4757bd 100644
--- a/src/include/catalog/heap.h
+++ b/src/include/catalog/heap.h
@@ -59,7 +59,8 @@ extern Relation heap_create(const char *relname,
bool mapped_relation,
bool allow_system_table_mods,
TransactionId *relfrozenxid,
- MultiXactId *relminmxid);
+ MultiXactId *relminmxid,
+ bool create_storage);
extern Oid heap_create_with_catalog(const char *relname,
Oid relnamespace,
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index d6bf1f3..b6f689e 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -11040,6 +11040,22 @@
proname => 'binary_upgrade_set_missing_value', provolatile => 'v',
proparallel => 'u', prorettype => 'void', proargtypes => 'oid text text',
prosrc => 'binary_upgrade_set_missing_value' },
+{ oid => '4545', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_heap_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_heap_relfilenode' },
+{ oid => '4546', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_index_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_index_relfilenode' },
+{ oid => '4547', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_toast_relfilenode', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_toast_relfilenode' },
+{ oid => '4548', descr => 'for use by pg_upgrade',
+ proname => 'binary_upgrade_set_next_pg_tablespace_oid', provolatile => 'v',
+ proparallel => 'u', prorettype => 'void', proargtypes => 'oid',
+ prosrc => 'binary_upgrade_set_next_pg_tablespace_oid' },
# conversion functions
{ oid => '4302',
diff --git a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
index ac0ddce..1ee65ed 100644
--- a/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
+++ b/src/test/modules/spgist_name_ops/expected/spgist_name_ops.out
@@ -52,14 +52,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
-- Verify clean failure when INCLUDE'd columns result in overlength tuple
-- The error message details are platform-dependent, so show only SQLSTATE
@@ -97,14 +101,18 @@ select * from t
------------------------------------------------------+----+------------------------------------------------------
binary_upgrade_set_next_array_pg_type_oid | | binary_upgrade_set_next_array_pg_type_oid
binary_upgrade_set_next_heap_pg_class_oid | | binary_upgrade_set_next_heap_pg_class_oid
+ binary_upgrade_set_next_heap_relfilenode | 1 | binary_upgrade_set_next_heap_relfilenode
binary_upgrade_set_next_index_pg_class_oid | 1 | binary_upgrade_set_next_index_pg_class_oid
+ binary_upgrade_set_next_index_relfilenode | | binary_upgrade_set_next_index_relfilenode
binary_upgrade_set_next_multirange_array_pg_type_oid | 1 | binary_upgrade_set_next_multirange_array_pg_type_oid
binary_upgrade_set_next_multirange_pg_type_oid | 1 | binary_upgrade_set_next_multirange_pg_type_oid
binary_upgrade_set_next_pg_authid_oid | | binary_upgrade_set_next_pg_authid_oid
binary_upgrade_set_next_pg_enum_oid | | binary_upgrade_set_next_pg_enum_oid
+ binary_upgrade_set_next_pg_tablespace_oid | | binary_upgrade_set_next_pg_tablespace_oid
binary_upgrade_set_next_pg_type_oid | | binary_upgrade_set_next_pg_type_oid
binary_upgrade_set_next_toast_pg_class_oid | 1 | binary_upgrade_set_next_toast_pg_class_oid
-(9 rows)
+ binary_upgrade_set_next_toast_relfilenode | | binary_upgrade_set_next_toast_relfilenode
+(13 rows)
\set VERBOSITY sqlstate
insert into t values(repeat('xyzzy', 12), 42, repeat('xyzzy', 4000));
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-17 19:05 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-01-17 19:05 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Dec 14, 2021 at 1:21 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> Thanks, Robert for the updated version. I reviewed the changes and it
> looks fine.
> I also tested the patch. The patch works as expected.
Thanks.
> > - I adjusted the function header comment for heap_create. Your
> > proposed comment seemed like it was pretty detailed but not 100%
> > correct. It also made one of the lines kind of long because you didn't
> > wrap the text in the surrounding style. I decided to make it simpler
> > and shorter instead of longer still and 100% correct.
>
> The comment update looks fine. However, I still feel it would be good to
> mention on which (rare) circumstance a valid relfilenode can get passed.
In general, I think it's the job of a function parameter comment to
describe what the parameter does, not how the callers actually use it.
One problem with describing the latter is that, if someone later adds
another caller, there is a pretty good chance that they won't notice
that the comment needs to be changed. More fundamentally, the
parameter function comments should be like an instruction manual for
how to use the function. If you are trying to figure out how to use
this function, it is not helpful to know that "most callers like to
pass false" for this parameter. What you need to know is what value
your new call site should pass, and knowing what "most callers" do or
that something is "rare" doesn't really help. If we want to make this
comment more detailed, we should approach it from the point of view of
explaining how it ought to be set.
I've committed the v8-0001 patch you attached. I'll write separately
about v8-0002.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-17 21:03 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-01-17 21:03 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Jan 17, 2022 at 9:57 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> I have rebased and generated the patches on top of PostgreSQL commit
> ID cf925936ecc031355cd56fbd392ec3180517a110.
> Kindly apply v8-0001-pg_upgrade-Preserve-relfilenodes-and-tablespace-O.patch
> first and then v8-0002-Preserve-database-OIDs-in-pg_upgrade.patch.
OK, so looking over 0002, I noticed a few things:
1. datlastsysoid isn't being used for anything any more. That's not a
defect in your patch, but I've separately proposed to remove it.
2. I realized that the whole idea here depends on not having initdb
create more than one database without a fixed OID. The patch solves
that by nailing down the OID of template0, which is a sufficient
solution. However, I think nailing down the (initial) OID of postgres
as well would be a good idea, just in case somebody in the future
decides to add another system-created database.
3. The changes to gram.y don't do anything. Sure, you've added a new
"OID" token, but nothing generates that token, so it has no effect.
The reason the syntax works is that createdb_opt_name accepts "IDENT",
which means any string that's not in the keyword list (see kwlist.h).
But that's there already, so you don't need to do anything in this
file.
4. I felt that the documentation and comments could be somewhat improved.
Here's an updated version in which I've reverted the changes to gram.y
and tried to improve the comments and documentation. Could you have a
look at implementing (2) above?
--
Robert Haas
EDB: http://www.enterprisedb.com
Attachments:
v9-0001-pg_upgrade-perserve-database-OID-patch-from-shrut.patch (application/octet-stream, 13.3K) download
From bf0cd45f726c20b61e306ee7262d34541182106a Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Mon, 17 Jan 2022 16:02:08 -0500
Subject: [PATCH v9] pg_upgrade perserve database OID patch from shruthi
w/changes by me.
---
doc/src/sgml/ref/create_database.sgml | 18 +++++++-
src/backend/commands/dbcommands.c | 59 ++++++++++++++++++++++++---
src/bin/initdb/initdb.c | 33 +++++++++++----
src/bin/pg_dump/pg_dump.c | 16 +++++++-
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++--
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +-
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 3 ++
src/include/catalog/unused_oids | 5 +++
11 files changed, 126 insertions(+), 28 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb4068ec..f22e28dc81 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,21 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier to be used for the new database. If this
+ parameter is not specified, the database will choose a suitable
+ OID automatically. This parameter is primarily intended for internal
+ use by <application>pg_upgrade</application>, and only
+ <application>pg_upgrade</application> can specify a value less
+ than 16384.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 509d1a3e92..668d84a9e2 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,32 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * We don't normally permit new databases to be created with
+ * system-assigned OIDs. pg_upgrade tries to preserve database
+ * OIDs, and we don't want any OIDs that are used in this database
+ * cluster to be OIDs that might be used for some other cluster
+ * in a future version of PostgreSQL. We assume all such OIDs
+ * will be from the system-managed OID range.
+ *
+ * As an exception, however, we permit any OID to be assigned
+ * when allow_system_table_mods=on (so that initdb can assign a
+ * system OID to template0) or when performing a binary upgrade
+ * (so that pg_upgrade can preserve whatever OIDs it finds in the
+ * source cluster).
+ */
+ if (dboid < FirstNormalObjectId &&
+ !allowSystemTableMods && !IsBinaryUpgrade)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +530,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database oid %u is already used by database %s",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory exists for database oid %u", dboid));
+ }
+ else
+ {
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f964a000f8..23146a5fb5 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,15 +1839,23 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
+ * enough to drop and recreate a conflicting database with the same name,
+ * but if the same OID were used for one system-created database in the
+ * old cluster and a different system-created database in the new cluster,
+ * it would fail. To avoid that, assign a fixed OID to template0 rather
+ * than letting the server choose one.
+ *
+ * (Note that, while the user could have dropped and recreated these
+ * objects in the old cluster, the problem scenario only exists if the OID
+ * that is in use in the old cluster is also used in th new cluster - and
+ * the new cluster should be the result of a fresh initdb.)
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1879,6 +1888,14 @@ make_postgres(FILE *cmdfd)
static const char *const postgres_setup[] = {
"CREATE DATABASE postgres;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c2f1d3044..cd14d1f840 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2838,8 +2838,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70a7c..384834a4cc 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa0820d6..69ef23119f 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d0f8..1db8e3f0fb 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253cc3e..2f4deb3416 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 6bd33a06cb..502b5c5751 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2633,7 +2633,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca5a0..fe76c36daf 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,9 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OID 4 is reserved for Template0 database */
+#define Template0ObjectId 4
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index e55bc6fa3c..314208b79e 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
2.24.3 (Apple Git-128)
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-18 06:48 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-18 06:48 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Jan 18, 2022 at 12:35 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Tue, Dec 14, 2021 at 1:21 PM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Thanks, Robert for the updated version. I reviewed the changes and it
> > looks fine.
> > I also tested the patch. The patch works as expected.
>
> Thanks.
>
> > > - I adjusted the function header comment for heap_create. Your
> > > proposed comment seemed like it was pretty detailed but not 100%
> > > correct. It also made one of the lines kind of long because you didn't
> > > wrap the text in the surrounding style. I decided to make it simpler
> > > and shorter instead of longer still and 100% correct.
> >
> > The comment update looks fine. However, I still feel it would be good to
> > mention on which (rare) circumstance a valid relfilenode can get passed.
>
> In general, I think it's the job of a function parameter comment to
> describe what the parameter does, not how the callers actually use it.
> One problem with describing the latter is that, if someone later adds
> another caller, there is a pretty good chance that they won't notice
> that the comment needs to be changed. More fundamentally, the
> parameter function comments should be like an instruction manual for
> how to use the function. If you are trying to figure out how to use
> this function, it is not helpful to know that "most callers like to
> pass false" for this parameter. What you need to know is what value
> your new call site should pass, and knowing what "most callers" do or
> that something is "rare" doesn't really help. If we want to make this
> comment more detailed, we should approach it from the point of view of
> explaining how it ought to be set.
It's clear now. Thanks for clarifying.
> I've committed the v8-0001 patch you attached. I'll write separately
> about v8-0002.
Sure. Thank you.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-20 12:09 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-20 12:09 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Jan 18, 2022 at 2:34 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Mon, Jan 17, 2022 at 9:57 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > I have rebased and generated the patches on top of PostgreSQL commit
> > ID cf925936ecc031355cd56fbd392ec3180517a110.
> > Kindly apply v8-0001-pg_upgrade-Preserve-relfilenodes-and-tablespace-O.patch
> > first and then v8-0002-Preserve-database-OIDs-in-pg_upgrade.patch.
>
> OK, so looking over 0002, I noticed a few things:
>
> 1. datlastsysoid isn't being used for anything any more. That's not a
> defect in your patch, but I've separately proposed to remove it.
okay
> 2. I realized that the whole idea here depends on not having initdb
> create more than one database without a fixed OID. The patch solves
> that by nailing down the OID of template0, which is a sufficient
> solution. However, I think nailing down the (initial) OID of postgres
> as well would be a good idea, just in case somebody in the future
> decides to add another system-created database.
I agree with your thought. In my latest patch, postgres db gets
created with a fixed OID.
I have chosen an arbitrary number 16000 as postgres OID from the
unpinned object OID range1200 - 16383.
> 3. The changes to gram.y don't do anything. Sure, you've added a new
> "OID" token, but nothing generates that token, so it has no effect.
> The reason the syntax works is that createdb_opt_name accepts "IDENT",
> which means any string that's not in the keyword list (see kwlist.h).
> But that's there already, so you don't need to do anything in this
> file.
okay
> 4. I felt that the documentation and comments could be somewhat improved.
The documentation and comment updates are more accurate with the
required details. Thanks.
> Here's an updated version in which I've reverted the changes to gram.y
> and tried to improve the comments and documentation. Could you have a
> look at implementing (2) above?
Attached is the patch that implements comment (2).
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments:
v10-0001-pg_upgrade-perserve-database-OID-patch.patch (application/octet-stream, 13.5K) download
From 28cd5f7a5ea5a1aa3f75f767333b7f95a0d6603c Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Thu, 20 Jan 2022 17:23:32 +0530
Subject: [PATCH v10] pg_upgrade perserve database OID patch
---
doc/src/sgml/ref/create_database.sgml | 18 ++++++++++-
src/backend/commands/dbcommands.c | 59 ++++++++++++++++++++++++++++++++---
src/bin/initdb/initdb.c | 40 ++++++++++++++++++------
src/bin/pg_dump/pg_dump.c | 16 ++++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 4 +++
src/include/catalog/unused_oids | 5 +++
11 files changed, 133 insertions(+), 29 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..f22e28d 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,21 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier to be used for the new database. If this
+ parameter is not specified, the database will choose a suitable
+ OID automatically. This parameter is primarily intended for internal
+ use by <application>pg_upgrade</application>, and only
+ <application>pg_upgrade</application> can specify a value less
+ than 16384.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 509d1a3..cc8ebc4 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -117,7 +117,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -217,6 +217,32 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * We don't normally permit new databases to be created with
+ * system-assigned OIDs. pg_upgrade tries to preserve database
+ * OIDs, and we don't want any OIDs that are used in this database
+ * cluster to be OIDs that might be used for some other cluster in
+ * a future version of PostgreSQL. We assume all such OIDs will be
+ * from the system-managed OID range.
+ *
+ * As an exception, however, we permit any OID to be assigned when
+ * allow_system_table_mods=on (so that initdb can assign system
+ * OIDs to template0 and postgres) or when performing a binary
+ * upgrade (so that pg_upgrade can preserve whatever OIDs it finds
+ * in the source cluster).
+ */
+ if (dboid < FirstNormalObjectId &&
+ !allowSystemTableMods && !IsBinaryUpgrade)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("Invalid value for option \"%s\"", defel->defname),
+ errhint("The specified OID %u is less than the minimum OID for user objects %u.",
+ dboid, FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -504,11 +530,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database oid %u is already used by database %s",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory exists for database oid %u", dboid));
+ }
+ else
+ {
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f964a00..6842741 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,15 +1839,23 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
- static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
- /*
- * We use the OID of template0 to determine datlastsysoid
- */
- "UPDATE pg_database SET datlastsysoid = "
- " (SELECT oid FROM pg_database "
- " WHERE datname = 'template0');\n\n",
+ /*
+ * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
+ * enough to drop and recreate a conflicting database with the same name,
+ * but if the same OID were used for one system-created database in the
+ * old cluster and a different system-created database in the new cluster,
+ * it would fail. To avoid that, assign a fixed OID to template0 and
+ * postgres rather than letting the server choose one.
+ *
+ * (Note that, while the user could have dropped and recreated these
+ * objects in the old cluster, the problem scenario only exists if the OID
+ * that is in use in the old cluster is also used in th new cluster - and
+ * the new cluster should be the result of a fresh initdb.)
+ */
+ static const char *const template0_setup[] = {
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1876,9 +1885,22 @@ static void
make_postgres(FILE *cmdfd)
{
const char *const *line;
+
+ /*
+ * Assign a fixed OID to postgres. See comments in make_template0() for
+ * more details on why a fixed OID is assigned.
+ */
static const char *const postgres_setup[] = {
- "CREATE DATABASE postgres;\n\n",
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) ";\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+
+ /*
+ * We use the OID of postgres to determine datlastsysoid
+ */
+ "UPDATE pg_database SET datlastsysoid = "
+ " (SELECT oid FROM pg_database "
+ " WHERE datname = 'postgres');\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c2f1d3..cd14d1f 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2838,8 +2838,20 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa082..69ef231 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d..1db8e3f 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253c..2f4deb3 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 6bd33a0..502b5c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2633,7 +2633,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca..a9c25fc 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,10 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OIDs of Template0 and Postgres database are fixed */
+#define Template0ObjectId 4
+#define PostgresObjectId 16000
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index e55bc6f..314208b 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-20 14:27 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-01-20 14:27 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Jan 20, 2022 at 7:09 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Here's an updated version in which I've reverted the changes to gram.y
> > and tried to improve the comments and documentation. Could you have a
> > look at implementing (2) above?
>
> Attached is the patch that implements comment (2).
This probably needs minor rebasing on account of the fact that I just
pushed the patch to remove datlastsysoid. I intended to do that before
you posted a new version to save you the trouble, but I was too slow
(or you were too fast, however you want to look at it).
+ errmsg("Invalid value for option \"%s\"", defel->defname),
Per the "error message style" section of the documentation, primary
error messages neither begin with a capital letter nor end with a
period, while errdetail() messages are complete sentences and thus
both begin with a capital letter and end with a period. But what I
think you should really do here is get rid of the error detail and
convey all the information in a primary error message. e.g. "OID %u is
a system OID", or maybe better, "OIDs less than %u are reserved for
system objects".
+ errmsg("database oid %u is already used by database %s",
+ errmsg("data directory exists for database oid %u", dboid));
Usually we write "OID" rather than "oid" in error messages. I think
maybe it would be best to change the text slightly too. I suggest:
database OID %u is already in use by database \"%s\"
data directory already exists for database with OID %u
+ * it would fail. To avoid that, assign a fixed OID to template0 and
+ * postgres rather than letting the server choose one.
a fixed OID -> fixed OIDs
one -> them
Or maybe put this comment back the way I had it and just talk about
postgres, and then change the comment in make_postgres to say "Assign
a fixed OID to postgres, for the same reasons as template0."
+ /*
+ * Make sure that binary upgrade propagate the database OID to the new
+ * cluster
+ */
This comment doesn't really seem necessary. It's sort of self-explanatory.
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
Don't you need to do this for PostgresObjectId also?
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-20 16:03 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-20 16:03 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Jan 20, 2022 at 7:57 PM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Thu, Jan 20, 2022 at 7:09 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > > Here's an updated version in which I've reverted the changes to gram.y
> > > and tried to improve the comments and documentation. Could you have a
> > > look at implementing (2) above?
> >
> > Attached is the patch that implements comment (2).
>
> This probably needs minor rebasing on account of the fact that I just
> pushed the patch to remove datlastsysoid. I intended to do that before
> you posted a new version to save you the trouble, but I was too slow
> (or you were too fast, however you want to look at it).
I have rebased the patch. Kindly have a look at it.
> + errmsg("Invalid value for option \"%s\"", defel->defname),
>
> Per the "error message style" section of the documentation, primary
> error messages neither begin with a capital letter nor end with a
> period, while errdetail() messages are complete sentences and thus
> both begin with a capital letter and end with a period. But what I
> think you should really do here is get rid of the error detail and
> convey all the information in a primary error message. e.g. "OID %u is
> a system OID", or maybe better, "OIDs less than %u are reserved for
> system objects".
Fixed
> + errmsg("database oid %u is already used by database %s",
> + errmsg("data directory exists for database oid %u", dboid));
>
> Usually we write "OID" rather than "oid" in error messages. I think
> maybe it would be best to change the text slightly too. I suggest:
>
> database OID %u is already in use by database \"%s\"
> data directory already exists for database with OID %u
The second error message will be reported when the data directory with
the given OID exists in the data path but the corresponding DB does
not exist. This could happen only if the user creates directories in
the data folder which is indeed not an invalid usage. I have updated
the error message to "data directory with the specified OID %u already
exists" because the error message you recommended gives a slightly
different meaning.
> + * it would fail. To avoid that, assign a fixed OID to template0 and
> + * postgres rather than letting the server choose one.
>
> a fixed OID -> fixed OIDs
> one -> them
>
> Or maybe put this comment back the way I had it and just talk about
> postgres, and then change the comment in make_postgres to say "Assign
> a fixed OID to postgres, for the same reasons as template0."
Fixed
> + /*
> + * Make sure that binary upgrade propagate the database OID to the new
> + * cluster
> + */
>
> This comment doesn't really seem necessary. It's sort of self-explanatory.
Removed
> +# Push the OID that is reserved for template0 database.
> +my $Template0ObjectId =
> + Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
> +push @{$oids}, $Template0ObjectId;
>
> Don't you need to do this for PostgresObjectId also?
It is not required for PostgresObjectId. The unused_oids script
provides a list of unused oids in the manually-assignable OIDs range
(1-9999).
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments:
v11-0001-pg_upgrade-perserve-database-OID-patch.patch (application/octet-stream, 12.8K) download
From 808e4d43d7e282fa98eba7d7084a0927b5cafc93 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Thu, 20 Jan 2022 21:15:46 +0530
Subject: [PATCH v11] pg_upgrade perserve database OID patch
---
doc/src/sgml/ref/create_database.sgml | 18 ++++++++++-
src/backend/commands/dbcommands.c | 57 ++++++++++++++++++++++++++++++++---
src/bin/initdb/initdb.c | 22 ++++++++++++--
src/bin/pg_dump/pg_dump.c | 12 ++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 4 +++
src/include/catalog/unused_oids | 5 +++
11 files changed, 116 insertions(+), 22 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..f22e28d 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,21 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier to be used for the new database. If this
+ parameter is not specified, the database will choose a suitable
+ OID automatically. This parameter is primarily intended for internal
+ use by <application>pg_upgrade</application>, and only
+ <application>pg_upgrade</application> can specify a value less
+ than 16384.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index da83455..02529e5 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -115,7 +115,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -215,6 +215,30 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * We don't normally permit new databases to be created with
+ * system-assigned OIDs. pg_upgrade tries to preserve database
+ * OIDs, and we don't want any OIDs that are used in this database
+ * cluster to be OIDs that might be used for some other cluster in
+ * a future version of PostgreSQL. We assume all such OIDs will be
+ * from the system-managed OID range.
+ *
+ * As an exception, however, we permit any OID to be assigned when
+ * allow_system_table_mods=on (so that initdb can assign system
+ * OIDs to template0 and postgres) or when performing a binary
+ * upgrade (so that pg_upgrade can preserve whatever OIDs it finds
+ * in the source cluster).
+ */
+ if (dboid < FirstNormalObjectId &&
+ !allowSystemTableMods && !IsBinaryUpgrade)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("OIDs less than %u are reserved for system objects", FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -502,11 +526,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database OID %u is already in use by database \"%s\"",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory with the specified OID %u already exists", dboid));
+ }
+ else
+ {
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 0a2dba7..7e03b51 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,8 +1839,23 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
+
+ /*
+ * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
+ * enough to drop and recreate a conflicting database with the same name,
+ * but if the same OID were used for one system-created database in the
+ * old cluster and a different system-created database in the new cluster,
+ * it would fail. To avoid that, assign a fixed OID to template0 rather
+ * than letting the server choose one.
+ *
+ * (Note that, while the user could have dropped and recreated these
+ * objects in the old cluster, the problem scenario only exists if the OID
+ * that is in use in the old cluster is also used in th new cluster - and
+ * the new cluster should be the result of a fresh initdb.)
+ */
static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1869,8 +1885,10 @@ static void
make_postgres(FILE *cmdfd)
{
const char *const *line;
+
+ /* Assign a fixed OID to postgres, for the same reasons as template0 */
static const char *const postgres_setup[] = {
- "CREATE DATABASE postgres;\n\n",
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) ";\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c2f1d3..d2847c7 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2838,8 +2838,16 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..384834a 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa082..69ef231 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d..1db8e3f 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253c..2f4deb3 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 6bd33a0..502b5c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2633,7 +2633,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca..a9c25fc 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,10 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OIDs of Template0 and Postgres database are fixed */
+#define Template0ObjectId 4
+#define PostgresObjectId 16000
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index e55bc6f..314208b 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,11 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the OID that is reserved for template0 database.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-20 19:38 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-01-20 19:38 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Thu, Jan 20, 2022 at 11:03 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> It is not required for PostgresObjectId. The unused_oids script
> provides a list of unused oids in the manually-assignable OIDs range
> (1-9999).
Well, so ... why are we not treating the OIDs for these two databases
the same? If there's a range from which we can assign OIDs without
risk of duplication and without needing to update this script, perhaps
we ought to assign both of them from that range and leave the script
alone.
+ * that is in use in the old cluster is also used in th new cluster - and
th -> the.
+preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
Insert "and" before "relfilenode".
I think this is in pretty good shape now.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-21 13:40 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-21 13:40 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Fri, Jan 21, 2022 at 1:08 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Thu, Jan 20, 2022 at 11:03 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > It is not required for PostgresObjectId. The unused_oids script
> > provides a list of unused oids in the manually-assignable OIDs range
> > (1-9999).
>
> Well, so ... why are we not treating the OIDs for these two databases
> the same? If there's a range from which we can assign OIDs without
> risk of duplication and without needing to update this script, perhaps
> we ought to assign both of them from that range and leave the script
> alone.
From what I see in the code, template0 and postgres are the last
things that get created in initdb phase. The system OIDs that get
assigned to these DBs vary from release to release. At present, the
system assigned OIDs of template0 and postgres are 13679 and 13680
respectively. I feel it would be safe to assign 16000 and 16001 for
template0 and postgres respectively from the unpinned object OID range
12000 - 16383. In the future, even if the initdb unpinned objects
reach the range of 16000 issues can only arise if initdb() creates
another system-created database for which the system assigns these
reserved OIDs (16000, 16001).
> + * that is in use in the old cluster is also used in th new cluster - and
>
> th -> the.
Fixed
> +preserves the DB, tablespace, relfilenode OIDs so TOAST and other references
>
> Insert "and" before "relfilenode".
Fixed
Attached is the latest patch for review.
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments:
v12-0001-pg_upgrade-perserve-database-OID-patch.patch (application/octet-stream, 12.1K) download
From 0337ef18dd11a598b0194ba946e1b2dcdd903a18 Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Fri, 21 Jan 2022 18:24:57 +0530
Subject: [PATCH v12] pg_upgrade perserve database OID patch
---
doc/src/sgml/ref/create_database.sgml | 18 ++++++++++-
src/backend/commands/dbcommands.c | 57 ++++++++++++++++++++++++++++++++---
src/bin/initdb/initdb.c | 22 ++++++++++++--
src/bin/pg_dump/pg_dump.c | 12 ++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 4 +++
10 files changed, 111 insertions(+), 22 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..f22e28d 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,21 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier to be used for the new database. If this
+ parameter is not specified, the database will choose a suitable
+ OID automatically. This parameter is primarily intended for internal
+ use by <application>pg_upgrade</application>, and only
+ <application>pg_upgrade</application> can specify a value less
+ than 16384.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index da83455..02529e5 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -115,7 +115,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -215,6 +215,30 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * We don't normally permit new databases to be created with
+ * system-assigned OIDs. pg_upgrade tries to preserve database
+ * OIDs, and we don't want any OIDs that are used in this database
+ * cluster to be OIDs that might be used for some other cluster in
+ * a future version of PostgreSQL. We assume all such OIDs will be
+ * from the system-managed OID range.
+ *
+ * As an exception, however, we permit any OID to be assigned when
+ * allow_system_table_mods=on (so that initdb can assign system
+ * OIDs to template0 and postgres) or when performing a binary
+ * upgrade (so that pg_upgrade can preserve whatever OIDs it finds
+ * in the source cluster).
+ */
+ if (dboid < FirstNormalObjectId &&
+ !allowSystemTableMods && !IsBinaryUpgrade)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("OIDs less than %u are reserved for system objects", FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -502,11 +526,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database OID %u is already in use by database \"%s\"",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory with the specified OID %u already exists", dboid));
+ }
+ else
+ {
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 0a2dba7..d78e8e6 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,8 +1839,23 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
+
+ /*
+ * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
+ * enough to drop and recreate a conflicting database with the same name,
+ * but if the same OID were used for one system-created database in the
+ * old cluster and a different system-created database in the new cluster,
+ * it would fail. To avoid that, assign a fixed OID to template0 rather
+ * than letting the server choose one.
+ *
+ * (Note that, while the user could have dropped and recreated these
+ * objects in the old cluster, the problem scenario only exists if the OID
+ * that is in use in the old cluster is also used in the new cluster - and
+ * the new cluster should be the result of a fresh initdb.)
+ */
static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1869,8 +1885,10 @@ static void
make_postgres(FILE *cmdfd)
{
const char *const *line;
+
+ /* Assign a fixed OID to postgres, for the same reasons as template0 */
static const char *const postgres_setup[] = {
- "CREATE DATABASE postgres;\n\n",
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) ";\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c2f1d3..d2847c7 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2838,8 +2838,16 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..358e933 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, and relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa082..69ef231 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d..1db8e3f 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253c..2f4deb3 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 6bd33a0..502b5c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2633,7 +2633,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca..545a84d 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,10 @@ FullTransactionIdAdvance(FullTransactionId *dest)
#define FirstUnpinnedObjectId 12000
#define FirstNormalObjectId 16384
+/* OIDs of Template0 and Postgres database are fixed */
+#define Template0ObjectId 16000
+#define PostgresObjectId 16001
+
/*
* VariableCache is a data structure in shared memory that is used to track
* OID and XID assignment state. For largely historical reasons, there is
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-21 18:47 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 2 replies; 90+ messages in thread
From: Robert Haas @ 2022-01-21 18:47 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Fri, Jan 21, 2022 at 8:40 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> From what I see in the code, template0 and postgres are the last
> things that get created in initdb phase. The system OIDs that get
> assigned to these DBs vary from release to release. At present, the
> system assigned OIDs of template0 and postgres are 13679 and 13680
> respectively. I feel it would be safe to assign 16000 and 16001 for
> template0 and postgres respectively from the unpinned object OID range
> 12000 - 16383. In the future, even if the initdb unpinned objects
> reach the range of 16000 issues can only arise if initdb() creates
> another system-created database for which the system assigns these
> reserved OIDs (16000, 16001).
It doesn't seem safe to me to rely on that. We don't know what could
happen in the future if the number of built-in objects increases.
Looking at the lengthy comment on this topic in transam.h, I see that
there are three ranges:
1-9999 manually assigned OIDs
10000-11999 OIDs assigned by genbki.pl
12000-16384 OIDs assigned to unpinned objects post-bootstrap
It seems to me that what this comment is saying is that OIDs in the
second and third categories are doled out by counters. Therefore, we
can't know which of those OIDs will get used, or how many of them will
get used, or which objects will get which OIDs. Therefore, I think we
should go back to the approach that you were using for template0 and
handle both that database and postgres using that method. That is,
assign an OID manually, and make sure unused_oids knows that it should
be counted as already used.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-21 18:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Tom Lane @ 2022-01-21 18:57 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> It seems to me that what this comment is saying is that OIDs in the
> second and third categories are doled out by counters. Therefore, we
> can't know which of those OIDs will get used, or how many of them will
> get used, or which objects will get which OIDs. Therefore, I think we
> should go back to the approach that you were using for template0 and
> handle both that database and postgres using that method. That is,
> assign an OID manually, and make sure unused_oids knows that it should
> be counted as already used.
Indeed. If you're going to manually assign OIDs to these databases,
do it honestly, and put them into the range intended for that purpose.
Trying to take short-cuts is just going to cause trouble down the road.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-22 07:17 Shruthi Gowda <gowdashru@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-22 07:17 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Robert Haas <robertmhaas@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Sat, Jan 22, 2022 at 12:27 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Robert Haas <robertmhaas@gmail.com> writes:
> > It seems to me that what this comment is saying is that OIDs in the
> > second and third categories are doled out by counters. Therefore, we
> > can't know which of those OIDs will get used, or how many of them will
> > get used, or which objects will get which OIDs. Therefore, I think we
> > should go back to the approach that you were using for template0 and
> > handle both that database and postgres using that method. That is,
> > assign an OID manually, and make sure unused_oids knows that it should
> > be counted as already used.
>
> Indeed. If you're going to manually assign OIDs to these databases,
> do it honestly, and put them into the range intended for that purpose.
> Trying to take short-cuts is just going to cause trouble down the road.
Understood. I will rework the patch accordingly. Thanks
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-22 07:20 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
1 sibling, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-22 07:20 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Sat, Jan 22, 2022 at 12:17 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Fri, Jan 21, 2022 at 8:40 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > From what I see in the code, template0 and postgres are the last
> > things that get created in initdb phase. The system OIDs that get
> > assigned to these DBs vary from release to release. At present, the
> > system assigned OIDs of template0 and postgres are 13679 and 13680
> > respectively. I feel it would be safe to assign 16000 and 16001 for
> > template0 and postgres respectively from the unpinned object OID range
> > 12000 - 16383. In the future, even if the initdb unpinned objects
> > reach the range of 16000 issues can only arise if initdb() creates
> > another system-created database for which the system assigns these
> > reserved OIDs (16000, 16001).
>
> It doesn't seem safe to me to rely on that. We don't know what could
> happen in the future if the number of built-in objects increases.
> Looking at the lengthy comment on this topic in transam.h, I see that
> there are three ranges:
>
> 1-9999 manually assigned OIDs
> 10000-11999 OIDs assigned by genbki.pl
> 12000-16384 OIDs assigned to unpinned objects post-bootstrap
>
> It seems to me that what this comment is saying is that OIDs in the
> second and third categories are doled out by counters. Therefore, we
> can't know which of those OIDs will get used, or how many of them will
> get used, or which objects will get which OIDs. Therefore, I think we
> should go back to the approach that you were using for template0 and
> handle both that database and postgres using that method. That is,
> assign an OID manually, and make sure unused_oids knows that it should
> be counted as already used.
Agree. In the latest patch, the template0 and postgres OIDs are fixed
to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
no more listed as unused OIDs.
Regards,
Shruthi KC
EnterpriseDB: http://www.enterprisedb.com
Attachments:
v13-0001-pg_upgrade-perserve-database-OID-patch.patch (application/octet-stream, 12.9K) download
From 981645f7c1b278aa1c2a4770c132c0daa3d5085c Mon Sep 17 00:00:00 2001
From: shruthikc-gowda <shruthi.kc@enterprisedb.com>
Date: Sat, 22 Jan 2022 12:38:19 +0530
Subject: [PATCH v13] pg_upgrade perserve database OID patch
---
doc/src/sgml/ref/create_database.sgml | 18 ++++++++++-
src/backend/commands/dbcommands.c | 57 ++++++++++++++++++++++++++++++++---
src/bin/initdb/initdb.c | 22 ++++++++++++--
src/bin/pg_dump/pg_dump.c | 12 ++++++--
src/bin/pg_upgrade/IMPLEMENTATION | 2 +-
src/bin/pg_upgrade/info.c | 9 ++----
src/bin/pg_upgrade/pg_upgrade.h | 3 +-
src/bin/pg_upgrade/relfilenode.c | 4 +--
src/bin/psql/tab-complete.c | 2 +-
src/include/access/transam.h | 4 +++
src/include/catalog/unused_oids | 9 ++++++
11 files changed, 120 insertions(+), 22 deletions(-)
diff --git a/doc/src/sgml/ref/create_database.sgml b/doc/src/sgml/ref/create_database.sgml
index 41cb406..f22e28d 100644
--- a/doc/src/sgml/ref/create_database.sgml
+++ b/doc/src/sgml/ref/create_database.sgml
@@ -31,7 +31,8 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ]
- [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ] ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ OID [=] <replaceable class="parameter">oid</replaceable> ] ]
</synopsis>
</refsynopsisdiv>
@@ -203,6 +204,21 @@ CREATE DATABASE <replaceable class="parameter">name</replaceable>
</para>
</listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><replaceable class="parameter">oid</replaceable></term>
+ <listitem>
+ <para>
+ The object identifier to be used for the new database. If this
+ parameter is not specified, the database will choose a suitable
+ OID automatically. This parameter is primarily intended for internal
+ use by <application>pg_upgrade</application>, and only
+ <application>pg_upgrade</application> can specify a value less
+ than 16384.
+ </para>
+ </listitem>
+ </varlistentry>
+
</variablelist>
<para>
diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index da83455..02529e5 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -115,7 +115,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
HeapTuple tuple;
Datum new_record[Natts_pg_database];
bool new_record_nulls[Natts_pg_database];
- Oid dboid;
+ Oid dboid = InvalidOid;
Oid datdba;
ListCell *option;
DefElem *dtablespacename = NULL;
@@ -215,6 +215,30 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
errhint("Consider using tablespaces instead."),
parser_errposition(pstate, defel->location)));
}
+ else if (strcmp(defel->defname, "oid") == 0)
+ {
+ dboid = defGetInt32(defel);
+
+ /*
+ * We don't normally permit new databases to be created with
+ * system-assigned OIDs. pg_upgrade tries to preserve database
+ * OIDs, and we don't want any OIDs that are used in this database
+ * cluster to be OIDs that might be used for some other cluster in
+ * a future version of PostgreSQL. We assume all such OIDs will be
+ * from the system-managed OID range.
+ *
+ * As an exception, however, we permit any OID to be assigned when
+ * allow_system_table_mods=on (so that initdb can assign system
+ * OIDs to template0 and postgres) or when performing a binary
+ * upgrade (so that pg_upgrade can preserve whatever OIDs it finds
+ * in the source cluster).
+ */
+ if (dboid < FirstNormalObjectId &&
+ !allowSystemTableMods && !IsBinaryUpgrade)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("OIDs less than %u are reserved for system objects", FirstNormalObjectId));
+ }
else
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
@@ -502,11 +526,34 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
*/
pg_database_rel = table_open(DatabaseRelationId, RowExclusiveLock);
- do
+ /*
+ * If database OID is configured, check if the OID is already in use or
+ * data directory already exists.
+ */
+ if (OidIsValid(dboid))
{
- dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
- Anum_pg_database_oid);
- } while (check_db_file_conflict(dboid));
+ char *existing_dbname = get_database_name(dboid);
+
+ if (existing_dbname != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("database OID %u is already in use by database \"%s\"",
+ dboid, existing_dbname));
+
+ if (check_db_file_conflict(dboid))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE)),
+ errmsg("data directory with the specified OID %u already exists", dboid));
+ }
+ else
+ {
+ /* Select an OID for the new database if is not explicitly configured. */
+ do
+ {
+ dboid = GetNewOidWithIndex(pg_database_rel, DatabaseOidIndexId,
+ Anum_pg_database_oid);
+ } while (check_db_file_conflict(dboid));
+ }
/*
* Insert a new tuple into pg_database. This establishes our ownership of
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 0a2dba7..d78e8e6 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -59,6 +59,7 @@
#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 */
@@ -1838,8 +1839,23 @@ static void
make_template0(FILE *cmdfd)
{
const char *const *line;
+
+ /*
+ * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
+ * enough to drop and recreate a conflicting database with the same name,
+ * but if the same OID were used for one system-created database in the
+ * old cluster and a different system-created database in the new cluster,
+ * it would fail. To avoid that, assign a fixed OID to template0 rather
+ * than letting the server choose one.
+ *
+ * (Note that, while the user could have dropped and recreated these
+ * objects in the old cluster, the problem scenario only exists if the OID
+ * that is in use in the old cluster is also used in the new cluster - and
+ * the new cluster should be the result of a fresh initdb.)
+ */
static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false;\n\n",
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
+ CppAsString2(Template0ObjectId) ";\n\n",
/*
* Explicitly revoke public create-schema and create-temp-table
@@ -1869,8 +1885,10 @@ static void
make_postgres(FILE *cmdfd)
{
const char *const *line;
+
+ /* Assign a fixed OID to postgres, for the same reasons as template0 */
static const char *const postgres_setup[] = {
- "CREATE DATABASE postgres;\n\n",
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) ";\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 7c2f1d3..d2847c7 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2838,8 +2838,16 @@ dumpDatabase(Archive *fout)
* are left to the DATABASE PROPERTIES entry, so that they can be applied
* after reconnecting to the target DB.
*/
- appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
- qdatname);
+ if (dopt->binary_upgrade)
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0 OID = %u",
+ qdatname, dbCatId.oid);
+ }
+ else
+ {
+ appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
+ qdatname);
+ }
if (strlen(encoding) > 0)
{
appendPQExpBufferStr(creaQry, " ENCODING = ");
diff --git a/src/bin/pg_upgrade/IMPLEMENTATION b/src/bin/pg_upgrade/IMPLEMENTATION
index 69fcd70..358e933 100644
--- a/src/bin/pg_upgrade/IMPLEMENTATION
+++ b/src/bin/pg_upgrade/IMPLEMENTATION
@@ -84,7 +84,7 @@ cluster using the first part of the pg_dumpall output.
Next, pg_upgrade executes the remainder of the script produced earlier
by pg_dumpall --- this script effectively creates the complete
user-defined metadata from the old cluster to the new cluster. It
-preserves the relfilenode numbers so TOAST and other references
+preserves the DB, tablespace, and relfilenode OIDs so TOAST and other references
to relfilenodes in user data is preserved. (See binary-upgrade usage
in pg_dump).
diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c
index f7fa082..69ef231 100644
--- a/src/bin/pg_upgrade/info.c
+++ b/src/bin/pg_upgrade/info.c
@@ -190,10 +190,8 @@ create_rel_filename_map(const char *old_data, const char *new_data,
map->new_tablespace_suffix = new_cluster.tablespace_suffix;
}
- map->old_db_oid = old_db->db_oid;
- map->new_db_oid = new_db->db_oid;
-
- /* relfilenode is preserved across old and new cluster */
+ /* DB oid and relfilenodes are preserved between old and new cluster */
+ map->db_oid = old_db->db_oid;
map->relfilenode = old_rel->relfilenode;
/* used only for logging and error reporting, old/new are identical */
@@ -324,8 +322,7 @@ get_db_infos(ClusterInfo *cluster)
" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
" ON d.dattablespace = t.oid "
"WHERE d.datallowconn = true "
- /* we don't preserve pg_database.oid so we sort by name */
- "ORDER BY 2");
+ "ORDER BY 1");
res = executeQueryOrDie(conn, "%s", query);
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index da6770d..1db8e3f 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -145,8 +145,7 @@ typedef struct
const char *new_tablespace;
const char *old_tablespace_suffix;
const char *new_tablespace_suffix;
- Oid old_db_oid;
- Oid new_db_oid;
+ Oid db_oid;
Oid relfilenode;
/* the rest are used only for logging and error reporting */
char *nspname; /* namespaces */
diff --git a/src/bin/pg_upgrade/relfilenode.c b/src/bin/pg_upgrade/relfilenode.c
index 6e0253c..2f4deb3 100644
--- a/src/bin/pg_upgrade/relfilenode.c
+++ b/src/bin/pg_upgrade/relfilenode.c
@@ -193,14 +193,14 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
map->old_tablespace,
map->old_tablespace_suffix,
- map->old_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
map->new_tablespace,
map->new_tablespace_suffix,
- map->new_db_oid,
+ map->db_oid,
map->relfilenode,
type_suffix,
extent_suffix);
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 6bd33a0..502b5c5 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2633,7 +2633,7 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH("OWNER", "TEMPLATE", "ENCODING", "TABLESPACE",
"IS_TEMPLATE",
"ALLOW_CONNECTIONS", "CONNECTION LIMIT",
- "LC_COLLATE", "LC_CTYPE", "LOCALE");
+ "LC_COLLATE", "LC_CTYPE", "LOCALE", "OID");
else if (Matches("CREATE", "DATABASE", MatchAny, "TEMPLATE"))
COMPLETE_WITH_QUERY(Query_for_list_of_template_databases);
diff --git a/src/include/access/transam.h b/src/include/access/transam.h
index 338dfca..9a2816d 100644
--- a/src/include/access/transam.h
+++ b/src/include/access/transam.h
@@ -196,6 +196,10 @@ 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 track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/unused_oids b/src/include/catalog/unused_oids
index e55bc6f..61d41e7 100755
--- a/src/include/catalog/unused_oids
+++ b/src/include/catalog/unused_oids
@@ -32,6 +32,15 @@ my @input_files = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
+# Push the template0 and postgres database OIDs.
+my $Template0ObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
+push @{$oids}, $Template0ObjectId;
+
+my $PostgresObjectId =
+ Catalog::FindDefinedSymbol('access/transam.h', '..', 'PostgresObjectId');
+push @{$oids}, $PostgresObjectId;
+
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
--
1.8.3.1
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-24 17:30 Bruce Momjian <bruce@momjian.us>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Bruce Momjian @ 2022-01-24 17:30 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Robert Haas <robertmhaas@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Sat, Jan 22, 2022 at 12:47:35PM +0530, Shruthi Gowda wrote:
> On Sat, Jan 22, 2022 at 12:27 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >
> > Robert Haas <robertmhaas@gmail.com> writes:
> > > It seems to me that what this comment is saying is that OIDs in the
> > > second and third categories are doled out by counters. Therefore, we
> > > can't know which of those OIDs will get used, or how many of them will
> > > get used, or which objects will get which OIDs. Therefore, I think we
> > > should go back to the approach that you were using for template0 and
> > > handle both that database and postgres using that method. That is,
> > > assign an OID manually, and make sure unused_oids knows that it should
> > > be counted as already used.
> >
> > Indeed. If you're going to manually assign OIDs to these databases,
> > do it honestly, and put them into the range intended for that purpose.
> > Trying to take short-cuts is just going to cause trouble down the road.
>
> Understood. I will rework the patch accordingly. Thanks
Thanks. To get the rsync update reduction we need to preserve database
oids.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
If only the physical world exists, free will is an illusion.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-24 19:44 Robert Haas <robertmhaas@gmail.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 3 replies; 90+ messages in thread
From: Robert Haas @ 2022-01-24 19:44 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Sat, Jan 22, 2022 at 2:20 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> Agree. In the latest patch, the template0 and postgres OIDs are fixed
> to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
> no more listed as unused OIDs.
Thanks. Committed with a few more cosmetic changes.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-01-25 04:49 Shruthi Gowda <gowdashru@gmail.com>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Shruthi Gowda @ 2022-01-25 04:49 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Jan 25, 2022 at 1:14 AM Robert Haas <robertmhaas@gmail.com> wrote:
>
> On Sat, Jan 22, 2022 at 2:20 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Agree. In the latest patch, the template0 and postgres OIDs are fixed
> > to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
> > no more listed as unused OIDs.
>
> Thanks. Committed with a few more cosmetic changes.
Thanks, Robert for committing this patch.
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-03-12 16:12 Justin Pryzby <pryzby@telsasoft.com>
parent: Shruthi Gowda <gowdashru@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Justin Pryzby @ 2022-03-12 16:12 UTC (permalink / raw)
To: Shruthi Gowda <gowdashru@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers@postgresql.org, Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Tue, Jan 25, 2022 at 10:19:53AM +0530, Shruthi Gowda wrote:
> On Tue, Jan 25, 2022 at 1:14 AM Robert Haas <robertmhaas@gmail.com> wrote:
> > On Sat, Jan 22, 2022 at 2:20 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > > Agree. In the latest patch, the template0 and postgres OIDs are fixed
> > > to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
> > > no more listed as unused OIDs.
> >
> > Thanks. Committed with a few more cosmetic changes.
>
> Thanks, Robert for committing this patch.
If I'm not wrong, this can be closed.
https://commitfest.postgresql.org/37/3296/
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-03-22 00:52 Andres Freund <andres@anarazel.de>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Andres Freund @ 2022-03-22 00:52 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On 2022-01-24 14:44:10 -0500, Robert Haas wrote:
> On Sat, Jan 22, 2022 at 2:20 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
> > Agree. In the latest patch, the template0 and postgres OIDs are fixed
> > to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
> > no more listed as unused OIDs.
>
> Thanks. Committed with a few more cosmetic changes.
I noticed this still has an open CF entry: https://commitfest.postgresql.org/37/3296/
I assume it can be marked as committed?
- Andres
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-03-22 15:49 Robert Haas <robertmhaas@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 0 replies; 90+ messages in thread
From: Robert Haas @ 2022-03-22 15:49 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>; Tom Lane <tgl@sss.pgh.pa.us>
On Mon, Mar 21, 2022 at 8:52 PM Andres Freund <andres@anarazel.de> wrote:
> I noticed this still has an open CF entry: https://commitfest.postgresql.org/37/3296/
> I assume it can be marked as committed?
Yeah, done now. But don't forget that we still need to do something on
the "wrong fds used for refilenodes after pg_upgrade relfilenode
changes Reply-To:" thread, and I think the ball is in your court
there.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-20 18:34 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
2 siblings, 1 reply; 90+ messages in thread
From: Tom Lane @ 2022-04-20 18:34 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> On Sat, Jan 22, 2022 at 2:20 AM Shruthi Gowda <gowdashru@gmail.com> wrote:
>> Agree. In the latest patch, the template0 and postgres OIDs are fixed
>> to unused manually assigned OIDs 4 and 5 respectively. These OIDs are
>> no more listed as unused OIDs.
> Thanks. Committed with a few more cosmetic changes.
I happened to take a closer look at this patch today, and I'm pretty
unhappy with the way that the assignment of those OIDs was managed.
There are two big problems:
1. IsPinnedObject() will now report that template0 and postgres are
pinned. This seems not to prevent one from dropping them (I guess
dropdb() doesn't consult IsPinnedObject), but it would probably bollix
any pg_shdepend management that should happen for them.
2. The Catalog.pm infrastructure knows nothing about these OIDs.
While the unused_oids script was hack-n-slashed to claim that the
OIDs are used, other scripts won't know about them; for example
duplicate_oids won't report conflicts if someone tries to reuse
those OIDs.
The attached draft patch attempts to improve this situation.
It reserves these OIDs, and creates the associated macros, through
the normal BKI infrastructure by adding entries in pg_database.dat.
We have to delete those rows again during initdb, which is slightly
ugly but surely no more so than initdb's other direct manipulations
of pg_database.
There are a few loose ends:
* I'm a bit inclined to simplify IsPinnedObject by just teaching
it that *no* entries of pg_database are pinned, which would correspond
to the evident lack of enforcement in dropdb(). Can anyone see a
reason why we might pin some database in future?
* I had to set up the additional pg_database entries with nonzero
datfrozenxid to avoid an assertion failure during initdb's first VACUUM.
(That VACUUM will overwrite template1's datfrozenxid before computing
the global minimum frozen XID, but not these others; and it doesn't like
finding that the minimum is zero.) This feels klugy. An alternative is
to delete the extra pg_database rows before that VACUUM, which would
mean taking those deletes out of make_template0 and make_postgres and
putting them somewhere seemingly unrelated, so that's a bit crufty too.
Anybody have a preference?
* The new macro names seem ill-chosen. Template0ObjectId is spelled
randomly differently from the longstanding TemplateDbOid, and surely
PostgresObjectId is about as vague a name as could possibly have
been thought of (please name an object that it couldn't apply to).
I'm a little inclined to rename TemplateDbOid to Template1DbOid and
use Template0DbOid and PostgresDbOid for the others, but I didn't pull
that trigger here.
Comments?
regards, tom lane
Attachments:
fix-assignment-of-initdb-database-OIDs.patch (text/x-diff, 7.5K) download
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 == DatabaseRelationId &&
- objectId == TemplateDbOid)
+ (objectId == TemplateDbOid ||
+ objectId == Template0ObjectId ||
+ objectId == PostgresObjectId))
return false;
/* the public namespace is not pinned */
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1cb4a5b0d2..04454b3d7c 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"
@@ -1806,14 +1806,24 @@ make_template0(FILE *cmdfd)
* objects in the old cluster, the problem scenario only exists if the OID
* that is in use in the old cluster is also used in the new cluster - and
* the new cluster should be the result of a fresh initdb.)
- *
- * We use "STRATEGY = file_copy" here because checkpoints during initdb
- * are cheap. "STRATEGY = wal_log" would generate more WAL, which would
- * be a little bit slower and make the new cluster a little bit bigger.
*/
static const char *const template0_setup[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
- CppAsString2(Template0ObjectId)
+ /*
+ * Since pg_database.dat includes a dummy row for template0, we have
+ * to remove that before creating the database for real.
+ */
+ "DELETE FROM pg_database WHERE datname = 'template0';\n\n",
+
+ /*
+ * Clone template1 to make template0.
+ *
+ * We use "STRATEGY = file_copy" here because checkpoints during
+ * initdb are cheap. "STRATEGY = wal_log" would generate more WAL,
+ * which would be a little bit slower and make the new cluster a
+ * little bit bigger.
+ */
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false"
+ " OID = " CppAsString2(Template0ObjectId)
" STRATEGY = file_copy;\n\n",
/*
@@ -1836,12 +1846,11 @@ make_template0(FILE *cmdfd)
"REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n",
"REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n",
- "COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n",
-
/*
- * Finally vacuum to clean up dead rows in pg_database
+ * Note: postgres.bki filled in a comment for template0, so we need
+ * not do that here.
*/
- "VACUUM pg_database;\n\n",
+
NULL
};
@@ -1858,12 +1867,19 @@ make_postgres(FILE *cmdfd)
const char *const *line;
/*
- * Just as we did for template0, and for the same reasons, assign a fixed
- * OID to postgres and select the file_copy strategy.
+ * Comments in make_template0() mostly apply here too.
*/
static const char *const postgres_setup[] = {
- "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) " STRATEGY = file_copy;\n\n",
- "COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n",
+ "DELETE FROM pg_database WHERE datname = 'postgres';\n\n",
+
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId)
+ " STRATEGY = file_copy;\n\n",
+
+ /*
+ * Finally vacuum to clean up dead rows in pg_database
+ */
+ "VACUUM pg_database;\n\n",
+
NULL
};
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 969e2a7a46..bcb81e02c4 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2844,10 +2844,11 @@ dumpDatabase(Archive *fout)
qdatname = pg_strdup(fmtId(datname));
/*
- * Prepare the CREATE DATABASE command. We must specify 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.
+ * 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 track
* OID and XID assignment state. For largely historical reasons, there is
diff --git a/src/include/catalog/pg_database.dat b/src/include/catalog/pg_database.dat
index 5feedff7bf..6c2221a4e9 100644
--- a/src/include/catalog/pg_database.dat
+++ b/src/include/catalog/pg_database.dat
@@ -19,4 +19,24 @@
datminmxid => '1', dattablespace => 'pg_default', datcollate => 'LC_COLLATE',
datctype => 'LC_CTYPE', daticulocale => 'ICU_LOCALE', datacl => '_null_' },
+# The template0 and postgres entries are included here to reserve their
+# associated OIDs. We show their correct properties for reference, but
+# note that these pg_database rows are removed and replaced by initdb.
+# Unlike the row for template1, their datfrozenxids will not be overwritten
+# during initdb's first VACUUM, so we have to provide normal-looking XIDs.
+
+{ oid => '4', oid_symbol => 'Template0ObjectId',
+ descr => 'unmodifiable empty database',
+ datname => 'template0', encoding => 'ENCODING', datlocprovider => 'LOCALE_PROVIDER', datistemplate => 't',
+ datallowconn => 'f', datconnlimit => '-1', datfrozenxid => '4',
+ datminmxid => '1', dattablespace => 'pg_default', datcollate => 'LC_COLLATE',
+ datctype => 'LC_CTYPE', daticulocale => 'ICU_LOCALE', datacl => '_null_' },
+
+{ oid => '5', oid_symbol => 'PostgresObjectId',
+ descr => 'default administrative connection database',
+ datname => 'postgres', encoding => 'ENCODING', datlocprovider => 'LOCALE_PROVIDER', datistemplate => 'f',
+ datallowconn => 't', datconnlimit => '-1', datfrozenxid => '4',
+ datminmxid => '1', dattablespace => 'pg_default', datcollate => 'LC_COLLATE',
+ datctype => 'LC_CTYPE', daticulocale => 'ICU_LOCALE', datacl => '_null_' },
+
]
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 = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
-# Push the template0 and postgres database OIDs.
-my $Template0ObjectId =
- Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
-push @{$oids}, $Template0ObjectId;
-
-my $PostgresObjectId =
- Catalog::FindDefinedSymbol('access/transam.h', '..', 'PostgresObjectId');
-push @{$oids}, $PostgresObjectId;
-
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-20 19:28 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-04-20 19:28 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Wed, Apr 20, 2022 at 2:34 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> The attached draft patch attempts to improve this situation.
> It reserves these OIDs, and creates the associated macros, through
> the normal BKI infrastructure by adding entries in pg_database.dat.
> We have to delete those rows again during initdb, which is slightly
> ugly but surely no more so than initdb's other direct manipulations
> of pg_database.
I'm not sure I really like this approach, but if you're firmly
convinced that it's better than cleaning up the loose ends in some
other way, I'm not going to waste a lot of energy fighting about it.
It doesn't seem horrible.
> There are a few loose ends:
>
> * I'm a bit inclined to simplify IsPinnedObject by just teaching
> it that *no* entries of pg_database are pinned, which would correspond
> to the evident lack of enforcement in dropdb(). Can anyone see a
> reason why we might pin some database in future?
It's kind of curious that we don't pin anything now. There's kind of
nothing to stop you from hosing the system by dropping template0
and/or template1, or mutating them into some crazy form that doesn't
work. But having said that, if as a matter of policy we don't even pin
template0 or template1 or postgres, then it seems unlikely that we
would suddenly decide to pin some new database in the future.
> * I had to set up the additional pg_database entries with nonzero
> datfrozenxid to avoid an assertion failure during initdb's first VACUUM.
> (That VACUUM will overwrite template1's datfrozenxid before computing
> the global minimum frozen XID, but not these others; and it doesn't like
> finding that the minimum is zero.) This feels klugy. An alternative is
> to delete the extra pg_database rows before that VACUUM, which would
> mean taking those deletes out of make_template0 and make_postgres and
> putting them somewhere seemingly unrelated, so that's a bit crufty too.
> Anybody have a preference?
Not really. If anything that's an argument against this entire
approach, but I already commented on that point above.
> * The new macro names seem ill-chosen. Template0ObjectId is spelled
> randomly differently from the longstanding TemplateDbOid, and surely
> PostgresObjectId is about as vague a name as could possibly have
> been thought of (please name an object that it couldn't apply to).
> I'm a little inclined to rename TemplateDbOid to Template1DbOid and
> use Template0DbOid and PostgresDbOid for the others, but I didn't pull
> that trigger here.
Seems totally reasonable. I don't find the current naming horrible or
I'd not have committed it that way, but putting Db in there makes
sense, too.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-20 20:56 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Tom Lane @ 2022-04-20 20:56 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> On Wed, Apr 20, 2022 at 2:34 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> The attached draft patch attempts to improve this situation.
>> It reserves these OIDs, and creates the associated macros, through
>> the normal BKI infrastructure by adding entries in pg_database.dat.
>> We have to delete those rows again during initdb, which is slightly
>> ugly but surely no more so than initdb's other direct manipulations
>> of pg_database.
> I'm not sure I really like this approach, but if you're firmly
> convinced that it's better than cleaning up the loose ends in some
> other way, I'm not going to waste a lot of energy fighting about it.
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);
That would result in (a) the OIDs becoming known to Catalog.pm
as reserved, though it wouldn't have any great clue about their
semantics; and (b) this getting emitted into pg_database_d.h:
#define Template0ObjectId 4
#define PostgresObjectId 5
Then we'd not need the dummy entries in pg_database.dat, which
does seem cleaner now that I think about it. A downside is that
with no context, Catalog.pm could not provide name translation
services during postgres.bki generation for such OIDs --- but
at least for these entries, we don't need that.
If that seems more plausible to you I'll set about preparing a patch.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-20 23:03 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-04-20 23:03 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Wed, Apr 20, 2022 at 4:56 PM Tom Lane <tgl@sss.pgh.pa.us> 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);
>
> If that seems more plausible to you I'll set about preparing a patch.
I like it!
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-21 17:53 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 90+ messages in thread
From: Tom Lane @ 2022-04-21 17:53 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> On Wed, Apr 20, 2022 at 4:56 PM Tom Lane <tgl@sss.pgh.pa.us> 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
Attachments: download all (2 files, .tar.gz)
0001-fix-assignment-of-database-OIDs-2.patch (text/x-diff, 7.6K) download
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} = [];
$catalog{toasting} = [];
$catalog{indexing} = [];
+ $catalog{other_oids} = [];
+ $catalog{foreign_keys} = [];
$catalog{client_code} = [];
open(my $ifh, '<', $input_file) || die "$input_file: $!";
@@ -118,6 +120,14 @@ sub ParseHeader
index_decl => $6
};
}
+ elsif (/^DECLARE_OID_DEFINING_MACRO\(\s*(\w+),\s*(\d+)\)/)
+ {
+ push @{ $catalog{other_oids} },
+ {
+ other_name => $1,
+ other_oid => $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 == DatabaseRelationId &&
- objectId == TemplateDbOid)
+ (objectId == TemplateDbOid ||
+ objectId == Template0ObjectId ||
+ objectId == 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 = pg_strdup(fmtId(datname));
/*
- * Prepare the CREATE DATABASE command. We must specify 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.
+ * 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 track
* OID and XID assignment state. For largely historical reasons, there is
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_variable
#define DECLARE_UNIQUE_INDEX_PKEY(name,oid,oidmacro,decl) extern int no_such_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 subject
+ * to uniqueness verification and renumber_oids.pl renumbering. A C macro
+ * 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_database.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, PgDatabaseToastTable, 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 template0
+ * 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/renumber_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 = 1;
}
}
+ elsif (/^(DECLARE_OID_DEFINING_MACRO\(\s*\w+,\s*)(\d+)\)/)
+ {
+ if (exists $maphash{$2})
+ {
+ my $repl = $1 . $maphash{$2} . ")";
+ $line =~
+ s/^DECLARE_OID_DEFINING_MACRO\(\s*\w+,\s*\d+\)/$repl/;
+ $changed = 1;
+ }
+ }
elsif ($line =~ 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 = glob("pg_*.h");
my $oids = Catalog::FindAllOidsFromHeaders(@input_files);
-# Push the template0 and postgres database OIDs.
-my $Template0ObjectId =
- Catalog::FindDefinedSymbol('access/transam.h', '..', 'Template0ObjectId');
-push @{$oids}, $Template0ObjectId;
-
-my $PostgresObjectId =
- Catalog::FindDefinedSymbol('access/transam.h', '..', 'PostgresObjectId');
-push @{$oids}, $PostgresObjectId;
-
# Also push FirstGenbkiObjectId to serve as a terminator for the last gap.
my $FirstGenbkiObjectId =
Catalog::FindDefinedSymbol('access/transam.h', '..', 'FirstGenbkiObjectId');
0002-rename-symbols-and-pin-no-DBs.patch (text/x-diff, 5.3K) download
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 => '1', oid_symbol => 'TemplateDbOid',
+{ oid => '1', oid_symbol => 'Template1DbOid',
descr => 'database\'s default template',
- datname => 'template1', encoding => 'ENCODING', datistemplate => 't',
+ datname => 'template1', encoding => 'ENCODING',
+ datlocprovider => 'LOCALE_PROVIDER', datistemplate => 't',
datallowconn => 't', datconnlimit => '-1', datfrozenxid => '0',
datminmxid => '1', dattablespace => 'pg_default', datcollate => 'LC_COLLATE',
- datctype => 'LC_CTYPE', datacl => '_null_' },
+ datctype => 'LC_CTYPE', daticulocale => 'ICU_LOCALE', datacl => '_null_' },
]
]]></programlisting>
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/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 = FirstMultiXactId;
checkPoint.nextMultiOffset = 0;
checkPoint.oldestXid = FirstNormalTransactionId;
- checkPoint.oldestXidDB = TemplateDbOid;
+ checkPoint.oldestXidDB = Template1DbOid;
checkPoint.oldestMulti = FirstMultiXactId;
- checkPoint.oldestMultiDB = TemplateDbOid;
+ checkPoint.oldestMultiDB = Template1DbOid;
checkPoint.oldestCommitTsXid = InvalidTransactionId;
checkPoint.newestCommitTsXid = InvalidTransactionId;
checkPoint.time = (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 == DatabaseRelationId &&
- (objectId == TemplateDbOid ||
- objectId == Template0ObjectId ||
- objectId == PostgresObjectId))
- return false;
-
/* the public namespace is not pinned */
if (classId == NamespaceRelationId &&
objectId == PG_PUBLIC_NAMESPACE)
return false;
+ /*
+ * Databases are never pinned. It might seem that it'd be prudent to pin
+ * at least template0; but we do this intentionally so that template0 and
+ * 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 == 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/postinit.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 char *username,
*/
if (bootstrap)
{
- MyDatabaseId = TemplateDbOid;
+ MyDatabaseId = Template1DbOid;
MyDatabaseTableSpace = DEFAULTTABLESPACE_OID;
}
else if (in_dbname != 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[] = {
- "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false OID = "
- CppAsString2(Template0ObjectId)
+ "CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false"
+ " OID = " CppAsString2(Template0DbOid)
" STRATEGY = 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[] = {
- "CREATE DATABASE postgres OID = " CppAsString2(PostgresObjectId) " STRATEGY = file_copy;\n\n",
+ "CREATE DATABASE postgres OID = " CppAsString2(PostgresDbOid)
+ " STRATEGY = file_copy;\n\n",
"COMMENT ON DATABASE postgres IS 'default administrative connection database';\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 => '1', oid_symbol => 'TemplateDbOid',
+{ oid => '1', oid_symbol => 'Template1DbOid',
descr => 'default template for new databases',
datname => 'template1', encoding => 'ENCODING', datlocprovider => 'LOCALE_PROVIDER', datistemplate => 't',
datallowconn => 't', datconnlimit => '-1', datfrozenxid => '0',
diff --git a/src/include/catalog/pg_database.h b/src/include/catalog/pg_database.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, DatabaseOidIndexId, 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 */
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-21 19:25 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 90+ messages in thread
From: Robert Haas @ 2022-04-21 19:25 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
On Thu, Apr 21, 2022 at 1:53 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
> > On Wed, Apr 20, 2022 at 4:56 PM Tom Lane <tgl@sss.pgh.pa.us> 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.
Both patches look good to me.
--
Robert Haas
EDB: http://www.enterprisedb.com
^ permalink raw reply [nested|flat] 90+ messages in thread
* Re: preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce)
@ 2022-04-21 20:24 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 0 replies; 90+ messages in thread
From: Tom Lane @ 2022-04-21 20:24 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Shruthi Gowda <gowdashru@gmail.com>; Julien Rouhaud <rjuju123@gmail.com>; Sadhuprasad Patro <b.sadhu@gmail.com>; Stephen Frost <sfrost@snowman.net>; Bruce Momjian <bruce@momjian.us>; Andres Freund <andres@anarazel.de>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Masahiko Sawada <sawada.mshk@gmail.com>; Tom Kincaid <tomjohnkincaid@gmail.com>; Amit Kapila <amit.kapila16@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers; Masahiko Sawada <masahiko.sawada@2ndquadrant.com>
Robert Haas <robertmhaas@gmail.com> writes:
> On Thu, Apr 21, 2022 at 1:53 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> 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.
> Both patches look good to me.
Pushed, thanks for looking.
regards, tom lane
^ permalink raw reply [nested|flat] 90+ messages in thread
end of thread, other threads:[~2022-04-21 20:24 UTC | newest]
Thread overview: 90+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17 15:56 preserving db/ts/relfilenode OIDs across pg_upgrade (was Re: storing an explicit nonce) Robert Haas <robertmhaas@gmail.com>
2021-08-17 16:12 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-17 16:33 ` Stephen Frost <sfrost@snowman.net>
2021-08-17 16:42 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-17 17:37 ` Robert Haas <robertmhaas@gmail.com>
2021-08-17 17:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-17 17:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-17 18:50 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 15:00 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 15:24 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 15:35 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 15:44 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 15:36 ` Stephen Frost <sfrost@snowman.net>
2021-08-26 15:48 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 16:34 ` Stephen Frost <sfrost@snowman.net>
2021-08-26 16:49 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 17:03 ` Stephen Frost <sfrost@snowman.net>
2021-08-26 17:16 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 17:24 ` Stephen Frost <sfrost@snowman.net>
2021-08-26 17:37 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 16:37 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 16:51 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 17:20 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 17:34 ` Bruce Momjian <bruce@momjian.us>
2021-08-26 15:39 ` Stephen Frost <sfrost@snowman.net>
2021-08-26 15:52 ` Robert Haas <robertmhaas@gmail.com>
2021-08-26 16:42 ` Stephen Frost <sfrost@snowman.net>
2021-08-17 18:07 ` Shruthi Gowda <gowdashru@gmail.com>
2021-08-17 18:32 ` Bruce Momjian <bruce@momjian.us>
2021-08-20 17:36 ` Shruthi Gowda <gowdashru@gmail.com>
2021-08-23 20:57 ` Robert Haas <robertmhaas@gmail.com>
2021-08-23 21:12 ` Stephen Frost <sfrost@snowman.net>
2021-08-24 15:24 ` Robert Haas <robertmhaas@gmail.com>
2021-08-24 16:43 ` Bruce Momjian <bruce@momjian.us>
2021-08-24 17:26 ` Robert Haas <robertmhaas@gmail.com>
2021-08-24 17:46 ` Stephen Frost <sfrost@snowman.net>
2021-08-24 18:16 ` Bruce Momjian <bruce@momjian.us>
2021-08-24 18:34 ` Robert Haas <robertmhaas@gmail.com>
2021-08-24 18:49 ` Bruce Momjian <bruce@momjian.us>
2021-08-24 17:25 ` Stephen Frost <sfrost@snowman.net>
2021-08-24 00:29 ` Bruce Momjian <bruce@momjian.us>
2021-08-24 09:10 ` Shruthi Gowda <gowdashru@gmail.com>
2021-08-24 15:28 ` Robert Haas <robertmhaas@gmail.com>
2021-08-24 16:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-24 16:39 ` Bruce Momjian <bruce@momjian.us>
2021-08-24 17:23 ` Robert Haas <robertmhaas@gmail.com>
2021-08-24 16:40 ` Bruce Momjian <bruce@momjian.us>
2021-09-22 19:06 ` Shruthi Gowda <gowdashru@gmail.com>
2021-09-23 19:13 ` Robert Haas <robertmhaas@gmail.com>
2021-10-04 16:44 ` Shruthi Gowda <gowdashru@gmail.com>
2021-10-06 20:35 ` Robert Haas <robertmhaas@gmail.com>
2021-10-07 07:24 ` Shruthi Gowda <gowdashru@gmail.com>
2021-10-07 14:02 ` Robert Haas <robertmhaas@gmail.com>
2021-10-26 13:24 ` Shruthi Gowda <gowdashru@gmail.com>
2021-12-06 04:44 ` Sadhuprasad Patro <b.sadhu@gmail.com>
2021-12-06 17:55 ` Robert Haas <robertmhaas@gmail.com>
2021-12-13 15:13 ` Shruthi Gowda <gowdashru@gmail.com>
2021-12-17 07:33 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-15 05:47 ` Julien Rouhaud <rjuju123@gmail.com>
2022-01-17 14:57 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-17 21:03 ` Robert Haas <robertmhaas@gmail.com>
2022-01-20 12:09 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-20 14:27 ` Robert Haas <robertmhaas@gmail.com>
2022-01-20 16:03 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-20 19:38 ` Robert Haas <robertmhaas@gmail.com>
2022-01-21 13:40 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-21 18:47 ` Robert Haas <robertmhaas@gmail.com>
2022-01-21 18:57 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-01-22 07:17 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-24 17:30 ` Bruce Momjian <bruce@momjian.us>
2022-01-22 07:20 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-24 19:44 ` Robert Haas <robertmhaas@gmail.com>
2022-01-25 04:49 ` Shruthi Gowda <gowdashru@gmail.com>
2022-03-12 16:12 ` Justin Pryzby <pryzby@telsasoft.com>
2022-03-22 00:52 ` Andres Freund <andres@anarazel.de>
2022-03-22 15:49 ` Robert Haas <robertmhaas@gmail.com>
2022-04-20 18:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-04-20 19:28 ` Robert Haas <robertmhaas@gmail.com>
2022-04-20 20:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-04-20 23:03 ` Robert Haas <robertmhaas@gmail.com>
2022-04-21 17:53 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-04-21 19:25 ` Robert Haas <robertmhaas@gmail.com>
2022-04-21 20:24 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-12-13 14:40 ` Shruthi Gowda <gowdashru@gmail.com>
2021-12-13 21:05 ` Robert Haas <robertmhaas@gmail.com>
2021-12-14 18:21 ` Shruthi Gowda <gowdashru@gmail.com>
2022-01-17 19:05 ` Robert Haas <robertmhaas@gmail.com>
2022-01-18 06:48 ` Shruthi Gowda <gowdashru@gmail.com>
2021-12-14 18:39 ` tushar <tushar.ahuja@enterprisedb.com>
2021-12-15 17:03 ` tushar <tushar.ahuja@enterprisedb.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox