Skip to content

Commit 0df7717

Browse files
committed
Fix ALTER INDEX RENAME so that if the index belongs to a unique or primary key
constraint, the constraint is renamed as well. This avoids inconsistent situations that could confuse pg_dump (not to mention humans). We might at some point provide ALTER TABLE RENAME CONSTRAINT as a more general solution, but there seems no reason not to allow doing it this way too. Per bug #3854 and related discussions.
1 parent d07de6c commit 0df7717

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

src/backend/catalog/pg_constraint.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/pg_constraint.c,v 1.37 2008/01/01 19:45:48 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/pg_constraint.c,v 1.38 2008/01/17 18:56:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -25,6 +25,7 @@
2525
#include "utils/array.h"
2626
#include "utils/builtins.h"
2727
#include "utils/fmgroids.h"
28+
#include "utils/lsyscache.h"
2829
#include "utils/syscache.h"
2930

3031

@@ -568,6 +569,67 @@ RemoveConstraintById(Oid conId)
568569
heap_close(conDesc, RowExclusiveLock);
569570
}
570571

572+
/*
573+
* RenameConstraintById
574+
* Rename a constraint.
575+
*
576+
* Note: this isn't intended to be a user-exposed function; it doesn't check
577+
* permissions etc. Currently this is only invoked when renaming an index
578+
* that is associated with a constraint, but it's made a little more general
579+
* than that with the expectation of someday having ALTER TABLE RENAME
580+
* CONSTRAINT.
581+
*/
582+
void
583+
RenameConstraintById(Oid conId, const char *newname)
584+
{
585+
Relation conDesc;
586+
HeapTuple tuple;
587+
Form_pg_constraint con;
588+
589+
conDesc = heap_open(ConstraintRelationId, RowExclusiveLock);
590+
591+
tuple = SearchSysCacheCopy(CONSTROID,
592+
ObjectIdGetDatum(conId),
593+
0, 0, 0);
594+
if (!HeapTupleIsValid(tuple))
595+
elog(ERROR, "cache lookup failed for constraint %u", conId);
596+
con = (Form_pg_constraint) GETSTRUCT(tuple);
597+
598+
/*
599+
* We need to check whether the name is already in use --- note that
600+
* there currently is not a unique index that would catch this.
601+
*/
602+
if (OidIsValid(con->conrelid) &&
603+
ConstraintNameIsUsed(CONSTRAINT_RELATION,
604+
con->conrelid,
605+
con->connamespace,
606+
newname))
607+
ereport(ERROR,
608+
(errcode(ERRCODE_DUPLICATE_OBJECT),
609+
errmsg("constraint \"%s\" for relation \"%s\" already exists",
610+
newname, get_rel_name(con->conrelid))));
611+
if (OidIsValid(con->contypid) &&
612+
ConstraintNameIsUsed(CONSTRAINT_DOMAIN,
613+
con->contypid,
614+
con->connamespace,
615+
newname))
616+
ereport(ERROR,
617+
(errcode(ERRCODE_DUPLICATE_OBJECT),
618+
errmsg("constraint \"%s\" for domain \"%s\" already exists",
619+
newname, format_type_be(con->contypid))));
620+
621+
/* OK, do the rename --- tuple is a copy, so OK to scribble on it */
622+
namestrcpy(&(con->conname), newname);
623+
624+
simple_heap_update(conDesc, &tuple->t_self, tuple);
625+
626+
/* update the system catalog indexes */
627+
CatalogUpdateIndexes(conDesc, tuple);
628+
629+
heap_freetuple(tuple);
630+
heap_close(conDesc, RowExclusiveLock);
631+
}
632+
571633
/*
572634
* AlterConstraintNamespaces
573635
* Find any constraints belonging to the specified object,

src/backend/commands/tablecmds.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.239 2008/01/02 23:34:42 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.240 2008/01/17 18:56:54 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1659,13 +1659,13 @@ renamerel(Oid myrelid, const char *newrelname, ObjectType reltype)
16591659
* or ALTER INDEX is used to rename a sequence or view.
16601660
*/
16611661
relkind = targetrelation->rd_rel->relkind;
1662-
if (reltype == OBJECT_SEQUENCE && relkind != 'S')
1662+
if (reltype == OBJECT_SEQUENCE && relkind != RELKIND_SEQUENCE)
16631663
ereport(ERROR,
16641664
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
16651665
errmsg("\"%s\" is not a sequence",
16661666
RelationGetRelationName(targetrelation))));
16671667

1668-
if (reltype == OBJECT_VIEW && relkind != 'v')
1668+
if (reltype == OBJECT_VIEW && relkind != RELKIND_VIEW)
16691669
ereport(ERROR,
16701670
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
16711671
errmsg("\"%s\" is not a view",
@@ -1711,6 +1711,17 @@ renamerel(Oid myrelid, const char *newrelname, ObjectType reltype)
17111711
if (OidIsValid(targetrelation->rd_rel->reltype))
17121712
TypeRename(targetrelation->rd_rel->reltype, newrelname, namespaceId);
17131713

1714+
/*
1715+
* Also rename the associated constraint, if any.
1716+
*/
1717+
if (relkind == RELKIND_INDEX)
1718+
{
1719+
Oid constraintId = get_index_constraint(myrelid);
1720+
1721+
if (OidIsValid(constraintId))
1722+
RenameConstraintById(constraintId, newrelname);
1723+
}
1724+
17141725
/*
17151726
* Close rel, but keep exclusive lock!
17161727
*/

src/include/catalog/pg_constraint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/catalog/pg_constraint.h,v 1.26 2008/01/01 19:45:56 momjian Exp $
11+
* $PostgreSQL: pgsql/src/include/catalog/pg_constraint.h,v 1.27 2008/01/17 18:56:54 tgl Exp $
1212
*
1313
* NOTES
1414
* the genbki.sh script reads this file and generates .bki
@@ -201,6 +201,7 @@ extern Oid CreateConstraintEntry(const char *constraintName,
201201
const char *conSrc);
202202

203203
extern void RemoveConstraintById(Oid conId);
204+
extern void RenameConstraintById(Oid conId, const char *newname);
204205

205206
extern bool ConstraintNameIsUsed(ConstraintCategory conCat, Oid objId,
206207
Oid objNamespace, const char *conname);

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