diff options
-rw-r--r-- | src/backend/catalog/index.c | 25 | ||||
-rw-r--r-- | src/backend/commands/tablecmds.c | 3 | ||||
-rw-r--r-- | 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 39ba4869af6..2e023d5ab56 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -926,6 +926,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 @@ -1093,6 +1094,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 @@ -1105,6 +1108,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); @@ -1129,6 +1133,19 @@ index_constraint_create(Relation heapRelation, 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. */ conOid = CreateConstraintEntry(constraintName, @@ -1161,12 +1178,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 68467b68b5e..437c2773af0 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -5223,7 +5223,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 071db7f4012..4172f0e4006 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -60,6 +60,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); |