Re: [PROPOSAL] Expand OR clauses in joins to UNION ALL paths

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Ayoub Kazar <ayoub(dot)kazar(at)data-bene(dot)io>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PROPOSAL] Expand OR clauses in joins to UNION ALL paths
Date: 2026-10-12 22:13:11
Message-ID: CAN4CZFOPWhh11CD8koPZ9NndemDY2og5qZeMMxmNvvUg6L5uMg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

+ /* Only plain base relations are permitted */
+ if (rte->rtekind != RTE_RELATION)
+ return false;

This will even reject simple GROUP BY (RTE_GROUP)

SELECT a.x FROM a JOIN b ... WHERE a.f=1 OR b.g=2 GROUP BY a.x

And there are probably many other interesting query features the patch
could support without problems.

I also want to point out that this can result in queries that
otherwise work on master erroring out with the patch:

CREATE TABLE item (id int PRIMARY KEY, kind text);
CREATE TABLE attr (item_id int, val text, tag int);
INSERT INTO item
SELECT g, CASE WHEN g % 100 = 0 THEN 'txt' ELSE 'num' END
FROM generate_series(1, 100000) g;
INSERT INTO attr
SELECT g, CASE WHEN g % 100 = 0 THEN 'abc' ELSE (g % 500)::text END,
CASE WHEN g % 100 = 0 THEN 7 ELSE g % 1000 END
FROM generate_series(1, 100000) g;
CREATE INDEX ON item (kind);
CREATE INDEX ON attr (tag);
ANALYZE item, attr;

SELECT count(*) FROM item i JOIN attr a ON a.item_id = i.id
WHERE i.kind = 'txt' OR (a.val::int = 7 AND a.tag = 7);

Every 'txt' item in this database fails the right side of the OR, but
because the left side is true, the right side is never evaluated.
The evaluation order isn't guaranteed in this case, the documentation
explicitly tells people not to do this. So in that sense, breaking it
isn't wrong, but it is still a noticeable behavior change.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-10-12 22:29:15 Re: [Patch] New pg_stat_tablespace view
Previous Message Greg Burd 2026-10-12 22:00:45 Re: Tepid: selective index updates for heap relations