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

Commit 37798a8

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 367f362 commit 37798a8

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/backend/commands/indexcmds.c

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

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

src/test/regress/expected/insert_conflict.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,10 +876,10 @@ drop table parted_conflict;
876876
-- partition
877877
create table parted_conflict (a int, b text) partition by range (a);
878878
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
879+
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
879880
create unique index on only parted_conflict_1 (a);
880881
create unique index on only parted_conflict (a);
881882
alter index parted_conflict_a_idx attach partition parted_conflict_1_a_idx;
882-
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
883883
insert into parted_conflict values (40, 'forty');
884884
insert into parted_conflict_1 values (40, 'cuarenta')
885885
on conflict (a) do update set b = excluded.b;

src/test/regress/sql/insert_conflict.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,10 @@ drop table parted_conflict;
551551
-- partition
552552
create table parted_conflict (a int, b text) partition by range (a);
553553
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
554+
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
554555
create unique index on only parted_conflict_1 (a);
555556
create unique index on only parted_conflict (a);
556557
alter index parted_conflict_a_idx attach partition parted_conflict_1_a_idx;
557-
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
558558
insert into parted_conflict values (40, 'forty');
559559
insert into parted_conflict_1 values (40, 'cuarenta')
560560
on conflict (a) do update set b = excluded.b;

0 commit comments

Comments
 (0)