Skip to content

Commit 71a05b2

Browse files
committed
Don't mark partitioned indexes invalid unnecessarily
When an indexes is created on a partitioned table using ONLY (don't recurse to partitions), it gets marked invalid until index partitions are attached for each table partition. But there's no reason to do this if there are no partitions ... and moreover, there's no way to get the index to become valid afterwards, because all partitions that get created/attached get their own index partition already attached to the parent index, so there's no chance to do ALTER INDEX ... ATTACH PARTITION that would make the parent index valid. Fix by not marking the index as invalid to begin with. This is very similar to 9139aa1, but the pg_dump aspect does not appear to be relevant until we add FKs that can point to PKs on partitioned tables. (I tried to cause the pg_upgrade test to break by leaving some of these bogus tables around, but wasn't able to.) Making this change means that an index that was supposed to be invalid in the insert_conflict regression test is no longer invalid; reorder the DDL so that the test continues to verify the behavior we want it to. Author: Álvaro Herrera Reviewed-by: Amit Langote Discussion: https://postgr.es/m/20181203225019.2vvdef2ybnkxt364@alvherre.pgsql
1 parent 99f9cce commit 71a05b2

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/backend/commands/indexcmds.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,18 @@ DefineIndex(Oid relationId,
833833
flags |= INDEX_CREATE_PARTITIONED;
834834
if (stmt->primary)
835835
flags |= INDEX_CREATE_IS_PRIMARY;
836+
837+
/*
838+
* If the table is partitioned, and recursion was declined but partitions
839+
* exist, mark the index as invalid.
840+
*/
836841
if (partitioned && stmt->relation && !stmt->relation->inh)
837-
flags |= INDEX_CREATE_INVALID;
842+
{
843+
PartitionDesc pd = RelationGetPartitionDesc(rel);
844+
845+
if (pd->nparts != 0)
846+
flags |= INDEX_CREATE_INVALID;
847+
}
838848

839849
if (stmt->deferrable)
840850
constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE;

src/test/regress/expected/insert_conflict.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,10 @@ drop table parted_conflict;
813813
-- partition
814814
create table parted_conflict (a int, b text) partition by range (a);
815815
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
816+
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
816817
create unique index on only parted_conflict_1 (a);
817818
create unique index on only parted_conflict (a);
818819
alter index parted_conflict_a_idx attach partition parted_conflict_1_a_idx;
819-
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
820820
insert into parted_conflict values (40, 'forty');
821821
insert into parted_conflict_1 values (40, 'cuarenta')
822822
on conflict (a) do update set b = excluded.b;

src/test/regress/sql/insert_conflict.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ drop table parted_conflict;
528528
-- partition
529529
create table parted_conflict (a int, b text) partition by range (a);
530530
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
531+
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
531532
create unique index on only parted_conflict_1 (a);
532533
create unique index on only parted_conflict (a);
533534
alter index parted_conflict_a_idx attach partition parted_conflict_1_a_idx;
534-
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
535535
insert into parted_conflict values (40, 'forty');
536536
insert into parted_conflict_1 values (40, 'cuarenta')
537537
on conflict (a) do update set b = excluded.b;

0 commit comments

Comments
 (0)