diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index e553afb7f01..bf45c355b77 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -999,6 +999,15 @@ infer_arbiter_indexes(PlannerInfo *root) if (!idxForm->indisready) continue; + /* + * Ignore invalid indexes for partitioned tables. It's possible that + * some partitions don't have the index (yet), and then we would not + * find a match during ExecInitPartitionInfo. + */ + if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE && + !idxForm->indisvalid) + continue; + /* * Note that we do not perform a check against indcheckxmin (like e.g. * get_relation_info()) here to eliminate candidates, because diff --git a/src/test/regress/expected/partition_info.out b/src/test/regress/expected/partition_info.out index 42b6bc77cad..4defa66e5b3 100644 --- a/src/test/regress/expected/partition_info.out +++ b/src/test/regress/expected/partition_info.out @@ -349,3 +349,10 @@ SELECT pg_partition_root('ptif_li_child'); DROP VIEW ptif_test_view; DROP MATERIALIZED VIEW ptif_test_matview; DROP TABLE ptif_li_parent, ptif_li_child; +-- Test about selection of arbiter indexes for partitioned tables with +-- non-valid index on the parent table +CREATE TABLE pt (a int PRIMARY KEY) PARTITION BY RANGE (a); +CREATE TABLE p1 PARTITION OF pt FOR VALUES FROM (1) to (2) PARTITION BY RANGE (a); +CREATE TABLE p1_1 PARTITION OF p1 FOR VALUES FROM (1) TO (2); +CREATE UNIQUE INDEX ON ONLY p1 (a); +INSERT INTO p1 VALUES (1) ON CONFLICT (a) DO NOTHING; diff --git a/src/test/regress/sql/partition_info.sql b/src/test/regress/sql/partition_info.sql index b5060bec7f0..2ef65292bab 100644 --- a/src/test/regress/sql/partition_info.sql +++ b/src/test/regress/sql/partition_info.sql @@ -127,3 +127,11 @@ SELECT pg_partition_root('ptif_li_child'); DROP VIEW ptif_test_view; DROP MATERIALIZED VIEW ptif_test_matview; DROP TABLE ptif_li_parent, ptif_li_child; + +-- Test about selection of arbiter indexes for partitioned tables with +-- non-valid index on the parent table +CREATE TABLE pt (a int PRIMARY KEY) PARTITION BY RANGE (a); +CREATE TABLE p1 PARTITION OF pt FOR VALUES FROM (1) to (2) PARTITION BY RANGE (a); +CREATE TABLE p1_1 PARTITION OF p1 FOR VALUES FROM (1) TO (2); +CREATE UNIQUE INDEX ON ONLY p1 (a); +INSERT INTO p1 VALUES (1) ON CONFLICT (a) DO NOTHING;