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

Commit a0e8df5

Browse files
committed
Allow ALTER TYPE .. ADD ATTRIBUTE .. CASCADE to recurse to descendants.
Without this, adding an attribute to a typed table with an inheritance child fails, which is surprising. Noah Misch, with minor changes by me.
1 parent 8ede427 commit a0e8df5

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

doc/src/sgml/ref/alter_type.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD VALUE <replacea
122122
<listitem>
123123
<para>
124124
Automatically propagate the operation to typed tables of the
125-
type being altered.
125+
type being altered, and their descendants.
126126
</para>
127127
</listitem>
128128
</varlistentry>

src/backend/commands/tablecmds.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -3876,7 +3876,8 @@ ATSimpleRecursion(List **wqueue, Relation rel,
38763876
* ATTypedTableRecursion
38773877
*
38783878
* Propagate ALTER TYPE operations to the typed tables of that type.
3879-
* Also check the RESTRICT/CASCADE behavior.
3879+
* Also check the RESTRICT/CASCADE behavior. Given CASCADE, also permit
3880+
* recursion to inheritance children of the typed tables.
38803881
*/
38813882
static void
38823883
ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
@@ -3898,7 +3899,7 @@ ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
38983899

38993900
childrel = relation_open(childrelid, lockmode);
39003901
CheckTableNotInUse(childrel, "ALTER TABLE");
3901-
ATPrepCmd(wqueue, childrel, cmd, false, true, lockmode);
3902+
ATPrepCmd(wqueue, childrel, cmd, true, true, lockmode);
39023903
relation_close(childrel, NoLock);
39033904
}
39043905
}

src/test/regress/expected/alter_table.out

+15
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,7 @@ ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
18451845
ERROR: cannot alter type "test_type1" because column "test_tbl1"."y" uses it
18461846
CREATE TYPE test_type2 AS (a int, b text);
18471847
CREATE TABLE test_tbl2 OF test_type2;
1848+
CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
18481849
\d test_type2
18491850
Composite type "public.test_type2"
18501851
Column | Type | Modifiers
@@ -1858,6 +1859,7 @@ Composite type "public.test_type2"
18581859
--------+---------+-----------
18591860
a | integer |
18601861
b | text |
1862+
Number of child tables: 1 (Use \d+ to list them.)
18611863
Typed table of type: test_type2
18621864

18631865
ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
@@ -1879,6 +1881,7 @@ Composite type "public.test_type2"
18791881
a | integer |
18801882
b | text |
18811883
c | text |
1884+
Number of child tables: 1 (Use \d+ to list them.)
18821885
Typed table of type: test_type2
18831886

18841887
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
@@ -1900,6 +1903,7 @@ ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
19001903
a | integer |
19011904
b | character varying |
19021905
c | text |
1906+
Number of child tables: 1 (Use \d+ to list them.)
19031907
Typed table of type: test_type2
19041908

19051909
ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
@@ -1919,6 +1923,7 @@ Composite type "public.test_type2"
19191923
--------+---------+-----------
19201924
a | integer |
19211925
c | text |
1926+
Number of child tables: 1 (Use \d+ to list them.)
19221927
Typed table of type: test_type2
19231928

19241929
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
@@ -1938,8 +1943,18 @@ Composite type "public.test_type2"
19381943
--------+---------+-----------
19391944
aa | integer |
19401945
c | text |
1946+
Number of child tables: 1 (Use \d+ to list them.)
19411947
Typed table of type: test_type2
19421948

1949+
\d test_tbl2_subclass
1950+
Table "public.test_tbl2_subclass"
1951+
Column | Type | Modifiers
1952+
--------+---------+-----------
1953+
aa | integer |
1954+
c | text |
1955+
Inherits: test_tbl2
1956+
1957+
DROP TABLE test_tbl2_subclass;
19431958
CREATE TYPE test_type_empty AS ();
19441959
DROP TYPE test_type_empty;
19451960
--

src/test/regress/sql/alter_table.sql

+4
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
13441344

13451345
CREATE TYPE test_type2 AS (a int, b text);
13461346
CREATE TABLE test_tbl2 OF test_type2;
1347+
CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
13471348
\d test_type2
13481349
\d test_tbl2
13491350

@@ -1366,6 +1367,9 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
13661367
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
13671368
\d test_type2
13681369
\d test_tbl2
1370+
\d test_tbl2_subclass
1371+
1372+
DROP TABLE test_tbl2_subclass;
13691373

13701374
CREATE TYPE test_type_empty AS ();
13711375
DROP TYPE test_type_empty;

0 commit comments

Comments
 (0)