pgsql: Obtain required table lock during cross-table constraint updates

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Obtain required table lock during cross-table constraint updates
Date: 2025-06-29 17:56:19
Message-ID: E1uVwGJ-003uh9-0z@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Obtain required table lock during cross-table constraint updates.

Sometimes a table's constraint may depend on a column of another
table, so that we have to update the constraint when changing the
referenced column's type. We need to have lock on the constraint's
table to do that. ATPostAlterTypeCleanup believed that this case
was only possible for FOREIGN KEY constraints, but it's wrong at
least for CHECK and EXCLUDE constraints; and in general, we'd
probably need exclusive lock to alter any sort of constraint.
So just remove the contype check and acquire lock for any other
table. This prevents a "you don't have lock" assertion failure,
though no ill effect is observed in production builds.

We'll error out later anyway because we don't presently support
physically altering column types within stored composite columns.
But the catalog-munging is basically all there, so we may as well
make that part work.

Bug: #18970
Reported-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Diagnosed-by: jian he <jian(dot)universality(at)gmail(dot)com>
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/18970-a7d1cfe1f8d5d8d9@postgresql.org
Backpatch-through: 13

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/25cab4473525cd3705fc8ee524b85721db967b65

Modified Files
--------------
src/backend/commands/tablecmds.c | 21 +++++++++++----------
src/test/regress/expected/alter_table.out | 7 +++++++
src/test/regress/sql/alter_table.sql | 9 +++++++++
3 files changed, 27 insertions(+), 10 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2025-06-29 19:04:41 pgsql: Fix some new issues with planning of PlaceHolderVars.
Previous Message Peter Eisentraut 2025-06-29 15:32:57 pgsql: pg_recvlogical: Rename --two-phase and --failover options.