Skip to content

Commit 1d8198b

Browse files
committed
Add support for ALTER TABLE IF EXISTS ... RENAME CONSTRAINT
Also add regression test. Previously this was documented to work, but didn't.
1 parent feeb526 commit 1d8198b

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/backend/commands/tablecmds.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2497,9 +2497,16 @@ RenameConstraint(RenameStmt *stmt)
24972497
{
24982498
/* lock level taken here should match rename_constraint_internal */
24992499
relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock,
2500-
false, false,
2500+
stmt->missing_ok, false,
25012501
RangeVarCallbackForRenameAttribute,
25022502
NULL);
2503+
if (!OidIsValid(relid))
2504+
{
2505+
ereport(NOTICE,
2506+
(errmsg("relation \"%s\" does not exist, skipping",
2507+
stmt->relation->relname)));
2508+
return InvalidObjectAddress;
2509+
}
25032510
}
25042511

25052512
return

src/backend/parser/gram.y

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7706,6 +7706,17 @@ RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
77067706
n->relation = $3;
77077707
n->subname = $6;
77087708
n->newname = $8;
7709+
n->missing_ok = false;
7710+
$$ = (Node *)n;
7711+
}
7712+
| ALTER TABLE IF_P EXISTS relation_expr RENAME CONSTRAINT name TO name
7713+
{
7714+
RenameStmt *n = makeNode(RenameStmt);
7715+
n->renameType = OBJECT_TABCONSTRAINT;
7716+
n->relation = $5;
7717+
n->subname = $8;
7718+
n->newname = $10;
7719+
n->missing_ok = true;
77097720
$$ = (Node *)n;
77107721
}
77117722
| ALTER FOREIGN TABLE relation_expr RENAME opt_column name TO name

src/test/regress/expected/alter_table.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ Inherits: constraint_rename_test
289289

290290
DROP TABLE constraint_rename_test2;
291291
DROP TABLE constraint_rename_test;
292+
ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
293+
NOTICE: relation "constraint_not_exist" does not exist, skipping
292294
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
293295
NOTICE: relation "constraint_rename_test" does not exist, skipping
294296
-- FOREIGN KEY CONSTRAINT adding TEST

src/test/regress/sql/alter_table.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ ALTER TABLE constraint_rename_test RENAME CONSTRAINT con3 TO con3foo; -- ok
228228
\d constraint_rename_test2
229229
DROP TABLE constraint_rename_test2;
230230
DROP TABLE constraint_rename_test;
231+
ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
231232
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
232233

233234
-- FOREIGN KEY CONSTRAINT adding TEST

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy