]> granicus.if.org Git - postgresql/commitdiff
Include a pointer to the query's source text in QueryDesc structs. This is
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 2 Jan 2009 20:42:00 +0000 (20:42 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 2 Jan 2009 20:42:00 +0000 (20:42 +0000)
practically free given prior 8.4 changes in plancache and portal management,
and it makes it a lot easier for ExecutorStart/Run/End hooks to get at the
query text.  Extracted from Itagaki Takahiro's pg_stat_statements patch,
with minor editorialization.

src/backend/commands/copy.c
src/backend/commands/explain.c
src/backend/commands/prepare.c
src/backend/executor/functions.c
src/backend/executor/spi.c
src/backend/tcop/pquery.c
src/include/commands/explain.h
src/include/executor/execdesc.h

index 0b38ee0bb78ca191a3c8046ac1047555b5c97500..c2e2c82205274cd59c7bffd0dd7b775ce2705729 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.303 2009/01/01 17:23:37 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.304 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1054,7 +1054,8 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
                ((DR_copy *) dest)->cstate = cstate;
 
                /* Create a QueryDesc requesting no output */
-               cstate->queryDesc = CreateQueryDesc(plan, GetActiveSnapshot(),
+               cstate->queryDesc = CreateQueryDesc(plan, queryString,
+                                                                                       GetActiveSnapshot(),
                                                                                        InvalidSnapshot,
                                                                                        dest, NULL, false);
 
index 4f520ccb5c849b03ed7d33a921fc5726df0f958e..8ad877e1652af27dff6b932d1d0d8c80f4761c1f 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.183 2009/01/01 17:23:37 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.184 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -172,7 +172,7 @@ ExplainOneQuery(Query *query, ExplainStmt *stmt, const char *queryString,
                plan = pg_plan_query(query, 0, params);
 
                /* run it (if needed) and produce output */
-               ExplainOnePlan(plan, params, stmt, tstate);
+               ExplainOnePlan(plan, stmt, queryString, params, tstate);
        }
 }
 
@@ -218,8 +218,9 @@ ExplainOneUtility(Node *utilityStmt, ExplainStmt *stmt,
  * to call it.
  */
 void
-ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
-                          ExplainStmt *stmt, TupOutputState *tstate)
+ExplainOnePlan(PlannedStmt *plannedstmt, ExplainStmt *stmt,
+                          const char *queryString, ParamListInfo params,
+                          TupOutputState *tstate)
 {
        QueryDesc  *queryDesc;
        instr_time      starttime;
@@ -234,7 +235,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
        PushUpdatedSnapshot(GetActiveSnapshot());
 
        /* Create a QueryDesc requesting no output */
-       queryDesc = CreateQueryDesc(plannedstmt,
+       queryDesc = CreateQueryDesc(plannedstmt, queryString,
                                                                GetActiveSnapshot(), InvalidSnapshot,
                                                                None_Receiver, params,
                                                                stmt->analyze);
index 29f3df711a6549503ebccacc2c218e3bd5e516e6..e1c0edb8e85ac59a1c8921ec03b5420248d594a2 100644 (file)
@@ -10,7 +10,7 @@
  * Copyright (c) 2002-2009, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.95 2009/01/01 17:23:39 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.96 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -636,6 +636,9 @@ DropAllPreparedStatements(void)
 
 /*
  * Implements the 'EXPLAIN EXECUTE' utility statement.
+ *
+ * Note: the passed-in queryString is that of the EXPLAIN EXECUTE,
+ * not the original PREPARE; we get the latter string from the plancache.
  */
 void
 ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
@@ -643,6 +646,7 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
                                        ParamListInfo params, TupOutputState *tstate)
 {
        PreparedStatement *entry;
+       const char *query_string;
        CachedPlan *cplan;
        List       *plan_list;
        ListCell   *p;
@@ -659,6 +663,8 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
        if (!entry->plansource->fixed_result)
                elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
 
+       query_string = entry->plansource->query_string;
+
        /* Replan if needed, and acquire a transient refcount */
        cplan = RevalidateCachedPlan(entry->plansource, true);
 
@@ -701,11 +707,12 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
                                pstmt->intoClause = execstmt->into;
                        }
 
-                       ExplainOnePlan(pstmt, paramLI, stmt, tstate);
+                       ExplainOnePlan(pstmt, stmt, query_string,
+                                                  paramLI, tstate);
                }
                else
                {
-                       ExplainOneUtility((Node *) pstmt, stmt, queryString,
+                       ExplainOneUtility((Node *) pstmt, stmt, query_string,
                                                          params, tstate);
                }
 
index a2eac5af8b4ba0a35ad0de5cf507a42eb201a9e4..a8673b1f6074989c7b40336ae2f6b9fd3243f027 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.131 2009/01/01 17:23:41 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.132 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -411,11 +411,13 @@ postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
 
        if (IsA(es->stmt, PlannedStmt))
                es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
+                                                                fcache->src,
                                                                 snapshot, InvalidSnapshot,
                                                                 dest,
                                                                 fcache->paramLI, false);
        else
                es->qd = CreateUtilityQueryDesc(es->stmt,
+                                                                               fcache->src,
                                                                                snapshot,
                                                                                dest,
                                                                                fcache->paramLI);
index c3b4f619bcf49ba6fba382890491a7231005664d..4bc45dc17f82c450dce039fccbc1b23a5038b442 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.203 2009/01/01 17:23:42 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.204 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1791,6 +1791,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
                                        snap = InvalidSnapshot;
 
                                qdesc = CreateQueryDesc((PlannedStmt *) stmt,
+                                                                               plansource->query_string,
                                                                                snap, crosscheck_snapshot,
                                                                                dest,
                                                                                paramLI, false);
index c4481d779001fffe6c09bf21fcfe88a58a66eb52..062881858c194370730d12406fddf0500cd7c1e2 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.128 2009/01/01 17:23:48 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.129 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -36,6 +36,7 @@ Portal                ActivePortal = NULL;
 
 
 static void ProcessQuery(PlannedStmt *plan,
+                        const char *sourceText,
                         ParamListInfo params,
                         DestReceiver *dest,
                         char *completionTag);
@@ -61,6 +62,7 @@ static void DoPortalRewind(Portal portal);
  */
 QueryDesc *
 CreateQueryDesc(PlannedStmt *plannedstmt,
+                               const char *sourceText,
                                Snapshot snapshot,
                                Snapshot crosscheck_snapshot,
                                DestReceiver *dest,
@@ -72,6 +74,7 @@ CreateQueryDesc(PlannedStmt *plannedstmt,
        qd->operation = plannedstmt->commandType;       /* operation */
        qd->plannedstmt = plannedstmt;          /* plan */
        qd->utilitystmt = plannedstmt->utilityStmt; /* in case DECLARE CURSOR */
+       qd->sourceText = sourceText;            /* query text */
        qd->snapshot = RegisterSnapshot(snapshot);      /* snapshot */
        /* RI check snapshot */
        qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
@@ -93,6 +96,7 @@ CreateQueryDesc(PlannedStmt *plannedstmt,
  */
 QueryDesc *
 CreateUtilityQueryDesc(Node *utilitystmt,
+                                          const char *sourceText,
                                           Snapshot snapshot,
                                           DestReceiver *dest,
                                           ParamListInfo params)
@@ -102,6 +106,7 @@ CreateUtilityQueryDesc(Node *utilitystmt,
        qd->operation = CMD_UTILITY;    /* operation */
        qd->plannedstmt = NULL;
        qd->utilitystmt = utilitystmt;          /* utility command */
+       qd->sourceText = sourceText;            /* query text */
        qd->snapshot = RegisterSnapshot(snapshot);      /* snapshot */
        qd->crosscheck_snapshot = InvalidSnapshot;      /* RI check snapshot */
        qd->dest = dest;                        /* output dest */
@@ -141,6 +146,7 @@ FreeQueryDesc(QueryDesc *qdesc)
  *             or PORTAL_ONE_RETURNING portal
  *
  *     plan: the plan tree for the query
+ *     sourceText: the source text of the query
  *     params: any parameters needed
  *     dest: where to send results
  *     completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
@@ -153,6 +159,7 @@ FreeQueryDesc(QueryDesc *qdesc)
  */
 static void
 ProcessQuery(PlannedStmt *plan,
+                        const char *sourceText,
                         ParamListInfo params,
                         DestReceiver *dest,
                         char *completionTag)
@@ -169,7 +176,7 @@ ProcessQuery(PlannedStmt *plan,
        /*
         * Create the QueryDesc object
         */
-       queryDesc = CreateQueryDesc(plan,
+       queryDesc = CreateQueryDesc(plan, sourceText,
                                                                GetActiveSnapshot(), InvalidSnapshot,
                                                                dest, params, false);
 
@@ -503,6 +510,7 @@ PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
                                 * the destination to DestNone.
                                 */
                                queryDesc = CreateQueryDesc((PlannedStmt *) linitial(portal->stmts),
+                                                                                       portal->sourceText,
                                                                                        GetActiveSnapshot(),
                                                                                        InvalidSnapshot,
                                                                                        None_Receiver,
@@ -1258,6 +1266,7 @@ PortalRunMulti(Portal portal, bool isTopLevel,
                        {
                                /* statement can set tag string */
                                ProcessQuery(pstmt,
+                                                        portal->sourceText,
                                                         portal->portalParams,
                                                         dest, completionTag);
                        }
@@ -1265,6 +1274,7 @@ PortalRunMulti(Portal portal, bool isTopLevel,
                        {
                                /* stmt added by rewrite cannot set tag */
                                ProcessQuery(pstmt,
+                                                        portal->sourceText,
                                                         portal->portalParams,
                                                         altdest, NULL);
                        }
index e3a2a65004bf6c2a6fb38ae71f4d5507d1444f5e..2903d394ba5add709acbbdd6da0e18fb5c009c84 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.37 2009/01/01 17:23:58 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.38 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -38,8 +38,10 @@ extern void ExplainOneUtility(Node *utilityStmt, ExplainStmt *stmt,
                                  ParamListInfo params,
                                  TupOutputState *tstate);
 
-extern void ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
-                          ExplainStmt *stmt, TupOutputState *tstate);
+extern void ExplainOnePlan(PlannedStmt *plannedstmt, ExplainStmt *stmt,
+                                                  const char *queryString,
+                                                  ParamListInfo params,
+                                                  TupOutputState *tstate);
 
 extern void ExplainPrintPlan(StringInfo str, QueryDesc *queryDesc,
                                                         bool analyze, bool verbose);
index fb3f56b78673163292ed07263df3a1b823bfcace..9682c4b73ad694c7f238a26b72759541b2aa3839 100644 (file)
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/executor/execdesc.h,v 1.39 2009/01/01 17:23:59 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/executor/execdesc.h,v 1.40 2009/01/02 20:42:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,6 +37,7 @@ typedef struct QueryDesc
        CmdType         operation;              /* CMD_SELECT, CMD_UPDATE, etc. */
        PlannedStmt *plannedstmt;       /* planner's output, or null if utility */
        Node       *utilitystmt;        /* utility statement, or null */
+       const char *sourceText;         /* source text of the query */
        Snapshot        snapshot;               /* snapshot to use for query */
        Snapshot        crosscheck_snapshot;    /* crosscheck for RI update/delete */
        DestReceiver *dest;                     /* the destination for tuple output */
@@ -54,6 +55,7 @@ typedef struct QueryDesc
 
 /* in pquery.c */
 extern QueryDesc *CreateQueryDesc(PlannedStmt *plannedstmt,
+                               const char *sourceText,
                                Snapshot snapshot,
                                Snapshot crosscheck_snapshot,
                                DestReceiver *dest,
@@ -61,6 +63,7 @@ extern QueryDesc *CreateQueryDesc(PlannedStmt *plannedstmt,
                                bool doInstrument);
 
 extern QueryDesc *CreateUtilityQueryDesc(Node *utilitystmt,
+                                          const char *sourceText,
                                           Snapshot snapshot,
                                           DestReceiver *dest,
                                           ParamListInfo params);