Regress test might fail due to deadlock between domain and alter_table

Started by Alexander Lakhinabout 2 months ago3 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Alexander Lakhin
exclusion@gmail.com

Hello hackers,

I happened to observe an interesting failure of `make check` being
executed concurrently against multiple databases:
diff -U3 --minimal .../regress_input_27/expected/domain.out .../regress_output_27/results/domain.out
--- .../expected/domain.out    2026-10-02 08:03:59.474274001 +0000
+++ .../results/domain.out    2026-10-02 08:26:49.225597020 +0000
@@ -1337,11 +1337,7 @@
              FROM information_schema.domain_constraints
              WHERE domain_name IN ('con', 'dom', 'pos_int', 'things'))
    ORDER BY constraint_name;
- constraint_catalog | constraint_schema | constraint_name  | check_clause
---------------------+-------------------+------------------+-------------------
- regress027         | public            | con_check        | (VALUE > 0)
- regress027         | public            | meow             | (VALUE < 11)
- regress027         | public            | pos_int_check    | (VALUE > 0)
- regress027         | public            | pos_int_not_null | VALUE IS NOT NULL
-(4 rows)
-
+ERROR:  deadlock detected
+DETAIL:  Process 322216 waits for AccessShareLock on relation 1197300 of database 16411; blocked by process 322217.
+Process 322217 waits for AccessExclusiveLock on relation 1197314 of database 16411; blocked by process 322216.
+HINT:  See server log for query details.

I've managed to reproduce this with the attached patch:
# diff -U3 .../src/test/regress/expected/alter_table.out .../src/test/regress/results/alter_table.out
# --- .../src/test/regress/expected/alter_table.out       2026-10-07 12:53:24.843538437 -0400
# +++ .../src/test/regress/results/alter_table.out        2026-10-07 14:58:08.602062642 -0400
# @@ -4808,7 +4808,10 @@
#  (1 row)
#
#  alter table attbl alter column a type numeric;  -- someday this should work
# -ERROR:  cannot alter table "attbl" because column "atref.b" uses its row type
# +ERROR:  deadlock detected
# +DETAIL:  Process 2884690 waits for AccessExclusiveLock on relation 45440 of database 16384; blocked by process 2884685.
# +Process 2884685 waits for AccessShareLock on relation 45437 of database 16384; blocked by process 2884690.
# +HINT:  See server log for query details.
#  alter table atref drop constraint atref_b_check;
#  create statistics atref_stat on ((b).a is not null) from atref;
#  alter table attbl alter column a type numeric;  -- someday this should work

That is, the deadlock occurs between
"SELECT * FROM information_schema.check_constraint" / pg_get_constraintdef()
and
"alter table attbl alter column a type numeric;" / ATPostAlterTypeCleanup().

Reproduced starting from 8319e5cb5.

Best regards,
Alexander

Attachments:

t254095_1
reproduce-deadlock.patchtext/x-patch; charset=UTF-8; name=reproduce-deadlock.patchDownload+18-0
#2Osama Abdul Qader
osamaabdulqader.cs@gmail.com
In reply to: Alexander Lakhin (#1)
Re: Regress test might fail due to deadlock between domain and alter_table

Hi Alexander,

Hope that you are well.

I'm Osama Abdul Qader, currently working as an independent postgresql
contributor, I'm writing this email to express my interest in working with
you to fix this bug by producing the patch for it, I've already produced a
patch which was committed by Alvaro Hererra on September 8 [1]https://github.com/postgres/postgres/commit/25b21c0bb712b615f22cf5fcc176764a416368bd.

I have reviewed the work of Jan Nidzwetzki [2]https://commitfest.postgresql.org/patch/7276/.

Can't wait for morning (as I'm from Hyderabad, India and it's currently
01:50AM here) to work on the bug patch.

[1]: https://github.com/postgres/postgres/commit/25b21c0bb712b615f22cf5fcc176764a416368bd
https://github.com/postgres/postgres/commit/25b21c0bb712b615f22cf5fcc176764a416368bd
[2]: https://commitfest.postgresql.org/patch/7276/

With regards,
Osama Abdul Qader

On Thu, Oct 8, 2026 at 1:30 AM Alexander Lakhin <exclusion@gmail.com> wrote:

Show quoted text

Hello hackers,

I happened to observe an interesting failure of `make check` being
executed concurrently against multiple databases:
diff -U3 --minimal .../regress_input_27/expected/domain.out
.../regress_output_27/results/domain.out
--- .../expected/domain.out    2026-10-02 08:03:59.474274001 +0000
+++ .../results/domain.out    2026-10-02 08:26:49.225597020 +0000
@@ -1337,11 +1337,7 @@
FROM information_schema.domain_constraints
WHERE domain_name IN ('con', 'dom', 'pos_int', 'things'))
ORDER BY constraint_name;
- constraint_catalog | constraint_schema | constraint_name  | check_clause
---------------------+-------------------+------------------+-------------------
- regress027         | public            | con_check        | (VALUE > 0)
- regress027         | public            | meow             | (VALUE < 11)
- regress027         | public            | pos_int_check    | (VALUE > 0)
- regress027         | public            | pos_int_not_null | VALUE IS NOT
NULL
-(4 rows)
-
+ERROR:  deadlock detected
+DETAIL:  Process 322216 waits for AccessShareLock on relation 1197300 of
database 16411; blocked by process 322217.
+Process 322217 waits for AccessExclusiveLock on relation 1197314 of
database 16411; blocked by process 322216.
+HINT:  See server log for query details.

