diff options
| author | Michael Paquier <michael@paquier.xyz> | 2024-01-24 14:20:10 +0900 |
|---|---|---|
| committer | Michael Paquier <michael@paquier.xyz> | 2024-01-24 14:20:10 +0900 |
| commit | ad6fbbeeb07aa40dd3a90b417c0820543d8626eb (patch) | |
| tree | 0833519e33de37a02030524e269b213fac87aa0d | |
| parent | 3fd36be52b70b95eea6fe7d296ce871f49eedb50 (diff) | |
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
| -rw-r--r-- | src/backend/commands/tablecmds.c | 4 | ||||
| -rw-r--r-- | src/test/regress/expected/inherit.out | 17 | ||||
| -rw-r--r-- | src/test/regress/sql/inherit.sql | 9 |
3 files changed, 30 insertions, 0 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 962bde5d38a..c487427545b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -6805,6 +6805,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, colDef->colname, RelationGetRelationName(rel)))); table_close(attrdesc, RowExclusiveLock); + + /* Make the child column change visible */ + CommandCounterIncrement(); + return InvalidObjectAddress; } } diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index 61e152213fc..33d9f13f15d 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -1088,6 +1088,23 @@ Inherits: inht1, inhs1 DROP TABLE inhts; +-- Test for adding a column to a parent table with complex inheritance +CREATE TABLE inhta (); +CREATE TABLE inhtb () INHERITS (inhta); +CREATE TABLE inhtc () INHERITS (inhtb); +CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc); +ALTER TABLE inhta ADD COLUMN i int; +NOTICE: merging definition of column "i" for child "inhtd" +NOTICE: merging definition of column "i" for child "inhtd" +\d+ inhta + Table "public.inhta" + Column | Type | Collation | Nullable | Default | Storage | Stats target | Description +--------+---------+-----------+----------+---------+---------+--------------+------------- + i | integer | | | | plain | | +Child tables: inhtb, + inhtd + +DROP TABLE inhta, inhtb, inhtc, inhtd; -- Test for renaming in diamond inheritance CREATE TABLE inht2 (x int) INHERITS (inht1); CREATE TABLE inht3 (y int) INHERITS (inht1); diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 76ea19783aa..bd908343042 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -372,6 +372,15 @@ ALTER TABLE inhts RENAME d TO dd; DROP TABLE inhts; +-- Test for adding a column to a parent table with complex inheritance +CREATE TABLE inhta (); +CREATE TABLE inhtb () INHERITS (inhta); +CREATE TABLE inhtc () INHERITS (inhtb); +CREATE TABLE inhtd () INHERITS (inhta, inhtb, inhtc); +ALTER TABLE inhta ADD COLUMN i int; +\d+ inhta +DROP TABLE inhta, inhtb, inhtc, inhtd; + -- Test for renaming in diamond inheritance CREATE TABLE inht2 (x int) INHERITS (inht1); CREATE TABLE inht3 (y int) INHERITS (inht1); |
