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 <[email protected]> Reported-by: Zheng Hacker <[email protected]> Author: Jeevan Chalke <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/CAM2+6=xaytmgwh27o7odtfaw7kv4vduvbxc2rvzwtkz_yse...@mail.gmail.com Discussion: https://postgr.es/m/[email protected] Backpatch-through: 15 Branch ------ REL_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/bf94082c101d077d55e436f1f9dc83a6d77a572b Modified Files -------------- src/backend/optimizer/util/pathnode.c | 46 ++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-)
