Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb8a91d9

Browse files
ALTER <thing> [IF EXISTS] ... allows silent DDL if required,
e.g. ALTER FOREIGN TABLE IF EXISTS foo RENAME TO barPavel Stehule
1 parent4993a49 commitb8a91d9

File tree

19 files changed

+534
-52
lines changed

19 files changed

+534
-52
lines changed

‎doc/src/sgml/ref/alter_foreign_table.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
24+
ALTER FOREIGN TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
2525
<replaceable class="PARAMETER">action</replaceable> [, ... ]
26-
ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
26+
ALTER FOREIGN TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
2727
RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
28-
ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
28+
ALTER FOREIGN TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
2929
RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
30-
ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
30+
ALTER FOREIGN TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
3131
SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
3232

3333
<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
@@ -75,6 +75,16 @@ ALTER FOREIGN TABLE <replaceable class="PARAMETER">name</replaceable>
7575
</listitem>
7676
</varlistentry>
7777

78+
<varlistentry>
79+
<term><literal>IF EXISTS</literal></term>
80+
<listitem>
81+
<para>
82+
Do not throw an error if the sequence does not exist. A notice is issued
83+
in this case.
84+
</para>
85+
</listitem>
86+
</varlistentry>
87+
7888
<varlistentry>
7989
<term><literal>SET DATA TYPE</literal></term>
8090
<listitem>

