On Mon, Apr 18, 2011 at 10:44:53PM +0300, Peter Eisentraut wrote:
> On Sat, 2011-04-09 at 21:57 -0400, Noah Misch wrote:
> > * Inheriting from a typed table blocks further type DDL
> >   CREATE TYPE t AS (x int);
> >   CREATE TABLE parent OF t;
> >   CREATE TABLE child () INHERITS (parent);
> >   ALTER TYPE t ADD ATTRIBUTE y int CASCADE;
> >   -- ERROR:  column must be added to child tables too
> > We ought to just set INH_YES on the downstream command in 
> > ATTypedTableRecursion.
> > If we get to that point, the user did choose ALTER TYPE CASCADE; it seems 
> > fair
> > to assume he'd want inheritance recursion rather than a later error.
> 
> Agreed.

Patch attached for that.  Apart from a comment, a test case and a doc update, it
turned out to be a one-liner.
diff --git a/doc/src/sgml/ref/alter_type.sgml b/doc/src/sgml/ref/alter_type.sgml
index e889ffb..4bdf99f 100644
*** a/doc/src/sgml/ref/alter_type.sgml
--- b/doc/src/sgml/ref/alter_type.sgml
***************
*** 122,128 **** ALTER TYPE <replaceable class="PARAMETER">name</replaceable> 
ADD VALUE <replacea
      <listitem>
       <para>
        Automatically propagate the operation to typed tables of the
!       type being altered.
       </para>
      </listitem>
     </varlistentry>
--- 122,128 ----
      <listitem>
       <para>
        Automatically propagate the operation to typed tables of the
!       type being altered and any descendants thereof.
       </para>
      </listitem>
     </varlistentry>
diff --git a/src/backend/commands/tableindex 1f709a4..9bab341 100644
*** a/src/backend/commands/tablecmds.c
--- b/src/backend/commands/tablecmds.c
***************
*** 3858,3864 **** ATSimpleRecursion(List **wqueue, Relation rel,
   * ATTypedTableRecursion
   *
   * Propagate ALTER TYPE operations to the typed tables of that type.
!  * Also check the RESTRICT/CASCADE behavior.
   */
  static void
  ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
--- 3858,3865 ----
   * ATTypedTableRecursion
   *
   * Propagate ALTER TYPE operations to the typed tables of that type.
!  * Also check the RESTRICT/CASCADE behavior.  Given CASCADE, also permit
!  * recursion to inheritance children of the typed tables.
   */
  static void
  ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
***************
*** 3880,3886 **** ATTypedTableRecursion(List **wqueue, Relation rel, 
AlterTableCmd *cmd,
  
                childrel = relation_open(childrelid, lockmode);
                CheckTableNotInUse(childrel, "ALTER TABLE");
!               ATPrepCmd(wqueue, childrel, cmd, false, true, lockmode);
                relation_close(childrel, NoLock);
        }
  }
--- 3881,3887 ----
  
                childrel = relation_open(childrelid, lockmode);
                CheckTableNotInUse(childrel, "ALTER TABLE");
!               ATPrepCmd(wqueue, childrel, cmd, true, true, lockmode);
                relation_close(childrel, NoLock);
        }
  }
diff --git a/src/test/regress/expected/index 5b1223b..6db648d 100644
*** a/src/test/regress/expected/alter_table.out
--- b/src/test/regress/expected/alter_table.out
***************
*** 1845,1850 **** ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- 
fails
--- 1845,1851 ----
  ERROR:  cannot alter type "test_type1" because column "test_tbl1"."y" uses it
  CREATE TYPE test_type2 AS (a int, b text);
  CREATE TABLE test_tbl2 OF test_type2;
+ CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
  \d test_type2
  Composite type "public.test_type2"
   Column |  Type   | Modifiers 
***************
*** 1858,1863 **** Composite type "public.test_type2"
--- 1859,1865 ----
  --------+---------+-----------
   a      | integer | 
   b      | text    | 
+ Number of child tables: 1 (Use \d+ to list them.)
  Typed table of type: test_type2
  
  ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
***************
*** 1879,1884 **** Composite type "public.test_type2"
--- 1881,1887 ----
   a      | integer | 
   b      | text    | 
   c      | text    | 
+ Number of child tables: 1 (Use \d+ to list them.)
  Typed table of type: test_type2
  
  ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
***************
*** 1900,1905 **** ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
--- 1903,1909 ----
   a      | integer           | 
   b      | character varying | 
   c      | text              | 
+ Number of child tables: 1 (Use \d+ to list them.)
  Typed table of type: test_type2
  
  ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
***************
*** 1919,1924 **** Composite type "public.test_type2"
--- 1923,1929 ----
  --------+---------+-----------
   a      | integer | 
   c      | text    | 
+ Number of child tables: 1 (Use \d+ to list them.)
  Typed table of type: test_type2
  
  ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
***************
*** 1938,1944 **** Composite type "public.test_type2"
--- 1943,1958 ----
  --------+---------+-----------
   aa     | integer | 
   c      | text    | 
+ Number of child tables: 1 (Use \d+ to list them.)
  Typed table of type: test_type2
  
+ \d test_tbl2_subclass
+ Table "public.test_tbl2_subclass"
+  Column |  Type   | Modifiers 
+ --------+---------+-----------
+  aa     | integer | 
+  c      | text    | 
+ Inherits: test_tbl2
+ 
  CREATE TYPE test_type_empty AS ();
  DROP TYPE test_type_empty;
diff --git a/src/test/regress/sql/alter_table.sqindex 43a9ce9..7566f30 100644
*** a/src/test/regress/sql/alter_table.sql
--- b/src/test/regress/sql/alter_table.sql
***************
*** 1344,1349 **** ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- 
fails
--- 1344,1350 ----
  
  CREATE TYPE test_type2 AS (a int, b text);
  CREATE TABLE test_tbl2 OF test_type2;
+ CREATE TABLE test_tbl2_subclass () INHERITS (test_tbl2);
  \d test_type2
  \d test_tbl2
  
***************
*** 1366,1371 **** ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
--- 1367,1373 ----
  ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
  \d test_type2
  \d test_tbl2
+ \d test_tbl2_subclass
  
  CREATE TYPE test_type_empty AS ();
  DROP TYPE test_type_empty;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to