Re: BUG #16885: PostgreSQL upgrade to 12+ changes hash join to slow nested loop

From: Tavin Cole <tavin(dot)cole(at)gmail(dot)com>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: tavincole+pgbg(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #16885: PostgreSQL upgrade to 12+ changes hash join to slow nested loop
Date: 2021-02-22 14:12:55
Message-ID: CAP93hfyFhUtat3pEkff8m9Z-Yke0vOTTUSsfM-uvdpYO=b34ng@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Mon, Feb 22, 2021 at 11:56 AM Magnus Hagander <magnus(at)hagander(dot)net>
wrote:

>
> You'll have to post your actual query to get proper feedback.
>
> That said, a typical case for this specifically when going to version
> 12 would be that CTEs are no longer optimization barriers. If you do
> use CTEs in your query, try changing them from "WITH x AS (...)" to
> "WITH x AS MATERIALIZED (...)" to return to the previous behaviour and
> see if that improves things.
>

I don't know if I will be allowed to publicly post the actual query. I can
say it's quite complicated, and involves the following features:

* an inner SELECT DISTINCT from a particular primary table and some joins
* a join to the same primary table in the outer query, among others
* a join to a view in the outer query
* said view uses CTEs like you say, though based on unrelated tables
* then said view makes a UNION of 2 different SELECTs
* each of these again joins to the same primary table

So I'm not surprised the query planner gets confused. And your trick works.
Materializing those CTEs makes the query planner choose the hash join again.

I still think there is a bug here though. The query planner is getting
something really wrong, and at least now we know it's a consequence of
trying to optimize through the CTEs.

I will see what I can do about sharing the actual query. If this is
interesting to a pg developer, you're welcome to contact me.

Best,
Tavin

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Pavel Stehule 2021-02-22 14:21:21 Re: BUG #16885: PostgreSQL upgrade to 12+ changes hash join to slow nested loop
Previous Message Joachim Gehweiler 2021-02-22 13:07:07 Re: BUG #16827: macOS interrupted syscall leads to a crash