Skip to content

Commit 614ffb2

Browse files
committed
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@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
1 parent 4dc48ad commit 614ffb2

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/backend/commands/tablecmds.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13461,9 +13461,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1346113461
/*
1346213462
* Re-parse the index and constraint definitions, and attach them to the
1346313463
* appropriate work queue entries. We do this before dropping because in
13464-
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
13465-
* lock on the table the constraint is attached to, and we need to get
13466-
* that before reparsing/dropping.
13464+
* the case of a constraint on another table, we might not yet have
13465+
* exclusive lock on the table the constraint is attached to, and we need
13466+
* to get that before reparsing/dropping. (That's possible at least for
13467+
* FOREIGN KEY, CHECK, and EXCLUSION constraints; in non-FK cases it
13468+
* requires a dependency on the target table's composite type in the other
13469+
* table's constraint expressions.)
1346713470
*
1346813471
* We can't rely on the output of deparsing to tell us which relation to
1346913472
* operate on, because concurrent activity might have made the name
@@ -13479,7 +13482,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1347913482
Form_pg_constraint con;
1348013483
Oid relid;
1348113484
Oid confrelid;
13482-
char contype;
1348313485
bool conislocal;
1348413486

1348513487
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -13496,7 +13498,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1349613498
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
1349713499
}
1349813500
confrelid = con->confrelid;
13499-
contype = con->contype;
1350013501
conislocal = con->conislocal;
1350113502
ReleaseSysCache(tup);
1350213503

@@ -13513,12 +13514,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1351313514
continue;
1351413515

1351513516
/*
13516-
* When rebuilding an FK constraint that references the table we're
13517-
* modifying, we might not yet have any lock on the FK's table, so get
13518-
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
13519-
* step, so there's no value in asking for anything weaker.
13517+
* When rebuilding another table's constraint that references the
13518+
* table we're modifying, we might not yet have any lock on the other
13519+
* table, so get one now. We'll need AccessExclusiveLock for the DROP
13520+
* CONSTRAINT step, so there's no value in asking for anything weaker.
1352013521
*/
13521-
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
13522+
if (relid != tab->relid)
1352213523
LockRelationOid(relid, AccessExclusiveLock);
1352313524

1352413525
ATPostAlterTypeParse(oldId, relid, confrelid,

src/test/regress/expected/alter_table.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4575,6 +4575,13 @@ alter table attbl alter column p1 set data type bigint;
45754575
alter table atref alter column c1 set data type bigint;
45764576
drop table attbl, atref;
45774577
/* End test case for bug #17409 */
4578+
/* Test case for bug #18970 */
4579+
create table attbl(a int);
4580+
create table atref(b attbl check ((b).a is not null));
4581+
alter table attbl alter column a type numeric; -- someday this should work
4582+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4583+
drop table attbl, atref;
4584+
/* End test case for bug #18970 */
45784585
-- Test that ALTER TABLE rewrite preserves a clustered index
45794586
-- for normal indexes and indexes on constraints.
45804587
create table alttype_cluster (a int);

src/test/regress/sql/alter_table.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,6 +3014,15 @@ drop table attbl, atref;
30143014

30153015
/* End test case for bug #17409 */
30163016

3017+
/* Test case for bug #18970 */
3018+
3019+
create table attbl(a int);
3020+
create table atref(b attbl check ((b).a is not null));
3021+
alter table attbl alter column a type numeric; -- someday this should work
3022+
drop table attbl, atref;
3023+
3024+
/* End test case for bug #18970 */
3025+
30173026
-- Test that ALTER TABLE rewrite preserves a clustered index
30183027
-- for normal indexes and indexes on constraints.
30193028
create table alttype_cluster (a int);

0 commit comments

Comments
 (0)