postgres.git / summary / log / commit / refs
commit b80db376c177ecbf30f004975533370a0aa37c01
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat Nov 21 19:07:51 2026 +0000
Don't pfree a Path that belongs to a different parent relation.
add_path and add_partial_path pfree a dominated or rejected path
on the assumption, documented in add_path's header comment, that
"Paths of this rel cannot yet be referenced from any other rel".
At least two existing callers violate that: grouping_planner copies
the uppermost scan/join rel's paths to the query's final_rel, and
create_ordered_paths passes an already-sorted input_rel path
straight through to the ordered_rel. Most of the time we got away
with that, because the source relation's path list would not get
consulted again. But at least the grouping_planner case is a live
bug: since add_path and add_partial_path have somewhat different
comparison rules, it's possible for add_path to accept a Gather
path while add_partial_path rejects the corresponding partial path.
Then pfree'ing the partial path leaves a dangling pointer in the
Gather path, leading to crashes or strange planner failures if
the Gather path is selected for use. The create_ordered_paths
case has been reported to cause unexpected failures in debugging
situations, when one tries to dump the pathlist of the source rel.
To fix, refrain from pfree'ing a rejected path if its parent link
shows it was originally made for another relation. This almost
certainly indicates that it is still referenced in the other
rel's pathlist or partial_pathlist. Even if that list won't be
consulted again in normal planning, we want it to stay valid for
debugging purposes, so that this rule is unlikely to spare any
path that could safely be recycled.
There was discussion of solving this problem in a more principled
fashion by reference-counting Paths. But that would be a very
invasive (and not back-patchable) change, would add cycles that
we'd probably not win back, and it's not at all clear that it'd
make the planner any more robust anyway. We'd be replacing the
existing maybe-this-isn't-safe-to-pfree hazard with a hazard of
failing to update refcounts where needed, and that hazard would
be spread across much of the planner instead of being localized.
We have no test case that reliably exposes the grouping_planner bug
across branches, which is not so surprising because a case where
add_path and add_partial_path disagree on the merits of corresponding
paths is necessarily an edge case (probably involving fuzzily-equal
costs). So the behavior is exceedingly subject to change, and there's
little reason to hope that a test case that exposes the error today
would still do so in future versions. Hence, no test case in this
patch.
Bug: #19924
Reported-by: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Reported-by: Zheng Hacker <hackerzheng666@gmail.com>
Author: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAM2+6=XaYtMGwh27O7oDTfAw7kV4VdUVbxC2RVzWtKz_ysekKw@mail.gmail.com
Discussion: https://postgr.es/m/19924-8aecd924385fafcc@postgresql.org
Backpatch-through: 15
src/backend/optimizer/util/pathnode.c | 46 ++++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 12 deletions(-)
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 5d4f9788ad3..424130837eb 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -439,7 +439,7 @@ set_cheapest(RelOptInfo *parent_rel)
* memory consumption. We dare not try to free the substructure of a Path,
* since much of it may be shared with other Paths or the query tree itself;
* but just recycling discarded Path nodes is a very useful savings in
- * a large join tree. We can recycle the List nodes of pathlist, too.
+ * a large join tree.
*
* As noted in optimizer/README, deleting a previously-accepted Path is
* safe because we know that Paths of this rel cannot yet be referenced
@@ -450,6 +450,16 @@ set_cheapest(RelOptInfo *parent_rel)
* referenced as children of BitmapHeapPaths as well as being paths in
* their own right. Hence, we don't pfree IndexPaths when rejecting them.
*
+ * A related but distinct hazard is a Path that was built for, and is
+ * still linked to, some other rel than parent_rel. Some callers
+ * (e.g. create_ordered_paths()) pass in another relation's Path without
+ * copying it, so that it remains a live member of its original parent's
+ * own pathlist. We detect this generically by comparing the Path's
+ * path->parent against parent_rel: they will be equal if the Path was
+ * actually built for this rel. If they are not, we do not free the Path
+ * for fear that a live link to it remains in the original parent rel. (In
+ * most cases that link would not be used again, but there are exceptions.)
+ *
* 'parent_rel' is the relation entry to which the path corresponds.
* 'new_path' is a potential path for parent_rel.
*
@@ -623,11 +633,9 @@ add_path(RelOptInfo *parent_rel, Path *new_path)
{
parent_rel->pathlist = foreach_delete_current(parent_rel->pathlist,
p1);
-
- /*
- * Delete the data pointed-to by the deleted cell, if possible
- */
- if (!IsA(old_path, IndexPath))
+ /* Delete the old path, if it's safe; see header comment */
+ if (!IsA(old_path, IndexPath) &&
+ old_path->parent == parent_rel)
pfree(old_path);
}
else
@@ -659,8 +667,9 @@ add_path(RelOptInfo *parent_rel, Path *new_path)
}
else
{
- /* Reject and recycle the new path */
- if (!IsA(new_path, IndexPath))
+ /* Reject and recycle the new path, if it's safe */
+ if (!IsA(new_path, IndexPath) &&
+ new_path->parent == parent_rel)
pfree(new_path);
}
}
@@ -786,8 +795,18 @@ add_path_precheck(RelOptInfo *parent_rel, int disabled_nodes,
* another partial path; this requires that there be no other references to
* such paths yet. Hence, GatherPaths must not be created for a rel until
* we're done creating all partial paths for it. Unlike add_path, we don't
- * take an exception for IndexPaths as partial index paths won't be
+ * need an exception for IndexPaths as partial index paths won't be
* referenced by partial BitmapHeapPaths.
+ *
+ * But, like add_path, we refrain from pfreeing partial paths that belong
+ * to some other rel (i.e. path->parent != parent_rel). Ordinarily every
+ * partial path submitted here was freshly built for parent_rel, but
+ * grouping_planner intentionally passes a scan/join rel's paths and
+ * partial paths to add_path and add_partial_path for the query's final_rel
+ * (the latter so that Gathers can be built from them in outer query
+ * levels). In that usage there are edge cases where add_path can keep a
+ * Gather while add_partial_path will reject its underlying partial path,
+ * so that if we pfree the partial path we'll have a broken Gather path.
*/
void
add_partial_path(RelOptInfo *parent_rel, Path *new_path)
@@ -863,7 +882,9 @@ add_partial_path(RelOptInfo *parent_rel, Path *new_path)
{
parent_rel->partial_pathlist =
foreach_delete_current(parent_rel->partial_pathlist, p1);
- pfree(old_path);
+ /* Delete the old path, if it's safe; see header comment */
+ if (old_path->parent == parent_rel)
+ pfree(old_path);
}
else
{
@@ -894,8 +915,9 @@ add_partial_path(RelOptInfo *parent_rel, Path *new_path)
}
else
{
- /* Reject and recycle the new path */
- pfree(new_path);
+ /* Reject and recycle the new path, if it's safe */
+ if (new_path->parent == parent_rel)
+ pfree(new_path);
}
}
[parent: 0155c32fafd1]