Skip to content

Commit 8319e5c

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 6d12d5a commit 8319e5c

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
@@ -15415,9 +15415,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1541515415
/*
1541615416
* Re-parse the index and constraint definitions, and attach them to the
1541715417
* appropriate work queue entries. We do this before dropping because in
15418-
* the case of a FOREIGN KEY constraint, we might not yet have exclusive
15419-
* lock on the table the constraint is attached to, and we need to get
15420-
* that before reparsing/dropping.
15418+
* the case of a constraint on another table, we might not yet have
15419+
* exclusive lock on the table the constraint is attached to, and we need
15420+
* to get that before reparsing/dropping. (That's possible at least for
15421+
* FOREIGN KEY, CHECK, and EXCLUSION constraints; in non-FK cases it
15422+
* requires a dependency on the target table's composite type in the other
15423+
* table's constraint expressions.)
1542115424
*
1542215425
* We can't rely on the output of deparsing to tell us which relation to
1542315426
* operate on, because concurrent activity might have made the name
@@ -15433,7 +15436,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1543315436
Form_pg_constraint con;
1543415437
Oid relid;
1543515438
Oid confrelid;
15436-
char contype;
1543715439
bool conislocal;
1543815440

1543915441
tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(oldId));
@@ -15450,7 +15452,6 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1545015452
elog(ERROR, "could not identify relation associated with constraint %u", oldId);
1545115453
}
1545215454
confrelid = con->confrelid;
15453-
contype = con->contype;
1545415455
conislocal = con->conislocal;
1545515456
ReleaseSysCache(tup);
1545615457

@@ -15468,12 +15469,12 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1546815469
continue;
1546915470

1547015471
/*
15471-
* When rebuilding an FK constraint that references the table we're
15472-
* modifying, we might not yet have any lock on the FK's table, so get
15473-
* one now. We'll need AccessExclusiveLock for the DROP CONSTRAINT
15474-
* step, so there's no value in asking for anything weaker.
15472+
* When rebuilding another table's constraint that references the
15473+
* table we're modifying, we might not yet have any lock on the other
15474+
* table, so get one now. We'll need AccessExclusiveLock for the DROP
15475+
* CONSTRAINT step, so there's no value in asking for anything weaker.
1547515476
*/
15476-
if (relid != tab->relid && contype == CONSTRAINT_FOREIGN)
15477+
if (relid != tab->relid)
1547715478
LockRelationOid(relid, AccessExclusiveLock);
1547815479

1547915480
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
@@ -4745,6 +4745,13 @@ alter table attbl alter column p1 set data type bigint;
47454745
alter table atref alter column c1 set data type bigint;
47464746
drop table attbl, atref;
47474747
/* End test case for bug #17409 */
4748+
/* Test case for bug #18970 */
4749+
create table attbl(a int);
4750+
create table atref(b attbl check ((b).a is not null));
4751+
alter table attbl alter column a type numeric; -- someday this should work
4752+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4753+
drop table attbl, atref;
4754+
/* End test case for bug #18970 */
47484755
-- Test that ALTER TABLE rewrite preserves a clustered index
47494756
-- for normal indexes and indexes on constraints.
47504757
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
@@ -3069,6 +3069,15 @@ drop table attbl, atref;
30693069

30703070
/* End test case for bug #17409 */
30713071

3072+
/* Test case for bug #18970 */
3073+
3074+
create table attbl(a int);
3075+
create table atref(b attbl check ((b).a is not null));
3076+
alter table attbl alter column a type numeric; -- someday this should work
3077+
drop table attbl, atref;
3078+
3079+
/* End test case for bug #18970 */
3080+
30723081
-- Test that ALTER TABLE rewrite preserves a clustered index
30733082
-- for normal indexes and indexes on constraints.
30743083
create table alttype_cluster (a int);

0 commit comments

Comments
 (0)