[PATCH] Fix disabled_nodes propagation for single-child Append paths
Hi hackers,
A disabled IndexScan can still be chosen for a partitioned table even
when a non-disabled alternative is available.
This issue was originally reported to me by my colleague, Man Zeng.
For example:
```
CREATE TABLE dpart (a int, b int) PARTITION BY LIST (a);
CREATE TABLE dpart_1 PARTITION OF dpart FOR VALUES IN (1);
CREATE INDEX dpart_1_a_idx ON dpart_1(a);
INSERT INTO dpart VALUES (1,1), (1,2), (1,3);
SET enable_indexscan = off;
EXPLAIN (COSTS OFF)
SELECT * FROM dpart ORDER BY a;
```
This produces:
```
QUERY PLAN
-------------------------------------------------
Index Scan using dpart_1_a_idx on dpart_1 dpart
Disabled: true
(2 rows)
```
There is a non-disabled alternative using a SeqScan followed by a
Sort, so the disabled IndexScan should not be preferred.
The problem is in `create_append_path()`. When an Append has only one
child and both have the same parallel-awareness, it skips
`cost_append()` and copies the child's rows and costs directly:
```c
pathnode->path.rows = child->rows;
pathnode->path.startup_cost = child->startup_cost;
pathnode->path.total_cost = child->total_cost;
```
However, `child->disabled_nodes` is not copied. As a result, an
AppendPath whose child has `disabled_nodes = 1` can incorrectly have
`disabled_nodes = 0`, which affects subsequent path selection.
The attached patch propagates `disabled_nodes` in this single-child
case and adds a regression test.
With the patch, the example above produces:
```
QUERY PLAN
-------------------------------
Sort
Sort Key: dpart.a
-> Seq Scan on dpart_1 dpart
(3 rows)
```
Thoughts?
--
Thanks,
Tender Wang
On Mon, Aug 17, 2026 at 3:59 PM Tender Wang <tndrwang@gmail.com> wrote:
The problem is in `create_append_path()`. When an Append has only one
child and both have the same parallel-awareness, it skips
`cost_append()` and copies the child's rows and costs directly:
...
However, `child->disabled_nodes` is not copied. As a result, an
AppendPath whose child has `disabled_nodes = 1` can incorrectly have
`disabled_nodes = 0`, which affects subsequent path selection.
Good catch, and the fix is right. setrefs.c removes such an Append so
the node is gone at execution time and copying the child's count
verbatim is what we want.
I think we need to update the comment above this code. Also, I don't
think we need the test case. None of previous fixes to oversights of
this shape carried test case, such as 47c110f77 and 6e466e1e8. A
plan-shape test here does not seem stable enough.
For the same reason as those commits, I'd also leave this master-only.
It is a v17 regression, but planner fixes risk destabilizing plans in
stable branches, and this doesn't seem severe enough to be an
exception.
I plan to push the attached patch if there are no objections.
- Richard
On Thu, 27 Aug 2026 at 19:36, Richard Guo <guofenglinux@gmail.com> wrote:
For the same reason as those commits, I'd also leave this master-only.
It is a v17 regression, but planner fixes risk destabilizing plans in
stable branches, and this doesn't seem severe enough to be an
exception.
IMO, disabling a node type is quite a clear request that you only want
that node type used if there's no alternative. I don't think the
normal reasons for not wanting to destabilise plans here apply to
this. If you leave this in, it'll mean there could be an inability in
what happens based on how many partitions are pruned. It's also hard
to imagine someone complaining that we stopped picking the plan with
the disabled node type and started choosing the next best alternative.
If they don't like the fact that the bug got fixed because they want
the plan with the disabled node, then why do they have that node
disabled in the first place?
It just feels more likely someone will encounter undesired behaviour
as a result of this rather than accidentally getting something good
out of it.
David
On Thu, Aug 27, 2026 at 8:36 PM David Rowley <dgrowleyml@gmail.com> wrote:
On Thu, 27 Aug 2026 at 19:36, Richard Guo <guofenglinux@gmail.com> wrote:
For the same reason as those commits, I'd also leave this master-only.
It is a v17 regression, but planner fixes risk destabilizing plans in
stable branches, and this doesn't seem severe enough to be an
exception.
IMO, disabling a node type is quite a clear request that you only want
that node type used if there's no alternative. I don't think the
normal reasons for not wanting to destabilise plans here apply to
this.
Makes sense. I don't think anyone would get upset that we stopped
picking a plan containing a node they had disabled.
Maybe 47c110f77 and 6e466e1e8 should have been back-patched too?
Anyway, let's back-patch this one at least.
- Richard
On Fri, 28 Aug 2026 at 15:03, Richard Guo <guofenglinux@gmail.com> wrote:
Maybe 47c110f77 and 6e466e1e8 should have been back-patched too?
On a quick glance, what I said seems to apply to both of those commits
too, but I'm no authority on the matter. I'm just telling you what I
think. My understanding of "don't backpatch as that might cause plan
changes" is so we don't accidentally cause an inadvertent plan change
for someone who has already tested their workload on that major
version of PostgreSQL and is happy enough with the performance. If
someone has disabled a node and we don't obey that disabled property,
then that seems like a bug fix for a feature that's not working as
intended. If they disabled the node type, then they quite clearly want
to avoid using that node type either completely or whenever there's no
other alternative plan.
I also think the one reported here is slightly worse due to the
behaviour depending on the number of pruned partitions. I didn't study
it in detail, but it looks to me like 47c110f77 and 6e466e1e8 are more
likely to consistently do the wrong thing, but you might tell me I'm
wrong there as it looks like you've looked at those issues in more
detail than I have.
David
On Fri, Aug 28, 2026 at 2:02 PM David Rowley <dgrowleyml@gmail.com> wrote:
I also think the one reported here is slightly worse due to the
behaviour depending on the number of pruned partitions.
Agreed, and I think you are right about the other two being consistent
in a way this one is not. This one only misbehaviors when a single
child survives, so the same query can honour enable_indexscan=off or
not depending on how many partitions are pruned.
I've pushed this patch and back-patched it to v18. (I have a typo in
my previous email saying this is a v17 regression, but it should be
v18).
- Richard
Richard Guo <guofenglinux@gmail.com> 于2026年8月28日周五 14:30写道:
On Fri, Aug 28, 2026 at 2:02 PM David Rowley <dgrowleyml@gmail.com> wrote:
I also think the one reported here is slightly worse due to the
behaviour depending on the number of pruned partitions.Agreed, and I think you are right about the other two being consistent
in a way this one is not. This one only misbehaviors when a single
child survives, so the same query can honour enable_indexscan=off or
not depending on how many partitions are pruned.I've pushed this patch and back-patched it to v18. (I have a typo in
my previous email saying this is a v17 regression, but it should be
v18).
Thanks for pushing.
--
Thanks,
Tender Wang