]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/plan/setrefs.c
Do parse analysis of an EXPLAIN's contained statement during the normal
[postgresql] / src / backend / optimizer / plan / setrefs.c
index 83447082f5b39cb6660c78c21b46c9b54877e726..aa4fd4e1ebe71b0eabf0eb826c50fe81119de8ea 100644 (file)
@@ -4,12 +4,12 @@
  *       Post-processing of a completed plan tree: fix references to subplan
  *       vars, compute regproc values for operators, etc
  *
- * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.147 2008/12/28 18:53:57 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.157 2010/01/15 22:36:32 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -101,6 +101,10 @@ static Var *search_indexed_tlist_for_var(Var *var,
 static Var *search_indexed_tlist_for_non_var(Node *node,
                                                                 indexed_tlist *itlist,
                                                                 Index newvarno);
+static Var *search_indexed_tlist_for_sortgroupref(Node *node,
+                                                                         Index sortgroupref,
+                                                                         indexed_tlist *itlist,
+                                                                         Index newvarno);
 static List *fix_join_expr(PlannerGlobal *glob,
                          List *clauses,
                          indexed_tlist *outer_itlist,
@@ -116,7 +120,7 @@ static Node *fix_upper_expr_mutator(Node *node,
                                           fix_upper_expr_context *context);
 static bool fix_opfuncids_walker(Node *node, void *context);
 static bool extract_query_dependencies_walker(Node *node,
-                                                                                         PlannerGlobal *context);
+                                                                 PlannerGlobal *context);
 
 
 /*****************************************************************************
@@ -166,12 +170,14 @@ static bool extract_query_dependencies_walker(Node *node,
  *     glob: global data for planner run
  *     plan: the topmost node of the plan
  *     rtable: the rangetable for the current subquery
+ *     rowmarks: the PlanRowMark list for the current subquery
  *
  * The return value is normally the same Plan node passed in, but can be
  * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
  *
- * The flattened rangetable entries are appended to glob->finalrtable, and
- * plan dependencies are appended to glob->relationOids (for relations)
+ * The flattened rangetable entries are appended to glob->finalrtable,
+ * and we also append rowmarks entries to glob->finalrowmarks.
+ * Plan dependencies are appended to glob->relationOids (for relations)
  * and glob->invalItems (for everything else).
  *
  * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
@@ -180,7 +186,8 @@ static bool extract_query_dependencies_walker(Node *node,
  * it's not so safe to assume that for expression tree nodes.
  */
 Plan *
-set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
+set_plan_references(PlannerGlobal *glob, Plan *plan,
+                                       List *rtable, List *rowmarks)
 {
        int                     rtoffset = list_length(glob->finalrtable);
        ListCell   *lc;
@@ -189,7 +196,9 @@ set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
         * In the flat rangetable, we zero out substructure pointers that are not
         * needed by the executor; this reduces the storage space and copying cost
         * for cached plans.  We keep only the alias and eref Alias fields, which
-        * are needed by EXPLAIN.
+        * are needed by EXPLAIN, and the selectedCols and modifiedCols bitmaps,
+        * which are needed for executor-startup permissions checking and for
+        * trigger event checking.
         */
        foreach(lc, rtable)
        {
@@ -229,6 +238,27 @@ set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
                                                                                         newrte->relid);
        }
 
+       /*
+        * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
+        */
+       foreach(lc, rowmarks)
+       {
+               PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
+               PlanRowMark *newrc;
+
+               Assert(IsA(rc, PlanRowMark));
+
+               /* flat copy is enough since all fields are scalars */
+               newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
+               memcpy(newrc, rc, sizeof(PlanRowMark));
+
+               /* adjust indexes */
+               newrc->rti += rtoffset;
+               newrc->prti += rtoffset;
+
+               glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
+       }
+
        /* Now fix the Plan tree */
        return set_plan_refs(glob, plan, rtoffset);
 }
