Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89db887

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 havealready done pull_up_IN_clauses so the IN join will be done, or at leastplanned, anyway. The trouble case comes when the sub-SELECT is itself a joinand 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 havepropagated the Vars up to the upper join point, leading to "variable not foundin subplan target lists" error. Fix by adding an extra scan of in_info_listand forcing all Vars mentioned therein to be propagated up to the IN joinpoint. Per bug report from Miroslav Sulc.
1 parent87dfa0d commit89db887

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

‎src/backend/optimizer/plan/initsplan.c

Lines changed: 35 additions & 1 deletion
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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/0401:13:45 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.102 2007/10/0420:44:47 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -72,6 +72,7 @@ extern intjoin_collapse_limit;
7272

7373
externvoidadd_base_rels_to_query(PlannerInfo*root,Node*jtnode);
7474
externvoidbuild_base_rel_tlists(PlannerInfo*root,List*final_tlist);
75+
externvoidadd_IN_vars_to_tlists(PlannerInfo*root);
7576
externvoidadd_vars_to_targetlist(PlannerInfo*root,List*vars,
7677
Relidswhere_needed);
7778
externList*deconstruct_jointree(PlannerInfo*root);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp