From: | Richard Guo <rguo(at)postgresql(dot)org> |
---|---|
To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
Subject: | pgsql: Fix planner error when estimating SubPlan cost |
Date: | 2025-09-03 07:02:15 |
Message-ID: | E1uthVX-000GpE-1C@gemulon.postgresql.org |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-committers |
Fix planner error when estimating SubPlan cost
SubPlan nodes are typically built very early, before any RelOptInfos
have been constructed for the parent query level. As a result, the
simple_rel_array in the parent root has not yet been initialized.
Currently, during cost estimation of a SubPlan's testexpr, we may call
examine_variable() to look up statistical data about the expressions.
This can lead to "no relation entry for relid" errors.
To fix, pass root as NULL to cost_qual_eval() in cost_subplan(), since
the root does not yet contain enough information to safely consult
statistics.
One exception is SubPlan nodes built for the initplans of MIN/MAX
aggregates from indexes. In this case, having a NULL root is safe
because testexpr will be NULL. Additionally, an initplan will by
definition not consult anything from the parent plan.
Backpatch to all supported branches. Although the reported call path
that triggers this error is not reachable prior to v17, there's no
guarantee that other code paths -- especially in extensions -- could
not encounter the same issue when cost_qual_eval() is called with a
root that lacks a valid simple_rel_array. The test case is not
included in pre-v17 branches though.
Bug: #19037
Reported-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Diagnosed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Reviewed-by: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Discussion: https://postgr.es/m/19037-3d1c7bb553c7ce84@postgresql.org
Backpatch-through: 13
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/aba8f61c30911cbac1a310b7d21777e04711c66e
Modified Files
--------------
src/backend/optimizer/path/costsize.c | 18 ++++++++++++++++--
src/test/regress/expected/subselect.out | 19 +++++++++++++++++++
src/test/regress/sql/subselect.sql | 9 +++++++++
3 files changed, 44 insertions(+), 2 deletions(-)
From | Date | Subject | |
---|---|---|---|
Next Message | Richard Guo | 2025-09-03 07:04:47 | pgsql: Fix planner error when estimating SubPlan cost |
Previous Message | Amit Kapila | 2025-09-03 06:42:23 | pgsql: Fix use-after-free issue in slot synchronization. |