Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2f72428

Browse files
committed
Fix ALTER TABLE .. ADD COLUMN with complex inheritance trees
This command, when used to add a column on a parent table with a complex inheritance tree, tried to update multiple times the same tuple in pg_attribute for a child table when incrementing attinhcount, causing failures with "tuple already updated by self" because of a missing CommandCounterIncrement() between two updates. This exists for a rather long time, so backpatch all the way down. Reported-by: Alexander Lakhin Author: Tender Wang Reviewed-by: Richard Guo Discussion: https://postgr.es/m/18297-b04cd83a55b51e35@postgresql.org Backpatch-through: 12
1 parent 8103822 commit 2f72428

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6001,6 +6001,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
60016001
colDef->colname, RelationGetRelationName(rel))));
60026002

60036003
table_close(attrdesc, RowExclusiveLock);
6004+
6005+
/* Make the child column change visible */
6006+
CommandCounterIncrement();
6007+
60046008
return InvalidObjectAddress;
60056009
}
60066010
}

src/test/regress/expected/inherit.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,23 @@ Inherits: inht1,
10631063
inhs1
10641064

10651065
DROP TABLE inhts;
1066+
-- Test for adding a column to a parent table with complex inheritance
1067+
CREATE TABLE inhta ();
1068+
CREATE TABLE inhtb () INHERITS (inhta);
1069+
CREATE TABLE inhtc () INHERITS (inhtb);
1070+
CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc);
1071+
ALTER TABLE inhta ADD COLUMN i int;
1072+
NOTICE: merging definition of column "i" for child "inhtd"
1073+
NOTICE: merging definition of column "i" for child "inhtd"
1074+
\d+ inhta
1075+
Table "public.inhta"
1076+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
1077+
--------+---------+-----------+----------+---------+---------+--------------+-------------
1078+
i | integer | | | | plain | |
1079+
Child tables: inhtb,
1080+
inhtd
1081+
1082+
DROP TABLE inhta, inhtb, inhtc, inhtd;
10661083
-- Test for renaming in diamond inheritance
10671084
CREATE TABLE inht2 (x int) INHERITS (inht1);
10681085
CREATE TABLE inht3 (y int) INHERITS (inht1);

src/test/regress/sql/inherit.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ ALTER TABLE inhts RENAME d TO dd;
353353

354354
DROP TABLE inhts;
355355

356+
-- Test for adding a column to a parent table with complex inheritance
357+
CREATE TABLE inhta ();
358+
CREATE TABLE inhtb () INHERITS (inhta);
359+
CREATE TABLE inhtc () INHERITS (inhtb);
360+
CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc);
361+
ALTER TABLE inhta ADD COLUMN i int;
362+
\d+ inhta
363+
DROP TABLE inhta, inhtb, inhtc, inhtd;
364+
356365
-- Test for renaming in diamond inheritance
357366
CREATE TABLE inht2 (x int) INHERITS (inht1);
358367
CREATE TABLE inht3 (y int) INHERITS (inht1);

0 commit comments

Comments
 (0)