From 89db887b1edbeac7d7070e566606bbea4792f7f6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 4 Oct 2007 20:44:47 +0000 Subject: [PATCH] Keep the planner from failing on "WHERE false AND something IN (SELECT ...)". eval_const_expressions simplifies this to just "WHERE false", but we have already done pull_up_IN_clauses so the IN join will be done, or at least planned, anyway. The trouble case comes when the sub-SELECT is itself a join and we decide to implement the IN by unique-ifying the sub-SELECT outputs: with no remaining reference to the output Vars in WHERE, we won't have propagated the Vars up to the upper join point, leading to "variable not found in subplan target lists" error. Fix by adding an extra scan of in_info_list and forcing all Vars mentioned therein to be propagated up to the IN join point. Per bug report from Miroslav Sulc. --- src/backend/optimizer/plan/initsplan.c | 36 +++++++++++++++++++++++++- src/backend/optimizer/plan/planmain.c | 9 ++++++- src/include/optimizer/planmain.h | 3 ++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c index b71cf6dae2e..70543613fe2 100644 --- a/src/backend/optimizer/plan/initsplan.c +++ b/src/backend/optimizer/plan/initsplan.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.133 2007/08/31 01:44:05 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.134 2007/10/04 20:44:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -136,6 +136,40 @@ build_base_rel_tlists(PlannerInfo *root, List *final_tlist) } } +/* + * add_IN_vars_to_tlists + * Add targetlist entries for each var needed in InClauseInfo entries + * to the appropriate base relations. + * + * Normally this is a waste of time because scanning of the WHERE clause + * will have added them. But it is possible that eval_const_expressions() + * simplified away all references to the vars after the InClauseInfos were + * made. We need the IN's righthand-side vars to be available at the join + * anyway, in case we try to unique-ify the subselect's outputs. (The only + * known case that provokes this is "WHERE false AND foo IN (SELECT ...)". + * We don't try to be very smart about such cases, just correct.) + */ +void +add_IN_vars_to_tlists(PlannerInfo *root) +{ + ListCell *l; + + foreach(l, root->in_info_list) + { + InClauseInfo *ininfo = (InClauseInfo *) lfirst(l); + List *in_vars; + + in_vars = pull_var_clause((Node *) ininfo->sub_targetlist, false); + if (in_vars != NIL) + { + add_vars_to_targetlist(root, in_vars, + bms_union(ininfo->lefthand, + ininfo->righthand)); + list_free(in_vars); + } + } +} + /* * add_vars_to_targetlist * For each variable appearing in the list, add it to the owning diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c index f8ff3a71508..772ee84e8d5 100644 --- a/src/backend/optimizer/plan/planmain.c +++ b/src/backend/optimizer/plan/planmain.c @@ -14,7 +14,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.102 2007/07/07 20:46:45 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.103 2007/10/04 20:44:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -211,6 +211,13 @@ query_planner(PlannerInfo *root, List *tlist, joinlist = deconstruct_jointree(root); + /* + * Vars mentioned in InClauseInfo items also have to be added to baserel + * targetlists. Nearly always, they'd have got there from the original + * WHERE qual, but in corner cases maybe not. + */ + add_IN_vars_to_tlists(root); + /* * Reconsider any postponed outer-join quals now that we have built up * equivalence classes. (This could result in further additions or diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h index 319c38c66b5..4673d53098f 100644 --- a/src/include/optimizer/planmain.h +++ b/src/include/optimizer/planmain.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.101 2007/05/04 01:13:45 tgl Exp $ + * $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.102 2007/10/04 20:44:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -72,6 +72,7 @@ extern int join_collapse_limit; extern void add_base_rels_to_query(PlannerInfo *root, Node *jtnode); extern void build_base_rel_tlists(PlannerInfo *root, List *final_tlist); +extern void add_IN_vars_to_tlists(PlannerInfo *root); extern void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed); extern List *deconstruct_jointree(PlannerInfo *root); -- 2.39.5