Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c293ba9

Browse files
committed
If an index depends on no columns of its table, give it a dependency on the
whole table instead, to ensure that it goes away when the table is dropped. Per bug #3723 from Sam Mason. Backpatch as far as 7.4; AFAICT 7.3 does not have the issue, because it doesn't have general-purpose expression indexes and so there must be at least one column referenced by an index.
1 parent f55e6c0 commit c293ba9

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/backend/catalog/index.c

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.286 2007/10/12 18:55:12 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.287 2007/11/08 23:22:54 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -41,6 +41,7 @@
4141
#include "executor/executor.h"
4242
#include "miscadmin.h"
4343
#include "optimizer/clauses.h"
44+
#include "optimizer/var.h"
4445
#include "parser/parse_expr.h"
4546
#include "storage/procarray.h"
4647
#include "storage/smgr.h"
@@ -723,6 +724,8 @@ index_create(Oid heapRelationId,
723724
}
724725
else
725726
{
727+
bool have_simple_col = false;
728+
726729
/* Create auto dependencies on simply-referenced columns */
727730
for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++)
728731
{
@@ -733,8 +736,29 @@ index_create(Oid heapRelationId,
733736
referenced.objectSubId = indexInfo->ii_KeyAttrNumbers[i];
734737

735738
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
739+
740+
have_simple_col = true;
736741
}
737742
}
743+
744+
/*
745+
* It's possible for an index to not depend on any columns of
746+
* the table at all, in which case we need to give it a dependency
747+
* on the table as a whole; else it won't get dropped when the
748+
* table is dropped. This edge case is not totally useless;
749+
* for example, a unique index on a constant expression can serve
750+
* to prevent a table from containing more than one row.
751+
*/
752+
if (!have_simple_col &&
753+
!contain_vars_of_level((Node *) indexInfo->ii_Expressions, 0) &&
754+
!contain_vars_of_level((Node *) indexInfo->ii_Predicate, 0))
755+
{
756+
referenced.classId = RelationRelationId;
757+
referenced.objectId = heapRelationId;
758+
referenced.objectSubId = 0;
759+
760+
recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
761+
}
738762
}
739763

740764
/* Store dependency on operator classes */

0 commit comments

Comments
 (0)