Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 89db887

Browse files
committed
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.
1 parent 87dfa0d commit 89db887

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/backend/optimizer/plan/initsplan.c

+35-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.133 2007/08/31 01:44:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.134 2007/10/04 20:44:47 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -136,6 +136,40 @@ build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
136136
}
137137
}
138138

139+
/*
140+
* add_IN_vars_to_tlists
141+
* Add targetlist entries for each var needed in InClauseInfo entries
142+
* to the appropriate base relations.
143+
*
144+
* Normally this is a waste of time because scanning of the WHERE clause
145+
* will have added them. But it is possible that eval_const_expressions()
146+
* simplified away all references to the vars after the InClauseInfos were
147+
* made. We need the IN's righthand-side vars to be available at the join
148+
* anyway, in case we try to unique-ify the subselect's outputs. (The only
149+
* known case that provokes this is "WHERE false AND foo IN (SELECT ...)".
150+
* We don't try to be very smart about such cases, just correct.)
151+
*/
152+
void
153+
add_IN_vars_to_tlists(PlannerInfo *root)
154+
{
155+
ListCell *l;
156+
157+
foreach(l, root->in_info_list)
158+
{
159+
InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
160+
List *in_vars;
161+
162+
in_vars = pull_var_clause((Node *) ininfo->sub_targetlist, false);
163+
if (in_vars != NIL)
164+
{
165+
add_vars_to_targetlist(root, in_vars,
166+
bms_union(ininfo->lefthand,
167+
ininfo->righthand));
168+
list_free(in_vars);
169+
}
170+
}
171+
}
172+
139173
/*
140174
* add_vars_to_targetlist
141175
* For each variable appearing in the list, add it to the owning

src/backend/optimizer/plan/planmain.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.102 2007/07/07 20:46:45 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planmain.c,v 1.103 2007/10/04 20:44:47 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -211,6 +211,13 @@ query_planner(PlannerInfo *root, List *tlist,
211211

212212
joinlist = deconstruct_jointree(root);
213213

214+
/*
215+
* Vars mentioned in InClauseInfo items also have to be added to baserel
216+
* targetlists. Nearly always, they'd have got there from the original
217+
* WHERE qual, but in corner cases maybe not.
218+
*/
219+
add_IN_vars_to_tlists(root);
220+
214221
/*
215222
* Reconsider any postponed outer-join quals now that we have built up
216223
* equivalence classes. (This could result in further additions or

src/include/optimizer/planmain.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.101 2007/05/04 01:13:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.102 2007/10/04 20:44:47 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -72,6 +72,7 @@ extern int join_collapse_limit;
7272

7373
extern void add_base_rels_to_query(PlannerInfo *root, Node *jtnode);
7474
extern void build_base_rel_tlists(PlannerInfo *root, List *final_tlist);
75+
extern void add_IN_vars_to_tlists(PlannerInfo *root);
7576
extern void add_vars_to_targetlist(PlannerInfo *root, List *vars,
7677
Relids where_needed);
7778
extern List *deconstruct_jointree(PlannerInfo *root);

0 commit comments

Comments
 (0)