From: Noah Misch Date: Tue, 30 Jul 2013 22:36:52 +0000 (-0400) Subject: Restore REINDEX constraint validation. X-Git-Tag: REL9_1_10~34 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ed33ad39a2f353acc3d38591a749f324de3b6c23;p=postgresql Restore REINDEX constraint validation. Refactoring as part of commit 8ceb24568054232696dddc1166a8563bc78c900a had the unintended effect of making REINDEX TABLE and REINDEX DATABASE no longer validate constraints enforced by the indexes in question; REINDEX INDEX still did so. Indexes marked invalid remained so, and constraint violations arising from data corruption went undetected. Back-patch to 9.0, like the causative commit. --- diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 34153f5eb9..ab73567ad2 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -1553,7 +1553,9 @@ ReindexTable(RangeVar *relation) ReleaseSysCache(tuple); - if (!reindex_relation(heapOid, REINDEX_REL_PROCESS_TOAST)) + if (!reindex_relation(heapOid, + REINDEX_REL_PROCESS_TOAST | + REINDEX_REL_CHECK_CONSTRAINTS)) ereport(NOTICE, (errmsg("table \"%s\" has no indexes", relation->relname))); @@ -1666,7 +1668,9 @@ ReindexDatabase(const char *databaseName, bool do_system, bool do_user) StartTransactionCommand(); /* functions in indexes may want a snapshot set */ PushActiveSnapshot(GetTransactionSnapshot()); - if (reindex_relation(relid, REINDEX_REL_PROCESS_TOAST)) + if (reindex_relation(relid, + REINDEX_REL_PROCESS_TOAST | + REINDEX_REL_CHECK_CONSTRAINTS)) ereport(NOTICE, (errmsg("table \"%s.%s\" was reindexed", get_namespace_name(get_rel_namespace(relid)), diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out index b23c712204..7113ba30ce 100644 --- a/src/test/regress/expected/create_index.out +++ b/src/test/regress/expected/create_index.out @@ -1190,9 +1190,13 @@ COMMIT; BEGIN; CREATE INDEX std_index on concur_heap(f2); COMMIT; --- check to make sure that the failed indexes were cleaned up properly and the --- successful indexes are created properly. Notably that they do NOT have the --- "invalid" flag set. +-- Failed builds are left invalid by VACUUM FULL, fixed by REINDEX +VACUUM FULL concur_heap; +REINDEX TABLE concur_heap; +ERROR: could not create unique index "concur_index3" +DETAIL: Key (f2)=(b) is duplicated. +DELETE FROM concur_heap WHERE f1 = 'b'; +VACUUM FULL concur_heap; \d concur_heap Table "public.concur_heap" Column | Type | Modifiers @@ -1208,6 +1212,22 @@ Indexes: "concur_index5" btree (f2) WHERE f1 = 'x'::text "std_index" btree (f2) +REINDEX TABLE concur_heap; +\d concur_heap +Table "public.concur_heap" + Column | Type | Modifiers +--------+------+----------- + f1 | text | + f2 | text | +Indexes: + "concur_index2" UNIQUE, btree (f1) + "concur_index3" UNIQUE, btree (f2) + "concur_heap_expr_idx" btree ((f2 || f1)) + "concur_index1" btree (f2, f1) + "concur_index4" btree (f2) WHERE f1 = 'a'::text + "concur_index5" btree (f2) WHERE f1 = 'x'::text + "std_index" btree (f2) + DROP TABLE concur_heap; -- -- Test ADD CONSTRAINT USING INDEX diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sql index bf27379f59..29c8bef8a6 100644 --- a/src/test/regress/sql/create_index.sql +++ b/src/test/regress/sql/create_index.sql @@ -401,10 +401,13 @@ BEGIN; CREATE INDEX std_index on concur_heap(f2); COMMIT; --- check to make sure that the failed indexes were cleaned up properly and the --- successful indexes are created properly. Notably that they do NOT have the --- "invalid" flag set. - +-- Failed builds are left invalid by VACUUM FULL, fixed by REINDEX +VACUUM FULL concur_heap; +REINDEX TABLE concur_heap; +DELETE FROM concur_heap WHERE f1 = 'b'; +VACUUM FULL concur_heap; +\d concur_heap +REINDEX TABLE concur_heap; \d concur_heap DROP TABLE concur_heap;