]> granicus.if.org Git - postgresql/commitdiff
Keep the planner from failing on "WHERE false AND something IN (SELECT ...)".
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Oct 2007 20:45:17 +0000 (20:45 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Oct 2007 20:45:17 +0000 (20:45 +0000)
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
src/backend/optimizer/plan/planmain.c
src/include/optimizer/planmain.h

index b1e69b7b3a545c25b262426eb2972efd85a3e0e4..25efaf59d7d5dd0e2c4535a8626da6bf546ece50 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.91.2.3 2005/09/28 21:17:48 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.91.2.4 2007/10/04 20:45:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -125,6 +125,40 @@ build_base_rel_tlists(Query *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(Query *root)
+{
+       List       *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));
+                       freeList(in_vars);
+               }
+       }
+}
+
 /*
  * add_vars_to_targetlist
  *       For each variable appearing in the list, add it to the owning
index a8159dd972b12eb773e920d95b3f940e03d9b4ef..94365808b841058a1cebd131262ae2bebc0c2ae1 100644 (file)
@@ -14,7 +14,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.78.4.1 2005/09/28 21:17:49 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planmain.c,v 1.78.4.2 2007/10/04 20:45:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -135,6 +135,13 @@ query_planner(Query *root, List *tlist, double tuple_fraction,
 
        (void) distribute_quals_to_rels(root, (Node *) root->jointree, false);
 
+       /*
+        * 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);
+
        /*
         * Use the completed lists of equijoined keys to deduce any implied
         * but unstated equalities (for example, A=B and B=C imply A=C).
index d288c4e3374d17b3ff94854dd31452c0d3891d68..1b252a8f63d9fd7b09e8abf7f3e8a6b62ac96e73 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: planmain.h,v 1.75.4.1 2005/09/28 21:17:50 tgl Exp $
+ * $Id: planmain.h,v 1.75.4.2 2007/10/04 20:45:17 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -57,6 +57,7 @@ extern Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
  */
 extern void add_base_rels_to_query(Query *root, Node *jtnode);
 extern void build_base_rel_tlists(Query *root, List *final_tlist);
+extern void add_IN_vars_to_tlists(Query *root);
 extern Relids distribute_quals_to_rels(Query *root, Node *jtnode,
                                                                           bool below_outer_join);
 extern void process_implied_equality(Query *root,