diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2007-11-08 23:22:54 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2007-11-08 23:22:54 +0000 |
commit | c293ba9effba68f17fec74d3ca0e8f900f3c8a53 (patch) | |
tree | ba2d15d4460edc85b20c9ef31eb497ef55086576 /src | |
parent | f55e6c07c37aa4e995ee9445e484a52ac77670a9 (diff) | |
download | postgresql-c293ba9effba68f17fec74d3ca0e8f900f3c8a53.tar.gz postgresql-c293ba9effba68f17fec74d3ca0e8f900f3c8a53.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/catalog/index.c | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 0e76bc9800d..7f2bad8c0f6 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.286 2007/10/12 18:55:12 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/index.c,v 1.287 2007/11/08 23:22:54 tgl Exp $ * * * INTERFACE ROUTINES @@ -41,6 +41,7 @@ #include "executor/executor.h" #include "miscadmin.h" #include "optimizer/clauses.h" +#include "optimizer/var.h" #include "parser/parse_expr.h" #include "storage/procarray.h" #include "storage/smgr.h" @@ -723,6 +724,8 @@ index_create(Oid heapRelationId, } else { + bool have_simple_col = false; + /* Create auto dependencies on simply-referenced columns */ for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) { @@ -733,8 +736,29 @@ index_create(Oid heapRelationId, referenced.objectSubId = indexInfo->ii_KeyAttrNumbers[i]; recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); + + have_simple_col = true; } } + + /* + * It's possible for an index to not depend on any columns of + * the table at all, in which case we need to give it a dependency + * on the table as a whole; else it won't get dropped when the + * table is dropped. This edge case is not totally useless; + * for example, a unique index on a constant expression can serve + * to prevent a table from containing more than one row. + */ + if (!have_simple_col && + !contain_vars_of_level((Node *) indexInfo->ii_Expressions, 0) && + !contain_vars_of_level((Node *) indexInfo->ii_Predicate, 0)) + { + referenced.classId = RelationRelationId; + referenced.objectId = heapRelationId; + referenced.objectSubId = 0; + + recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); + } } /* Store dependency on operator classes */ |