Re: Wrong plan with extra parallel workers

From: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
To: Guilherme Pereira <guiperpt(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Wrong plan with extra parallel workers
Date: 2018-04-27 12:47:59
Message-ID: CAA4eK1JMgdood_M9o4Xj_ou-tQvbLrLRrcy0Y9znFYMbf2hfAA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Apr 27, 2018 at 2:48 PM, Guilherme Pereira <guiperpt(at)gmail(dot)com> wrote:
> Hi,
>
> Having a strange situation, where adding extra parallel workers
> (max_parallel_workers_per_gather), the planner chooses a different plan,

I think I can see what is going on here. The planner chooses a
different plan because the cost of that plan
(cost=1001.10..31949141.56) is cheap as compared to the original plan
(cost=31676816.72..32928717.16)

> with nested loops, which makes the query twice as slow.

It is slow because it needs to transmit many rows (rows=23845842) from
workers to master backend.

> Strangely with the
> COUNT_DISTINCT implementation from Tomas Vondra
> (https://github.com/tvondra/count_distinct) it scales nicely (almost
> linearly) with the workers.
>

The main difference is that count_distinct is a parallel_safe
aggregate function which can be allowed to push down to workers which
in turn lead to very few rows (rows=9) being transferred from workers
to master backend.

I think ideally it should not have picked the plan which leads to
transmitting so many rows, but the cost turns out to be lesser.

--
With Regards,
Amit Kapila.
EnterpriseDB: http://www.enterprisedb.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2018-04-27 13:29:32 Re: Toast issues with OldestXmin going backwards
Previous Message Stephen Frost 2018-04-27 12:26:30 Re: "could not reattach to shared memory" on buildfarm member dory