Fix ALTER TABLE .. ADD COLUMN with complex inheritance trees
authorMichael Paquier <michael@paquier.xyz>
Wed, 24 Jan 2024 05:20:08 +0000 (14:20 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 24 Jan 2024 05:20:08 +0000 (14:20 +0900)
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

src/backend/commands/tablecmds.c
src/test/regress/expected/inherit.out
src/test/regress/sql/inherit.sql

index da705ae468bc35df273fea677110965705b692d6..25f3a3d1948eaf24d488b15b90180d9fc93da646 100644 (file)
@@ -6866,6 +6866,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;
        }
    }
index 0aa0d410a122c2c0114d5bf635b2a7428f3c8a5a..4943429e9bf46f5bcc647cc28c729a0038868642 100644 (file)
@@ -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);
index e15f9c364c2d23bcb1d4cc98893373ba3d307c52..fe699c54d5666171cb5215cf8e2f3eb522ec1f4b 100644 (file)
@@ -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);