Commit e73131a1 authored by Andrew Dunstan's avatar Andrew Dunstan

DROP IF EXISTS for columns and constraints. Andres Freund.

parent 31d1f233
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.106 2009/05/03 20:45:43 tgl Exp $ $PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.107 2009/07/20 02:42:27 adunstan Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -33,7 +33,7 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable> ...@@ -33,7 +33,7 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
where <replaceable class="PARAMETER">action</replaceable> is one of: where <replaceable class="PARAMETER">action</replaceable> is one of:
ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ] ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ] DROP [ COLUMN ] [ IF EXISTS ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">type</replaceable> [ USING <replaceable class="PARAMETER">expression</replaceable> ] ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ SET DATA ] TYPE <replaceable class="PARAMETER">type</replaceable> [ USING <replaceable class="PARAMETER">expression</replaceable> ]
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
...@@ -41,7 +41,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -41,7 +41,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
ADD <replaceable class="PARAMETER">table_constraint</replaceable> ADD <replaceable class="PARAMETER">table_constraint</replaceable>
DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ] DROP CONSTRAINT [ IF EXISTS ] <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ] DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ] ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable> ENABLE REPLICA TRIGGER <replaceable class="PARAMETER">trigger_name</replaceable>
...@@ -82,7 +82,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -82,7 +82,7 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>DROP COLUMN</literal></term> <term><literal>DROP COLUMN [ IF EXISTS ]</literal></term>
<listitem> <listitem>
<para> <para>
This form drops a column from a table. Indexes and This form drops a column from a table. Indexes and
...@@ -90,6 +90,9 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -90,6 +90,9 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
dropped as well. You will need to say <literal>CASCADE</> if dropped as well. You will need to say <literal>CASCADE</> if
anything outside the table depends on the column, for example, anything outside the table depends on the column, for example,
foreign key references or views. foreign key references or views.
If <literal>IF EXISTS</literal> is specified and the column
does not exist, no error is thrown. In this case a notice
is issued instead.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -192,10 +195,12 @@ where <replaceable class="PARAMETER">action</replaceable> is one of: ...@@ -192,10 +195,12 @@ where <replaceable class="PARAMETER">action</replaceable> is one of:
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term><literal>DROP CONSTRAINT</literal></term> <term><literal>DROP CONSTRAINT [ IF EXISTS ]</literal></term>
<listitem> <listitem>
<para> <para>
This form drops the specified constraint on a table. This form drops the specified constraint on a table.
If <literal>IF EXISTS</literal> is specified and the constraint
does not exist, no error is thrown. In this case a notice is issued instead.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.290 2009/07/16 06:33:42 petere Exp $ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.291 2009/07/20 02:42:27 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -285,7 +285,8 @@ static void ATExecSetStorage(Relation rel, const char *colName, ...@@ -285,7 +285,8 @@ static void ATExecSetStorage(Relation rel, const char *colName,
Node *newValue); Node *newValue);
static void ATExecDropColumn(List **wqueue, Relation rel, const char *colName, static void ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
DropBehavior behavior, DropBehavior behavior,
bool recurse, bool recursing); bool recurse, bool recursing,
bool missing_ok);
static void ATExecAddIndex(AlteredTableInfo *tab, Relation rel, static void ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
IndexStmt *stmt, bool is_rebuild); IndexStmt *stmt, bool is_rebuild);
static void ATExecAddConstraint(List **wqueue, static void ATExecAddConstraint(List **wqueue,
...@@ -298,8 +299,9 @@ static void ATAddCheckConstraint(List **wqueue, ...@@ -298,8 +299,9 @@ static void ATAddCheckConstraint(List **wqueue,
static void ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel, static void ATAddForeignKeyConstraint(AlteredTableInfo *tab, Relation rel,
FkConstraint *fkconstraint); FkConstraint *fkconstraint);
static void ATExecDropConstraint(Relation rel, const char *constrName, static void ATExecDropConstraint(Relation rel, const char *constrName,
DropBehavior behavior, DropBehavior behavior,
bool recurse, bool recursing); bool recurse, bool recursing,
bool missing_ok);
static void ATPrepAlterColumnType(List **wqueue, static void ATPrepAlterColumnType(List **wqueue,
AlteredTableInfo *tab, Relation rel, AlteredTableInfo *tab, Relation rel,
bool recurse, bool recursing, bool recurse, bool recursing,
...@@ -2620,11 +2622,11 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, ...@@ -2620,11 +2622,11 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
break; break;
case AT_DropColumn: /* DROP COLUMN */ case AT_DropColumn: /* DROP COLUMN */
ATExecDropColumn(wqueue, rel, cmd->name, ATExecDropColumn(wqueue, rel, cmd->name,
cmd->behavior, false, false); cmd->behavior, false, false, cmd->missing_ok);
break; break;
case AT_DropColumnRecurse: /* DROP COLUMN with recursion */ case AT_DropColumnRecurse: /* DROP COLUMN with recursion */
ATExecDropColumn(wqueue, rel, cmd->name, ATExecDropColumn(wqueue, rel, cmd->name,
cmd->behavior, true, false); cmd->behavior, true, false, cmd->missing_ok);
break; break;
case AT_AddIndex: /* ADD INDEX */ case AT_AddIndex: /* ADD INDEX */
ATExecAddIndex(tab, rel, (IndexStmt *) cmd->def, false); ATExecAddIndex(tab, rel, (IndexStmt *) cmd->def, false);
...@@ -2639,10 +2641,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel, ...@@ -2639,10 +2641,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, Relation rel,
ATExecAddConstraint(wqueue, tab, rel, cmd->def, true); ATExecAddConstraint(wqueue, tab, rel, cmd->def, true);
break; break;
case AT_DropConstraint: /* DROP CONSTRAINT */ case AT_DropConstraint: /* DROP CONSTRAINT */
ATExecDropConstraint(rel, cmd->name, cmd->behavior, false, false); ATExecDropConstraint(rel, cmd->name, cmd->behavior,
false, false,
cmd->missing_ok);
break; break;
case AT_DropConstraintRecurse: /* DROP CONSTRAINT with recursion */ case AT_DropConstraintRecurse: /* DROP CONSTRAINT with recursion */
ATExecDropConstraint(rel, cmd->name, cmd->behavior, true, false); ATExecDropConstraint(rel, cmd->name, cmd->behavior,
true, false,
cmd->missing_ok);
break; break;
case AT_AlterColumnType: /* ALTER COLUMN TYPE */ case AT_AlterColumnType: /* ALTER COLUMN TYPE */
ATExecAlterColumnType(tab, rel, cmd->name, (TypeName *) cmd->def); ATExecAlterColumnType(tab, rel, cmd->name, (TypeName *) cmd->def);
...@@ -4160,7 +4166,8 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue) ...@@ -4160,7 +4166,8 @@ ATExecSetStorage(Relation rel, const char *colName, Node *newValue)
static void static void
ATExecDropColumn(List **wqueue, Relation rel, const char *colName, ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
DropBehavior behavior, DropBehavior behavior,
bool recurse, bool recursing) bool recurse, bool recursing,
bool missing_ok)
{ {
HeapTuple tuple; HeapTuple tuple;
Form_pg_attribute targetatt; Form_pg_attribute targetatt;
...@@ -4176,11 +4183,21 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName, ...@@ -4176,11 +4183,21 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
* get the number of the attribute * get the number of the attribute
*/ */
tuple = SearchSysCacheAttName(RelationGetRelid(rel), colName); tuple = SearchSysCacheAttName(RelationGetRelid(rel), colName);
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple)){
ereport(ERROR, if (!missing_ok){
(errcode(ERRCODE_UNDEFINED_COLUMN), ereport(ERROR,
errmsg("column \"%s\" of relation \"%s\" does not exist", (errcode(ERRCODE_UNDEFINED_COLUMN),
colName, RelationGetRelationName(rel)))); errmsg("column \"%s\" of relation \"%s\" does not exist",
colName, RelationGetRelationName(rel))));
}
else
{
ereport(NOTICE,
(errmsg("column \"%s\" of relation \"%s\" does not exist, skipping",
colName, RelationGetRelationName(rel))));
return;
}
}
targetatt = (Form_pg_attribute) GETSTRUCT(tuple); targetatt = (Form_pg_attribute) GETSTRUCT(tuple);
attnum = targetatt->attnum; attnum = targetatt->attnum;
...@@ -4246,7 +4263,8 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName, ...@@ -4246,7 +4263,8 @@ ATExecDropColumn(List **wqueue, Relation rel, const char *colName,
{ {
/* Time to delete this child column, too */ /* Time to delete this child column, too */
ATExecDropColumn(wqueue, childrel, colName, ATExecDropColumn(wqueue, childrel, colName,
behavior, true, true); behavior, true, true,
false);
} }
else else
{ {
...@@ -5360,7 +5378,8 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint, ...@@ -5360,7 +5378,8 @@ createForeignKeyTriggers(Relation rel, FkConstraint *fkconstraint,
static void static void
ATExecDropConstraint(Relation rel, const char *constrName, ATExecDropConstraint(Relation rel, const char *constrName,
DropBehavior behavior, DropBehavior behavior,
bool recurse, bool recursing) bool recurse, bool recursing,
bool missing_ok)
{ {
List *children; List *children;
ListCell *child; ListCell *child;
...@@ -5422,12 +5441,22 @@ ATExecDropConstraint(Relation rel, const char *constrName, ...@@ -5422,12 +5441,22 @@ ATExecDropConstraint(Relation rel, const char *constrName,
systable_endscan(scan); systable_endscan(scan);
if (!found) if (!found){
ereport(ERROR, if (!missing_ok){
(errcode(ERRCODE_UNDEFINED_OBJECT), ereport(ERROR,
errmsg("constraint \"%s\" of relation \"%s\" does not exist", (errcode(ERRCODE_UNDEFINED_OBJECT),
constrName, RelationGetRelationName(rel)))); errmsg("constraint \"%s\" of relation \"%s\" does not exist",
constrName, RelationGetRelationName(rel))));
}
else
{
ereport(NOTICE,
(errmsg("constraint \"%s\" of relation \"%s\" does not exist, skipping",
constrName, RelationGetRelationName(rel))));
heap_close(conrel, RowExclusiveLock);
return;
}
}
/* /*
* Propagate to children as appropriate. Unlike most other ALTER * Propagate to children as appropriate. Unlike most other ALTER
* routines, we have to do this one level of recursion at a time; we can't * routines, we have to do this one level of recursion at a time; we can't
...@@ -5490,7 +5519,8 @@ ATExecDropConstraint(Relation rel, const char *constrName, ...@@ -5490,7 +5519,8 @@ ATExecDropConstraint(Relation rel, const char *constrName,
{ {
/* Time to delete this child constraint, too */ /* Time to delete this child constraint, too */
ATExecDropConstraint(childrel, constrName, behavior, ATExecDropConstraint(childrel, constrName, behavior,
true, true); true, true,
false);
} }
else else
{ {
......
...@@ -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.433 2009/07/16 06:33:42 petere Exp $ * $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.434 2009/07/20 02:42:27 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -2272,6 +2272,7 @@ _copyAlterTableCmd(AlterTableCmd *from) ...@@ -2272,6 +2272,7 @@ _copyAlterTableCmd(AlterTableCmd *from)
COPY_NODE_FIELD(def); COPY_NODE_FIELD(def);
COPY_NODE_FIELD(transform); COPY_NODE_FIELD(transform);
COPY_SCALAR_FIELD(behavior); COPY_SCALAR_FIELD(behavior);
COPY_SCALAR_FIELD(missing_ok);
return newnode; return newnode;
} }
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,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.356 2009/07/16 06:33:42 petere Exp $ * $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.357 2009/07/20 02:42:27 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -958,6 +958,7 @@ _equalAlterTableCmd(AlterTableCmd *a, AlterTableCmd *b) ...@@ -958,6 +958,7 @@ _equalAlterTableCmd(AlterTableCmd *a, AlterTableCmd *b)
COMPARE_NODE_FIELD(def); COMPARE_NODE_FIELD(def);
COMPARE_NODE_FIELD(transform); COMPARE_NODE_FIELD(transform);
COMPARE_SCALAR_FIELD(behavior); COMPARE_SCALAR_FIELD(behavior);
COMPARE_SCALAR_FIELD(missing_ok);
return true; return true;
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.670 2009/07/16 06:33:43 petere Exp $ * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.671 2009/07/20 02:42:28 adunstan Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -1610,6 +1610,16 @@ alter_table_cmd: ...@@ -1610,6 +1610,16 @@ alter_table_cmd:
n->def = (Node *) makeString($6); n->def = (Node *) makeString($6);
$$ = (Node *)n; $$ = (Node *)n;
} }
/* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
| DROP opt_column IF_P EXISTS ColId opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_DropColumn;
n->name = $5;
n->behavior = $6;
n->missing_ok = TRUE;
$$ = (Node *)n;
}
/* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */ /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
| DROP opt_column ColId opt_drop_behavior | DROP opt_column ColId opt_drop_behavior
{ {
...@@ -1617,6 +1627,7 @@ alter_table_cmd: ...@@ -1617,6 +1627,7 @@ alter_table_cmd:
n->subtype = AT_DropColumn; n->subtype = AT_DropColumn;
n->name = $3; n->name = $3;
n->behavior = $4; n->behavior = $4;
n->missing_ok = FALSE;
$$ = (Node *)n; $$ = (Node *)n;
} }
/* /*
...@@ -1640,6 +1651,16 @@ alter_table_cmd: ...@@ -1640,6 +1651,16 @@ alter_table_cmd:
n->def = $2; n->def = $2;
$$ = (Node *)n; $$ = (Node *)n;
} }
/* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
| DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
{
AlterTableCmd *n = makeNode(AlterTableCmd);
n->subtype = AT_DropConstraint;
n->name = $5;
n->behavior = $6;
n->missing_ok = TRUE;
$$ = (Node *)n;
}
/* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */ /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
| DROP CONSTRAINT name opt_drop_behavior | DROP CONSTRAINT name opt_drop_behavior
{ {
...@@ -1647,6 +1668,7 @@ alter_table_cmd: ...@@ -1647,6 +1668,7 @@ alter_table_cmd:
n->subtype = AT_DropConstraint; n->subtype = AT_DropConstraint;
n->name = $3; n->name = $3;
n->behavior = $4; n->behavior = $4;
n->missing_ok = FALSE;
$$ = (Node *)n; $$ = (Node *)n;
} }
/* ALTER TABLE <name> SET WITH OIDS */ /* ALTER TABLE <name> SET WITH OIDS */
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, 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.396 2009/07/16 06:33:45 petere Exp $ * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.397 2009/07/20 02:42:28 adunstan Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1145,6 +1145,7 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */ ...@@ -1145,6 +1145,7 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */
* index, constraint, or parent table */ * 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 */
bool missing_ok; /* skip error if missing? */
} AlterTableCmd; } AlterTableCmd;
......
...@@ -1150,6 +1150,12 @@ alter table gc1 drop column name; ...@@ -1150,6 +1150,12 @@ alter table gc1 drop column name;
ERROR: column "name" of relation "gc1" does not exist ERROR: column "name" of relation "gc1" does not exist
-- should work and drop the attribute in all tables -- should work and drop the attribute in all tables
alter table p2 drop column height; alter table p2 drop column height;
-- IF EXISTS test
create table dropColumnExists ();
alter table dropColumnExists drop column non_existing; --fail
ERROR: column "non_existing" of relation "dropcolumnexists" does not exist
alter table dropColumnExists drop column if exists non_existing; --succeed
NOTICE: column "non_existing" of relation "dropcolumnexists" does not exist, skipping
select relname, attname, attinhcount, attislocal select relname, attname, attinhcount, attislocal
from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid) from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
...@@ -1421,6 +1427,10 @@ alter table anothertab alter column atcol1 type boolean ...@@ -1421,6 +1427,10 @@ alter table anothertab alter column atcol1 type boolean
ERROR: operator does not exist: boolean <= integer ERROR: operator does not exist: boolean <= integer
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
alter table anothertab drop constraint anothertab_chk; alter table anothertab drop constraint anothertab_chk;
alter table anothertab drop constraint anothertab_chk; -- fails
ERROR: constraint "anothertab_chk" of relation "anothertab" does not exist
alter table anothertab drop constraint IF EXISTS anothertab_chk; -- succeeds
NOTICE: constraint "anothertab_chk" of relation "anothertab" does not exist, skipping
alter table anothertab alter column atcol1 type boolean alter table anothertab alter column atcol1 type boolean
using case when atcol1 % 2 = 0 then true else false end; using case when atcol1 % 2 = 0 then true else false end;
select * from anothertab; select * from anothertab;
......
...@@ -816,6 +816,8 @@ create table dropColumnAnother (d int) inherits (dropColumnChild); ...@@ -816,6 +816,8 @@ create table dropColumnAnother (d int) inherits (dropColumnChild);
alter table dropColumnchild drop column a; alter table dropColumnchild drop column a;
alter table only dropColumnChild drop column b; alter table only dropColumnChild drop column b;
-- these three should work -- these three should work
alter table only dropColumn drop column e; alter table only dropColumn drop column e;
alter table dropColumnChild drop column c; alter table dropColumnChild drop column c;
...@@ -913,6 +915,11 @@ alter table gc1 drop column name; ...@@ -913,6 +915,11 @@ alter table gc1 drop column name;
-- should work and drop the attribute in all tables -- should work and drop the attribute in all tables
alter table p2 drop column height; alter table p2 drop column height;
-- IF EXISTS test
create table dropColumnExists ();
alter table dropColumnExists drop column non_existing; --fail
alter table dropColumnExists drop column if exists non_existing; --succeed
select relname, attname, attinhcount, attislocal select relname, attname, attinhcount, attislocal
from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid) from pg_class join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped where relname in ('p1','p2','c1','gc1') and attnum > 0 and not attisdropped
...@@ -1057,6 +1064,8 @@ alter table anothertab alter column atcol1 drop default; ...@@ -1057,6 +1064,8 @@ alter table anothertab alter column atcol1 drop default;
alter table anothertab alter column atcol1 type boolean alter table anothertab alter column atcol1 type boolean
using case when atcol1 % 2 = 0 then true else false end; -- fails using case when atcol1 % 2 = 0 then true else false end; -- fails
alter table anothertab drop constraint anothertab_chk; alter table anothertab drop constraint anothertab_chk;
alter table anothertab drop constraint anothertab_chk; -- fails
alter table anothertab drop constraint IF EXISTS anothertab_chk; -- succeeds
alter table anothertab alter column atcol1 type boolean alter table anothertab alter column atcol1 type boolean
using case when atcol1 % 2 = 0 then true else false end; using case when atcol1 % 2 = 0 then true else false end;
......
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