‎doc/src/sgml/ref/alter_index.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
25-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> SET TABLESPACE <replaceable class="PARAMETER">tablespace_name</replaceable>
26-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
27-
ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
24+
ALTER INDEX[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable> RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
25+
ALTER INDEX[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable> SET TABLESPACE <replaceable class="PARAMETER">tablespace_name</replaceable>
26+
ALTER INDEX[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable> SET ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] )
27+
ALTER INDEX[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable> RESET ( <replaceable class="PARAMETER">storage_parameter</replaceable> [, ... ] )
2828
</synopsis>
2929
</refsynopsisdiv>
3030

@@ -37,6 +37,16 @@ ALTER INDEX <replaceable class="PARAMETER">name</replaceable> RESET ( <replaceab
3737

3838
<variablelist>
3939

40+
<varlistentry>
41+
<term><literal>IF EXISTS</literal></term>
42+
<listitem>
43+
<para>
44+
Do not throw an error if the index does not exist. A notice is issued
45+
in this case.
46+
</para>
47+
</listitem>
48+
</varlistentry>
49+
4050
<varlistentry>
4151
<term><literal>RENAME</literal></term>
4252
<listitem>

‎doc/src/sgml/ref/alter_sequence.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ PostgreSQL documentation
2323

2424
<refsynopsisdiv>
2525
<synopsis>
26-
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
26+
ALTER SEQUENCE[ IF EXISTS ]<replaceable class="parameter">name</replaceable> [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
2727
[ MINVALUE <replaceable class="parameter">minvalue</replaceable> | NO MINVALUE ] [ MAXVALUE <replaceable class="parameter">maxvalue</replaceable> | NO MAXVALUE ]
2828
[ START [ WITH ] <replaceable class="parameter">start</replaceable> ]
2929
[ RESTART [ [ WITH ] <replaceable class="parameter">restart</replaceable> ] ]
3030
[ CACHE <replaceable class="parameter">cache</replaceable> ] [ [ NO ] CYCLE ]
3131
[ OWNED BY { <replaceable class="parameter">table</replaceable>.<replaceable class="parameter">column</replaceable> | NONE } ]
32-
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
33-
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
34-
ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
32+
ALTER SEQUENCE[ IF EXISTS ]<replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
33+
ALTER SEQUENCE[ IF EXISTS ]<replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
34+
ALTER SEQUENCE[ IF EXISTS ]<replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
3535
</synopsis>
3636
</refsynopsisdiv>
3737

@@ -70,6 +70,16 @@ ALTER SEQUENCE <replaceable class="parameter">name</replaceable> SET SCHEMA <rep
7070
</listitem>
7171
</varlistentry>
7272

73+
<varlistentry>
74+
<term><literal>IF EXISTS</literal></term>
75+
<listitem>
76+
<para>
77+
Do not throw an error if the sequence does not exist. A notice is issued
78+
in this case.
79+
</para>
80+
</listitem>
81+
</varlistentry>
82+
7383
<varlistentry>
7484
<term><replaceable class="parameter">increment</replaceable></term>
7585
<listitem>

‎doc/src/sgml/ref/alter_table.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
24+
ALTER TABLE [IF EXISTS ] [ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
2525
<replaceable class="PARAMETER">action</replaceable> [, ... ]
26-
ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
26+
ALTER TABLE [IF EXISTS ] [ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
2727
RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
28-
ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
28+
ALTER TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
2929
RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
30-
ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
30+
ALTER TABLE[ IF EXISTS ]<replaceable class="PARAMETER">name</replaceable>
3131
SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
3232

3333
<phrase>where <replaceable class="PARAMETER">action</replaceable> is one of:</phrase>
@@ -109,6 +109,16 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
109109
</listitem>
110110
</varlistentry>
111111

112+
<varlistentry>
113+
<term><literal>IF EXISTS</literal></term>
114+
<listitem>
115+
<para>
116+
Do not throw an error if the table does not exist. A notice is issued
117+
in this case.
118+
</para>
119+
</listitem>
120+
</varlistentry>
121+
112122
<varlistentry>
113123
<term><literal>SET DATA TYPE</literal></term>
114124
<listitem>

‎doc/src/sgml/ref/alter_view.sgml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ PostgreSQL documentation
2121

2222
<refsynopsisdiv>
2323
<synopsis>
24-
ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
25-
ALTER VIEW <replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
26-
ALTER VIEW <replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
27-
ALTER VIEW <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
28-
ALTER VIEW <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
29-
ALTER VIEW <replaceable class="parameter">name</replaceable> SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )
30-
ALTER VIEW <replaceable class="parameter">name</replaceable> RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )
24+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
25+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
26+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
27+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
28+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
29+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> SET ( <replaceable class="parameter">view_option_name</replaceable> [= <replaceable class="parameter">view_option_value</replaceable>] [, ... ] )
30+
ALTER VIEW[ IF EXISTS ]<replaceable class="parameter">name</replaceable> RESET ( <replaceable class="parameter">view_option_name</replaceable> [, ... ] )
3131
</synopsis>
3232
</refsynopsisdiv>
3333

@@ -65,6 +65,16 @@ ALTER VIEW <replaceable class="parameter">name</replaceable> RESET ( <replaceabl
6565
</listitem>
6666
</varlistentry>
6767

68+
<varlistentry>
69+
<term><literal>IF EXISTS</literal></term>
70+
<listitem>
71+
<para>
72+
Do not throw an error if the view does not exist. A notice is issued
73+
in this case.
74+
</para>
75+
</listitem>
76+
</varlistentry>
77+
6878
<varlistentry>
6979
<term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
7080
<listitem>

‎src/backend/commands/sequence.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,15 @@ AlterSequence(AlterSeqStmt *stmt)
425425
List*owned_by;
426426

427427
/* Open and lock sequence. */
428-
relid=RangeVarGetRelid(stmt->sequence,AccessShareLock, false);
428+
relid=RangeVarGetRelid(stmt->sequence,AccessShareLock,stmt->missing_ok);
429+
if (relid==InvalidOid)
430+
{
431+
ereport(NOTICE,
432+
(errmsg("relation \"%s\" does not exist, skipping",
433+
stmt->sequence->relname)));
434+
return;
435+
}
436+
429437
init_sequence(relid,&elm,&seqrel);
430438

431439
/* allow ALTER to sequence owner only */

‎src/backend/commands/tablecmds.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2310,9 +2310,18 @@ renameatt(RenameStmt *stmt)
23102310

23112311
/* lock level taken here should match renameatt_internal */
23122312
relid=RangeVarGetRelidExtended(stmt->relation,AccessExclusiveLock,
2313-
false, false,
2313+
stmt->missing_ok, false,
23142314
RangeVarCallbackForRenameAttribute,
23152315
NULL);
2316+
2317+
if (!OidIsValid(relid))
2318+
{
2319+
ereport(NOTICE,
2320+
(errmsg("relation \"%s\" does not exist, skipping",
2321+
stmt->relation->relname)));
2322+
return;
2323+
}
2324+
23162325
renameatt_internal(relid,
23172326
stmt->subname,/* old att name */
23182327
stmt->newname,/* new att name */
@@ -2338,10 +2347,18 @@ RenameRelation(RenameStmt *stmt)
23382347
* lock escalation.
23392348
*/
23402349
relid=RangeVarGetRelidExtended(stmt->relation,AccessExclusiveLock,
2341-
false, false,
2350+
stmt->missing_ok, false,
23422351
RangeVarCallbackForAlterRelation,
23432352
(void*)stmt);
23442353

2354+
if (!OidIsValid(relid))
2355+
{
2356+
ereport(NOTICE,
2357+
(errmsg("relation \"%s\" does not exist, skipping",
2358+
stmt->relation->relname)));
2359+
return;
2360+
}
2361+
23452362
/* Do the work */
23462363
RenameRelationInternal(relid,stmt->newname);
23472364
}
@@ -2482,7 +2499,7 @@ CheckTableNotInUse(Relation rel, const char *stmt)
24822499
Oid
24832500
AlterTableLookupRelation(AlterTableStmt*stmt,LOCKMODElockmode)
24842501
{
2485-
returnRangeVarGetRelidExtended(stmt->relation,lockmode,false, false,
2502+
returnRangeVarGetRelidExtended(stmt->relation,lockmode,stmt->missing_ok, false,
24862503
RangeVarCallbackForAlterRelation,
24872504
(void*)stmt);
24882505
}
@@ -9434,9 +9451,18 @@ AlterTableNamespace(AlterObjectSchemaStmt *stmt)
94349451
RangeVar*newrv;
94359452

94369453
relid=RangeVarGetRelidExtended(stmt->relation,AccessExclusiveLock,
9437-
false, false,
9454+
stmt->missing_ok, false,
94389455
RangeVarCallbackForAlterRelation,
94399456
(void*)stmt);
9457+
9458+
if (!OidIsValid(relid))
9459+
{
9460+
ereport(NOTICE,
9461+
(errmsg("relation \"%s\" does not exist, skipping",
9462+
stmt->relation->relname)));
9463+
return;
9464+
}
9465+
94409466
rel=relation_open(relid,NoLock);
94419467

94429468
oldNspOid=RelationGetNamespace(rel);

‎src/backend/nodes/copyfuncs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,7 @@ _copyAlterTableStmt(const AlterTableStmt *from)
25402540
COPY_NODE_FIELD(relation);
25412541
COPY_NODE_FIELD(cmds);
25422542
COPY_SCALAR_FIELD(relkind);
2543+
COPY_SCALAR_FIELD(missing_ok);
25432544

25442545
returnnewnode;
25452546
}
@@ -2904,6 +2905,7 @@ _copyRenameStmt(const RenameStmt *from)
29042905
COPY_STRING_FIELD(subname);
29052906
COPY_STRING_FIELD(newname);
29062907
COPY_SCALAR_FIELD(behavior);
2908+
COPY_SCALAR_FIELD(missing_ok);
29072909

29082910
returnnewnode;
29092911
}
@@ -2919,6 +2921,7 @@ _copyAlterObjectSchemaStmt(const AlterObjectSchemaStmt *from)
29192921
COPY_NODE_FIELD(objarg);
29202922
COPY_STRING_FIELD(addname);
29212923
COPY_STRING_FIELD(newschema);
2924+
COPY_SCALAR_FIELD(missing_ok);
29222925

29232926
returnnewnode;
29242927
}
@@ -3222,6 +3225,7 @@ _copyAlterSeqStmt(const AlterSeqStmt *from)
32223225

32233226
COPY_NODE_FIELD(sequence);
32243227
COPY_NODE_FIELD(options);
3228+
COPY_SCALAR_FIELD(missing_ok);
32253229

32263230
returnnewnode;
32273231
}

‎src/backend/nodes/equalfuncs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ _equalAlterTableStmt(const AlterTableStmt *a, const AlterTableStmt *b)
10101010
COMPARE_NODE_FIELD(relation);
10111011
COMPARE_NODE_FIELD(cmds);
10121012
COMPARE_SCALAR_FIELD(relkind);
1013+
COMPARE_SCALAR_FIELD(missing_ok);
10131014

10141015
return true;
10151016
}
@@ -1310,6 +1311,7 @@ _equalRenameStmt(const RenameStmt *a, const RenameStmt *b)
13101311
COMPARE_STRING_FIELD(subname);
13111312
COMPARE_STRING_FIELD(newname);
13121313
COMPARE_SCALAR_FIELD(behavior);
1314+
COMPARE_SCALAR_FIELD(missing_ok);
13131315

13141316
return true;
13151317
}
@@ -1323,6 +1325,7 @@ _equalAlterObjectSchemaStmt(const AlterObjectSchemaStmt *a, const AlterObjectSch
13231325
COMPARE_NODE_FIELD(objarg);
13241326
COMPARE_STRING_FIELD(addname);
13251327
COMPARE_STRING_FIELD(newschema);
1328+
COMPARE_SCALAR_FIELD(missing_ok);
13261329

13271330
return true;
13281331
}
@@ -1576,6 +1579,7 @@ _equalAlterSeqStmt(const AlterSeqStmt *a, const AlterSeqStmt *b)
15761579
{
15771580
COMPARE_NODE_FIELD(sequence);
15781581
COMPARE_NODE_FIELD(options);
1582+
COMPARE_SCALAR_FIELD(missing_ok);
15791583

15801584
return true;
15811585
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp