如何在 PostgreSQL 中禁用并行查询?

Eva*_*oll 4 postgresql performance parallelism configuration query-performance

我想对 PostgreSQL 中的一些东西进行基准测试。当我这样做时,我通常会关闭数据库当前正在使用的一些功能,仅用于我的会话,以查看次优计划是什么。

虽然设置max_parallel_works = 0,我仍然得到一个平行的计划。

                                                         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)
Run Code Online (Sandbox Code Playgroud)

我知道它说Workers Launched: 0。但是,我希望它看起来像一个预平行计划,其中计划本身甚至没有计划为平行计划。我怎样才能做到这一点?

Eva*_*oll 9

从文档中,

max_parallel_workers_per_gather (integer)设置单个 Gather 或 Gather Merge 节点可以启动的最大工作线程数。并行工作程序从由 建立、由max_worker_processes限制的进程池中提取max_parallel_workers。请注意,请求的工作人员数量可能在运行时实际上不可用。如果发生这种情况,计划将使用比预期更少的工人运行,这可能是低效的。默认值为2将此值设置为0禁用并行查询执行。

所以要禁用并行查询计划使用,

SET max_parallel_workers_per_gather = 0;
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到,同样的查询,

                                                  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
Run Code Online (Sandbox Code Playgroud)

  • `显示max_parallel_workers_per_gather;` (2认同)