]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Obtain required table lock during cross-table constraint updates.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 29 Jun 2025 17:56:03 +0000 (13:56 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 29 Jun 2025 17:56:03 +0000 (13:56 -0400)
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@gmail.com>
Diagnosed-by: jian he <jian.universality@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/18970-a7d1cfe1f8d5d8d9@postgresql.org
Backpatch-through: 13

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 4f7be0247c2fa9b438c1b9b8eb02b06de0627dd1..2f649ba75820f7abd4a3dba8831dbdbc3a0b610e 100644 (file)
@@ -13461,9 +13461,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
        /*
         * Re-parse the index and constraint definitions, and attach them to the
         * appropriate work queue entries.  We do this before dropping because in
-        * the case of a FOREIGN KEY constraint, we might not yet have exclusive
-        * lock on the table the constraint is attached to, and we need to get
-        * that before reparsing/dropping.
+        * the case of a constraint on another table, we might not yet have
+        * exclusive lock on the table the constraint is attached to, and we need
+        * to get that before reparsing/dropping.  (That's possible at least for
+        * FOREIGN KEY, CHECK, and EXCLUSION constraints; in non-FK cases it
+        * requires a dependency on the target table's composite type in the other
+        * table's constraint expressions.)
         *
         * We can't rely on the output of deparsing to tell us which relation to
         * operate on, because concurrent activity might have made the name
@@ -13479,7 +13482,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
                Form_pg_constraint con;
                Oid                     relid;
                Oid                     confrelid;
-               char            contype;
                bool            conislocal;
 
                tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -13496,7 +13498,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
                                elog(ERROR, "could not identify relation associated with constraint %u", oldId);
                }
                confrelid = con->confrelid;
-               contype = con->contype;
                conislocal = con->conislocal;
                ReleaseSysCache(tup);
 
@@ -13513,12 +13514,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
                        continue;
 
                /*
-                * When rebuilding an FK constraint that references the table we're
-                * modifying, we might not yet have any lock on the FK's table, so get
-                * one now.  We'll need AccessExclusiveLock for the DROP CONSTRAINT
-                * step, so there's no value in asking for anything weaker.
+                * When rebuilding another table's constraint that references the
+                * table we're modifying, we might not yet have any lock on the other
+                * table, so get one now.  We'll need AccessExclusiveLock for the DROP
+                * CONSTRAINT step, so there's no value in asking for anything weaker.
                 */
-               if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
+               if (relid != tab->relid)
                        LockRelationOid(relid, AccessExclusiveLock);
 
                ATPostAlterTypeParse(oldId, relid, confrelid,
index 8bfe2629650d9ccf39cd81ed38b27a9a6120dbb7..e1636d3f82ea4e7ee4c64438f995d0496bee8716 100644 (file)
@@ -4575,6 +4575,13 @@ alter table attbl alter column p1 set data type bigint;
 alter table atref alter column c1 set data type bigint;
 drop table attbl, atref;
 /* End test case for bug #17409 */
+/* Test case for bug #18970 */
+create table attbl(a int);
+create table atref(b attbl check ((b).a is not null));
+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
+drop table attbl, atref;
+/* End test case for bug #18970 */
 -- Test that ALTER TABLE rewrite preserves a clustered index
 -- for normal indexes and indexes on constraints.
 create table alttype_cluster (a int);
index 62d9aa8c0c07c0b1fe187e4b0917f84b5dadbd3a..43c3c728403055adfa65c491a175e303d6718eb7 100644 (file)
@@ -3014,6 +3014,15 @@ drop table attbl, atref;
 
 /* End test case for bug #17409 */
 
+/* Test case for bug #18970 */
+
+create table attbl(a int);
+create table atref(b attbl check ((b).a is not null));
+alter table attbl alter column a type numeric;  -- someday this should work
+drop table attbl, atref;
+
+/* End test case for bug #18970 */
+
 -- Test that ALTER TABLE rewrite preserves a clustered index
 -- for normal indexes and indexes on constraints.
 create table alttype_cluster (a int);