From 1d8198bb44e7e7e3e96d96942f642e9964111628 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 24 Mar 2015 19:52:47 -0400 Subject: [PATCH] Add support for ALTER TABLE IF EXISTS ... RENAME CONSTRAINT Also add regression test. Previously this was documented to work, but didn't. --- src/backend/commands/tablecmds.c | 9 ++++++++- src/backend/parser/gram.y | 11 +++++++++++ src/test/regress/expected/alter_table.out | 2 ++ src/test/regress/sql/alter_table.sql | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index a9f79431e5..32e19c5e4f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2497,9 +2497,16 @@ RenameConstraint(RenameStmt *stmt) { /* lock level taken here should match rename_constraint_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 InvalidObjectAddress; + } } return diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 873ca79492..2c4a5ccece 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -7706,6 +7706,17 @@ 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 CONSTRAINT name TO name + { + RenameStmt *n = makeNode(RenameStmt); + n->renameType = OBJECT_TABCONSTRAINT; + 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 diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 51db1b6792..6a31df7a29 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -289,6 +289,8 @@ Inherits: constraint_rename_test DROP TABLE constraint_rename_test2; DROP TABLE constraint_rename_test; +ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok +NOTICE: relation "constraint_not_exist" does not exist, skipping ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a); NOTICE: relation "constraint_rename_test" does not exist, skipping -- FOREIGN KEY CONSTRAINT adding TEST diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index d0d29ee62b..b5ee7b087d 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -228,6 +228,7 @@ ALTER TABLE constraint_rename_test RENAME CONSTRAINT con3 TO con3foo; -- ok \d constraint_rename_test2 DROP TABLE constraint_rename_test2; DROP TABLE constraint_rename_test; +ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a); -- FOREIGN KEY CONSTRAINT adding TEST -- 2.40.0