Skip to content

Commit 25cab44

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 e2f0628 commit 25cab44

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
@@ -12894,9 +12894,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1289412894
/*
1289512895
* Re-parse the index and constraint definitions, and attach them to the
1289612896
* appropriate work queue entries. We do this before dropping because in
12897-
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
12898-
* lock on the table the constraint is attached to, and we need to get
12899-
* that before reparsing/dropping.
12897+
* the case of a constraint on another table, we might not yet have
12898+
* exclusive lock on the table the constraint is attached to, and we need
12899+
* to get that before reparsing/dropping. (That's possible at least for
12900+
* FOREIGN KEY, CHECK, and EXCLUSION constraints; in non-FK cases it
12901+
* requires a dependency on the target table's composite type in the other
12902+
* table's constraint expressions.)
1290012903
*
1290112904
* We can't rely on the output of deparsing to tell us which relation to
1290212905
* operate on, because concurrent activity might have made the name
@@ -12912,7 +12915,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1291212915
Form_pg_constraint con;
1291312916
Oid relid;
1291412917
Oid confrelid;
12915-
char contype;
1291612918
bool conislocal;
1291712919

1291812920
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -12929,7 +12931,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1292912931
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
1293012932
}
1293112933
confrelid = con->confrelid;
12932-
contype = con->contype;
1293312934
conislocal = con->conislocal;
1293412935
ReleaseSysCache(tup);
1293512936

@@ -12946,12 +12947,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1294612947
continue;
1294712948

1294812949
/*
12949-
* When rebuilding an FK constraint that references the table we're
12950-
* modifying, we might not yet have any lock on the FK's table, so get
12951-
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
12952-
* step, so there's no value in asking for anything weaker.
12950+
* When rebuilding another table's constraint that references the
12951+
* table we're modifying, we might not yet have any lock on the other
12952+
* table, so get one now. We'll need AccessExclusiveLock for the DROP
12953+
* CONSTRAINT step, so there's no value in asking for anything weaker.
1295312954
*/
12954-
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
12955+
if (relid != tab->relid)
1295512956
LockRelationOid(relid, AccessExclusiveLock);
1295612957

1295712958
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
@@ -4582,6 +4582,13 @@ alter table attbl alter column p1 set data type bigint;
45824582
alter table atref alter column c1 set data type bigint;
45834583
drop table attbl, atref;
45844584
/* End test case for bug #17409 */
4585+
/* Test case for bug #18970 */
4586+
create table attbl(a int);
4587+
create table atref(b attbl check ((b).a is not null));
4588+
alter table attbl alter column a type numeric; -- someday this should work
4589+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4590+
drop table attbl, atref;
4591+
/* End test case for bug #18970 */
45854592
-- Test that ALTER TABLE rewrite preserves a clustered index
45864593
-- for normal indexes and indexes on constraints.
45874594
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
@@ -3022,6 +3022,15 @@ drop table attbl, atref;
30223022

30233023
/* End test case for bug #17409 */
30243024

3025+
/* Test case for bug #18970 */
3026+
3027+
create table attbl(a int);
3028+
create table atref(b attbl check ((b).a is not null));
3029+
alter table attbl alter column a type numeric; -- someday this should work
3030+
drop table attbl, atref;
3031+
3032+
/* End test case for bug #18970 */
3033+
30253034
-- Test that ALTER TABLE rewrite preserves a clustered index
30263035
-- for normal indexes and indexes on constraints.
30273036
create table alttype_cluster (a int);

0 commit comments

Comments
 (0)