Re: BUG #17964: Missed query planner optimization

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: mathiaskunter(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17964: Missed query planner optimization
Date: 2023-06-06 21:32:10
Message-ID: CAApHDvpj1rjTsOn34K0W9WpVptQHJkMQHt+db0FVWOxVK0OewQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, 7 Jun 2023 at 04:44, PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
> In the example below, the query planner uses a sequential scan (query 1)
> even though it could use an index scan (query 2).
>
> EXPLAIN ANALYZE SELECT id, name FROM (SELECT id, name FROM table1 UNION
> SELECT id, name FROM table2) AS q
> WHERE id IN (SELECT id FROM table3);

> EXPLAIN ANALYZE SELECT id, name FROM (SELECT id, name FROM table1 UNION
> SELECT id, name FROM table2) AS q
> WHERE id IN (1538,8836,5486,3464,2673);

It's not a bug that the planner does not consider evaluating the join
before the UNION, it's just an optimisation opportunity we don't
currently explore.

If you want that, then write:

EXPLAIN ANALYZE SELECT id, name FROM table1 WHERE id IN (SELECT id
FROM table3) UNION SELECT id, name FROM table2 WHERE id IN (SELECT id
FROM table3);

David

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2023-06-07 07:46:20 BUG #17965: SIGSEGV from LLVM13 JIT when executing SQL.
Previous Message Tom Lane 2023-06-06 13:07:11 Re: BUG #17963: Recursive table cannot be referenced when using LEFT JOIN instead of LATERAL in recursive term