]> granicus.if.org Git - postgresql/commitdiff
ALTER <thing> [IF EXISTS] ... allows silent DDL if required,
authorSimon Riggs <simon@2ndQuadrant.com>
Mon, 23 Jan 2012 23:25:04 +0000 (23:25 +0000)
committerSimon Riggs <simon@2ndQuadrant.com>
Mon, 23 Jan 2012 23:25:04 +0000 (23:25 +0000)
e.g. ALTER FOREIGN TABLE IF EXISTS foo RENAME TO bar

Pavel Stehule

19 files changed:
doc/src/sgml/ref/alter_foreign_table.sgml
doc/src/sgml/ref/alter_index.sgml
doc/src/sgml/ref/alter_sequence.sgml
doc/src/sgml/ref/alter_table.sgml
doc/src/sgml/ref/alter_view.sgml
src/backend/commands/sequence.c
src/backend/commands/tablecmds.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/parser/gram.y
src/backend/parser/parse_utilcmd.c
src/backend/tcop/utility.c
src/include/nodes/parsenodes.h
src/test/regress/expected/alter_table.out
src/test/regress/expected/foreign_data.out
src/test/regress/expected/sequence.out
src/test/regress/sql/alter_table.sql
src/test/regress/sql/foreign_data.sql
src/test/regress/sql/sequence.sql

index 99e8e906d28661c052739226758e4ab1c54b4787..455527c5bfa46c0ffc155a570b5cc28827d944d8 100644 (file)
@@ -21,13 +21,13 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER FOREIGN TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     <replaceable class="PARAMETER">action</replaceable> [, ... ]
-ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER FOREIGN TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
-ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER FOREIGN TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
-ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER FOREIGN TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
 
 <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
@@ -75,6 +75,16 @@ ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
     </listitem>
    </varlistentry>
 
+   <varlistentry>
+    <term><literal>IF EXISTS</literal></term>
+    <listitem>
+     <para>
+      Do not throw an error if the sequence does not exist. A notice is issued
+      in this case.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>SET DATA TYPE</literal></term>
     <listitem>