@@ -348,7 +378,7 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                        break;
                case T_CteScan:
                        {
-                               CteScan *splan = (CteScan *) plan;
+                               CteScan    *splan = (CteScan *) plan;
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
@@ -395,6 +425,27 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                         */
                        Assert(plan->qual == NIL);
                        break;
+               case T_LockRows:
+                       {
+                               LockRows   *splan = (LockRows *) plan;
+
+                               /*
+                                * Like the plan types above, LockRows doesn't evaluate its
+                                * tlist or quals.  But we have to fix up the RT indexes
+                                * in its rowmarks.
+                                */
+                               set_dummy_tlist_references(plan, rtoffset);
+                               Assert(splan->plan.qual == NIL);
+
+                               foreach(l, splan->rowMarks)
+                               {
+                                       PlanRowMark *rc = (PlanRowMark *) lfirst(l);
+
+                                       rc->rti += rtoffset;
+                                       rc->prti += rtoffset;
+                               }
+                       }
+                       break;
                case T_Limit:
                        {
                                Limit      *splan = (Limit *) plan;
@@ -441,6 +492,36 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                        fix_scan_expr(glob, splan->resconstantqual, rtoffset);
                        }
                        break;
+               case T_ModifyTable:
+                       {
+                               ModifyTable *splan = (ModifyTable *) plan;
+
+                               /*
+                                * planner.c already called set_returning_clause_references,
+                                * so we should not process either the targetlist or the
+                                * returningLists.
+                                */
+                               Assert(splan->plan.qual == NIL);
+
+                               foreach(l, splan->resultRelations)
+                               {
+                                       lfirst_int(l) += rtoffset;
+                               }
+                               foreach(l, splan->rowMarks)
+                               {
+                                       PlanRowMark *rc = (PlanRowMark *) lfirst(l);
+
+                                       rc->rti += rtoffset;
+                                       rc->prti += rtoffset;
+                               }
+                               foreach(l, splan->plans)
+                               {
+                                       lfirst(l) = set_plan_refs(glob,
+                                                                                         (Plan *) lfirst(l),
+                                                                                         rtoffset);
+                               }
+                       }
+                       break;
                case T_Append:
                        {
                                Append     *splan = (Append *) plan;
@@ -529,10 +610,12 @@ set_subqueryscan_references(PlannerGlobal *glob,
        Plan       *result;
 
        /* First, recursively process the subplan */
-       plan->subplan = set_plan_references(glob, plan->subplan, plan->subrtable);
+       plan->subplan = set_plan_references(glob, plan->subplan,
+                                                                               plan->subrtable, plan->subrowmark);
 
-       /* subrtable is no longer needed in the plan tree */
+       /* subrtable/subrowmark are no longer needed in the plan tree */
        plan->subrtable = NIL;
+       plan->subrowmark = NIL;
 
        if (trivial_subqueryscan(plan))
        {
@@ -712,13 +795,13 @@ fix_expr_common(PlannerGlobal *glob, Node *node)
        {
                set_sa_opfuncid((ScalarArrayOpExpr *) node);
                record_plan_function_dependency(glob,
-                                                                               ((ScalarArrayOpExpr *) node)->opfuncid);
+                                                                        ((ScalarArrayOpExpr *) node)->opfuncid);
        }
        else if (IsA(node, ArrayCoerceExpr))
        {
                if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
                        record_plan_function_dependency(glob,
-                                                                                       ((ArrayCoerceExpr *) node)->elemfuncid);
+                                                                        ((ArrayCoerceExpr *) node)->elemfuncid);
        }
        else if (IsA(node, Const))
        {
@@ -758,8 +841,8 @@ fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
                 * If rtoffset == 0, we don't need to change any Vars, and if there
                 * are no placeholders anywhere we won't need to remove them.  Then
                 * it's OK to just scribble on the input node tree instead of copying
-                * (since the only change, filling in any unset opfuncid fields,
-                * is harmless).  This saves just enough cycles to be noticeable on
+                * (since the only change, filling in any unset opfuncid fields, is
+                * harmless).  This saves just enough cycles to be noticeable on
                 * trivial queries.
                 */
                (void) fix_scan_expr_walker(node, &context);
@@ -1118,10 +1201,25 @@ set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
                TargetEntry *tle = (TargetEntry *) lfirst(l);
                Node       *newexpr;
 
-               newexpr = fix_upper_expr(glob,
-                                                                (Node *) tle->expr,
-                                                                subplan_itlist,
-                                                                rtoffset);
+               /* If it's a non-Var sort/group item, first try to match by sortref */
+               if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
+               {
+                       newexpr = (Node *)
+                               search_indexed_tlist_for_sortgroupref((Node *) tle->expr,
+                                                                                                         tle->ressortgroupref,
+                                                                                                         subplan_itlist,
+                                                                                                         OUTER);
+                       if (!newexpr)
+                               newexpr = fix_upper_expr(glob,
+                                                                                (Node *) tle->expr,
+                                                                                subplan_itlist,
+                                                                                rtoffset);
+               }
+               else
+                       newexpr = fix_upper_expr(glob,
+                                                                        (Node *) tle->expr,
+                                                                        subplan_itlist,
+                                                                        rtoffset);
                tle = flatCopyTargetEntry(tle);
                tle->expr = (Expr *) newexpr;
                output_targetlist = lappend(output_targetlist, tle);
@@ -1365,6 +1463,49 @@ search_indexed_tlist_for_non_var(Node *node,
        return NULL;                            /* no match */
 }
 
+/*
+ * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
+ *             (which is assumed not to be just a Var)
+ *
+ * If a match is found, return a Var constructed to reference the tlist item.
+ * If no match, return NULL.
+ *
+ * This is needed to ensure that we select the right subplan TLE in cases
+ * where there are multiple textually-equal()-but-volatile sort expressions.
+ * And it's also faster than search_indexed_tlist_for_non_var.
+ */
+static Var *
+search_indexed_tlist_for_sortgroupref(Node *node,
+                                                                         Index sortgroupref,
+                                                                         indexed_tlist *itlist,
+                                                                         Index newvarno)
+{
+       ListCell   *lc;
+
+       foreach(lc, itlist->tlist)
+       {
+               TargetEntry *tle = (TargetEntry *) lfirst(lc);
+
+               /* The equal() check should be redundant, but let's be paranoid */
+               if (tle->ressortgroupref == sortgroupref &&
+                       equal(node, tle->expr))
+               {
+                       /* Found a matching subplan output expression */
+                       Var                *newvar;
+
+                       newvar = makeVar(newvarno,
+                                                        tle->resno,
+                                                        exprType((Node *) tle->expr),
+                                                        exprTypmod((Node *) tle->expr),
+                                                        0);
+                       newvar->varnoold = 0;   /* wasn't ever a plain Var */
+                       newvar->varoattno = 0;
+                       return newvar;
+               }
+       }
+       return NULL;                            /* no match */
+}
+
 /*
  * fix_join_expr
  *        Create a new set of targetlist entries or join qual clauses by
@@ -1599,7 +1740,7 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
  *
  * If the query involves more than just the result table, we have to
  * adjust any Vars that refer to other tables to reference junk tlist
- * entries in the top plan's targetlist.  Vars referencing the result
+ * entries in the top subplan's targetlist.  Vars referencing the result
  * table should be left alone, however (the executor will evaluate them
  * using the actual heap tuple, after firing triggers if any). In the
  * adjusted RETURNING list, result-table Vars will still have their
@@ -1609,8 +1750,8 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
  * glob->relationOids.
  *
  * 'rlist': the RETURNING targetlist to be fixed
- * 'topplan': the top Plan node for the query (not yet passed through
- *             set_plan_references)
+ * 'topplan': the top subplan node that will be just below the ModifyTable
+ *             node (note it's not yet passed through set_plan_references)
  * 'resultRelation': RT index of the associated result relation
  *
  * Note: we assume that result relations will have rtoffset zero, that is,
@@ -1632,11 +1773,11 @@ set_returning_clause_references(PlannerGlobal *glob,
         * entries, while leaving result-rel Vars as-is.
         *
         * PlaceHolderVars will also be sought in the targetlist, but no
-        * more-complex expressions will be.  Note that it is not possible for
-        * a PlaceHolderVar to refer to the result relation, since the result
-        * is never below an outer join.  If that case could happen, we'd have
-        * to be prepared to pick apart the PlaceHolderVar and evaluate its
-        * contained expression instead.
+        * more-complex expressions will be.  Note that it is not possible for a
+        * PlaceHolderVar to refer to the result relation, since the result is
+        * never below an outer join.  If that case could happen, we'd have to be
+        * prepared to pick apart the PlaceHolderVar and evaluate its contained
+        * expression instead.
         */
        itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
 
@@ -1733,8 +1874,8 @@ record_plan_function_dependency(PlannerGlobal *glob, Oid funcid)
         * we just assume they'll never change (or at least not in ways that'd
         * invalidate plans using them).  For this purpose we can consider a
         * built-in function to be one with OID less than FirstBootstrapObjectId.
-        * Note that the OID generator guarantees never to generate such an
-        * OID after startup, even at OID wraparound.
+        * Note that the OID generator guarantees never to generate such an OID
+        * after startup, even at OID wraparound.
         */
        if (funcid >= (Oid) FirstBootstrapObjectId)
        {
@@ -1764,14 +1905,15 @@ record_plan_function_dependency(PlannerGlobal *glob, Oid funcid)
 
 /*
  * extract_query_dependencies
- *             Given a list of not-yet-planned queries (i.e. Query nodes),
- *             extract their dependencies just as set_plan_references would do.
+ *             Given a not-yet-planned query or queries (i.e. a Query node or list
+ *             of Query nodes), extract dependencies just as set_plan_references
+ *             would do.
  *
  * This is needed by plancache.c to handle invalidation of cached unplanned
  * queries.
  */
 void
-extract_query_dependencies(List *queries,
+extract_query_dependencies(Node *query,
                                                   List **relationOids,
                                                   List **invalItems)
 {
@@ -1783,7 +1925,7 @@ extract_query_dependencies(List *queries,
        glob.relationOids = NIL;
        glob.invalItems = NIL;
 
-       (void) extract_query_dependencies_walker((Node *) queries, &glob);
+       (void) extract_query_dependencies_walker(query, &glob);
 
        *relationOids = glob.relationOids;
        *invalItems = glob.invalItems;
@@ -1802,6 +1944,19 @@ extract_query_dependencies_walker(Node *node, PlannerGlobal *context)
                Query      *query = (Query *) node;
                ListCell   *lc;
 
+               if (query->commandType == CMD_UTILITY)
+               {
+                       /* Ignore utility statements, except EXPLAIN */
+                       if (IsA(query->utilityStmt, ExplainStmt))
+                       {
+                               query = (Query *) ((ExplainStmt *) query->utilityStmt)->query;
+                               Assert(IsA(query, Query));
+                               Assert(query->commandType != CMD_UTILITY);
+                       }
+                       else
+                               return false;
+               }
+
                /* Collect relation OIDs in this Query's rtable */
                foreach(lc, query->rtable)
                {