Wrong results with initplans moved by setrefs.c
Here is another wrong-result issue found when fuzzing with LLM. It
happens in all supported branches.
create table p (a int) partition by list (a);
create table p1 partition of p for values in (1);
insert into p values (1);
select v.x, s.y from (values (1), (2)) v(x),
lateral (select (select v.x) as y from p offset 0) s
where s.y > 0;
x | y
---+---
1 | 1
2 | 1
(2 rows)
Without the OFFSET 0, we get the right answer:
select v.x, s.y from (values (1), (2)) v(x),
lateral (select (select v.x) as y from p) s
where s.y > 0;
x | y
---+---
1 | 1
2 | 2
(2 rows)
In the first query, the initplan for "(select v.x)" is attached to the
Append over p. When setrefs.c removes that single-child Append,
clean_up_removed_plan_level() moves the initplan down to the Seq Scan
on p1. But the Seq Scan's extParam and allParam were computed by
SS_finalize_plan() before the move, so they don't include the Param
for v.x that the initplan depends on. So when the Subquery Scan is
rescanned for the second outer row, UpdateChangedParamSet() filters
that Param out, the initplan is never re-evaluated, and we keep
returning the value from the first row.
I believe the same bug can happen when a trivial SubqueryScan or a
single-child MergeAppend is removed, although I don't have a repro
query for that at the moment.
I think the fix is simply to merge the removed node's extParam and
allParam into the child's, something like below in
clean_up_removed_plan_level():
child->initPlan = list_concat(parent->initPlan,
child->initPlan);
+
+ child->extParam = bms_union(child->extParam, parent->extParam);
+ child->allParam = bms_union(child->allParam, parent->allParam);
Thoughts?
- Richard
On Mon, 9 Nov 2026 at 15:54, Richard Guo <guofenglinux@gmail.com> wrote:
I think the fix is simply to merge the removed node's extParam and
allParam into the child's, something like below in
clean_up_removed_plan_level():child->initPlan = list_concat(parent->initPlan, child->initPlan); + + child->extParam = bms_union(child->extParam, parent->extParam); + child->allParam = bms_union(child->allParam, parent->allParam);
Looking at finalize_plan(), I'd say that's correct. But shouldn't it
be bms_add_member() rather than bms_union()? While mostly harmless, I
don't think creating a new set is the correct thing to do here. I
think doing bms_union() where the variable being assigned is the same
as the bms_union's first parameter is only valid in cases where you
don't want the first parameter to be modified, e.g in cases like
createplan.c:4325 where req_outer points to some field in a struct. I
think all the cases where we're assigning bms_union's return to a
struct field and that field is the same as the bms_union's first input
are probably all candidates for using bms_add_member() instead.
REALLOCATE_BITMAPSETS was designed to help identify cases where
Bitmapset pointers are being shared. I'm personally not so keen on
adjusting the other existing ones due to the poor risk to reward
ratio, but since you're adding these lines, I'd say bms_add_member is
a better option.
David
On Mon, Nov 9, 2026 at 2:11 PM David Rowley <dgrowleyml@gmail.com> wrote:
Looking at finalize_plan(), I'd say that's correct. But shouldn't it
be bms_add_member() rather than bms_union()?
Thanks for looking! I assume you mean bms_add_members(). Agreed.
finalize_plan() allocates a fresh extParam and allParam for each plan
node, and nothing else keeps a pointer to them, so we can safely
modify the child's sets in place, and there's no point in allocating
new ones.
Attached is a patch for this fix.
(Since v14 will reach EOL before this can be pushed, maybe
back-patching to v15 is enough.)
- Richard
On Mon, 9 Nov 2026 at 19:30, Richard Guo <guofenglinux@gmail.com> wrote:
On Mon, Nov 9, 2026 at 2:11 PM David Rowley <dgrowleyml@gmail.com> wrote:
Looking at finalize_plan(), I'd say that's correct. But shouldn't it
be bms_add_member() rather than bms_union()?Thanks for looking! I assume you mean bms_add_members().
Yeah. I did mean to type that.
Attached is a patch for this fix.
(Since v14 will reach EOL before this can be pushed, maybe
back-patching to v15 is enough.)
Looks mostly ok. I'd use "add" instead of "merge" in "To fix, merge
the removed", and I expect "Backpatch-through: 14" should be 15.
David
On Mon, Nov 9, 2026 at 4:36 PM David Rowley <dgrowleyml@gmail.com> wrote:
Looks mostly ok. I'd use "add" instead of "merge" in "To fix, merge
the removed", and I expect "Backpatch-through: 14" should be 15.
Thanks! Fixed both and then pushed and back-patched to v15.
- Richard