index c701d165b838637978cfd8e4efd73d636378787e..d2100770361365c70949b568d9194fc9f900bbf8 100644 (file)
@@ -21,10 +21,10 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
-ALTER INDEX <replaceable class="PARAMETER">name</replaceable> SET TABLESPACE <replaceable class="PARAMETER">tablespace_name</replaceable>
-ALTER INDEX <replaceable class="PARAMETER">name</replaceable> SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
-ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
+ALTER INDEX [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
+ALTER INDEX [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> SET TABLESPACE <replaceable class="PARAMETER">tablespace_name</replaceable>
+ALTER INDEX [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
+ALTER INDEX [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable> RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
 </synopsis>
  </refsynopsisdiv>
 
@@ -37,6 +37,16 @@ ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RESET ( <replaceab
 
   <variablelist>
 
+   <varlistentry>
+    <term><literal>IF EXISTS</literal></term>
+    <listitem>
+     <para>
+      Do not throw an error if the index does not exist. A notice is issued
+      in this case.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>RENAME</literal></term>
     <listitem>
index 79795d9db093ba54313843782a1e75c8b4217fd4..6ddd461fe93f2734302812bce0ceebb2999d82c3 100644 (file)
@@ -23,15 +23,15 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-ALTER SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
+ALTER SEQUENCE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
     [ MINVALUE <replaceable class="parameter">minvalue</replaceable> | NO MINVALUE ] [ MAXVALUE <replaceable class="parameter">maxvalue</replaceable> | NO MAXVALUE ]
     [ START [ WITH ] <replaceable class="parameter">start</replaceable> ]
     [ RESTART [ [ WITH ] <replaceable class="parameter">restart</replaceable> ] ]
     [ CACHE <replaceable class="parameter">cache</replaceable> ] [ [ NO ] CYCLE ]
     [ OWNED BY { <replaceable class="parameter">table</replaceable>.<replaceable class="parameter">column</replaceable> | NONE } ]
-ALTER SEQUENCE <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
-ALTER SEQUENCE <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
-ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
+ALTER SEQUENCE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
+ALTER SEQUENCE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
+ALTER SEQUENCE [ IF EXISTS ] <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
 </synopsis>
  </refsynopsisdiv>
 
@@ -70,6 +70,16 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><literal>IF EXISTS</literal></term>
+      <listitem>
+       <para>
+        Do not throw an error if the sequence does not exist. A notice is issued
+        in this case.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><replaceable class="parameter">increment</replaceable></term>
       <listitem>
index 6f1917f5c47042d933102d904f10011c22952461..951b63b5b4fa41efbd2594d510a27e907040a042 100644 (file)
@@ -21,13 +21,13 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
+ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
     <replaceable class="PARAMETER">action</replaceable> [, ... ]
-ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
+ALTER TABLE [ IF EXISTS ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
     RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
-ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
-ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
+ALTER TABLE [ IF EXISTS ] <replaceable class="PARAMETER">name</replaceable>
     SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
 
 <phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
@@ -109,6 +109,16 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
     </listitem>
    </varlistentry>
 
+   <varlistentry>
+    <term><literal>IF EXISTS</literal></term>
+    <listitem>
+     <para>
+      Do not throw an error if the table does not exist. A notice is issued
+      in this case.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>SET DATA TYPE</literal></term>
     <listitem>
index e78176bce807438ff3db878afdbcf13c500923f8..c518d936491f0a129ff3fc935c2a3eae2d391143 100644 (file)
@@ -21,13 +21,13 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
-ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
-ALTER VIEW <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
-ALTER VIEW <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
-ALTER VIEW <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
-ALTER VIEW <replaceable class="parameter">name</replaceable> SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )
-ALTER VIEW <replaceable class="parameter">name</replaceable> RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )
+ALTER VIEW [ IF EXISTS ] <replaceable class="parameter">name</replaceable> RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )
 </synopsis>
  </refsynopsisdiv>
 
@@ -65,6 +65,16 @@ ALTER VIEW <replaceable class="parameter">name</replaceable> RESET ( <replaceabl
     </listitem>
    </varlistentry>
 
+   <varlistentry>
+    <term><literal>IF EXISTS</literal></term>
+    <listitem>
+     <para>
+      Do not throw an error if the view does not exist. A notice is issued
+      in this case.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
     <listitem>
index f7712a96556fa8b2f6c5cc3550fe00ef0b98afab..d3739cb0952fe3c09e7e36c70bd0d9289cc4c999 100644 (file)
@@ -425,7 +425,15 @@ AlterSequence(AlterSeqStmt *stmt)
        List       *owned_by;
 
        /* Open and lock sequence. */
-       relid = RangeVarGetRelid(stmt->sequence, AccessShareLock, false);
+       relid = RangeVarGetRelid(stmt->sequence, AccessShareLock, stmt->missing_ok);
+       if (relid == InvalidOid)
+       {
+               ereport(NOTICE,
+                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       stmt->sequence->relname)));
+               return;
+       }
+
        init_sequence(relid, &elm, &seqrel);
 
        /* allow ALTER to sequence owner only */
index cc210f06d3037f1fa57e4308d806b30806dac7ba..cb8ac67812ab9ba9c9fab1135f623b82acc62fa6 100644 (file)
@@ -2310,9 +2310,18 @@ renameatt(RenameStmt *stmt)
 
        /* lock level taken here should match renameatt_internal */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        false, false,
+                                                                        stmt->missing_ok, false,
                                                                         RangeVarCallbackForRenameAttribute,
                                                                         NULL);
+
+       if (!OidIsValid(relid))
+       {
+               ereport(NOTICE,
+                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       stmt->relation->relname)));
+               return;
+       }
+
        renameatt_internal(relid,
                                           stmt->subname,       /* old att name */
                                           stmt->newname,       /* new att name */
@@ -2338,10 +2347,18 @@ RenameRelation(RenameStmt *stmt)
         * lock escalation.
         */
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        false, false,
+                                                                        stmt->missing_ok, false,
                                                                         RangeVarCallbackForAlterRelation,
                                                                         (void *) stmt);
 
+       if (!OidIsValid(relid))
+       {
+               ereport(NOTICE,
+                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       stmt->relation->relname)));
+               return;
+       }
+
        /* Do the work */
        RenameRelationInternal(relid, stmt->newname);
 }
@@ -2482,7 +2499,7 @@ CheckTableNotInUse(Relation rel, const char *stmt)
 Oid
 AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode)
 {
-       return RangeVarGetRelidExtended(stmt->relation, lockmode, false, false,
+       return RangeVarGetRelidExtended(stmt->relation, lockmode, stmt->missing_ok, false,
                                                                        RangeVarCallbackForAlterRelation,
                                                                        (void *) stmt);
 }
@@ -9434,9 +9451,18 @@ AlterTableNamespace(AlterObjectSchemaStmt *stmt)
        RangeVar   *newrv;
 
        relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
-                                                                        false, false,
+                                                                        stmt->missing_ok, false,
                                                                         RangeVarCallbackForAlterRelation,
                                                                         (void *) stmt);
+
+       if (!OidIsValid(relid))
+       {
+               ereport(NOTICE,
+                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       stmt->relation->relname)));
+               return;
+       }
+
        rel = relation_open(relid, NoLock);
 
        oldNspOid = RelationGetNamespace(rel);
index 71da0d8564bed4f968be374505673b2e47b444d6..cc3168d906670ba1ca3ea4765fd9de5a8db343f6 100644 (file)
@@ -2540,6 +2540,7 @@ _copyAlterTableStmt(const AlterTableStmt *from)
        COPY_NODE_FIELD(relation);
        COPY_NODE_FIELD(cmds);
        COPY_SCALAR_FIELD(relkind);
+       COPY_SCALAR_FIELD(missing_ok);
 
        return newnode;
 }
@@ -2904,6 +2905,7 @@ _copyRenameStmt(const RenameStmt *from)
        COPY_STRING_FIELD(subname);
        COPY_STRING_FIELD(newname);
        COPY_SCALAR_FIELD(behavior);
