--
-- Test partitioning planner code
--
+-- Force generic plans to be used for all prepared statements in this file.
+set plan_cache_mode = force_generic_plan;
create table lp (a char) partition by list (a);
create table lp_default partition of lp default;
create table lp_ef partition of lp for values in ('e', 'f');
set enable_indexonlyscan = off;
prepare ab_q1 (int, int, int) as
select * from ab where a between $1 and $2 and b <= $3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q1 (1, 8, 3);
- a | b
----+---
-(0 rows)
-
-execute ab_q1 (1, 8, 3);
- a | b
----+---
-(0 rows)
-
-execute ab_q1 (1, 8, 3);
- a | b
----+---
-(0 rows)
-
-execute ab_q1 (1, 8, 3);
- a | b
----+---
-(0 rows)
-
-execute ab_q1 (1, 8, 3);
- a | b
----+---
-(0 rows)
-
explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2, 3);
QUERY PLAN
---------------------------------------------------------
-- Runtime pruning after optimizer pruning
prepare ab_q1 (int, int) as
select a from ab where a between $1 and $2 and b < 3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q1 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q1 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q1 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q1 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q1 (1, 8);
- a
----
-(0 rows)
-
explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2);
QUERY PLAN
-------------------------------------------------------
-- different levels of partitioning.
prepare ab_q2 (int, int) as
select a from ab where a between $1 and $2 and b < (select 3);
-execute ab_q2 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q2 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q2 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q2 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q2 (1, 8);
- a
----
-(0 rows)
-
explain (analyze, costs off, summary off, timing off) execute ab_q2 (2, 2);
QUERY PLAN
--------------------------------------------------------
-- As above, but swap the PARAM_EXEC Param to the first partition level
prepare ab_q3 (int, int) as
select a from ab where b between $1 and $2 and a < (select 3);
-execute ab_q3 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q3 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q3 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q3 (1, 8);
- a
----
-(0 rows)
-
-execute ab_q3 (1, 8);
- a
----
-(0 rows)
-
explain (analyze, costs off, summary off, timing off) execute ab_q3 (2, 2);
QUERY PLAN
--------------------------------------------------------
set parallel_tuple_cost = 0;
set min_parallel_table_scan_size = 0;
set max_parallel_workers_per_gather = 2;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q4 (1, 8);
- avg
------
-
-(1 row)
-
-execute ab_q4 (1, 8);
- avg
------
-
-(1 row)
-
-execute ab_q4 (1, 8);
- avg
------
-
-(1 row)
-
-execute ab_q4 (1, 8);
- avg
------
-
-(1 row)
-
-execute ab_q4 (1, 8);
- avg
------
-
-(1 row)
-
select explain_parallel_append('execute ab_q4 (2, 2)');
explain_parallel_append
-------------------------------------------------------------------------------
-- Test run-time pruning with IN lists.
prepare ab_q5 (int, int, int) as
select avg(a) from ab where a in($1,$2,$3) and b < 4;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q5 (1, 2, 3);
- avg
------
-
-(1 row)
-
-execute ab_q5 (1, 2, 3);
- avg
------
-
-(1 row)
-
-execute ab_q5 (1, 2, 3);
- avg
------
-
-(1 row)
-
-execute ab_q5 (1, 2, 3);
- avg
------
-
-(1 row)
-
-execute ab_q5 (1, 2, 3);
- avg
------
-
-(1 row)
-
select explain_parallel_append('execute ab_q5 (1, 1, 1)');
explain_parallel_append
-------------------------------------------------------------------------------
insert into xy_1 values(100,-10);
set enable_bitmapscan = 0;
set enable_indexscan = 0;
-set plan_cache_mode = 'force_generic_plan';
prepare ab_q6 as
select * from (
select tableoid::regclass,a,b from ab
reset enable_bitmapscan;
reset enable_indexscan;
-reset plan_cache_mode;
deallocate ab_q1;
deallocate ab_q2;
deallocate ab_q3;
alter table part_cab attach partition part_abc_p1 for values in(3);
prepare part_abc_q1 (int, int, int) as
select * from part_abc where a = $1 and b = $2 and c = $3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute part_abc_q1 (1, 2, 3);
- a | b | c
----+---+---
-(0 rows)
-
-execute part_abc_q1 (1, 2, 3);
- a | b | c
----+---+---
-(0 rows)
-
-execute part_abc_q1 (1, 2, 3);
- a | b | c
----+---+---
-(0 rows)
-
-execute part_abc_q1 (1, 2, 3);
- a | b | c
----+---+---
-(0 rows)
-
-execute part_abc_q1 (1, 2, 3);
- a | b | c
----+---+---
-(0 rows)
-
-- Single partition should be scanned.
explain (analyze, costs off, summary off, timing off) execute part_abc_q1 (1, 2, 3);
QUERY PLAN
-- partitions before finally detecting the correct set of 2nd level partitions
-- which match the given parameter.
prepare q1 (int,int) as select * from listp where b in ($1,$2);
-execute q1 (1,2);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2);
- a | b
----+---
-(0 rows)
-
explain (analyze, costs off, summary off, timing off) execute q1 (1,1);
QUERY PLAN
-----------------------------------------------------
deallocate q1;
-- Test more complex cases where a not-equal condition further eliminates partitions.
prepare q1 (int,int,int,int) as select * from listp where b in($1,$2) and $3 <> b and $4 <> b;
-execute q1 (1,2,3,4);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2,3,4);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2,3,4);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2,3,4);
- a | b
----+---
-(0 rows)
-
-execute q1 (1,2,3,4);
- a | b
----+---
-(0 rows)
-
-- Both partitions allowed by IN clause, but one disallowed by <> clause
explain (analyze, costs off, summary off, timing off) execute q1 (1,2,2,0);
QUERY PLAN
-- a combination of runtime parameters is specified, not all of whose values
-- are available at the same time
--
-set plan_cache_mode = force_generic_plan;
prepare ps1 as
select * from mc3p where a = $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
(8 rows)
deallocate ps2;
-reset plan_cache_mode;
drop table mc3p;
-- Ensure runtime pruning works with initplans params with boolean types
create table boolvalues (value bool not null);
create index on ma_test (b);
analyze ma_test;
prepare mt_q1 (int) as select a from ma_test where a >= $1 and a % 10 = 5 order by b;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute mt_q1(0);
- a
-----
- 5
- 15
- 25
-(3 rows)
-
-execute mt_q1(0);
- a
-----
- 5
- 15
- 25
-(3 rows)
-
-execute mt_q1(0);
- a
-----
- 5
- 15
- 25
-(3 rows)
-
-execute mt_q1(0);
- a
-----
- 5
- 15
- 25
-(3 rows)
-
-execute mt_q1(0);
- a
-----
- 5
- 15
- 25
-(3 rows)
-
explain (analyze, costs off, summary off, timing off) execute mt_q1(15);
QUERY PLAN
-------------------------------------------------------------------------------
select 1, 1, 1
) s(a, b, c)
where s.a = $1 and s.b = $2 and s.c = (select 1);
-set plan_cache_mode to force_generic_plan;
explain (costs off) execute q (1, 1);
QUERY PLAN
---------------------------------------------------------------
1 | 1 | 1
(1 row)
-reset plan_cache_mode;
drop table p, q;
-- Ensure run-time pruning works correctly when we match a partitioned table
-- on the first level but find no matching partitions on the second level.
--
-- Test partitioning planner code
--
+
+-- Force generic plans to be used for all prepared statements in this file.
+set plan_cache_mode = force_generic_plan;
+
create table lp (a char) partition by list (a);
create table lp_default partition of lp default;
create table lp_ef partition of lp for values in ('e', 'f');
prepare ab_q1 (int, int, int) as
select * from ab where a between $1 and $2 and b <= $3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q1 (1, 8, 3);
-execute ab_q1 (1, 8, 3);
-execute ab_q1 (1, 8, 3);
-execute ab_q1 (1, 8, 3);
-execute ab_q1 (1, 8, 3);
-
explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2, 3);
explain (analyze, costs off, summary off, timing off) execute ab_q1 (1, 2, 3);
prepare ab_q1 (int, int) as
select a from ab where a between $1 and $2 and b < 3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q1 (1, 8);
-execute ab_q1 (1, 8);
-execute ab_q1 (1, 8);
-execute ab_q1 (1, 8);
-execute ab_q1 (1, 8);
-
explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2);
explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 4);
prepare ab_q2 (int, int) as
select a from ab where a between $1 and $2 and b < (select 3);
-execute ab_q2 (1, 8);
-execute ab_q2 (1, 8);
-execute ab_q2 (1, 8);
-execute ab_q2 (1, 8);
-execute ab_q2 (1, 8);
-
explain (analyze, costs off, summary off, timing off) execute ab_q2 (2, 2);
-- As above, but swap the PARAM_EXEC Param to the first partition level
prepare ab_q3 (int, int) as
select a from ab where b between $1 and $2 and a < (select 3);
-execute ab_q3 (1, 8);
-execute ab_q3 (1, 8);
-execute ab_q3 (1, 8);
-execute ab_q3 (1, 8);
-execute ab_q3 (1, 8);
-
explain (analyze, costs off, summary off, timing off) execute ab_q3 (2, 2);
-- Test a backwards Append scan
set min_parallel_table_scan_size = 0;
set max_parallel_workers_per_gather = 2;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q4 (1, 8);
-execute ab_q4 (1, 8);
-execute ab_q4 (1, 8);
-execute ab_q4 (1, 8);
-execute ab_q4 (1, 8);
select explain_parallel_append('execute ab_q4 (2, 2)');
-- Test run-time pruning with IN lists.
prepare ab_q5 (int, int, int) as
select avg(a) from ab where a in($1,$2,$3) and b < 4;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute ab_q5 (1, 2, 3);
-execute ab_q5 (1, 2, 3);
-execute ab_q5 (1, 2, 3);
-execute ab_q5 (1, 2, 3);
-execute ab_q5 (1, 2, 3);
-
select explain_parallel_append('execute ab_q5 (1, 1, 1)');
select explain_parallel_append('execute ab_q5 (2, 3, 3)');
set enable_bitmapscan = 0;
set enable_indexscan = 0;
-set plan_cache_mode = 'force_generic_plan';
prepare ab_q6 as
select * from (
reset enable_bitmapscan;
reset enable_indexscan;
-reset plan_cache_mode;
deallocate ab_q1;
deallocate ab_q2;
prepare part_abc_q1 (int, int, int) as
select * from part_abc where a = $1 and b = $2 and c = $3;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute part_abc_q1 (1, 2, 3);
-execute part_abc_q1 (1, 2, 3);
-execute part_abc_q1 (1, 2, 3);
-execute part_abc_q1 (1, 2, 3);
-execute part_abc_q1 (1, 2, 3);
-
-- Single partition should be scanned.
explain (analyze, costs off, summary off, timing off) execute part_abc_q1 (1, 2, 3);
-- which match the given parameter.
prepare q1 (int,int) as select * from listp where b in ($1,$2);
-execute q1 (1,2);
-execute q1 (1,2);
-execute q1 (1,2);
-execute q1 (1,2);
-execute q1 (1,2);
-
explain (analyze, costs off, summary off, timing off) execute q1 (1,1);
explain (analyze, costs off, summary off, timing off) execute q1 (2,2);
-- Test more complex cases where a not-equal condition further eliminates partitions.
prepare q1 (int,int,int,int) as select * from listp where b in($1,$2) and $3 <> b and $4 <> b;
-execute q1 (1,2,3,4);
-execute q1 (1,2,3,4);
-execute q1 (1,2,3,4);
-execute q1 (1,2,3,4);
-execute q1 (1,2,3,4);
-
-- Both partitions allowed by IN clause, but one disallowed by <> clause
explain (analyze, costs off, summary off, timing off) execute q1 (1,2,2,0);
-- a combination of runtime parameters is specified, not all of whose values
-- are available at the same time
--
-set plan_cache_mode = force_generic_plan;
prepare ps1 as
select * from mc3p where a = $1 and abs(b) < (select 3);
explain (analyze, costs off, summary off, timing off)
explain (analyze, costs off, summary off, timing off)
execute ps2(1);
deallocate ps2;
-reset plan_cache_mode;
drop table mc3p;
analyze ma_test;
prepare mt_q1 (int) as select a from ma_test where a >= $1 and a % 10 = 5 order by b;
--- Execute query 5 times to allow choose_custom_plan
--- to start considering a generic plan.
-execute mt_q1(0);
-execute mt_q1(0);
-execute mt_q1(0);
-execute mt_q1(0);
-execute mt_q1(0);
-
explain (analyze, costs off, summary off, timing off) execute mt_q1(15);
execute mt_q1(15);
explain (analyze, costs off, summary off, timing off) execute mt_q1(25);
) s(a, b, c)
where s.a = $1 and s.b = $2 and s.c = (select 1);
-set plan_cache_mode to force_generic_plan;
-
explain (costs off) execute q (1, 1);
execute q (1, 1);
-reset plan_cache_mode;
drop table p, q;
-- Ensure run-time pruning works correctly when we match a partitioned table