mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-12 17:37:07 +08:00
Like Gather, we spawn multiple workers and run the same plan in each one; however, Gather Merge is used when each worker produces the same output ordering and we want to preserve that output ordering while merging together the streams of tuples from various workers. (In a way, Gather Merge is like a hybrid of Gather and MergeAppend.) This works out to a win if it saves us from having to perform an expensive Sort. In cases where only a small amount of data would need to be sorted, it may actually be faster to use a regular Gather node and then sort the results afterward, because Gather Merge sometimes needs to wait synchronously for tuples whereas a pure Gather generally doesn't. But if this avoids an expensive sort then it's a win. Rushabh Lathia, reviewed and tested by Amit Kapila, Thomas Munro, and Neha Sharma, and reviewed and revised by me. Discussion: http://postgr.es/m/CAGPqQf09oPX-cQRpBKS0Gq49Z+m6KBxgxd_p9gX8CKk_d75HoQ@mail.gmail.com
107 lines
3.0 KiB
PL/PgSQL
107 lines
3.0 KiB
PL/PgSQL
--
|
|
-- PARALLEL
|
|
--
|
|
|
|
create or replace function parallel_restricted(int) returns int as
|
|
$$begin return $1; end$$ language plpgsql parallel restricted;
|
|
|
|
-- Serializable isolation would disable parallel query, so explicitly use an
|
|
-- arbitrary other level.
|
|
begin isolation level repeatable read;
|
|
|
|
-- 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;
|
|
|
|
explain (costs off)
|
|
select count(*) from a_star;
|
|
select count(*) from a_star;
|
|
|
|
-- test that parallel_restricted function doesn't run in worker
|
|
alter table tenk1 set (parallel_workers = 4);
|
|
explain (verbose, costs off)
|
|
select parallel_restricted(unique1) from tenk1
|
|
where stringu1 = 'GRAAAA' order by 1;
|
|
|
|
-- test parallel plan when group by expression is in target list.
|
|
explain (costs off)
|
|
select length(stringu1) from tenk1 group by length(stringu1);
|
|
select length(stringu1) from tenk1 group by length(stringu1);
|
|
|
|
explain (costs off)
|
|
select stringu1, count(*) from tenk1 group by stringu1 order by stringu1;
|
|
|
|
-- test that parallel plan for aggregates is not selected when
|
|
-- target list contains parallel restricted clause.
|
|
explain (costs off)
|
|
select sum(parallel_restricted(unique1)) from tenk1
|
|
group by(parallel_restricted(unique1));
|
|
|
|
-- test parallel plans for queries containing un-correlated subplans.
|
|
alter table tenk2 set (parallel_workers = 0);
|
|
explain (costs off)
|
|
select count(*) from tenk1 where (two, four) not in
|
|
(select hundred, thousand from tenk2 where thousand > 100);
|
|
select count(*) from tenk1 where (two, four) not in
|
|
(select hundred, thousand from tenk2 where thousand > 100);
|
|
alter table tenk2 reset (parallel_workers);
|
|
|
|
-- test parallel index scans.
|
|
set enable_seqscan to off;
|
|
set enable_bitmapscan to off;
|
|
|
|
explain (costs off)
|
|
select count((unique1)) from tenk1 where hundred > 1;
|
|
select count((unique1)) from tenk1 where hundred > 1;
|
|
|
|
-- test parallel index-only scans.
|
|
explain (costs off)
|
|
select count(*) from tenk1 where thousand > 95;
|
|
select count(*) from tenk1 where thousand > 95;
|
|
|
|
reset enable_seqscan;
|
|
reset enable_bitmapscan;
|
|
|
|
-- test parallel bitmap heap scan.
|
|
set enable_seqscan to off;
|
|
set enable_indexscan to off;
|
|
|
|
explain (costs off)
|
|
select count((unique1)) from tenk1 where hundred > 1;
|
|
|
|
reset enable_seqscan;
|
|
reset enable_indexscan;
|
|
|
|
-- 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 gather merge
|
|
set enable_hashagg to off;
|
|
|
|
explain (costs off)
|
|
select string4, count((unique2)) from tenk1 group by string4 order by string4;
|
|
|
|
select string4, count((unique2)) from tenk1 group by string4 order by string4;
|
|
|
|
reset enable_hashagg;
|
|
|
|
set force_parallel_mode=1;
|
|
|
|
explain (costs off)
|
|
select stringu1::int2 from tenk1 where unique1 = 1;
|
|
|
|
-- provoke error in worker
|
|
select stringu1::int2 from tenk1 where unique1 = 1;
|
|
|
|
rollback;
|