| From: | Richard Guo <guofenglinux(at)gmail(dot)com> |
|---|---|
| To: | Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | "variable not found in subplan target lists" with partitionwise join |
| Date: | 2026-10-19 06:56:04 |
| Message-ID: | CAMbWs48bLLEGy6YC+n01w6=YR=28cj_kpoKcAgOHQR7jbtLb6w@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Fix-sorting-a-child-rel-by-an-expression-containi.patch | application/octet-stream | 8.0 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Peter Smith | 2026-10-19 06:57:31 | DOCS - CREATE PUBLICATION wording re 'ONLY' for partitioned tables. |
| Previous Message | David Rowley | 2026-10-19 06:51:36 | Re: Fix stale allocChunkLimit when recycling AllocSet |