+       COPY_SCALAR_FIELD(missing_ok);
 
        return newnode;
 }
@@ -2919,6 +2921,7 @@ _copyAlterObjectSchemaStmt(const AlterObjectSchemaStmt *from)
        COPY_NODE_FIELD(objarg);
        COPY_STRING_FIELD(addname);
        COPY_STRING_FIELD(newschema);
+       COPY_SCALAR_FIELD(missing_ok);
 
        return newnode;
 }
@@ -3222,6 +3225,7 @@ _copyAlterSeqStmt(const AlterSeqStmt *from)
 
        COPY_NODE_FIELD(sequence);
        COPY_NODE_FIELD(options);
+       COPY_SCALAR_FIELD(missing_ok);
 
        return newnode;
 }
index ba949db91f24a875122a3c88536cafa5989a224f..2295195fabc36a51d4687b1412424c702ca14a67 100644 (file)
@@ -1010,6 +1010,7 @@ _equalAlterTableStmt(const AlterTableStmt *a, const AlterTableStmt *b)
        COMPARE_NODE_FIELD(relation);
        COMPARE_NODE_FIELD(cmds);
        COMPARE_SCALAR_FIELD(relkind);
+       COMPARE_SCALAR_FIELD(missing_ok);
 
        return true;
 }
@@ -1310,6 +1311,7 @@ _equalRenameStmt(const RenameStmt *a, const RenameStmt *b)
        COMPARE_STRING_FIELD(subname);
        COMPARE_STRING_FIELD(newname);
        COMPARE_SCALAR_FIELD(behavior);
+       COMPARE_SCALAR_FIELD(missing_ok);
 
        return true;
 }
@@ -1323,6 +1325,7 @@ _equalAlterObjectSchemaStmt(const AlterObjectSchemaStmt *a, const AlterObjectSch
        COMPARE_NODE_FIELD(objarg);
        COMPARE_STRING_FIELD(addname);
        COMPARE_STRING_FIELD(newschema);
+       COMPARE_SCALAR_FIELD(missing_ok);
 
        return true;
 }
@@ -1576,6 +1579,7 @@ _equalAlterSeqStmt(const AlterSeqStmt *a, const AlterSeqStmt *b)
 {
        COMPARE_NODE_FIELD(sequence);
        COMPARE_NODE_FIELD(options);
+       COMPARE_SCALAR_FIELD(missing_ok);
 
        return true;
 }
index 0ec039b9a96be7457e459b58aaf50ec5ff78a28d..62fde67bd942276829baca7c86d1921ce2ed8b6a 100644 (file)
@@ -1629,6 +1629,16 @@ AlterTableStmt:
                                        n->relation = $3;
                                        n->cmds = $4;
                                        n->relkind = OBJECT_TABLE;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+               |       ALTER TABLE IF_P EXISTS relation_expr alter_table_cmds
