PostgresChina2018 刘东明 PostgreSQL并行查询
PostgresChina2018 刘东明 PostgreSQL并行查询
About Me
• Liu Dongming (刘东明)
• DRDS / PostgreSQL
• Alibaba Cloud PostgreSQL Group
2018年PostgreSQL中国技术大会
Parallel Features
• PostgreSQL 9.4, 9.5 [2014, 2015]
– Backgound workers
– Dynamic shared memory(DSM)
– Shared memory queues
• PostgreSQL 10 [2017]
– Partitions
– Executor nodes:Gather Merge,Parallel Index Scan,Parallel Bitmap Heap Scan
• PostgreSQL 11 [2018]
– Executor nodes:Parallel Append,Parallel Hash Join
– Planner:Partition-wise joins
– Parallel Create Index
2018年PostgreSQL中国技术大会
https://www.karlrupp.net/2018/02/42-years-of-microprocessor-trend-data/
https://en.wikipedia.org/wiki/Herb_Sutter#The_Free_Lunch_Is_Over
2018年PostgreSQL中国技术大会
• Shared-memory – All processors have equal access to a global memory and all disks
• Shared-disk – Each processor has its own private memory, but has equal access to all disks
• Shared-nothing – Each processor has its own private memory and disk(s)
2018年PostgreSQL中国技术大会
For Example
SELECT COUNT(*)
FROM people
WHERE inpgconn2018 = 'Y';
2018年PostgreSQL中国技术大会
Parallel Plan
Worker(W):each worker runs a Finalize
copy of the plan fragment beneath of Aggregate
the Gather node.
W L W
2018年PostgreSQL中国技术大会
Parser
Rewriter
Planner
Executor
postgres
(server process)
fork
fork kill
background
postgres worker
Client
(backend process) background
worker
2018年PostgreSQL中国技术大会
W
2018年PostgreSQL中国技术大会
W W
How parallel queries are executed?
2018年PostgreSQL中国技术大会
parallel-aware node
Node with Parallel prefix can be called parallel-aware operators.
Parallel-oblivious node is one where the node is unaware that it is part of a parallel plan.
Parallel
Parallel Seq Parallel Index Parallel Hash
Bitmap Heap
Scan Scan Join
Scan
Bitmap Heap
Seq Scan Index Scan Hash Join
Scan
2018年PostgreSQL中国技术大会
L W W
next
next
L W W
Parallel
• Similar to Parallel Seq Scan, but scan only pages that were found Bitmap Heap
to potentially contain interesting tuples Scan
Merge Join
The inner side is always a non-
parallel plan and therefore
executed in full. Gather
Hash Join
The inner side is executed in full by
every cooperating process to build
identical copies of the hash table.
This may be inefficient if the hash Gather
table is large or the plan is
expensive.
Partition-wise join
Divide and conquer for joins between partitioned table.
Append
Nest Loop
Merge Join Hash Join
Join
Parallel Append
Parallel
Parallel
Parallel
Append
Append
Append
Nest Loop
Merge Join Hash Join
Join
Cost-base Planner
①
① Think of all ways we could execute a query
② Estimate the runtime of each path, than
path path path path ...
choose the cheapest path
③ Convert path into a plan ready for execution
②
Parallel path
Gather
Nest Loop
VS
Join
Partial Nest
Loop Join
Partial Seq
Index Scan
Scan
2018年PostgreSQL中国技术大会
Costs
• SET parallel_setup_cost = 1000
– Cost of setting up shared memory for parallelism, and launching
workers.
– Discourage parallel query for short queries
Future work
• More operators support parallelism, such as sort
• Dynamic repartitioning
• Cost-based planning of parallel degree?
2018年PostgreSQL中国技术大会
References
• https://speakerdeck.com/macdice/parallelism-in-postgresql-11
• https://www.postgresql.org/docs/11/parallel-plans.html#PARALLEL-JOINS
• http://rhaas.blogspot.com/2013/10/parallelism-progress.html
• http://ashutoshpg.blogspot.com/2017/12/partition-wise-joins-divide-and-conquer.html
• http://www.gotw.ca/publications/concurrency-ddj.htm
• https://www.enterprisedb.com/blog/parallel-hash-postgresql
• https://write-skew.blogspot.com/2018/01/parallel-hash-for-postgresql.html
• http://amitkapila16.blogspot.com/2015/11/parallel-sequential-scans-in-play.html
• https://blog.2ndquadrant.com/parallel-aggregate/
Welcome to Alibaba Cloud
Database Technology Group.
[email protected]
Thanks