Skip to content

Commit bb812ab

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 bcd5b4b commit bb812ab

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
@@ -7077,6 +7077,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
70777077
colDef->colname, RelationGetRelationName(rel))));
70787078

70797079
table_close(attrdesc, RowExclusiveLock);
7080+
7081+
/* Make the child column change visible */
7082+
CommandCounterIncrement();
7083+
70807084
return InvalidObjectAddress;
70817085
}
70827086
}

src/test/regress/expected/inherit.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,23 @@ Inherits: inht1,
10881088
inhs1
10891089

10901090
DROP TABLE inhts;
1091+
-- Test for adding a column to a parent table with complex inheritance
1092+
CREATE TABLE inhta ();
1093+
CREATE TABLE inhtb () INHERITS (inhta);
1094+
CREATE TABLE inhtc () INHERITS (inhtb);
1095+
CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc);
1096+
ALTER TABLE inhta ADD COLUMN i int;
1097+
NOTICE: merging definition of column "i" for child "inhtd"
1098+
NOTICE: merging definition of column "i" for child "inhtd"
1099+
\d+ inhta
1100+
Table "public.inhta"
1101+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
1102+
--------+---------+-----------+----------+---------+---------+--------------+-------------
1103+
i | integer | | | | plain | |
1104+
Child tables: inhtb,
1105+
inhtd
1106+
1107+
DROP TABLE inhta, inhtb, inhtc, inhtd;
10911108
-- Test for renaming in diamond inheritance
10921109
CREATE TABLE inht2 (x int) INHERITS (inht1);
10931110
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
@@ -372,6 +372,15 @@ ALTER TABLE inhts RENAME d TO dd;
372372

373373
DROP TABLE inhts;
374374

375+
-- Test for adding a column to a parent table with complex inheritance
376+
CREATE TABLE inhta ();
377+
CREATE TABLE inhtb () INHERITS (inhta);
378+
CREATE TABLE inhtc () INHERITS (inhtb);
379+
CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc);
380+
ALTER TABLE inhta ADD COLUMN i int;
381+
\d+ inhta
382+
DROP TABLE inhta, inhtb, inhtc, inhtd;
383+
375384
-- Test for renaming in diamond inheritance
376385
CREATE TABLE inht2 (x int) INHERITS (inht1);
377386
CREATE TABLE inht3 (y int) INHERITS (inht1);

0 commit comments

Comments
 (0)