Commit f2a42783 authored by Peter Eisentraut's avatar Peter Eisentraut

Propagate ALTER TYPE operations to typed tables

This adds RESTRICT/CASCADE flags to ALTER TYPE ... ADD/DROP/ALTER/
RENAME ATTRIBUTE to control whether to alter typed tables as well.
parent fc946c39
......@@ -26,15 +26,15 @@ PostgreSQL documentation
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> <replaceable class="PARAMETER">action</replaceable> [, ... ]
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> TO <replaceable class="PARAMETER">new_attribute_name</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable> [ CASCADE | RESTRICT ]
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD VALUE <replaceable class="PARAMETER">new_enum_value</replaceable> [ { BEFORE | AFTER } <replaceable class="PARAMETER">existing_enum_value</replaceable> ]
<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
ADD ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable>
DROP ATTRIBUTE [ IF EXISTS ] <replaceable class="PARAMETER">attribute_name</replaceable>
ALTER ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">data_type</replaceable>
ADD ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ CASCADE | RESTRICT ]
DROP ATTRIBUTE [ IF EXISTS ] <replaceable class="PARAMETER">attribute_name</replaceable> [ CASCADE | RESTRICT ]
ALTER ATTRIBUTE <replaceable class="PARAMETER">attribute_name</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">data_type</replaceable> [ CASCADE | RESTRICT ]
</synopsis>
</refsynopsisdiv>
......@@ -116,6 +116,26 @@ ALTER TYPE <replaceable class="PARAMETER">name</replaceable> ADD VALUE <replacea
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>CASCADE</literal></term>
<listitem>
<para>
Automatically propagate the operation to typed tables of the
type being altered.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>RESTRICT</literal></term>
<listitem>
<para>
Refuse the operation if the type being altered is the type of a
typed table. This is the default.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
......
......@@ -125,11 +125,7 @@ ExecRenameStmt(RenameStmt *stmt)
}
case OBJECT_COLUMN:
case OBJECT_ATTRIBUTE:
renameatt(relid,
stmt->subname, /* old att name */
stmt->newname, /* new att name */
interpretInhOption(stmt->relation->inhOpt), /* recursive? */
0); /* expected inhcount */
renameatt(relid, stmt);
break;
case OBJECT_TRIGGER:
renametrig(relid,
......
This diff is collapsed.
......@@ -2803,6 +2803,7 @@ _copyRenameStmt(RenameStmt *from)
COPY_NODE_FIELD(objarg);
COPY_STRING_FIELD(subname);
COPY_STRING_FIELD(newname);
COPY_SCALAR_FIELD(behavior);
return newnode;
}
......
......@@ -1306,6 +1306,7 @@ _equalRenameStmt(RenameStmt *a, RenameStmt *b)
COMPARE_NODE_FIELD(objarg);
COMPARE_STRING_FIELD(subname);
COMPARE_STRING_FIELD(newname);
COMPARE_SCALAR_FIELD(behavior);
return true;
}
......
......@@ -2003,41 +2003,43 @@ alter_type_cmds:
;
alter_type_cmd:
/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> */
ADD_P ATTRIBUTE TableFuncElement
/* ALTER TYPE <name> ADD ATTRIBUTE <coldef> [RESTRICT|CASCADE] */
ADD_P ATTRIBUTE TableFuncElement opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_AddColumn;
n->def = $3;
n->behavior = $4;
$$ = (Node *)n;
}
/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> */
| DROP ATTRIBUTE IF_P EXISTS ColId
/* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> [RESTRICT|CASCADE] */
| DROP ATTRIBUTE IF_P EXISTS ColId opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_DropColumn;
n->name = $5;
n->behavior = DROP_RESTRICT; /* currently no effect */
n->behavior = $6;
n->missing_ok = TRUE;
$$ = (Node *)n;
}
/* ALTER TYPE <name> DROP ATTRIBUTE <attname> */
/* ALTER TYPE <name> DROP ATTRIBUTE <attname> [RESTRICT|CASCADE] */
| DROP ATTRIBUTE ColId opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_DropColumn;
n->name = $3;
n->behavior = DROP_RESTRICT; /* currently no effect */
n->behavior = $4;
n->missing_ok = FALSE;
$$ = (Node *)n;
}
/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> */
| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename
/* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> [RESTRICT|CASCADE] */
| ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_AlterColumnType;
n->name = $3;
n->def = (Node *) $6;
n->behavior = $7;
$$ = (Node *)n;
}
;
......@@ -6005,13 +6007,14 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
n->newname = $6;
$$ = (Node *)n;
}
| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name
| ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
{
RenameStmt *n = makeNode(RenameStmt);
n->renameType = OBJECT_ATTRIBUTE;
n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
n->subname = $6;
n->newname = $8;
n->behavior = $9;
$$ = (Node *)n;
}
;
......
......@@ -42,11 +42,7 @@ extern void CheckTableNotInUse(Relation rel, const char *stmt);
extern void ExecuteTruncate(TruncateStmt *stmt);
extern void renameatt(Oid myrelid,
const char *oldattname,
const char *newattname,
bool recurse,
int expected_parents);
extern void renameatt(Oid myrelid, RenameStmt *stmt);
extern void RenameRelation(Oid myrelid,
const char *newrelname,
......
......@@ -2073,6 +2073,7 @@ typedef struct RenameStmt
char *subname; /* name of contained object (column, rule,
* trigger, etc) */
char *newname; /* the new name */
DropBehavior behavior; /* RESTRICT or CASCADE behavior */
} RenameStmt;
/* ----------------------
......
......@@ -1760,13 +1760,100 @@ ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
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;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
b | text
\d test_tbl2
Table "public.test_tbl2"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
Typed table of type: test_type2
ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
ERROR: cannot alter type "test_type2" because it is the type of a typed table
HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
b | text
c | text
\d test_tbl2
Table "public.test_tbl2"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
b | text |
c | text |
Typed table of type: test_type2
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
ERROR: cannot alter type "test_type2" because it is the type of a typed table
HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+-------------------
a | integer
b | character varying
c | text
\d test_tbl2
Table "public.test_tbl2"
Column | Type | Modifiers
--------+-------------------+-----------
a | integer |
b | character varying |
c | text |
Typed table of type: test_type2
ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
ERROR: cannot alter type "test_type2" because it is the type of a typed table
ALTER TYPE test_type2 RENAME ATTRIBUTE b TO bb; -- fails
HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
a | integer
c | text
\d test_tbl2
Table "public.test_tbl2"
Column | Type | Modifiers
--------+---------+-----------
a | integer |
c | text |
Typed table of type: test_type2
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
ERROR: cannot alter type "test_type2" because it is the type of a typed table
HINT: Use ALTER ... CASCADE to alter the typed tables too.
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
\d test_type2
Composite type "public.test_type2"
Column | Type
--------+---------
aa | integer
c | text
\d test_tbl2
Table "public.test_tbl2"
Column | Type | Modifiers
--------+---------+-----------
aa | integer |
c | text |
Typed table of type: test_type2
CREATE TYPE test_type_empty AS ();
DROP TYPE test_type_empty;
......@@ -1272,10 +1272,28 @@ ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
CREATE TYPE test_type2 AS (a int, b text);
CREATE TABLE test_tbl2 OF test_type2;
\d test_type2
\d test_tbl2
ALTER TYPE test_type2 ADD ATTRIBUTE c text; -- fails
ALTER TYPE test_type2 ADD ATTRIBUTE c text CASCADE;
\d test_type2
\d test_tbl2
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar; -- fails
ALTER TYPE test_type2 ALTER ATTRIBUTE b TYPE varchar CASCADE;
\d test_type2
\d test_tbl2
ALTER TYPE test_type2 DROP ATTRIBUTE b; -- fails
ALTER TYPE test_type2 RENAME ATTRIBUTE b TO bb; -- fails
ALTER TYPE test_type2 DROP ATTRIBUTE b CASCADE;
\d test_type2
\d test_tbl2
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa; -- fails
ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
\d test_type2
\d test_tbl2
CREATE TYPE test_type_empty AS ();
DROP TYPE test_type_empty;
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