Skip to content

Commit e6dd6e6

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 8a010df commit e6dd6e6

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13533,6 +13533,14 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1353313533
Oid relid;
1353413534

1353513535
relid = IndexGetRelation(oldId, false);
13536+
13537+
/*
13538+
* As above, make sure we have lock on the index's table if it's not
13539+
* the same table.
13540+
*/
13541+
if (relid != tab->relid)
13542+
LockRelationOid(relid, AccessExclusiveLock);
13543+
1353613544
ATPostAlterTypeParse(oldId, relid, InvalidOid,
1353713545
(char *) lfirst(def_item),
1353813546
wqueue, lockmode, tab->rewrite);
@@ -13549,6 +13557,20 @@ ATPostAlterTypeCleanup(List **wqueue, AlteredTableInfo *tab, LOCKMODE lockmode)
1354913557
Oid relid;
1355013558

1355113559
relid = StatisticsGetRelation(oldId, false);
13560+
13561+
/*
13562+
* As above, make sure we have lock on the statistics object's table
13563+
* if it's not the same table. However, we take
13564+
* ShareUpdateExclusiveLock here, aligning with the lock level used in
13565+
* CreateStatistics and RemoveStatisticsById.
13566+
*
13567+
* CAUTION: this should be done after all cases that grab
13568+
* AccessExclusiveLock, else we risk causing deadlock due to needing
13569+
* to promote our table lock.
13570+
*/
13571+
if (relid != tab->relid)
13572+
LockRelationOid(relid, ShareUpdateExclusiveLock);
13573+
1355213574
ATPostAlterTypeParse(oldId, relid, InvalidOid,
1355313575
(char *) lfirst(def_item),
1355413576
wqueue, lockmode, tab->rewrite);

src/test/regress/expected/alter_table.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,6 +4580,14 @@ create table attbl(a int);
45804580
create table atref(b attbl check ((b).a is not null));
45814581
alter table attbl alter column a type numeric; -- someday this should work
45824582
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4583+
alter table atref drop constraint atref_b_check;
4584+
create statistics atref_stat on ((b).a is not null) from atref;
4585+
alter table attbl alter column a type numeric; -- someday this should work
4586+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
4587+
drop statistics atref_stat;
4588+
create index atref_idx on atref (((b).a));
4589+
alter table attbl alter column a type numeric; -- someday this should work
4590+
ERROR: cannot alter table "attbl" because column "atref.b" uses its row type
45834591
drop table attbl, atref;
45844592
/* End test case for bug #18970 */
45854593
-- Test that ALTER TABLE rewrite preserves a clustered index

src/test/regress/sql/alter_table.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,6 +3019,14 @@ drop table attbl, atref;
30193019
create table attbl(a int);
30203020
create table atref(b attbl check ((b).a is not null));
30213021
alter table attbl alter column a type numeric; -- someday this should work
3022+
alter table atref drop constraint atref_b_check;
3023+
3024+
create statistics atref_stat on ((b).a is not null) from atref;
3025+
alter table attbl alter column a type numeric; -- someday this should work
3026+
drop statistics atref_stat;
3027+
3028+
create index atref_idx on atref (((b).a));
3029+
alter table attbl alter column a type numeric; -- someday this should work
30223030
drop table attbl, atref;
30233031

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

0 commit comments

Comments
 (0)