Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YZ8b1-0003YK-GV for pgsql-bugs@arkaria.postgresql.org; Sat, 21 Mar 2015 01:53:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YZ8b0-0003qo-KE for pgsql-bugs@arkaria.postgresql.org; Sat, 21 Mar 2015 01:53:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YZ8az-0003qh-GD for pgsql-bugs@postgresql.org; Sat, 21 Mar 2015 01:53:01 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YZ8av-00042Q-TQ for pgsql-bugs@postgresql.org; Sat, 21 Mar 2015 01:52:59 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1YZ8as-0006CU-14; Fri, 20 Mar 2015 21:52:54 -0400 Date: Fri, 20 Mar 2015 21:52:54 -0400 From: Bruce Momjian To: Tom Lane Cc: Flavio Henrique Araque Gurgel , pgsql-bugs@postgresql.org, osalm@mirakl.com Subject: Re: ALTER TABLE documentation or parser bug Message-ID: <20150321015254.GC13192@momjian.us> References: <547F462E.8090001@gmail.com> <19734.1417628674@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Y7xTucakfITjPcLV" Content-Disposition: inline In-Reply-To: <19734.1417628674@sss.pgh.pa.us> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --Y7xTucakfITjPcLV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Dec 3, 2014 at 12:44:34PM -0500, Tom Lane wrote: > Flavio Henrique Araque Gurgel writes: > > Hello all > > In the documentation in: > > http://www.postgresql.org/docs/current/static/sql-altertable.html > > > It says that, to rename a constraint, we can use: > > > ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] > > RENAME CONSTRAINT constraint_name TO new_constraint_name > > > Who is wrong, parser or documentation (or myself)? > > Documentation, evidently: a look at the grammar shows that IF EXISTS has > been implemented only for a rather haphazard subset of ALTER ... RENAME > cases, and this isn't one of them. Rather than fixing the docs, I implemented the feature --- patch attached. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --Y7xTucakfITjPcLV Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="alter.diff" diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c new file mode 100644 index 4147457..ad81995 *** a/src/backend/commands/tablecmds.c --- b/src/backend/commands/tablecmds.c *************** RenameConstraint(RenameStmt *stmt) *** 2496,2504 **** { /* lock level taken here should match rename_constraint_internal */ relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock, ! false, false, RangeVarCallbackForRenameAttribute, NULL); } return --- 2496,2511 ---- { /* lock level taken here should match rename_constraint_internal */ relid = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock, ! 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 new file mode 100644 index 82405b9..4dc9d7f *** a/src/backend/parser/gram.y --- b/src/backend/parser/gram.y *************** RenameStmt: ALTER AGGREGATE func_name ag *** 7692,7697 **** --- 7692,7708 ---- 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 --Y7xTucakfITjPcLV Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --Y7xTucakfITjPcLV--