-- Save parallel worker stats, used for comparison at the end select pg_stat_force_next_flush(); select parallel_workers_to_launch as parallel_workers_to_launch_before,
parallel_workers_launched as parallel_workers_launched_before from pg_stat_database where datname = current_database() \gset
create function sp_parallel_restricted(int) returns intas
$$begin return $1; end$$ language plpgsql parallel restricted;
begin;
-- encourage use of parallel plans set parallel_setup_cost=0; set parallel_tuple_cost=0; set min_parallel_table_scan_size=0; set max_parallel_workers_per_gather=4;
-- Parallel Append with partial-subplans explain (costs off) select round(avg(aa)), sum(aa) from a_star; select round(avg(aa)), sum(aa) from a_star a1;
-- Parallel Append with both partial and non-partial subplans altertable c_star set (parallel_workers = 0); altertable d_star set (parallel_workers = 0); explain (costs off) select round(avg(aa)), sum(aa) from a_star; select round(avg(aa)), sum(aa) from a_star a2;
-- Parallel Append with only non-partial subplans altertable a_star set (parallel_workers = 0); altertable b_star set (parallel_workers = 0); altertable e_star set (parallel_workers = 0); altertable f_star set (parallel_workers = 0); explain (costs off) select round(avg(aa)), sum(aa) from a_star; select round(avg(aa)), sum(aa) from a_star a3;
-- Parallel Append that runs serially create function sp_test_func() returns setof text as
$$ select'foo'::varcharunionallselect'bar'::varchar $$
language sql stable; select sp_test_func() orderby1;
-- Parallel Append is not to be used when the subpath depends on the outer param createtable part_pa_test(a int, b int) partition by range(a); createtable part_pa_test_p1 partition of part_pa_test forvaluesfrom (minvalue) to (0); createtable part_pa_test_p2 partition of part_pa_test forvaluesfrom (0) to (maxvalue); explain (costs off) select (select max((select pa1.b from part_pa_test pa1 where pa1.a = pa2.a))) from part_pa_test pa2; droptable part_pa_test;
-- test with leader participation disabled set parallel_leader_participation = off; explain (costs off) select count(*) from tenk1 where stringu1 = 'GRAAAA'; select count(*) from tenk1 where stringu1 = 'GRAAAA';
-- test with leader participation disabled, but no workers available (so -- the leader will have to run the plan despite the setting) set max_parallel_workers = 0; explain (costs off) select count(*) from tenk1 where stringu1 = 'GRAAAA'; select count(*) from tenk1 where stringu1 = 'GRAAAA';
-- test that parallel_restricted function doesn't run in worker altertable tenk1 set (parallel_workers = 4); explain (verbose, costs off) select sp_parallel_restricted(unique1) from tenk1 where stringu1 = 'GRAAAA'orderby1;
-- test parallel plan when group by expression is in target list. explain (costs off) select length(stringu1) from tenk1 groupby length(stringu1); select length(stringu1) from tenk1 groupby length(stringu1);
-- test that parallel plan for aggregates is not selected when -- target list contains parallel restricted clause. explain (costs off) select sum(sp_parallel_restricted(unique1)) from tenk1 groupby(sp_parallel_restricted(unique1));
-- test prepared statement
prepare tenk1_count(integer) Asselect count((unique1)) from tenk1 where hundred > $1; explain (costs off) execute tenk1_count(1);
execute tenk1_count(1);
deallocate tenk1_count;
-- test parallel plans for queries containing un-correlated subplans. altertable tenk2 set (parallel_workers = 0); explain (costs off) select count(*) from tenk1 where (two, four) notin
(select hundred, thousand from tenk2 where thousand > 100); select count(*) from tenk1 where (two, four) notin
(select hundred, thousand from tenk2 where thousand > 100); -- this is not parallel-safe due to use of random() within SubLink's testexpr: explain (costs off) select * from tenk1 where (unique1 + random())::integernotin
(select ten from tenk2); altertable tenk2 reset (parallel_workers);
-- test parallel plan for a query containing initplan. set enable_indexscan = off; set enable_indexonlyscan = off; set enable_bitmapscan = off; altertable tenk2 set (parallel_workers = 2);
explain (costs off) select count(*) from tenk1 where tenk1.unique1 = (Select max(tenk2.unique1) from tenk2); select count(*) from tenk1 where tenk1.unique1 = (Select max(tenk2.unique1) from tenk2);
-- test parallel index scans. set enable_seqscan to off; set enable_bitmapscan to off; set random_page_cost = 2;
explain (costs off) select count((unique1)) from tenk1 where hundred > 1; select count((unique1)) from tenk1 where hundred > 1;
-- Parallel ScalarArrayOp index scan explain (costs off) select count((unique1)) from tenk1 where hundred = any ((select array_agg(i) from generate_series(1, 100, 15) i)::int[]); select count((unique1)) from tenk1 where hundred = any ((select array_agg(i) from generate_series(1, 100, 15) i)::int[]);
-- test parallel index-only scans. explain (costs off) select count(*) from tenk1 where thousand > 95; select count(*) from tenk1 where thousand > 95;
-- test rescan cases too set enable_material = false;
explain (costs off) select * from
(select count(unique1) from tenk1 where hundred > 10) ss rightjoin (values (1),(2),(3)) v(x) ontrue; select * from
(select count(unique1) from tenk1 where hundred > 10) ss rightjoin (values (1),(2),(3)) v(x) ontrue;
explain (costs off) select * from
(select count(*) from tenk1 where thousand > 99) ss rightjoin (values (1),(2),(3)) v(x) ontrue; select * from
(select count(*) from tenk1 where thousand > 99) ss rightjoin (values (1),(2),(3)) v(x) ontrue;
-- test rescans for a Limit node with a parallel node beneath it.
reset enable_seqscan; set enable_indexonlyscan to off; set enable_indexscan to off; altertable tenk1 set (parallel_workers = 0); altertable tenk2 set (parallel_workers = 1); explain (costs off) select count(*) from tenk1 leftjoin (select tenk2.unique1 from tenk2 orderby1limit1000) ss on tenk1.unique1 < ss.unique1 + 1 where tenk1.unique1 < 2; select count(*) from tenk1 leftjoin (select tenk2.unique1 from tenk2 orderby1limit1000) ss on tenk1.unique1 < ss.unique1 + 1 where tenk1.unique1 < 2; --reset the value of workers for each table as it was before this test. altertable tenk1 set (parallel_workers = 4); altertable tenk2 reset (parallel_workers);
-- test parallel bitmap heap scan. set enable_seqscan to off; set enable_indexscan to off; set enable_hashjoin to off; set enable_mergejoin to off; set enable_material to off; -- test prefetching, if the platform allows it
DO $$
BEGIN SET effective_io_concurrency = 50;
EXCEPTION WHEN invalid_parameter_value THEN
END $$; set work_mem='64kB'; --set small work mem to force lossy pages explain (costs off) select count(*) from tenk1, tenk2 where tenk1.hundred > 1and tenk2.thousand=0; select count(*) from tenk1, tenk2 where tenk1.hundred > 1and tenk2.thousand=0;
createtable bmscantest (a int, t text); insertinto bmscantest select r, 'fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'FROM generate_series(1,100000) r; createindex i_bmtest ON bmscantest(a); select count(*) from bmscantest where a>1;
-- test accumulation of stats for parallel nodes
reset enable_seqscan; altertable tenk2 set (parallel_workers = 0); explain (analyze, timing off, summary off, costs off, buffers off) select count(*) from tenk1, tenk2 where tenk1.hundred > 1 and tenk2.thousand=0; altertable tenk2 reset (parallel_workers);
reset work_mem; create function explain_parallel_sort_stats() returns setof text
language plpgsql as
$$ declare ln text;
begin for ln in explain (analyze, timing off, summary off, costs off, buffers off) select * from
(select ten from tenk1 where ten < 100orderby ten) ss rightjoin (values (1),(2),(3)) v(x) ontrue loop
ln := regexp_replace(ln, 'Memory: \S*', 'Memory: xxx'); return next ln;
end loop;
end;
$$; select * from explain_parallel_sort_stats();
reset enable_indexscan;
reset enable_hashjoin;
reset enable_mergejoin;
reset enable_material;
reset effective_io_concurrency; droptable bmscantest; drop function explain_parallel_sort_stats();
-- test parallel merge join path. set enable_hashjoin to off; set enable_nestloop to off;
explain (costs off) select count(*) from tenk1, tenk2 where tenk1.unique1 = tenk2.unique1; select count(*) from tenk1, tenk2 where tenk1.unique1 = tenk2.unique1;
reset enable_hashjoin;
reset enable_nestloop;
-- test parallel nestloop join path with materialization of the inner path altertable tenk2 set (parallel_workers = 0); explain (costs off) select * from tenk1 t1, tenk2 t2 where t1.two > t2.two;
-- test that parallel nestloop join is not generated if the inner path is -- not parallel-safe explain (costs off) select * from tenk1 t1 leftjoin lateral
(select t1.unique1 as x, * from tenk2 t2 orderby1) t2 ontrue where t1.two > t2.two; altertable tenk2 reset (parallel_workers);
-- test gather merge set enable_hashagg = false;
explain (costs off) select count(*) from tenk1 groupby twenty;
select count(*) from tenk1 groupby twenty;
--test expressions in targetlist are pushed down for gather merge create function sp_simple_func(var1 integer) returns integer as $$
begin return var1 + 10;
end;
$$ language plpgsql PARALLEL SAFE;
explain (costs off, verbose) select ten, sp_simple_func(ten) from tenk1 where ten < 100orderby ten;
drop function sp_simple_func(integer);
-- test handling of SRFs in targetlist (bug in 10.0)
explain (costs off) select count(*), generate_series(1,2) from tenk1 groupby twenty;
select count(*), generate_series(1,2) from tenk1 groupby twenty;
-- test gather merge with parallel leader participation disabled set parallel_leader_participation = off;
explain (costs off) select count(*) from tenk1 groupby twenty;
select count(*) from tenk1 groupby twenty;
reset parallel_leader_participation;
--test rescan behavior of gather merge set enable_material = false;
explain (costs off) select * from
(select string4, count(unique2) from tenk1 groupby string4 orderby string4) ss rightjoin (values (1),(2),(3)) v(x) ontrue;
select * from
(select string4, count(unique2) from tenk1 groupby string4 orderby string4) ss rightjoin (values (1),(2),(3)) v(x) ontrue;
-- gather merge test with a LIMIT explain (costs off) select fivethous from tenk1 orderby fivethous limit4;
select fivethous from tenk1 orderby fivethous limit4;
-- gather merge test with 0 worker set max_parallel_workers = 0; explain (costs off) select string4 from tenk1 orderby string4 limit5; select string4 from tenk1 orderby string4 limit5;
-- gather merge test with 0 workers, with parallel leader -- participation disabled (the leader will have to run the plan -- despite the setting) set parallel_leader_participation = off; explain (costs off) select string4 from tenk1 orderby string4 limit5; select string4 from tenk1 orderby string4 limit5;
create function parallel_safe_volatile(a int) returns intas
$$ begin return a; end; $$ parallel safe volatile language plpgsql;
-- Test gather merge atop of a sort of a partial path explain (costs off) select * from tenk1 where four = 2 orderby four, hundred, parallel_safe_volatile(thousand);
-- Test gather merge atop of an incremental sort a of partial path set min_parallel_index_scan_size = 0; set enable_seqscan = off;
explain (costs off) select * from tenk1 where four = 2 orderby four, hundred, parallel_safe_volatile(thousand);
-- Test GROUP BY with a gather merge path atop of a sort of a partial path explain (costs off) select count(*) from tenk1 groupby twenty, parallel_safe_volatile(two);
drop function parallel_safe_volatile(int);
SAVEPOINT settings; SET LOCAL debug_parallel_query = 1; explain (costs off) select stringu1::int2from tenk1 where unique1 = 1;
ROLLBACK TO SAVEPOINT settings;
-- exercise record typmod remapping between backends CREATE FUNCTION make_record(n int)
RETURNS RECORD LANGUAGE plpgsql PARALLEL SAFE AS
$$
BEGIN RETURNCASE n WHEN1THEN ROW(1) WHEN2THEN ROW(1, 2) WHEN3THEN ROW(1, 2, 3) WHEN4THEN ROW(1, 2, 3, 4) ELSE ROW(1, 2, 3, 4, 5)
END;
END;
$$;
SAVEPOINT settings; SET LOCAL debug_parallel_query = 1; SELECT make_record(x) FROM (SELECT generate_series(1, 5) x) ss ORDERBY x;
ROLLBACK TO SAVEPOINT settings; DROP function make_record(n int);
-- test the sanity of parallel query after the active role is dropped. drop role ifexists regress_parallel_worker; create role regress_parallel_worker; set role regress_parallel_worker;
reset session authorization; drop role regress_parallel_worker; set debug_parallel_query = 1; select count(*) from tenk1;
reset debug_parallel_query;
reset role;
-- Window function calculation can't be pushed to workers. explain (costs off, verbose) select count(*) from tenk1 a where (unique1, two) in
(select unique1, row_number() over() from tenk1 b);
-- LIMIT/OFFSET within sub-selects can't be pushed to workers. explain (costs off) select * from tenk1 a where two in
(select two from tenk1 b where stringu1 like'%AAAA'limit3);
-- to increase the parallel query test coverage
SAVEPOINT settings; SET LOCAL debug_parallel_query = 1; EXPLAIN (analyze, timing off, summary off, costs off, buffers off) SELECT * FROM tenk1;
ROLLBACK TO SAVEPOINT settings;
-- provoke error in worker -- (make the error message long enough to require multiple bufferloads)
SAVEPOINT settings; SET LOCAL debug_parallel_query = 1; select (stringu1 || repeat('abcd', 5000))::int2from tenk1 where unique1 = 1;
ROLLBACK TO SAVEPOINT settings;
-- test interaction with set-returning functions
SAVEPOINT settings;
-- multiple subqueries under a single Gather node -- must set parallel_setup_cost > 0 to discourage multiple Gather nodes SET LOCAL parallel_setup_cost = 10; EXPLAIN (COSTS OFF) SELECT unique1 FROM tenk1 WHERE fivethous = tenthous + 1 UNIONALL SELECT unique1 FROM tenk1 WHERE fivethous = tenthous + 1;
ROLLBACK TO SAVEPOINT settings;
-- can't use multiple subqueries under a single Gather node due to initPlans EXPLAIN (COSTS OFF) SELECT unique1 FROM tenk1 WHERE fivethous =
(SELECT unique1 FROM tenk1 WHERE fivethous = 1LIMIT1) UNIONALL SELECT unique1 FROM tenk1 WHERE fivethous =
(SELECT unique2 FROM tenk1 WHERE fivethous = 1LIMIT1) ORDERBY1;
-- test interaction with SRFs SELECT * FROM information_schema.foreign_data_wrapper_options ORDERBY1, 2, 3;
-- must disallow pushing sort below gather when pathkey contains an SRF EXPLAIN (VERBOSE, COSTS OFF) SELECT unnest(ARRAY[]::integer[]) + 1AS pathkey FROM tenk1 t1 JOIN tenk1 t2 ONTRUE ORDERBY pathkey;
-- test passing expanded-value representations to workers CREATE FUNCTION make_some_array(int,int) returns int[] as
$$declare x int[];
begin
x[1] := $1;
x[2] := $2; return x;
end$$ language plpgsql parallel safe; CREATETABLE fooarr(f1 text, f2 int[], f3 text); INSERTINTO fooarr VALUES('1', ARRAY[1,2], 'one');
PREPARE pstmt(text, int[]) ASSELECT * FROM fooarr WHERE f1 = $1AND f2 = $2; EXPLAIN (COSTS OFF) EXECUTE pstmt('1', make_some_array(1,2));
EXECUTE pstmt('1', make_some_array(1,2));
DEALLOCATE pstmt;
-- test interaction between subquery and partial_paths CREATE VIEW tenk1_vw_sec WITH (security_barrier) ASSELECT * FROM tenk1; EXPLAIN (COSTS OFF) SELECT1FROM tenk1_vw_sec WHERE (SELECT sum(f1) FROM int4_tbl WHERE f1 < unique1) < 100;
rollback;
-- test that a newly-created session role propagates to workers.
begin; create role regress_parallel_worker; set session authorization regress_parallel_worker; select current_setting('session_authorization'); set debug_parallel_query = 1; select current_setting('session_authorization');
rollback;
-- test that function option SET ROLE works in parallel workers. create role regress_parallel_worker;
create function set_and_report_role() returns text as
$$ select current_setting('role') $$ language sql parallel safe set role = regress_parallel_worker;
create function set_role_and_error(int) returns intas
$$ select1 / $1 $$ language sql parallel safe set role = regress_parallel_worker;
set debug_parallel_query = 0; select set_and_report_role(); select set_role_and_error(0); set debug_parallel_query = 1; select set_and_report_role(); select set_role_and_error(0);
reset debug_parallel_query;
drop function set_and_report_role(); drop function set_role_and_error(int); drop role regress_parallel_worker;
-- don't freeze in ParallelFinish while holding an LWLock
BEGIN;
CREATE FUNCTION my_cmp (int4, int4)
RETURNS int LANGUAGE sqlAS
$$ SELECT CASEWHEN $1 < $2THEN -1 WHEN $1 > $2THEN1 ELSE0
END;
$$;
CREATETABLE parallel_hang (i int4); INSERTINTO parallel_hang
(SELECT * FROM generate_series(1, 400) gs);
CREATE OPERATOR CLASS int4_custom_ops FOR TYPE int4USING btree AS
OPERATOR 1 < (int4, int4), OPERATOR 2 <= (int4, int4),
OPERATOR 3 = (int4, int4), OPERATOR 4 >= (int4, int4),
OPERATOR 5 > (int4, int4), FUNCTION 1 my_cmp(int4, int4);
CREATEUNIQUEINDEX parallel_hang_idx ON parallel_hang USING btree (i int4_custom_ops);
SET debug_parallel_query = on; DELETEFROM parallel_hang WHERE380 <= i AND i <= 420;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.