PlaceholderVars and join removal with appendrel parents

Started by Arne Roland4 months ago5 messageshackers
Jump to latest
#1Arne Roland
arne.roland@malkut.net

Hi Richard,

thank you for your great work with the optimizer!

I just stumbled and by accident landed on your commit 9a60f295bcb (and
then looked up the discussion
/messages/by-id/CALzhyqwryL2QywgO03VQr_237Sq3MEVgTTT2_A9G3nGT5-SRZg@mail.gmail.com).
I am under the impression we need to extend the same fix for or clauses
descending into the orclause sub-RestrictInfos.

This is the simplified minimal test case, that is failing for me:

CREATE TABLE a (id int PRIMARY KEY, b_id int);
CREATE TABLE uniq (id int PRIMARY KEY);
CREATE TABLE parted_b (id int PRIMARY KEY) PARTITION BY RANGE (id);
CREATE TABLE parted_b1 PARTITION OF parted_b FOR VALUES FROM (0) TO (10);

SELECT 1 FROM a, (SELECT t1.id FROM parted_b t1 LEFT JOIN uniq t2 ON
t1.id = t2.id) s
WHERE (s.id = 1 AND a.id = 2) OR (s.id = 3 AND a.id = 4) GROUP BY ();

My idea would be to put a bitmapset into remove_rel_from_query with the
seen ones and recurse removing it from the rinfo. I attached a sketch of
my idea. But maybe our have a better one.

Regards
Arne

Attachments:

phv_joininfo.patchtext/x-patch; charset=UTF-8; name=phv_joininfo.patchDownload+55-2
#2Tender Wang
tndrwang@gmail.com
In reply to: Arne Roland (#1)
Re: PlaceholderVars and join removal with appendrel parents

Arne Roland <arne.roland@malkut.net> 于2026年7月13日周一 18:24写道:

Hi Richard,

thank you for your great work with the optimizer!

I just stumbled and by accident landed on your commit 9a60f295bcb (and
then looked up the discussion
/messages/by-id/CALzhyqwryL2QywgO03VQr_237Sq3MEVgTTT2_A9G3nGT5-SRZg@mail.gmail.com).
I am under the impression we need to extend the same fix for or clauses
descending into the orclause sub-RestrictInfos.

This is the simplified minimal test case, that is failing for me:

CREATE TABLE a (id int PRIMARY KEY, b_id int);
CREATE TABLE uniq (id int PRIMARY KEY);
CREATE TABLE parted_b (id int PRIMARY KEY) PARTITION BY RANGE (id);
CREATE TABLE parted_b1 PARTITION OF parted_b FOR VALUES FROM (0) TO (10);

SELECT 1 FROM a, (SELECT t1.id FROM parted_b t1 LEFT JOIN uniq t2 ON
t1.id = t2.id) s
WHERE (s.id = 1 AND a.id = 2) OR (s.id = 3 AND a.id = 4) GROUP BY ();

Yeah, the last fix can't cover this case, because the
otherrel->baserestrictinfo is NIL when trying to remove outerjoin rel
from phv.
The the otherrel->baserestrictinfo would be assigned when calling
extract_restriction_or_clauses() in query_planner().
But in extract_restriction_or_clauses(), we had no way to make the thing right.

My idea would be to put a bitmapset into remove_rel_from_query with the
seen ones and recurse removing it from the rinfo. I attached a sketch of
my idea. But maybe our have a better one.

It seems that we have to process otherrel's baserestrictinfo and
joininfo if the query involves a partition table.
If we ignore one of them in add_other_rels_to_query(), the rel that
was removed is brought back to the child rel.
Then the crash would happen.

I took a quick look at this patch. It seems workable.

--
Thanks,
Tender Wang

#3Arne Roland
arne.roland@malkut.net
In reply to: Tender Wang (#2)
Re: PlaceholderVars and join removal with appendrel parents

Hi Tender,

thanks for having a look here again.

On 2026-07-13 3:44 PM, Tender Wang wrote:

Arne Roland <arne.roland@malkut.net> 于2026年7月13日周一 18:24写道:

Hi Richard,

thank you for your great work with the optimizer!

I just stumbled and by accident landed on your commit 9a60f295bcb (and
then looked up the discussion
/messages/by-id/CALzhyqwryL2QywgO03VQr_237Sq3MEVgTTT2_A9G3nGT5-SRZg@mail.gmail.com).
I am under the impression we need to extend the same fix for or clauses
descending into the orclause sub-RestrictInfos.

This is the simplified minimal test case, that is failing for me:

CREATE TABLE a (id int PRIMARY KEY, b_id int);
CREATE TABLE uniq (id int PRIMARY KEY);
CREATE TABLE parted_b (id int PRIMARY KEY) PARTITION BY RANGE (id);
CREATE TABLE parted_b1 PARTITION OF parted_b FOR VALUES FROM (0) TO (10);

SELECT 1 FROM a, (SELECT t1.id FROM parted_b t1 LEFT JOIN uniq t2 ON
t1.id = t2.id) s
WHERE (s.id = 1 AND a.id = 2) OR (s.id = 3 AND a.id = 4) GROUP BY ();

Yeah, the last fix can't cover this case, because the
otherrel->baserestrictinfo is NIL when trying to remove outerjoin rel
from phv.
The the otherrel->baserestrictinfo would be assigned when calling
extract_restriction_or_clauses() in query_planner().
But in extract_restriction_or_clauses(), we had no way to make the thing right.

My idea would be to put a bitmapset into remove_rel_from_query with the
seen ones and recurse removing it from the rinfo. I attached a sketch of
my idea. But maybe our have a better one.

It seems that we have to process otherrel's baserestrictinfo and
joininfo if the query involves a partition table.

The same should be true for union statements, plain inheritance and
pretty much anything, which creates an append.

If we ignore one of them in add_other_rels_to_query(), the rel that
was removed is brought back to the child rel.
Then the crash would happen.

I took a quick look at this patch. It seems workable.

Thank you, reviews are valuable!

Regards
Arne

#4Richard Guo
guofenglinux@gmail.com
In reply to: Arne Roland (#1)
Re: PlaceholderVars and join removal with appendrel parents

On Mon, Jul 13, 2026 at 7:23 PM Arne Roland <arne.roland@malkut.net> wrote:

My idea would be to put a bitmapset into remove_rel_from_query with the
seen ones and recurse removing it from the rinfo. I attached a sketch of
my idea. But maybe our have a better one.

Thanks for the report and the patch. I think the fix is on the right
track. (BTW, the seen_serials bitmap is to avoid processing any join
clause more than once, the same trick used in rebuild_joinclause_attr_needed.)

I've made some cosmetic tweaks to the patch, added a commit message,
and included a test case, as attached. I plan to push this soon,
barring any objections.

- Richard

Attachments:

v2-0001-Strip-removed-relation-references-from-PHVs-in-jo.patchapplication/octet-stream; name=v2-0001-Strip-removed-relation-references-from-PHVs-in-jo.patchDownload+92-6
#5Richard Guo
guofenglinux@gmail.com
In reply to: Richard Guo (#4)
Re: PlaceholderVars and join removal with appendrel parents

On Tue, Jul 14, 2026 at 4:02 PM Richard Guo <guofenglinux@gmail.com> wrote:

I've made some cosmetic tweaks to the patch, added a commit message,
and included a test case, as attached. I plan to push this soon,
barring any objections.

Pushed and back-patched.

- Richard