Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2cc611

Browse files
committed
Fix dependencies generated during ALTER TABLE ADD CONSTRAINT USING INDEX.
This command generated new pg_depend entries linking the index to theconstraint and the constraint to the table, which match the entries madewhen a unique or primary key constraint is built de novo. However, it didnot bother to get rid of the entries linking the index directly to thetable. We had considered the issue when the ADD CONSTRAINT USING INDEXpatch was written, and concluded that we didn't need to get rid of theextra entries. But this is wrong: ALTER COLUMN TYPE wasn't expecting suchredundant dependencies to exist, as reported by Hubert Depesz Lubaczewski.On reflection it seems rather likely to break other things as well, sincethere are many bits of code that crawl pg_depend for one purpose oranother, and most of them are pretty naive about what relationships they'reexpecting to find. Fortunately it's not that hard to get rid of the extradependency entries, so let's do that.Back-patch to 9.1, where ALTER TABLE ADD CONSTRAINT USING INDEX was added.
1 parent64d64a0 commitb2cc611

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

‎src/backend/catalog/index.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ index_create(Relation heapRelation,
926926
initdeferred,
927927
false,/* already marked primary */
928928
false,/* pg_index entry is OK */
929+
false,/* no old dependencies */
929930
allow_system_table_mods);
930931
}
931932
else
@@ -1093,6 +1094,8 @@ index_create(Relation heapRelation,
10931094
* initdeferred: constraint is INITIALLY DEFERRED
10941095
* mark_as_primary: if true, set flags to mark index as primary key
10951096
* update_pgindex: if true, update pg_index row (else caller's done that)
1097+
* remove_old_dependencies: if true, remove existing dependencies of index
1098+
*on table's columns
10961099
* allow_system_table_mods: allow table to be a system catalog
10971100
*/
10981101
void
@@ -1105,6 +1108,7 @@ index_constraint_create(Relation heapRelation,
11051108
boolinitdeferred,
11061109
boolmark_as_primary,
11071110
boolupdate_pgindex,
1111+
boolremove_old_dependencies,
11081112
boolallow_system_table_mods)
11091113
{
11101114
OidnamespaceId=RelationGetNamespace(heapRelation);
@@ -1128,6 +1132,19 @@ index_constraint_create(Relation heapRelation,
11281132
constraintType!=CONSTRAINT_EXCLUSION)
11291133
elog(ERROR,"constraints cannot have index expressions");
11301134

1135+
/*
1136+
* If we're manufacturing a constraint for a pre-existing index, we need
1137+
* to get rid of the existing auto dependencies for the index (the ones
1138+
* that index_create() would have made instead of calling this function).
1139+
*
1140+
* Note: this code would not necessarily do the right thing if the index
1141+
* has any expressions or predicate, but we'd never be turning such an
1142+
* index into a UNIQUE or PRIMARY KEY constraint.
1143+
*/
1144+
if (remove_old_dependencies)
1145+
deleteDependencyRecordsForClass(RelationRelationId,indexRelationId,
1146+
RelationRelationId,DEPENDENCY_AUTO);
1147+
11311148
/*
11321149
* Construct a pg_constraint entry.
11331150
*/
@@ -1161,12 +1178,8 @@ index_constraint_create(Relation heapRelation,
11611178
/*
11621179
* Register the index as internally dependent on the constraint.
11631180
*
1164-
* Note that the constraint has a dependency on the table, so when this
1165-
* path is taken we do not need any direct dependency from the index to
1166-
* the table. (But if one exists, no great harm is done, either. So in
1167-
* the case where we're manufacturing a constraint for a pre-existing
1168-
* index, we don't bother to try to get rid of the existing index->table
1169-
* dependency.)
1181+
* Note that the constraint has a dependency on the table, so we don't
1182+
* need (or want) any direct dependency from the index to the table.
11701183
*/
11711184
myself.classId=RelationRelationId;
11721185
myself.objectId=indexRelationId;

‎src/backend/commands/tablecmds.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5223,7 +5223,8 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel,
52235223
stmt->deferrable,
52245224
stmt->initdeferred,
52255225
stmt->primary,
5226-
true,
5226+
true,/* update pg_index */
5227+
true,/* remove old dependencies */
52275228
allowSystemTableMods);
52285229

52295230
index_close(indexRel,NoLock);

‎src/include/catalog/index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern void index_constraint_create(Relation heapRelation,
6060
boolinitdeferred,
6161
boolmark_as_primary,
6262
boolupdate_pgindex,
63+
boolremove_old_dependencies,
6364
boolallow_system_table_mods);
6465

6566
externvoidindex_drop(OidindexId);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp