Commit f58eac82 authored by Tom Lane's avatar Tom Lane

Code and docs review for ALTER TABLE INHERIT/NO INHERIT patch.

parent e1fdd226
This diff is collapsed.
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.90 2006/09/16 00:30:16 momjian Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.91 2006/10/13 21:43:18 tgl Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -294,28 +294,22 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -294,28 +294,22 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
<term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term> <term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
<listitem> <listitem>
<para> <para>
This form adds a new parent table to the table. This won't add new This form adds the target table as a new child of the specified parent
columns to the child table, instead all columns of the parent table must table. Subsequently, queries against the parent will include records
already exist in the child table. They must have matching data types, of the target table. To be added as a child, the target table must
already contain all the same columns as the parent (it could have
additional columns, too). The columns must have matching data types,
and if they have <literal>NOT NULL</literal> constraints in the parent and if they have <literal>NOT NULL</literal> constraints in the parent
then they must also have <literal>NOT NULL</literal> constraints in the then they must also have <literal>NOT NULL</literal> constraints in the
child. child.
</para> </para>
<para> <para>
There must also be matching table constraints for all There must also be matching child-table constraints for all
<literal>CHECK</literal> table constraints of the parent. Currently <literal>CHECK</literal> constraints of the parent. Currently
<literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and <literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
<literal>FOREIGN KEY</literal> constraints are ignored however this may <literal>FOREIGN KEY</literal> constraints are not considered, but
change in the future. this may change in the future.
</para>
<para>
The easiest way to create a suitable table is to create a table using
<literal>INHERITS</literal> and then remove it via <literal>NO
INHERIT</literal>. Alternatively create a table using
<literal>LIKE</literal> however note that <literal>LIKE</literal> does
not create the necessary constraints.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -324,7 +318,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -324,7 +318,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
<term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term> <term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
<listitem> <listitem>
<para> <para>
This form removes a parent table from the list of parents of the table. This form removes the target table from the list of children of the
specified parent table.
Queries against the parent table will no longer include records drawn Queries against the parent table will no longer include records drawn
from the target table. from the target table.
</para> </para>
...@@ -392,6 +387,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -392,6 +387,8 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
You must own the table to use <command>ALTER TABLE</>. You must own the table to use <command>ALTER TABLE</>.
To change the schema of a table, you must also have To change the schema of a table, you must also have
<literal>CREATE</literal> privilege on the new schema. <literal>CREATE</literal> privilege on the new schema.
To add the table as a new child of a parent table, you must own the
parent table as well.
To alter the owner, you must also be a direct or indirect member of the new To alter the owner, you must also be a direct or indirect member of the new
owning role, and that role must have <literal>CREATE</literal> privilege on owning role, and that role must have <literal>CREATE</literal> privilege on
the table's schema. (These restrictions enforce that altering the owner the table's schema. (These restrictions enforce that altering the owner
......
This diff is collapsed.
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.351 2006/10/04 00:29:53 momjian Exp $ * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.352 2006/10/13 21:43:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1821,7 +1821,6 @@ _copyAlterTableCmd(AlterTableCmd *from) ...@@ -1821,7 +1821,6 @@ _copyAlterTableCmd(AlterTableCmd *from)
COPY_SCALAR_FIELD(subtype); COPY_SCALAR_FIELD(subtype);
COPY_STRING_FIELD(name); COPY_STRING_FIELD(name);
COPY_NODE_FIELD(parent);
COPY_NODE_FIELD(def); COPY_NODE_FIELD(def);
COPY_NODE_FIELD(transform); COPY_NODE_FIELD(transform);
COPY_SCALAR_FIELD(behavior); COPY_SCALAR_FIELD(behavior);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.285 2006/10/04 00:29:53 momjian Exp $ * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.286 2006/10/13 21:43:18 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -768,7 +768,6 @@ _equalAlterTableCmd(AlterTableCmd *a, AlterTableCmd *b) ...@@ -768,7 +768,6 @@ _equalAlterTableCmd(AlterTableCmd *a, AlterTableCmd *b)
{ {
COMPARE_SCALAR_FIELD(subtype); COMPARE_SCALAR_FIELD(subtype);
COMPARE_STRING_FIELD(name); COMPARE_STRING_FIELD(name);
COMPARE_NODE_FIELD(parent);
COMPARE_NODE_FIELD(def); COMPARE_NODE_FIELD(def);
COMPARE_NODE_FIELD(transform); COMPARE_NODE_FIELD(transform);
COMPARE_SCALAR_FIELD(behavior); COMPARE_SCALAR_FIELD(behavior);
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.566 2006/09/28 20:51:42 tgl Exp $ * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.567 2006/10/13 21:43:19 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -1506,20 +1506,20 @@ alter_table_cmd: ...@@ -1506,20 +1506,20 @@ alter_table_cmd:
n->subtype = AT_DisableTrigUser; n->subtype = AT_DisableTrigUser;
$$ = (Node *)n; $$ = (Node *)n;
} }
/* ALTER TABLE <name> ALTER INHERITS ADD <parent> */ /* ALTER TABLE <name> INHERIT <parent> */
| INHERIT qualified_name | INHERIT qualified_name
{ {
AlterTableCmd *n = makeNode(AlterTableCmd); AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_AddInherits; n->subtype = AT_AddInherit;
n->parent = $2; n->def = (Node *) $2;
$$ = (Node *)n; $$ = (Node *)n;
} }
/* ALTER TABLE <name> alter INHERITS DROP <parent> */ /* ALTER TABLE <name> NO INHERIT <parent> */
| NO INHERIT qualified_name | NO INHERIT qualified_name
{ {
AlterTableCmd *n = makeNode(AlterTableCmd); AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_DropInherits; n->subtype = AT_DropInherit;
n->parent = $3; n->def = (Node *) $3;
$$ = (Node *)n; $$ = (Node *)n;
} }
| alter_rel_cmd | alter_rel_cmd
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.332 2006/10/11 16:42:59 tgl Exp $ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.333 2006/10/13 21:43:19 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -905,8 +905,8 @@ typedef enum AlterTableType ...@@ -905,8 +905,8 @@ typedef enum AlterTableType
AT_DisableTrigAll, /* DISABLE TRIGGER ALL */ AT_DisableTrigAll, /* DISABLE TRIGGER ALL */
AT_EnableTrigUser, /* ENABLE TRIGGER USER */ AT_EnableTrigUser, /* ENABLE TRIGGER USER */
AT_DisableTrigUser, /* DISABLE TRIGGER USER */ AT_DisableTrigUser, /* DISABLE TRIGGER USER */
AT_AddInherits, /* ADD INHERITS parent */ AT_AddInherit, /* INHERIT parent */
AT_DropInherits /* DROP INHERITS parent */ AT_DropInherit /* NO INHERIT parent */
} AlterTableType; } AlterTableType;
typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */
...@@ -915,9 +915,8 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */ ...@@ -915,9 +915,8 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */
AlterTableType subtype; /* Type of table alteration to apply */ AlterTableType subtype; /* Type of table alteration to apply */
char *name; /* column, constraint, or trigger to act on, char *name; /* column, constraint, or trigger to act on,
* or new owner or tablespace */ * or new owner or tablespace */
RangeVar *parent; /* Parent table for add/drop inherits */
Node *def; /* definition of new column, column type, Node *def; /* definition of new column, column type,
* index, or constraint */ * index, constraint, or parent table */
Node *transform; /* transformation expr for ALTER TYPE */ Node *transform; /* transformation expr for ALTER TYPE */
DropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */ DropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */
} AlterTableCmd; } AlterTableCmd;
......
...@@ -324,7 +324,7 @@ select test2 from atacc2; ...@@ -324,7 +324,7 @@ select test2 from atacc2;
-- fail due to missing constraint -- fail due to missing constraint
alter table atacc2 add constraint foo check (test2>0); alter table atacc2 add constraint foo check (test2>0);
alter table atacc3 inherit atacc2; alter table atacc3 inherit atacc2;
ERROR: child table missing constraint matching parent table constraint "foo" ERROR: child table is missing constraint "foo"
-- fail due to missing column -- fail due to missing column
alter table atacc3 rename test2 to testx; alter table atacc3 rename test2 to testx;
alter table atacc3 inherit atacc2; alter table atacc3 inherit atacc2;
...@@ -342,10 +342,10 @@ alter table atacc3 inherit atacc2; ...@@ -342,10 +342,10 @@ alter table atacc3 inherit atacc2;
alter table atacc3 inherit atacc2; alter table atacc3 inherit atacc2;
ERROR: inherited relation "atacc2" duplicated ERROR: inherited relation "atacc2" duplicated
alter table atacc2 inherit atacc3; alter table atacc2 inherit atacc3;
ERROR: circular inheritance structure found ERROR: circular inheritance not allowed
DETAIL: "atacc3" is already a child of "atacc2". DETAIL: "atacc3" is already a child of "atacc2".
alter table atacc2 inherit atacc2; alter table atacc2 inherit atacc2;
ERROR: circular inheritance structure found ERROR: circular inheritance not allowed
DETAIL: "atacc2" is already a child of "atacc2". DETAIL: "atacc2" is already a child of "atacc2".
-- test that we really are a child now (should see 4 not 3 and cascade should go through) -- test that we really are a child now (should see 4 not 3 and cascade should go through)
select test2 from atacc2; select test2 from atacc2;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment