How can I disable parallel queries in PostgreSQL?
-
20-01-2021 - |
سؤال
I want to benchmark some things in PostgreSQL. When I do this I normally turn off some of the features the database is currently using, just for my session, to see what the second-best plan is.
Though setting max_parallel_works = 0
, I still get a parallel plan.
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..120892.01 rows=1000 width=15) (actual time=0.444..4781.539 rows=666667 loops=1)
Workers Planned: 2
Workers Launched: 0
-> Parallel Seq Scan on foo (cost=0.00..119792.01 rows=417 width=15) (actual time=0.102..4711.530 rows=666667 loops=1)
Filter: (lower(x) ~~ '%999.com'::text)
Rows Removed by Filter: 9333333
Planning time: 0.124 ms
Execution time: 4801.491 ms
(8 rows)
I know it says Workers Launched: 0
. But, I would like that to look like a pre-Parallel plan where the plan itself is not even planned to be parallel. How can I accomplish this?
المحلول
From the docs,
max_parallel_workers_per_gather (integer)
Sets the maximum number of workers that can be started by a single Gather or Gather Merge node. Parallel workers are taken from the pool of processes established bymax_worker_processes
, limited bymax_parallel_workers
. Note that the requested number of workers may not actually be available at run time. If this occurs, the plan will run with fewer workers than expected, which may be inefficient. The default value is2
. Setting this value to0
disables parallel query execution.
So to disable the parallel query planning use,
SET max_parallel_workers_per_gather = 0;
You can see that here, same query,
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
Seq Scan on foo (cost=0.00..207292.03 rows=1000 width=15) (actual time=0.082..4715.721 rows=666667 loops=1)
Filter: (lower(x) ~~ '%999.com'::text)
Rows Removed by Filter: 9333333
Planning time: 0.083 ms
Execution time: 4736.202 ms