标签
PostgreSQL , cpu 并行 , smp 并行 , 并行计算 , gpu 并行 , 并行过程支持
背景
PostgreSQL 11 优化器已经支持了非常多场合的并行。简单估计,已支持27余种场景的并行计算。
parallel seq scan
parallel index scan
parallel index only scan
parallel bitmap scan
parallel filter
parallel hash agg
parallel group agg
parallel cte
parallel subquery
parallel create table
parallel create index
parallel select into
parallel CREATE MATERIALIZED VIEW
parallel 排序 : gather merge
parallel nestloop join
parallel hash join
parallel merge join
parallel 自定义并行聚合
parallel 自定义并行UDF
parallel append
parallel append merge
parallel union all
parallel fdw table scan
parallel partition join
parallel partition agg
parallel gather
parallel gather merge
parallel rc 并行
parallel rr 并行
parallel GPU 并行
parallel unlogged table
lead parallel
接下来进行一一介绍。
关键知识请先自行了解:
1、优化器自动并行度算法 CBO
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
parallel FDW scan (并行访问多个外部表) with parallel append (FDW must with IsForeignScanParallelSafe)
FDW是PG的外部数据访问接口,支持超级多的外部数据源,PG优化器已经支持了FDW的并行访问接口,当你需要访问多个FDW外部表时,只要这个外部表FDW是parallel safe的,就可以结合parallel append实现多个外部表的并行扫描。
https://wiki.postgresql.org/wiki/Fdw
通过union all 或者 继承表访问,都能够实现fdw tables的并行扫描。 (前提是FDW handler是parallel safe的),从目前来看内置的file_fdw是safe的,而postgres_fdw不是。
Scans of foreign tables, unless the foreign data wrapper has an IsForeignScanParallelSafe API which indicates otherwise.
https://www.postgresql.org/docs/current/parallel-safety.html
https://stackoverflow.com/questions/52501783/need-parallel-append-on-foreign-tables-in-postgresql
https://www.postgresql.org/docs/11/file-fdw.html
数据量:10亿
db5=# copy (select generate_series(1,1000000000/24)) to '/data01/digoal/pg_root8001/file1' ;
COPY 41666666
create extension file_fdw;
CREATE SERVER pglog FOREIGN DATA WRAPPER file_fdw;
do language plpgsql $$
declare
begin
for i in 0..23 loop
execute format('CREATE FOREIGN TABLE ft%s (i int) server pglog options (filename ''/data01/digoal/pg_root8001/file1'')', i);
end loop;
end;
$$;
create table ft(like ft0);
do language plpgsql $$
declare
begin
for i in 0..23 loop
execute format('alter table ft%s inherit ft', i);
end loop;
end;
$$;
analyze ft;
db5=# explain verbose select * from ft0 limit 10;
QUERY PLAN
---------------------------------------------------------------------------------
Limit (cost=0.00..1.04 rows=10 width=4)
Output: i
-> Foreign Scan on public.ft0 (cost=0.00..1181573.80 rows=11371528 width=4)
Output: i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
(6 rows)
场景 | 数据量 | 关闭并行 | 开启并行 | 并行度 | 开启并行性能提升倍数 |
---|---|---|---|---|---|
parallel scan mult FDW tables (通过继承表方式) | 10亿 | 180 秒 | 7.8 秒 | 24 | 23.1 倍 |
parallel scan mult FDW tables (通过union all) | 10亿 | 165.6 秒 | 27.8 秒 | 5 | 6 倍 |
postgres=# show max_worker_processes ;
max_worker_processes
----------------------
128
(1 row)
postgres=# set min_parallel_table_scan_size =0;
postgres=# set min_parallel_index_scan_size =0;
postgres=# set parallel_tuple_cost =0;
postgres=# set parallel_setup_cost =0;
postgres=# set max_parallel_workers=128;
postgres=# set max_parallel_workers_per_gather =24;
postgres=# set enable_parallel_hash =on;
postgres=# set enable_parallel_append =on;
postgres=# set enable_partitionwise_aggregate =off;
postgres=# set work_mem ='128MB';
postgres=# set client_min_messages =debug;
postgres=# set pg_hint_plan.debug_print =on;
postgres=# set pg_hint_plan.enable_hint=on;
postgres=# set pg_hint_plan.message_level =debug;
1、关闭并行,耗时: 180 秒 , 165.6 秒。
postgres=# set max_parallel_workers_per_gather =0;
postgres=# set enable_parallel_append =off;
db5=# explain select count(*) from ft;
QUERY PLAN
---------------------------------------------------------------------------------
Aggregate (cost=43033406.87..43033406.88 rows=1 width=8)
-> Append (cost=0.00..42057423.08 rows=390393516 width=0)
-> Seq Scan on ft (cost=0.00..0.00 rows=1 width=0)
-> Foreign Scan on ft0 (cost=0.00..4211087.40 rows=41666664 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft1 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft2 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft3 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft4 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft5 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft6 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft7 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft8 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft9 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft10 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft11 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft12 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft13 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft14 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft15 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft16 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft17 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft18 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft19 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft20 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft21 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft22 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on ft23 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
(75 rows)
db5=# select count(*) from ft;
count
-----------
999999984
(1 row)
Time: 180065.413 ms (03:00.065)
explain
select * from (
select count(*) from ft0 union all
select count(*) from ft1 union all
select count(*) from ft2 union all
select count(*) from ft3 union all
select count(*) from ft4 union all
select count(*) from ft5 union all
select count(*) from ft6 union all
select count(*) from ft7 union all
select count(*) from ft8 union all
select count(*) from ft9 union all
select count(*) from ft10 union all
select count(*) from ft11 union all
select count(*) from ft12 union all
select count(*) from ft13 union all
select count(*) from ft14 union all
select count(*) from ft15 union all
select count(*) from ft16 union all
select count(*) from ft17 union all
select count(*) from ft18 union all
select count(*) from ft19 union all
select count(*) from ft20 union all
select count(*) from ft21 union all
select count(*) from ft22 union all
select count(*) from ft23
) as t;
QUERY PLAN
---------------------------------------------------------------------------------
Append (cost=4315254.06..41081439.89 rows=24 width=8)
-> Aggregate (cost=4315254.06..4315254.07 rows=1 width=8)
-> Foreign Scan on ft0 (cost=0.00..4211087.40 rows=41666664 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft1 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft2 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft3 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft4 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft5 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft6 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft7 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft8 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft9 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft10 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft11 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft12 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft13 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft14 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft15 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft16 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft17 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft18 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft19 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft20 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft21 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft22 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
-> Foreign Scan on ft23 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
(97 rows)
count
----------
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
(24 rows)
Time: 165555.591 ms (02:45.556)
2、开启并行,耗时: 7.8 秒 , 27.8 秒。
postgres=# set max_parallel_workers_per_gather =24;
postgres=# set enable_parallel_append =on;
db5=# explain verbose /*+ Parallel(ft 24 hard) */ select count(*) from ft;
DEBUG: pg_hint_plan:
used hint:
Parallel(ft 24 hard)
not used hint:
duplication hint:
error hint:
QUERY PLAN
----------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=195196.81..195196.82 rows=1 width=8)
Output: count(*)
-> Gather (cost=195196.74..195196.75 rows=24 width=8)
Output: (PARTIAL count(*))
Workers Planned: 24
-> Partial Aggregate (cost=195196.74..195196.75 rows=1 width=8)
Output: PARTIAL count(*)
-> Parallel Append (cost=0.00..0.00 rows=78078694 width=0)
-> Foreign Scan on public.ft0 (cost=0.00..4211087.40 rows=41666664 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft1 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft2 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft3 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft4 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft5 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft6 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft7 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft8 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft9 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft10 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft11 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft12 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft13 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft14 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft15 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft16 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft17 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft18 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft19 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft20 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft21 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft22 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Foreign Scan on public.ft23 (cost=0.00..1560624.70 rows=15162037 width=0)
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Seq Scan on public.ft (cost=0.00..0.00 rows=1 width=0)
(81 rows)
/*+ Parallel(ft 24 hard) */ select count(*) from ft;
count
-----------
999999984
(1 row)
Time: 7779.985 ms (00:07.780)
db5=# explain verbose select sum(count) from (
select count(*) from ft0 union all
select count(*) from ft1 union all
select count(*) from ft2 union all
select count(*) from ft3 union all
select count(*) from ft4 union all
select count(*) from ft5 union all
select count(*) from ft6 union all
select count(*) from ft7 union all
select count(*) from ft8 union all
select count(*) from ft9 union all
select count(*) from ft10 union all
select count(*) from ft11 union all
select count(*) from ft12 union all
select count(*) from ft13 union all
select count(*) from ft14 union all
select count(*) from ft15 union all
select count(*) from ft16 union all
select count(*) from ft17 union all
select count(*) from ft18 union all
select count(*) from ft19 union all
select count(*) from ft20 union all
select count(*) from ft21 union all
select count(*) from ft22 union all
select count(*) from ft23
) as t;
QUERY PLAN
----------------------------------------------------------------------------------------------
Gather (cost=1598529.79..9110843.52 rows=24 width=8)
Output: (count(*))
Workers Planned: 5
-> Parallel Append (cost=1598529.79..9110843.52 rows=1 width=8)
-> Aggregate (cost=4315254.06..4315254.07 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft0 (cost=0.00..4211087.40 rows=41666664 width=0)
Output: ft0.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft1 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft1.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft2 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft2.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft3 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft3.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft4 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft4.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft5 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft5.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft6 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft6.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft7 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft7.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft8 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft8.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft9 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft9.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft10 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft10.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft11 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft11.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft12 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft12.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft13 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft13.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft14 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft14.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft15 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft15.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft16 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft16.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft17 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft17.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft18 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft18.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft19 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft19.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft20 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft20.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft21 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft21.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft22 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft22.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
-> Aggregate (cost=1598529.79..1598529.80 rows=1 width=8)
Output: count(*)
-> Foreign Scan on public.ft23 (cost=0.00..1560624.70 rows=15162037 width=0)
Output: ft23.i
Foreign File: /data01/digoal/pg_root8001/file1
Foreign File Size: 363888891 b
(148 rows)
count
----------
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
41666666
(24 rows)
Time: 27778.411 ms (00:27.778)
1、当多个外部表在union all子句中期望被并行扫描时,并行度不太好控制,即使使用HINT。
2、当多个外部表继承在一个普通表上时,并行度更好控制,直接使用外部表的父表(普通表)来控制即可(通过HINT)。
其他知识
1、优化器自动并行度算法 CBO
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
2、function, op 识别是否支持parallel
postgres=# select proparallel,proname from pg_proc;
proparallel | proname
-------------+----------------------------------------------
s | boolin
s | boolout
s | byteain
s | byteaout
3、subquery mapreduce unlogged table
对于一些情况,如果期望简化优化器对非常非常复杂的SQL并行优化的负担,可以自己将SQL拆成几段,中间结果使用unlogged table保存,类似mapreduce的思想。unlogged table同样支持parallel 计算。
4、vacuum,垃圾回收并行。
5、dblink 异步调用并行
《PostgreSQL VOPS 向量计算 + DBLINK异步并行 - 单实例 10亿 聚合计算跑进2秒》
《PostgreSQL 相似搜索分布式架构设计与实践 - dblink异步调用与多机并行(远程 游标+记录 UDF实例)》
暂时不允许并行的场景(将来PG会继续扩大支持范围):
1、修改行,锁行,除了create table as , select into, create mview这几个可以使用并行。
2、query 会被中断时,例如cursor , loop in PL/SQL ,因为涉及到中间处理,所以不建议开启并行。
3、paralle unsafe udf ,这种UDF不会并行
4、嵌套并行(udf (内部query并行)),外部调用这个UDF的SQL不会并行。(主要是防止large parallel workers )
5、SSI 隔离级别
参考
https://www.postgresql.org/docs/11/parallel-plans.html
《PostgreSQL 11 并行计算算法,参数,强制并行度设置》
《PostgreSQL 11 preview - 并行计算 增强 汇总》
《PostgreSQL 10 自定义并行计算聚合函数的原理与实践 - (含array_agg合并多个数组为单个一元数组的例子)》