pg_dump/restore failure (dependency?) on BF serinus
pg_dump/restore failure (dependency?) on BF serinus
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi, https://buildfarm.postgresql.org/cgi-bin/show_stage_log.pl?nm=serinus&dt=2025-04-07%2017%3A45%3A28&stg=pg_upgrade-check failed in a problem-indicating way in the new dump/restore test that Ashutosh added. > # Running: pg_restore --create -j2 -d postgres /home/bf/bf-build/serinus/HEAD/pgsql.build/testrun/pg_upgrade/002_pg_upgrade/data/tmp_test_KTss/regression.dump > pg_restore: error: could not execute query: ERROR: there is no unique constraint matching given keys for referenced table "pk" > Command was: ALTER TABLE fkpart5.fk > ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES fkpart5.pk(a); > > > pg_restore: warning: errors ignored on restore: 2 There are no other errors visible, neither in regress_log, nor in the server log. It's hard to tell without more logging, but the most likely explanation for this seems that somehow the primary key on fkpart5.pk hasn't yet been restored. However, looking at the pg_restore -v -l of the regression test database locally, the relevant dependencies look sane on a first glance. So I'm a bit confused how this could happen? Greetings, Andres Freund
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andres Freund writes: >> # Running: pg_restore --create -j2 -d postgres /home/bf/bf-build/serinus/HEAD/pgsql.build/testrun/pg_upgrade/002_pg_upgrade/data/tmp_test_KTss/regression.dump >> pg_restore: error: could not execute query: ERROR: there is no unique constraint matching given keys for referenced table "pk" >> Command was: ALTER TABLE fkpart5.fk >> ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES fkpart5.pk(a); > It's hard to tell without more logging, but the most likely explanation for > this seems that somehow the primary key on fkpart5.pk hasn't yet been > restored. However, looking at the pg_restore -v -l of the regression test > database locally, the relevant dependencies look sane on a first glance. This feels quite adjacent to my complaint here: https://www.postgresql.org/message-id/2045026.1743801143%40sss.pgh.pa.us though perhaps it's not exactly the same. I plan to start looking into a fix for that tomorrow. regards, tom lane
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Andres Freund <andres@anarazel.de>
Дата:
Hi, On 2025-04-08 00:11:55 -0400, Tom Lane wrote: > Andres Freund writes: > >> # Running: pg_restore --create -j2 -d postgres /home/bf/bf-build/serinus/HEAD/pgsql.build/testrun/pg_upgrade/002_pg_upgrade/data/tmp_test_KTss/regression.dump > >> pg_restore: error: could not execute query: ERROR: there is no unique constraint matching given keys for referenced table "pk" > >> Command was: ALTER TABLE fkpart5.fk > >> ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES fkpart5.pk(a); > > > It's hard to tell without more logging, but the most likely explanation for > > this seems that somehow the primary key on fkpart5.pk hasn't yet been > > restored. However, looking at the pg_restore -v -l of the regression test > > database locally, the relevant dependencies look sane on a first glance. > > This feels quite adjacent to my complaint here: > > https://www.postgresql.org/message-id/2045026.1743801143%40sss.pgh.pa.us > > though perhaps it's not exactly the same. That does sound rather plausible. What an odd coincidence that it failed like that so close to your email. While this specific failure probably couldn't have happened much earlier, it seems that it could have as part of pg_upgrade for longer. > I plan to start looking into a fix for that tomorrow. Cool. Greetings, Andres Freund
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andres Freund writes: > On 2025-04-08 00:11:55 -0400, Tom Lane wrote: >> This feels quite adjacent to my complaint here: >> https://www.postgresql.org/message-id/2045026.1743801143%40sss.pgh.pa.us >> though perhaps it's not exactly the same. > That does sound rather plausible. What an odd coincidence that it failed like > that so close to your email. While this specific failure probably couldn't > have happened much earlier, it seems that it could have as part of pg_upgrade > for longer. I think pg_upgrade is not vulnerable to the problem, or at least not the identical problem, because it doesn't expect pg_restore to load table data. So I think we didn't previously have any test cases that would expose this :-(. What I find surprising is that we didn't get field reports much sooner. regards, tom lane
Re: pg_dump/restore failure (dependency?) on BF serinus
On Tue, 8 Apr 2025 at 09:48, Tom Lane wrote: > > Andres Freund writes: > > On 2025-04-08 00:11:55 -0400, Tom Lane wrote: > >> This feels quite adjacent to my complaint here: > >> https://www.postgresql.org/message-id/2045026.1743801143%40sss.pgh.pa.us > >> though perhaps it's not exactly the same. > > > That does sound rather plausible. What an odd coincidence that it failed like > > that so close to your email. While this specific failure probably couldn't > > have happened much earlier, it seems that it could have as part of pg_upgrade > > for longer. > > I think pg_upgrade is not vulnerable to the problem, or at least not > the identical problem, because it doesn't expect pg_restore to load > table data. So I think we didn't previously have any test cases > that would expose this :-(. What I find surprising is that we > didn't get field reports much sooner. > > regards, tom lane > > I was able to reproduce this failure reliably on HEAD e5d25959cf87 Attached sh script (AI generated) fails 6-7 times per 100 iteration run for me. The root cause is catalog check race and it is not pg_restore-only. The error is real and server-side. The referenced partitioned PK index is left permanently indisvalid=false by a race between the last two concurrent ATTACH statements, and nothing ever retries the validation afterwards. So, I managed to create an isolation test with 2-level partitioning which hits this issue in the (i believe) same way as shell reproducer does. The issue does not reproduce with one-level partitioning. The key component here is that first level LEAF partitions are created invalid. So, when two last ATTACH statements validate top-level index validity inside ATExecAttachPartitionIdx -> validatePartitionedIndex they count the number of other valid indexes. With some execution ordering we can get into a schedule where 2 & 3 partition indexes are about-to-be valid (but this state is not yet committed), and they concurrently recount that there are 2 out of 3 valid indexes, then each of this statement commits. So, each statement runs before the other's transaction commits, so no snapshot taken by either can ever observe the complete set. I didn't came up with fix idea yet. -- Best regards, Kirill Reshke
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
On Mon, 5 Oct 2026 at 16:20, Kirill Reshke wrote: > > On Tue, 8 Apr 2025 at 09:48, Tom Lane wrote: > > > > Andres Freund writes: > > > On 2025-04-08 00:11:55 -0400, Tom Lane wrote: > > >> This feels quite adjacent to my complaint here: > > >> https://www.postgresql.org/message-id/2045026.1743801143%40sss.pgh.pa.us > > >> though perhaps it's not exactly the same. > > > > > That does sound rather plausible. What an odd coincidence that it failed like > > > that so close to your email. While this specific failure probably couldn't > > > have happened much earlier, it seems that it could have as part of pg_upgrade > > > for longer. > > > > I think pg_upgrade is not vulnerable to the problem, or at least not > > the identical problem, because it doesn't expect pg_restore to load > > table data. So I think we didn't previously have any test cases > > that would expose this :-(. What I find surprising is that we > > didn't get field reports much sooner. > > > > regards, tom lane > > > > > > I was able to reproduce this failure reliably on HEAD e5d25959cf87 > Attached sh script (AI generated) fails 6-7 times per 100 iteration run for me. > > The root cause is catalog check race and it is not pg_restore-only. > > The error is real and server-side. The referenced partitioned PK index > is left permanently indisvalid=false by a race between the last two > concurrent ATTACH statements, and nothing ever retries the validation > afterwards. > > So, I managed to create an isolation test with 2-level partitioning > which hits this issue in the (i believe) same way as shell reproducer > does. > The issue does not reproduce with one-level partitioning. The key > component here is that first level LEAF partitions are created > invalid. So, when two last ATTACH statements validate top-level index > validity inside ATExecAttachPartitionIdx -> validatePartitionedIndex > they count the number of other valid indexes. > With some execution ordering we can get into a schedule where 2 & 3 > partition indexes are about-to-be valid (but this state is not yet > committed), and they concurrently recount that there are 2 out of 3 > valid indexes, then each of this statement commits. > > So, each statement runs before the other's transaction commits, so no > snapshot taken by either can ever observe the complete set. > > I didn't came up with fix idea yet. > > -- > Best regards, > Kirill Reshke x4m (Andrey) suggest this diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -22618,7 +22618,12 @@ validatePartitionedIndex(Relation partedIdx, Relation partedTbl) validatePartitionedIndex(parentIdx, parentTbl); - relation_close(parentIdx, AccessExclusiveLock); - relation_close(parentTbl, AccessExclusiveLock); + /* + * Keep these locks until commit, so another validator cannot miss + * our uncommitted changes to a descendant's indisvalid flag and leave + * the parent index invalid. + */ + relation_close(parentIdx, NoLock); + relation_close(parentTbl, NoLock); } } -- Best regards, Kirill Reshke
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Álvaro Herrera <alvherre@alvh.no-ip.org>
Дата:
On 2026-Oct-05, Kirill Reshke wrote: > x4m (Andrey) suggest this > > diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c > --- a/src/backend/commands/tablecmds.c > +++ b/src/backend/commands/tablecmds.c > @@ -22618,7 +22618,12 @@ validatePartitionedIndex(Relation partedIdx, > Relation partedTbl) > validatePartitionedIndex(parentIdx, parentTbl); > > - relation_close(parentIdx, AccessExclusiveLock); > - relation_close(parentTbl, AccessExclusiveLock); > + /* > + * Keep these locks until commit, so another validator cannot miss > + * our uncommitted changes to a descendant's indisvalid flag and leave > + * the parent index invalid. > + */ > + relation_close(parentIdx, NoLock); > + relation_close(parentTbl, NoLock); > } > } Hmm, yeah, I think this is a plausible fix. This code is all from 8b08f7d4820f and I can't remember if I had any rationale for releasing the lock early after potentially doing a DDL change in the affected parent table. This is probably just a thinko. The reproducer script as given didn't fail very frequently for me -- just 1 out of 180 tries (i.e. I ran it thrice and had just one failure in total). However, I can make it fail almost 50% of the time by adding a pg_usleep(50*1000) immediately below these two relation_close(, AEL) calls; and then when I change them to retain the lock, I see no further failures. No other tests fail after this change either. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
On Mon, 5 Oct 2026, 18:19 Álvaro Herrera, <alvherre@alvh.no-ip.org> wrote:
On 2026-Oct-05, Kirill Reshke wrote:
> x4m (Andrey) suggest this
>
> diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
> --- a/src/backend/commands/tablecmds.c
> +++ b/src/backend/commands/tablecmds.c
> @@ -22618,7 +22618,12 @@ validatePartitionedIndex(Relation partedIdx,
> Relation partedTbl)
> validatePartitionedIndex(parentIdx, parentTbl);
>
> - relation_close(parentIdx, AccessExclusiveLock);
> - relation_close(parentTbl, AccessExclusiveLock);
> + /*
> + * Keep these locks until commit, so another validator cannot miss
> + * our uncommitted changes to a descendant's indisvalid flag and leave
> + * the parent index invalid.
> + */
> + relation_close(parentIdx, NoLock);
> + relation_close(parentTbl, NoLock);
> }
> }
Hmm, yeah, I think this is a plausible fix. This code is all from
8b08f7d4820f and I can't remember if I had any rationale for releasing
the lock early after potentially doing a DDL change in the affected
parent table. This is probably just a thinko.
Thanks for taking a look, LGTM then? I can send thus diff as .patch file if you need one.
AI-generated isolation test is too specific, reproducing this exact issue, I don't think we need to include it. It doesn't provide usefull coverage much. If we decide to nevertheless have test here - tell me, I will remove AI bloat from spec. I didn't have much time to do a legwork here yet.
The reproducer script as given didn't fail very frequently for me --
just 1 out of 180 tries (i.e. I ran it thrice and had just one failure
in total). However, I can make it fail almost 50% of the time by adding
a pg_usleep(50*1000) immediately below these two relation_close(, AEL)
calls; and then when I change them to retain the lock, I see no further
failures.
No other tests fail after this change either.
--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
Re: pg_dump/restore failure (dependency?) on BF serinus
От:
Álvaro Herrera <alvherre@alvh.no-ip.org>
Дата:
On 2026-Oct-07, Kirill Reshke wrote: > Thanks for taking a look, LGTM then? I can send thus diff as .patch file if > you need one. No need. I have pushed this to all branches, thanks for the bump. > AI-generated isolation test is too specific, reproducing this exact issue, > I don't think we need to include it. It doesn't provide usefull coverage > much. If we decide to nevertheless have test here - tell me, I will remove > AI bloat from spec. I didn't have much time to do a legwork here yet. Yeah, I agree it'd likely be too specific and unlikely to provide useful coverage. Did the test use an injection point? Thanks -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/