Skip to content

Commit f9ba071

Browse files
committed
Obtain required table lock during cross-table updates, redux.
Commits 8319e5c et al missed the fact that ATPostAlterTypeCleanup contains three calls to ATPostAlterTypeParse, and the other two also need protection against passing a relid that we don't yet have lock on. Add similar logic to those code paths, and add some test cases demonstrating the need for it. In v18 and master, the test cases demonstrate that there's a behavioral discrepancy between stored generated columns and virtual generated columns: we disallow changing the expression of a stored column if it's used in any composite-type columns, but not that of a virtual column. Since the expression isn't actually relevant to either sort of composite-type usage, this prohibition seems unnecessary; but changing it is a matter for separate discussion. For now we are just documenting the existing behavior. Reported-by: jian he <jian.universality@gmail.com> Author: jian he <jian.universality@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: CACJufxGKJtGNRRSXfwMW9SqVOPEMdP17BJ7DsBf=tNsv9pWU9g@mail.gmail.com Backpatch-through: 13
1 parent 4c7d2c2 commit f9ba071

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12632,6 +12632,14 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1263212632
Oid relid;
1263312633

1263412634
relid = IndexGetRelation(oldId, false);
12635+
12636+
/*
12637+
* As above, make sure we have lock on the index's table if it's not
12638+
* the same table.
12639+
*/
12640+
if (relid != tab->relid)
12641+
LockRelationOid(relid, AccessExclusiveLock);
12642+
1263512643
ATPostAlterTypeParse(oldId, relid, InvalidOid,
1263612644
(char *) lfirst(def_item),
1263712645
wqueue, lockmode, tab->rewrite);

src/test/regress/expected/alter_table.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,6 +4513,10 @@ create table attbl(a int);
45134513
create table atref(b attbl check ((b).a is not null));
45144514
alter table attbl alter column a type numeric; -- someday this should work
45154515
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4516+
alter table atref drop constraint atref_b_check;
4517+
create index atref_idx on atref (((b).a));
4518+
alter table attbl alter column a type numeric; -- someday this should work
4519+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
45164520
drop table attbl, atref;
45174521
/* End test case for bug #18970 */
45184522
-- Test that ALTER TABLE rewrite preserves a clustered index

src/test/regress/sql/alter_table.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,10 @@ update bar1 set a = a + 1;
29832983
create table attbl(a int);
29842984
create table atref(b attbl check ((b).a is not null));
29852985
alter table attbl alter column a type numeric; -- someday this should work
2986+
alter table atref drop constraint atref_b_check;
2987+
2988+
create index atref_idx on atref (((b).a));
2989+
alter table attbl alter column a type numeric; -- someday this should work
29862990
drop table attbl, atref;
29872991

29882992
/* End test case for bug #18970 */

0 commit comments

Comments
 (0)