I've managed to reproduce this with the attached patch:
# diff -U3 .../src/test/regress/expected/alter_table.out
.../src/test/regress/results/alter_table.out
# --- .../src/test/regress/expected/alter_table.out 2026-10-07
12:53:24.843538437 -0400
# +++ .../src/test/regress/results/alter_table.out 2026-10-07
14:58:08.602062642 -0400
# @@ -4808,7 +4808,10 @@
# (1 row)
#
# alter table attbl alter column a type numeric; -- someday this should
work
# -ERROR: cannot alter table "attbl" because column "atref.b" uses its
row type
# +ERROR: deadlock detected
# +DETAIL: Process 2884690 waits for AccessExclusiveLock on relation
45440 of database 16384; blocked by process 2884685.
# +Process 2884685 waits for AccessShareLock on relation 45437 of database
16384; blocked by process 2884690.
# +HINT: See server log for query details.
# alter table atref drop constraint atref_b_check;
# create statistics atref_stat on ((b).a is not null) from atref;
# alter table attbl alter column a type numeric; -- someday this should
work

That is, the deadlock occurs between
"SELECT * FROM information_schema.check_constraint" /
pg_get_constraintdef()
and
"alter table attbl alter column a type numeric;" /
ATPostAlterTypeCleanup().

Reproduced starting from 8319e5cb5.

Best regards,
Alexander

#3Osama Abdul Qader
osamaabdulqader.cs@gmail.com
In reply to: Alexander Lakhin (#1)
Re: Regress test might fail due to deadlock between domain and alter_table

Hi,

I'm currently investigating a reproducible deadlock involving concurrent
regression tests (alter_table and domain), starting from commit 8319e5cb5.

While setting up a clean PostgreSQL 20devel environment to reproduce the
issue, I ran into a separate bootstrap failure that I'm having trouble
explaining.

I rebuilt and installed the current source tree, and verified that both
binaries report PostgreSQL 20devel. I also verified that bthandler() in
src/backend/access/nbtree/nbtree.c returns a static IndexAmRoutine, and
that the generated FMGR table maps OID 330 to bthandler.

However, a completely fresh cluster fails during initdb:

$ rm -rf ~/pgdata-repro
$ ~/pg-install/bin/initdb -D ~/pgdata-repro

running bootstrap script ...
FATAL: index access method handler function 330 did not return an
IndexAmRoutine struct
PANIC: cannot abort transaction 1, it was already committed
Aborted (core dumped)
initdb: removing data directory "/home/osama/pgdata-repro"

The relevant check appears to be in src/backend/access/index/amapi.c:

datum = OidFunctionCall0(amhandler);
routine = (const IndexAmRoutine *) DatumGetPointer(datum);

if (routine == NULL || !IsA(routine, IndexAmRoutine))
elog(ERROR, "index access method handler function %u did not
return an IndexAmRoutine struct",
amhandler);

I have not modified nbtree.c, and the generated catalog contains:

330 bthandler ... bthandler

and fmgrtab.c contains:

{ 330, 1, true, false, "bthandler", bthandler },

I initially suspected a stale build, but I forced a rebuild of the nbtree
code and backend, reinstalled, and the failure persisted.

Could you point me toward what might cause bthandler() to fail the IsA(routine,
IndexAmRoutine) check during bootstrap?

I want to make sure my development build is sound before continuing with
the deadlock reproduction.

Thanks,
Osama Abdul Qader

On Thu, Oct 8, 2026 at 1:15 PM Osama Abdul Qader <
osamaabdulqader.cs@gmail.com> wrote:

Show quoted text

Hi Alexander,

Sure, I'll cooperate with you and also the senior hacker who is in charge
of committing the patches, surely it's a nice day to work with you.

Let us solve these issues and make our work scream in PostgreSQL commit
history.

With best regards,
Osama Abdul Qader

On Thu, Oct 8, 2026 at 10:30 AM Alexander Lakhin <exclusion@gmail.com>
wrote:

Hello Osama,

[ writing this off-list to minimize noise ]

07.10.2026 23:24, Osama Abdul Qader wrote:

Hi Alexander,

Hope that you are well.

I'm Osama Abdul Qader, currently working as an independent postgresql
contributor, I'm writing this email to express my interest in working with
you to fix this bug by producing the patch for it, I've already produced a
patch which was committed by Alvaro Hererra on September 8 [1].

I have reviewed the work of Jan Nidzwetzki [2].

Can't wait for morning (as I'm from Hyderabad, India and it's currently
01:50AM here) to work on the bug patch.

I'm not a committer (unlike Alvaro), so I think you would need to
cooperate with some of senior hackers who will eventually push the
patch anyway. To me, it looks like a tricky issue and I have no idea yet
how to fix it properly, so if you want to work on this, please step in.

Looking forward to your patch or deeper investigation on the mailing list.

Best regards,
Alexander