From ad66f705fa6796b40311a8210e9f37144df02ef5 Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Mon, 29 Dec 2025 11:38:49 +0900 Subject: Strip PlaceHolderVars from index operands When pulling up a subquery, we may need to wrap its targetlist items in PlaceHolderVars to enforce separate identity or as a result of outer joins. However, this causes any upper-level WHERE clauses referencing these outputs to contain PlaceHolderVars, which prevents indxpath.c from recognizing that they could be matched to index columns or index expressions, potentially affecting the planner's ability to use indexes. To fix, explicitly strip PlaceHolderVars from index operands. A PlaceHolderVar appearing in a relation-scan-level expression is effectively a no-op. Nevertheless, to play it safe, we strip only PlaceHolderVars that are not marked nullable. The stripping is performed recursively to handle cases where PlaceHolderVars are nested or interleaved with other node types. To minimize performance impact, we first use a lightweight walker to check for the presence of strippable PlaceHolderVars. The expensive mutator is invoked only if a candidate is found, avoiding unnecessary memory allocation and tree copying in the common case where no PlaceHolderVars are present. Back-patch to v18. Although this issue exists before that, changes in this version made it common enough to notice. Given the lack of field reports for older versions, I am not back-patching further. Reported-by: Haowu Ge Author: Richard Guo Discussion: https://postgr.es/m/62af586c-c270-44f3-9c5e-02c81d537e3d.gehaowu@bitmoe.com Backpatch-through: 18 --- src/include/optimizer/paths.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/include') diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h index f6a62df0b43..3ccf158e09d 100644 --- a/src/include/optimizer/paths.h +++ b/src/include/optimizer/paths.h @@ -81,6 +81,7 @@ extern bool indexcol_is_bool_constant_for_query(PlannerInfo *root, int indexcol); extern bool match_index_to_operand(Node *operand, int indexcol, IndexOptInfo *index); +extern Node *strip_phvs_in_index_operand(Node *operand); extern void check_index_predicates(PlannerInfo *root, RelOptInfo *rel); /* -- cgit v1.2.3