Skip to content

Commit bbfcbc4

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 07402e3 commit bbfcbc4

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
@@ -13854,9 +13854,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1385413854
/*
1385513855
* Re-parse the index and constraint definitions, and attach them to the
1385613856
* appropriate work queue entries. We do this before dropping because in
13857-
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
13858-
* lock on the table the constraint is attached to, and we need to get
13859-
* that before reparsing/dropping.
13857+
* the case of a constraint on another table, we might not yet have
13858+
* exclusive lock on the table the constraint is attached to, and we need
13859+
* to get that before reparsing/dropping. (That's possible at least for
13860+
* FOREIGN KEY, CHECK, and EXCLUSION constraints; in non-FK cases it
13861+
* requires a dependency on the target table's composite type in the other
13862+
* table's constraint expressions.)
1386013863
*
1386113864
* We can't rely on the output of deparsing to tell us which relation to
1386213865
* operate on, because concurrent activity might have made the name
@@ -13872,7 +13875,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1387213875
Form_pg_constraint con;
1387313876
Oid relid;
1387413877
Oid confrelid;
13875-
char contype;
1387613878
bool conislocal;
1387713879

1387813880
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -13889,7 +13891,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1388913891
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
1389013892
}
1389113893
confrelid = con->confrelid;
13892-
contype = con->contype;
1389313894
conislocal = con->conislocal;
1389413895
ReleaseSysCache(tup);
1389513896

@@ -13906,12 +13907,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1390613907
continue;
1390713908

1390813909
/*
13909-
* When rebuilding an FK constraint that references the table we're
13910-
* modifying, we might not yet have any lock on the FK's table, so get
13911-
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
13912-
* step, so there's no value in asking for anything weaker.
13910+
* When rebuilding another table's constraint that references the
13911+
* table we're modifying, we might not yet have any lock on the other
13912+
* table, so get one now. We'll need AccessExclusiveLock for the DROP
13913+
* CONSTRAINT step, so there's no value in asking for anything weaker.
1391313914
*/
13914-
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
13915+
if (relid != tab->relid)
1391513916
LockRelationOid(relid, AccessExclusiveLock);
1391613917

1391713918
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
@@ -4615,6 +4615,13 @@ alter table attbl alter column p1 set data type bigint;
46154615
alter table atref alter column c1 set data type bigint;
46164616
drop table attbl, atref;
46174617
/* End test case for bug #17409 */
4618+
/* Test case for bug #18970 */
4619+
create table attbl(a int);
4620+
create table atref(b attbl check ((b).a is not null));
4621+
alter table attbl alter column a type numeric; -- someday this should work
4622+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4623+
drop table attbl, atref;
4624+
/* End test case for bug #18970 */
46184625
-- Test that ALTER TABLE rewrite preserves a clustered index
46194626
-- for normal indexes and indexes on constraints.
46204627
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
@@ -3027,6 +3027,15 @@ drop table attbl, atref;
30273027

30283028
/* End test case for bug #17409 */
30293029

3030+
/* Test case for bug #18970 */
3031+
3032+
create table attbl(a int);
3033+
create table atref(b attbl check ((b).a is not null));
3034+
alter table attbl alter column a type numeric; -- someday this should work
3035+
drop table attbl, atref;
3036+
3037+
/* End test case for bug #18970 */
3038+
30303039
-- Test that ALTER TABLE rewrite preserves a clustered index
30313040
-- for normal indexes and indexes on constraints.
30323041
create table alttype_cluster (a int);

0 commit comments

Comments
 (0)