+                               {
+                                       AlterTableStmt *n = makeNode(AlterTableStmt);
+                                       n->relation = $5;
+                                       n->cmds = $6;
+                                       n->relkind = OBJECT_TABLE;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                |       ALTER INDEX qualified_name alter_table_cmds
@@ -1637,6 +1647,16 @@ AlterTableStmt:
                                        n->relation = $3;
                                        n->cmds = $4;
                                        n->relkind = OBJECT_INDEX;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+               |       ALTER INDEX IF_P EXISTS qualified_name alter_table_cmds
+                               {
+                                       AlterTableStmt *n = makeNode(AlterTableStmt);
+                                       n->relation = $5;
+                                       n->cmds = $6;
+                                       n->relkind = OBJECT_INDEX;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                |       ALTER SEQUENCE qualified_name alter_table_cmds
@@ -1645,6 +1665,16 @@ AlterTableStmt:
                                        n->relation = $3;
                                        n->cmds = $4;
                                        n->relkind = OBJECT_SEQUENCE;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+               |       ALTER SEQUENCE IF_P EXISTS qualified_name alter_table_cmds
+                               {
+                                       AlterTableStmt *n = makeNode(AlterTableStmt);
+                                       n->relation = $5;
+                                       n->cmds = $6;
+                                       n->relkind = OBJECT_SEQUENCE;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                |       ALTER VIEW qualified_name alter_table_cmds
@@ -1653,6 +1683,16 @@ AlterTableStmt:
                                        n->relation = $3;
                                        n->cmds = $4;
                                        n->relkind = OBJECT_VIEW;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+               |       ALTER VIEW IF_P EXISTS qualified_name alter_table_cmds
+                               {
+                                       AlterTableStmt *n = makeNode(AlterTableStmt);
+                                       n->relation = $5;
+                                       n->cmds = $6;
+                                       n->relkind = OBJECT_VIEW;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                ;
@@ -3068,8 +3108,18 @@ AlterSeqStmt:
                                        AlterSeqStmt *n = makeNode(AlterSeqStmt);
                                        n->sequence = $3;
                                        n->options = $4;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
+                       | ALTER SEQUENCE IF_P EXISTS qualified_name SeqOptList
+                               {
+                                       AlterSeqStmt *n = makeNode(AlterSeqStmt);
+                                       n->sequence = $5;
+                                       n->options = $6;
+                                       n->missing_ok = true;
+                                       $$ = (Node *)n;
+                               }
+
                ;
 
 OptSeqOptList: SeqOptList                                                      { $$ = $1; }
@@ -3906,6 +3956,16 @@ AlterForeignTableStmt:
                                        n->relation = $4;
                                        n->cmds = $5;
                                        n->relkind = OBJECT_FOREIGN_TABLE;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER FOREIGN TABLE IF_P EXISTS relation_expr alter_table_cmds
+                               {
+                                       AlterTableStmt *n = makeNode(AlterTableStmt);
+                                       n->relation = $6;
+                                       n->cmds = $7;
+                                       n->relkind = OBJECT_FOREIGN_TABLE;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                ;
@@ -6417,6 +6477,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->object = $3;
                                        n->objarg = $4;
                                        n->newname = $7;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER COLLATION any_name RENAME TO name
@@ -6425,6 +6486,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_COLLATION;
                                        n->object = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER CONVERSION_P any_name RENAME TO name
@@ -6433,6 +6495,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_CONVERSION;
                                        n->object = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER DATABASE database_name RENAME TO database_name
@@ -6441,6 +6504,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_DATABASE;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER DOMAIN_P any_name RENAME TO name
@@ -6449,6 +6513,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_DOMAIN;
                                        n->object = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER FOREIGN DATA_P WRAPPER name RENAME TO name
@@ -6457,6 +6522,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_FDW;
                                        n->subname = $5;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER FUNCTION function_with_argtypes RENAME TO name
@@ -6466,6 +6532,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->object = $3->funcname;
                                        n->objarg = $3->funcargs;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER GROUP_P RoleId RENAME TO RoleId
@@ -6474,6 +6541,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_ROLE;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER opt_procedural LANGUAGE name RENAME TO name
@@ -6482,6 +6550,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_LANGUAGE;
                                        n->subname = $4;
                                        n->newname = $7;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
@@ -6491,6 +6560,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->object = $4;
                                        n->subname = $6;
                                        n->newname = $9;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
@@ -6500,6 +6570,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->object = $4;
                                        n->subname = $6;
                                        n->newname = $9;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER SCHEMA name RENAME TO name
@@ -6508,6 +6579,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_SCHEMA;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER SERVER name RENAME TO name
@@ -6516,6 +6588,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_FOREIGN_SERVER;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TABLE relation_expr RENAME TO name
@@ -6525,6 +6598,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $3;
                                        n->subname = NULL;
                                        n->newname = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER TABLE IF_P EXISTS relation_expr RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_TABLE;
+                                       n->relation = $5;
+                                       n->subname = NULL;
+                                       n->newname = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER SEQUENCE qualified_name RENAME TO name
@@ -6534,6 +6618,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $3;
                                        n->subname = NULL;
                                        n->newname = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER SEQUENCE IF_P EXISTS qualified_name RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_SEQUENCE;
+                                       n->relation = $5;
+                                       n->subname = NULL;
+                                       n->newname = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER VIEW qualified_name RENAME TO name
@@ -6543,6 +6638,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $3;
                                        n->subname = NULL;
                                        n->newname = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER VIEW IF_P EXISTS qualified_name RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_VIEW;
+                                       n->relation = $5;
+                                       n->subname = NULL;
+                                       n->newname = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER INDEX qualified_name RENAME TO name
@@ -6552,6 +6658,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $3;
                                        n->subname = NULL;
                                        n->newname = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER INDEX IF_P EXISTS qualified_name RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_INDEX;
+                                       n->relation = $5;
+                                       n->subname = NULL;
+                                       n->newname = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER FOREIGN TABLE relation_expr RENAME TO name
@@ -6561,6 +6678,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $4;
                                        n->subname = NULL;
                                        n->newname = $7;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_FOREIGN_TABLE;
+                                       n->relation = $6;
+                                       n->subname = NULL;
+                                       n->newname = $9;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER TABLE relation_expr RENAME opt_column name TO name
@@ -6571,6 +6699,18 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $3;
                                        n->subname = $6;
                                        n->newname = $8;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_COLUMN;
+                                       n->relationType = OBJECT_TABLE;
+                                       n->relation = $5;
+                                       n->subname = $8;
+                                       n->newname = $10;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name
@@ -6581,6 +6721,18 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $4;
                                        n->subname = $7;
                                        n->newname = $9;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER FOREIGN TABLE IF_P EXISTS relation_expr RENAME opt_column name TO name
+                               {
+                                       RenameStmt *n = makeNode(RenameStmt);
+                                       n->renameType = OBJECT_COLUMN;
+                                       n->relationType = OBJECT_FOREIGN_TABLE;
+                                       n->relation = $6;
+                                       n->subname = $9;
+                                       n->newname = $11;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER TRIGGER name ON qualified_name RENAME TO name
@@ -6590,6 +6742,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->relation = $5;
                                        n->subname = $3;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER ROLE RoleId RENAME TO RoleId
@@ -6598,6 +6751,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_ROLE;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER USER RoleId RENAME TO RoleId
@@ -6606,6 +6760,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_ROLE;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TABLESPACE name RENAME TO name
@@ -6614,6 +6769,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TABLESPACE;
                                        n->subname = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TABLESPACE name SET reloptions
@@ -6640,6 +6796,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TSPARSER;
                                        n->object = $5;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
@@ -6648,6 +6805,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TSDICTIONARY;
                                        n->object = $5;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
@@ -6656,6 +6814,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TSTEMPLATE;
                                        n->object = $5;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
@@ -6664,6 +6823,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TSCONFIGURATION;
                                        n->object = $5;
                                        n->newname = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TYPE_P any_name RENAME TO name
@@ -6672,6 +6832,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->renameType = OBJECT_TYPE;
                                        n->object = $3;
                                        n->newname = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name opt_drop_behavior
@@ -6683,6 +6844,7 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
                                        n->subname = $6;
                                        n->newname = $8;
                                        n->behavior = $9;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                ;
@@ -6709,6 +6871,7 @@ AlterObjectSchemaStmt:
                                        n->object = $3;
                                        n->objarg = $4;
                                        n->newschema = $7;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER COLLATION any_name SET SCHEMA name
@@ -6717,6 +6880,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_COLLATION;
                                        n->object = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER CONVERSION_P any_name SET SCHEMA name
@@ -6725,6 +6889,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_CONVERSION;
                                        n->object = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER DOMAIN_P any_name SET SCHEMA name
@@ -6733,6 +6898,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_DOMAIN;
                                        n->object = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER EXTENSION any_name SET SCHEMA name
@@ -6741,6 +6907,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_EXTENSION;
                                        n->object = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER FUNCTION function_with_argtypes SET SCHEMA name
@@ -6750,6 +6917,7 @@ AlterObjectSchemaStmt:
                                        n->object = $3->funcname;
                                        n->objarg = $3->funcargs;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER OPERATOR any_operator oper_argtypes SET SCHEMA name
@@ -6759,6 +6927,7 @@ AlterObjectSchemaStmt:
                                        n->object = $3;
                                        n->objarg = $4;
                                        n->newschema = $7;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER OPERATOR CLASS any_name USING access_method SET SCHEMA name
@@ -6768,6 +6937,7 @@ AlterObjectSchemaStmt:
                                        n->object = $4;
                                        n->addname = $6;
                                        n->newschema = $9;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER OPERATOR FAMILY any_name USING access_method SET SCHEMA name
@@ -6777,6 +6947,7 @@ AlterObjectSchemaStmt:
                                        n->object = $4;
                                        n->addname = $6;
                                        n->newschema = $9;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TABLE relation_expr SET SCHEMA name
@@ -6785,6 +6956,16 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TABLE;
                                        n->relation = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER TABLE IF_P EXISTS relation_expr SET SCHEMA name
+                               {
+                                       AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
+                                       n->objectType = OBJECT_TABLE;
+                                       n->relation = $5;
+                                       n->newschema = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH PARSER any_name SET SCHEMA name
@@ -6793,6 +6974,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TSPARSER;
                                        n->object = $5;
                                        n->newschema = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH DICTIONARY any_name SET SCHEMA name
@@ -6801,6 +6983,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TSDICTIONARY;
                                        n->object = $5;
                                        n->newschema = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH TEMPLATE any_name SET SCHEMA name
@@ -6809,6 +6992,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TSTEMPLATE;
                                        n->object = $5;
                                        n->newschema = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER TEXT_P SEARCH CONFIGURATION any_name SET SCHEMA name
@@ -6817,6 +7001,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TSCONFIGURATION;
                                        n->object = $5;
                                        n->newschema = $8;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                        | ALTER SEQUENCE qualified_name SET SCHEMA name
@@ -6825,6 +7010,16 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_SEQUENCE;
                                        n->relation = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER SEQUENCE IF_P EXISTS qualified_name SET SCHEMA name
+                               {
+                                       AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
+                                       n->objectType = OBJECT_SEQUENCE;
+                                       n->relation = $5;
+                                       n->newschema = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER VIEW qualified_name SET SCHEMA name
@@ -6833,6 +7028,16 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_VIEW;
                                        n->relation = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER VIEW IF_P EXISTS qualified_name SET SCHEMA name
+                               {
+                                       AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
+                                       n->objectType = OBJECT_VIEW;
+                                       n->relation = $5;
+                                       n->newschema = $8;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER FOREIGN TABLE relation_expr SET SCHEMA name
@@ -6841,6 +7046,16 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_FOREIGN_TABLE;
                                        n->relation = $4;
                                        n->newschema = $7;
+                                       n->missing_ok = false;
+                                       $$ = (Node *)n;
+                               }
+                       | ALTER FOREIGN TABLE IF_P EXISTS relation_expr SET SCHEMA name
+                               {
+                                       AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
+                                       n->objectType = OBJECT_FOREIGN_TABLE;
+                                       n->relation = $6;
+                                       n->newschema = $9;
+                                       n->missing_ok = true;
                                        $$ = (Node *)n;
                                }
                        | ALTER TYPE_P any_name SET SCHEMA name
@@ -6849,6 +7064,7 @@ AlterObjectSchemaStmt:
                                        n->objectType = OBJECT_TYPE;
                                        n->object = $3;
                                        n->newschema = $6;
+                                       n->missing_ok = false;
                                        $$ = (Node *)n;
                                }
                ;
index 99157c534934a5418d52354e6746814bc71cf0c2..f1a108a9828abded3fda6b609028bf2d73de5328 100644 (file)
@@ -2283,7 +2283,15 @@ transformAlterTableStmt(AlterTableStmt *stmt, const char *queryString)
         * new commands we add after this must not upgrade the lock level
         * requested here.
         */
-       rel = relation_openrv(stmt->relation, lockmode);
+       rel = relation_openrv_extended(stmt->relation, lockmode, stmt->missing_ok);
+       if (rel == NULL)
+       {
+               /* this message is consistent with relation_openrv */
+               ereport(NOTICE,
+                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       stmt->relation->relname)));
+               return NIL;
+       }
 
        /* Set up pstate and CreateStmtContext */
        pstate = make_parsestate(NULL);
index de16a61454b2be548539f7acef09e35faa8d6e82..5b81c0bbedcf324f6832e856c8bc8d15e2f26c24 100644 (file)
@@ -714,34 +714,41 @@ standard_ProcessUtility(Node *parsetree,
                                lockmode = AlterTableGetLockLevel(atstmt->cmds);
                                relid = AlterTableLookupRelation(atstmt, lockmode);
 
-                               /* Run parse analysis ... */
-                               stmts = transformAlterTableStmt(atstmt, queryString);
-
-                               /* ... and do it */
-                               foreach(l, stmts)
+                               if (OidIsValid(relid))
                                {
-                                       Node       *stmt = (Node *) lfirst(l);
+                                       /* Run parse analysis ... */
+                                       stmts = transformAlterTableStmt(atstmt, queryString);
 
-                                       if (IsA(stmt, AlterTableStmt))
-                                       {
-                                               /* Do the table alteration proper */
-                                               AlterTable(relid, lockmode, (AlterTableStmt *) stmt);
-                                       }
-                                       else
+                                       /* ... and do it */
+                                       foreach(l, stmts)
                                        {
-                                               /* Recurse for anything else */
-                                               ProcessUtility(stmt,
-                                                                          queryString,
-                                                                          params,
-                                                                          false,
-                                                                          None_Receiver,
-                                                                          NULL);
-                                       }
+                                               Node       *stmt = (Node *) lfirst(l);
 
-                                       /* Need CCI between commands */
-                                       if (lnext(l) != NULL)
-                                               CommandCounterIncrement();
+                                               if (IsA(stmt, AlterTableStmt))
+                                               {
+                                                       /* Do the table alteration proper */
+                                                       AlterTable(relid, lockmode, (AlterTableStmt *) stmt);
+                                               }
+                                               else
+                                               {
+                                                       /* Recurse for anything else */
+                                                       ProcessUtility(stmt,
+                                                                                  queryString,
+                                                                                  params,
+                                                                                  false,
+                                                                                  None_Receiver,
+                                                                                  NULL);
+                                               }
+
+                                               /* Need CCI between commands */
+                                               if (lnext(l) != NULL)
+                                                       CommandCounterIncrement();
+                                       }
                                }
+                               else
+                                       ereport(NOTICE,
+                                               (errmsg("relation \"%s\" does not exist, skipping",
+                                                       atstmt->relation->relname)));
                        }
                        break;
 
index dce0e7270d1b2e2a32a0564aeca8e3fb7973f019..1d33cebc9b8ce371c87a0045ffc1b6df158b3490 100644 (file)
@@ -1171,6 +1171,7 @@ typedef struct AlterTableStmt
        RangeVar   *relation;           /* table to work on */
        List       *cmds;                       /* list of subcommands */
        ObjectType      relkind;                /* type of object */
+       bool       missing_ok;          /* skip error if table missing */
 } AlterTableStmt;
 
 typedef enum AlterTableType
@@ -1807,6 +1808,7 @@ typedef struct AlterSeqStmt
        NodeTag         type;
        RangeVar   *sequence;           /* the sequence to alter */
        List       *options;
+       bool            missing_ok;             /* skip error if a role is missing? */
 } AlterSeqStmt;
 
 /* ----------------------
@@ -2117,6 +2119,7 @@ typedef struct RenameStmt
                                                                 * trigger, etc) */
        char       *newname;            /* the new name */
        DropBehavior behavior;          /* RESTRICT or CASCADE behavior */
+       bool            missing_ok;     /* skip error if missing? */
 } RenameStmt;
 
 /* ----------------------
@@ -2132,6 +2135,7 @@ typedef struct AlterObjectSchemaStmt
        List       *objarg;                     /* argument types, if applicable */
        char       *addname;            /* additional name if needed */
        char       *newschema;          /* the new schema */
+       bool            missing_ok;     /* skip error if missing? */
 } AlterObjectSchemaStmt;
 
 /* ----------------------
index 57096f230ebadac358a9d5058ff452b04d8b4431..e99254950426b6edea30ae125b1f5ba8e538b79e 100644 (file)
@@ -128,6 +128,10 @@ DROP TABLE tmp_new;
 DROP TABLE tmp_new2;
 -- ALTER TABLE ... RENAME on non-table relations
 -- renaming indexes (FIXME: this should probably test the index's functionality)
+ALTER INDEX IF EXISTS __onek_unique1 RENAME TO tmp_onek_unique1;
+NOTICE:  relation "__onek_unique1" does not exist, skipping
+ALTER INDEX IF EXISTS __tmp_onek_unique1 RENAME TO onek_unique1;
+NOTICE:  relation "__tmp_onek_unique1" does not exist, skipping
 ALTER INDEX onek_unique1 RENAME TO tmp_onek_unique1;
 ALTER INDEX tmp_onek_unique1 RENAME TO onek_unique1;
 -- renaming views
@@ -1185,6 +1189,11 @@ ERROR:  inherited column "a" must be renamed in child tables too
 -- these should work
 alter table renameColumn rename column a to d;
 alter table renameColumnChild rename column b to a;
+-- these should work
+alter table if exists doesnt_exist_tab rename column a to d;
+NOTICE:  relation "doesnt_exist_tab" does not exist, skipping
+alter table if exists doesnt_exist_tab rename column b to a;
+NOTICE:  relation "doesnt_exist_tab" does not exist, skipping
 -- this should work
 alter table renameColumn add column w int;
 -- this should fail
@@ -2130,3 +2139,40 @@ ERROR:  new row for relation "test_drop_constr_child" violates check constraint
 DETAIL:  Failing row contains (null).
 DROP TABLE test_drop_constr_parent CASCADE;
 NOTICE:  drop cascades to table test_drop_constr_child
+--
+-- IF EXISTS test
+--
+ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+NOTICE:  relation "tt8" does not exist, skipping
+ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
+NOTICE:  relation "tt8" does not exist, skipping
+ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
+NOTICE:  relation "tt8" does not exist, skipping
+ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
+NOTICE:  relation "tt8" does not exist, skipping
+ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
+NOTICE:  relation "tt8" does not exist, skipping
+ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
+NOTICE:  relation "tt8" does not exist, skipping
+CREATE TABLE tt8(a int);
+CREATE SCHEMA alter2;
+ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
+NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "xxx" for table "tt8"
+ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
+ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
+ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
+ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
+\d alter2.tt8
+          Table "alter2.tt8"
+ Column |  Type   |     Modifiers      
+--------+---------+--------------------
+ a      | integer | 
+ f1     | integer | not null default 0
+Indexes:
+    "xxx" PRIMARY KEY, btree (f1)
+Check constraints:
+    "tt8_f_check" CHECK (f1 >= 0 AND f1 <= 10)
+
+DROP TABLE alter2.tt8;
+DROP SCHEMA alter2;
index 2a6e8766b81c1c3cef7b3fa2ab71916b87249f29..c12e0d0b8d3a6feb71bffa1e40c5c4edd7b2a98b 100644 (file)
@@ -789,6 +789,50 @@ ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
 Server: s0
 FDW Options: (quote '~', "be quoted" 'value', escape '@')
 
+-- alter noexisting table
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c4 integer;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c6 integer;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c7 integer NOT NULL;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c8 integer;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c9 integer;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c10 integer OPTIONS (p1 'v1');
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c6 SET NOT NULL;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c7 DROP NOT NULL;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 TYPE char(10);
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 SET DATA TYPE text;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c7 OPTIONS (ADD p1 'v1', ADD p2 'v2'),
+                        ALTER COLUMN c8 OPTIONS (ADD p1 'v1', ADD p2 'v2');
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 OPTIONS (SET p2 'V2', DROP p1);
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP CONSTRAINT IF EXISTS no_const;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP CONSTRAINT ft1_c1_check;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 OWNER TO regress_test_role;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 OPTIONS (DROP delimiter, SET quote '~', ADD escape '@');
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP COLUMN IF EXISTS no_column;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP COLUMN c9;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 SET SCHEMA foreign_schema;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 RENAME c1 TO foreign_column_1;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 RENAME TO foreign_table_1;
+NOTICE:  relation "doesnt_exist_ft1" does not exist, skipping
 -- Information schema
 SELECT * FROM information_schema.foreign_data_wrappers ORDER BY 1, 2;
  foreign_data_wrapper_catalog | foreign_data_wrapper_name | authorization_identifier | library_name | foreign_data_wrapper_language 
index a110a2eaa1229743e87d4baabb2d3693cdb5f2ed..1def070903ba297fec0030a83d522065346bb2ef 100644 (file)
@@ -241,6 +241,9 @@ DROP SEQUENCE myseq2;
 --
 -- Alter sequence
 --
+ALTER SEQUENCE IF EXISTS sequence_test2 RESTART WITH 24
+        INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
+NOTICE:  relation "sequence_test2" does not exist, skipping
 CREATE SEQUENCE sequence_test2 START WITH 32;
 SELECT nextval('sequence_test2');
  nextval 
index faafb224ef43f62e5e61fcfafe1f2af24ffbe04b..d9bf08d6d5067ae2fd1321c103f52214a23e082f 100644 (file)
@@ -166,6 +166,9 @@ DROP TABLE tmp_new2;
 
 -- ALTER TABLE ... RENAME on non-table relations
 -- renaming indexes (FIXME: this should probably test the index's functionality)
+ALTER INDEX IF EXISTS __onek_unique1 RENAME TO tmp_onek_unique1;
+ALTER INDEX IF EXISTS __tmp_onek_unique1 RENAME TO onek_unique1;
+
 ALTER INDEX onek_unique1 RENAME TO tmp_onek_unique1;
 ALTER INDEX tmp_onek_unique1 RENAME TO onek_unique1;
 -- renaming views
@@ -898,6 +901,10 @@ alter table only renameColumn rename column a to d;
 alter table renameColumn rename column a to d;
 alter table renameColumnChild rename column b to a;
 
+-- these should work
+alter table if exists doesnt_exist_tab rename column a to d;
+alter table if exists doesnt_exist_tab rename column b to a;
+
 -- this should work
 alter table renameColumn add column w int;
 
@@ -1463,3 +1470,28 @@ ALTER TABLE ONLY test_drop_constr_parent DROP CONSTRAINT "test_drop_constr_paren
 -- should fail
 INSERT INTO test_drop_constr_child (c) VALUES (NULL);
 DROP TABLE test_drop_constr_parent CASCADE;
+
+--
+-- IF EXISTS test
+--
+ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
+ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
+ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
+ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
+ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
+
+CREATE TABLE tt8(a int);
+CREATE SCHEMA alter2;
+
+ALTER TABLE IF EXISTS tt8 ADD COLUMN f int;
+ALTER TABLE IF EXISTS tt8 ADD CONSTRAINT xxx PRIMARY KEY(f);
+ALTER TABLE IF EXISTS tt8 ADD CHECK (f BETWEEN 0 AND 10);
+ALTER TABLE IF EXISTS tt8 ALTER COLUMN f SET DEFAULT 0;
+ALTER TABLE IF EXISTS tt8 RENAME COLUMN f TO f1;
+ALTER TABLE IF EXISTS tt8 SET SCHEMA alter2;
+
+\d alter2.tt8
+
+DROP TABLE alter2.tt8;
+DROP SCHEMA alter2;
index 95d02d1ce129a13c4104413e3a1307438b3ac112..b41a7e4a3068e755e2fb888ff9763aca7db21b52 100644 (file)
@@ -328,6 +328,32 @@ ALTER FOREIGN TABLE foreign_schema.ft1 RENAME c1 TO foreign_column_1;
 ALTER FOREIGN TABLE foreign_schema.ft1 RENAME TO foreign_table_1;
 \d foreign_schema.foreign_table_1
 
+-- alter noexisting table
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c4 integer;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c6 integer;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c7 integer NOT NULL;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c8 integer;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c9 integer;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ADD COLUMN c10 integer OPTIONS (p1 'v1');
+
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c6 SET NOT NULL;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c7 DROP NOT NULL;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 TYPE char(10);
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 SET DATA TYPE text;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c7 OPTIONS (ADD p1 'v1', ADD p2 'v2'),
+                        ALTER COLUMN c8 OPTIONS (ADD p1 'v1', ADD p2 'v2');
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 ALTER COLUMN c8 OPTIONS (SET p2 'V2', DROP p1);
+
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP CONSTRAINT IF EXISTS no_const;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP CONSTRAINT ft1_c1_check;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 OWNER TO regress_test_role;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 OPTIONS (DROP delimiter, SET quote '~', ADD escape '@');
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP COLUMN IF EXISTS no_column;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 DROP COLUMN c9;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 SET SCHEMA foreign_schema;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 RENAME c1 TO foreign_column_1;
+ALTER FOREIGN TABLE IF EXISTS doesnt_exist_ft1 RENAME TO foreign_table_1;
+
 -- Information schema
 
 SELECT * FROM information_schema.foreign_data_wrappers ORDER BY 1, 2;
index 97ffb60385ce1e5404638f86edf9cb1d225e8b0b..a32e0494a026eea6a5f87ae290a9eac0ed89771f 100644 (file)
@@ -113,6 +113,10 @@ DROP SEQUENCE myseq2;
 --
 -- Alter sequence
 --
+
+ALTER SEQUENCE IF EXISTS sequence_test2 RESTART WITH 24
+        INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
+
 CREATE SEQUENCE sequence_test2 START WITH 32;
 
 SELECT nextval('sequence_test2');