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

Commitb538003

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 parenta67d6d9 commitb538003

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
@@ -923,6 +923,7 @@ index_create(Relation heapRelation,
923923
initdeferred,
924924
false,/* already marked primary */
925925
false,/* pg_index entry is OK */
926+
false,/* no old dependencies */
926927
allow_system_table_mods);
927928
}
928929
else
@@ -1090,6 +1091,8 @@ index_create(Relation heapRelation,
10901091
* initdeferred: constraint is INITIALLY DEFERRED
10911092
* mark_as_primary: if true, set flags to mark index as primary key
10921093
* update_pgindex: if true, update pg_index row (else caller's done that)
1094+
* remove_old_dependencies: if true, remove existing dependencies of index
1095+
*on table's columns
10931096
* allow_system_table_mods: allow table to be a system catalog
10941097
*/
10951098
void
@@ -1102,6 +1105,7 @@ index_constraint_create(Relation heapRelation,
11021105
boolinitdeferred,
11031106
boolmark_as_primary,
11041107
boolupdate_pgindex,
1108+
boolremove_old_dependencies,
11051109
boolallow_system_table_mods)
11061110
{
11071111
OidnamespaceId=RelationGetNamespace(heapRelation);
@@ -1125,6 +1129,19 @@ index_constraint_create(Relation heapRelation,
11251129
constraintType!=CONSTRAINT_EXCLUSION)
11261130
elog(ERROR,"constraints cannot have index expressions");
11271131

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

‎src/backend/commands/tablecmds.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5487,7 +5487,8 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel,
54875487
stmt->deferrable,
54885488
stmt->initdeferred,
54895489
stmt->primary,
5490-
true,
5490+
true,/* update pg_index */
5491+
true,/* remove old dependencies */
54915492
allowSystemTableMods);
54925493

54935494
index_close(indexRel,NoLock);

‎src/include/catalog/index.h

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

6667
externvoidindex_drop(OidindexId,boolconcurrent);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp