From b53800355f9b3a4a4ee6e5e610accab77af8d1c3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 11 Aug 2012 12:51:24 -0400 Subject: [PATCH] Fix dependencies generated during ALTER TABLE ADD CONSTRAINT USING INDEX. This command generated new pg_depend entries linking the index to the constraint and the constraint to the table, which match the entries made when a unique or primary key constraint is built de novo. However, it did not bother to get rid of the entries linking the index directly to the table. We had considered the issue when the ADD CONSTRAINT USING INDEX patch was written, and concluded that we didn't need to get rid of the extra entries. But this is wrong: ALTER COLUMN TYPE wasn't expecting such redundant dependencies to exist, as reported by Hubert Depesz Lubaczewski. On reflection it seems rather likely to break other things as well, since there are many bits of code that crawl pg_depend for one purpose or another, and most of them are pretty naive about what relationships they're expecting to find. Fortunately it's not that hard to get rid of the extra dependency entries, so let's do that. Back-patch to 9.1, where ALTER TABLE ADD CONSTRAINT USING INDEX was added. --- src/backend/catalog/index.c | 25 +++++++++++++++++++------ src/backend/commands/tablecmds.c | 3 ++- src/include/catalog/index.h | 1 + 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 1546d480e3..464950b9af 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -923,6 +923,7 @@ index_create(Relation heapRelation, initdeferred, false, /* already marked primary */ false, /* pg_index entry is OK */ + false, /* no old dependencies */ allow_system_table_mods); } else @@ -1090,6 +1091,8 @@ index_create(Relation heapRelation, * initdeferred: constraint is INITIALLY DEFERRED * mark_as_primary: if true, set flags to mark index as primary key * update_pgindex: if true, update pg_index row (else caller's done that) + * remove_old_dependencies: if true, remove existing dependencies of index + * on table's columns * allow_system_table_mods: allow table to be a system catalog */ void @@ -1102,6 +1105,7 @@ index_constraint_create(Relation heapRelation, bool initdeferred, bool mark_as_primary, bool update_pgindex, + bool remove_old_dependencies, bool allow_system_table_mods) { Oid namespaceId = RelationGetNamespace(heapRelation); @@ -1125,6 +1129,19 @@ index_constraint_create(Relation heapRelation, constraintType != CONSTRAINT_EXCLUSION) elog(ERROR, "constraints cannot have index expressions"); + /* + * If we're manufacturing a constraint for a pre-existing index, we need + * to get rid of the existing auto dependencies for the index (the ones + * that index_create() would have made instead of calling this function). + * + * Note: this code would not necessarily do the right thing if the index + * has any expressions or predicate, but we'd never be turning such an + * index into a UNIQUE or PRIMARY KEY constraint. + */ + if (remove_old_dependencies) + deleteDependencyRecordsForClass(RelationRelationId, indexRelationId, + RelationRelationId, DEPENDENCY_AUTO); + /* * Construct a pg_constraint entry. */ @@ -1159,12 +1176,8 @@ index_constraint_create(Relation heapRelation, /* * Register the index as internally dependent on the constraint. * - * Note that the constraint has a dependency on the table, so when this - * path is taken we do not need any direct dependency from the index to - * the table. (But if one exists, no great harm is done, either. So in - * the case where we're manufacturing a constraint for a pre-existing - * index, we don't bother to try to get rid of the existing index->table - * dependency.) + * Note that the constraint has a dependency on the table, so we don't + * need (or want) any direct dependency from the index to the table. */ myself.classId = RelationRelationId; myself.objectId = indexRelationId; diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index a69544853f..00fe1138b2 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -5487,7 +5487,8 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel, stmt->deferrable, stmt->initdeferred, stmt->primary, - true, + true, /* update pg_index */ + true, /* remove old dependencies */ allowSystemTableMods); index_close(indexRel, NoLock); diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index 7c8198f31e..eb417cecb7 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -61,6 +61,7 @@ extern void index_constraint_create(Relation heapRelation, bool initdeferred, bool mark_as_primary, bool update_pgindex, + bool remove_old_dependencies, bool allow_system_table_mods); extern void index_drop(Oid indexId, bool concurrent);