"variable not found in subplan target lists" with partitionwise join

Started by Richard Guoabout 1 month ago3 messageshackers
Jump to latest
#1Richard Guo
guofenglinux@gmail.com

This can be reproduced with the query below in v14 to master.

create table pt (a int, b int) partition by list (a);
create table pt1 partition of pt for values in (1);
create table pt2 partition of pt for values in (2);
create index on pt (a);
create index on pt (b);
create table d (a int);

set enable_partitionwise_join = on;

select s.x + 1 from d
left join (select t1.b, coalesce(t2.b, t1.b) as x
from pt t1 join pt t2 on t1.a = t2.a) s on true
where s.b = s.x + 1 order by 1;
ERROR: variable not found in subplan target lists

In this query, the coalesce() is wrapped in a PHV, and we get a
MergeAppend over the partitionwise child joins, which needs each child
to emit "PHV + 1" as the sort column. The child join's tlist only has
the translated PHV, so prepare_sort_from_pathkeys() calls
find_computable_ec_member() to look for an EC member that can be
computed from that tlist.

find_computable_ec_member() ignores child members that do not belong
to the given relids, but nothing prevents it from choosing a parent
member. Usually this is fine, because the parent member's Vars cannot
appear in the child's tlist. But PHVs are different. equal()
compares PHVs only by phid, so the parent PHV is considered to match
the translated PHV in the child's tlist. So we choose the parent
member, and add the untranslated parent expression to the child join's
tlist. Then setrefs.c cannot find the parent Vars inside it, and we
get this error.

I think the fix is to also ignore parent members that do not belong to
the given relids, if relids is given. This should not change anything
for non-child rels, because any member that is computable from the
rel's tlist must belong to that rel anyway. If relids is NULL, we
still only filter child members, as before.

Attached is a patch for this fix.

- Richard

Attachments:

t254269_1
v1-0001-Fix-sorting-a-child-rel-by-an-expression-containi.patchapplication/octet-stream; name=v1-0001-Fix-sorting-a-child-rel-by-an-expression-containi.patchDownload+67-6
#2wenhui qiu
qiuwenhuifx@gmail.com
In reply to: Richard Guo (#1)
Re: "variable not found in subplan target lists" with partitionwise join

Hi Richard,
The fix looks reasonable to me. Checking em_relids for parent members
prevents an untranslated parent expression from being selected for a child
relation, while preserving the existing behavior when relids is NULL. I
reproduced the reported error on a local 20devel build based on 1863452.
After applying the core change and adding the regression case, the query
produces the expected plan and results, and the partition_join regression
test passes. I haven't tested the back branches.
One minor wording suggestion: the new comment says that equal() compares
PlaceHolderVars “only by phid”, but phlevelsup and phnullingrels are also
compared. The relevant point here is that phexpr and phrels are ignored, so
perhaps:
* otherwise match the translated ones in "exprs", since equal()
* ignores phexpr and phrels when comparing PlaceHolderVars.
What do you think?

Thanks!

#3Richard Guo
guofenglinux@gmail.com
In reply to: wenhui qiu (#2)
Re: "variable not found in subplan target lists" with partitionwise join

On Mon, Oct 19, 2026 at 5:14 PM wenhui qiu <qiuwenhuifx@gmail.com> wrote:

One minor wording suggestion: the new comment says that equal() compares PlaceHolderVars “only by phid”, but phlevelsup and phnullingrels are also compared. The relevant point here is that phexpr and phrels are ignored, so perhaps:
* otherwise match the translated ones in "exprs", since equal()
* ignores phexpr and phrels when comparing PlaceHolderVars.
What do you think?

Thanks! I've updated the comment, and then pushed and back-patched
it to v14.

- Richard