Skip to content

Commit bbeb0bb

Browse files
committed
Include a pointer to the query's source text in QueryDesc structs. This is
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.
1 parent ccd31eb commit bbeb0bb

File tree

8 files changed

+45
-18
lines changed

8 files changed

+45
-18
lines changed

src/backend/commands/copy.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.303 2009/01/01 17:23:37 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.304 2009/01/02 20:42:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1054,7 +1054,8 @@ DoCopy(const CopyStmt *stmt, const char *queryString)
10541054
((DR_copy *) dest)->cstate = cstate;
10551055

10561056
/* Create a QueryDesc requesting no output */
1057-
cstate->queryDesc = CreateQueryDesc(plan, GetActiveSnapshot(),
1057+
cstate->queryDesc = CreateQueryDesc(plan, queryString,
1058+
GetActiveSnapshot(),
10581059
InvalidSnapshot,
10591060
dest, NULL, false);
10601061

src/backend/commands/explain.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.183 2009/01/01 17:23:37 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/explain.c,v 1.184 2009/01/02 20:42:00 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -172,7 +172,7 @@ ExplainOneQuery(Query *query, ExplainStmt *stmt, const char *queryString,
172172
plan = pg_plan_query(query, 0, params);
173173

174174
/* run it (if needed) and produce output */
175-
ExplainOnePlan(plan, params, stmt, tstate);
175+
ExplainOnePlan(plan, stmt, queryString, params, tstate);
176176
}
177177
}
178178

@@ -218,8 +218,9 @@ ExplainOneUtility(Node *utilityStmt, ExplainStmt *stmt,
218218
* to call it.
219219
*/
220220
void
221-
ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
222-
ExplainStmt *stmt, TupOutputState *tstate)
221+
ExplainOnePlan(PlannedStmt *plannedstmt, ExplainStmt *stmt,
222+
const char *queryString, ParamListInfo params,
223+
TupOutputState *tstate)
223224
{
224225
QueryDesc *queryDesc;
225226
instr_time starttime;
@@ -234,7 +235,7 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
234235
PushUpdatedSnapshot(GetActiveSnapshot());
235236

236237
/* Create a QueryDesc requesting no output */
237-
queryDesc = CreateQueryDesc(plannedstmt,
238+
queryDesc = CreateQueryDesc(plannedstmt, queryString,
238239
GetActiveSnapshot(), InvalidSnapshot,
239240
None_Receiver, params,
240241
stmt->analyze);

src/backend/commands/prepare.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2002-2009, PostgreSQL Global Development Group
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.95 2009/01/01 17:23:39 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.96 2009/01/02 20:42:00 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -636,13 +636,17 @@ DropAllPreparedStatements(void)
636636

637637
/*
638638
* Implements the 'EXPLAIN EXECUTE' utility statement.
639+
*
640+
* Note: the passed-in queryString is that of the EXPLAIN EXECUTE,
641+
* not the original PREPARE; we get the latter string from the plancache.
639642
*/
640643
void
641644
ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
642645
const char *queryString,
643646
ParamListInfo params, TupOutputState *tstate)
644647
{
645648
PreparedStatement *entry;
649+
const char *query_string;
646650
CachedPlan *cplan;
647651
List *plan_list;
648652
ListCell *p;
@@ -659,6 +663,8 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
659663
if (!entry->plansource->fixed_result)
660664
elog(ERROR, "EXPLAIN EXECUTE does not support variable-result cached plans");
661665

666+
query_string = entry->plansource->query_string;
667+
662668
/* Replan if needed, and acquire a transient refcount */
663669
cplan = RevalidateCachedPlan(entry->plansource, true);
664670

@@ -701,11 +707,12 @@ ExplainExecuteQuery(ExecuteStmt *execstmt, ExplainStmt *stmt,
701707
pstmt->intoClause = execstmt->into;
702708
}
703709

704-
ExplainOnePlan(pstmt, paramLI, stmt, tstate);
710+
ExplainOnePlan(pstmt, stmt, query_string,
711+
paramLI, tstate);
705712
}
706713
else
707714
{
708-
ExplainOneUtility((Node *) pstmt, stmt, queryString,
715+
ExplainOneUtility((Node *) pstmt, stmt, query_string,
709716
params, tstate);
710717
}
711718

src/backend/executor/functions.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.131 2009/01/01 17:23:41 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.132 2009/01/02 20:42:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -411,11 +411,13 @@ postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
411411

412412
if (IsA(es->stmt, PlannedStmt))
413413
es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
414+
fcache->src,
414415
snapshot, InvalidSnapshot,
415416
dest,
416417
fcache->paramLI, false);
417418
else
418419
es->qd = CreateUtilityQueryDesc(es->stmt,
420+
fcache->src,
419421
snapshot,
420422
dest,
421423
fcache->paramLI);

src/backend/executor/spi.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.203 2009/01/01 17:23:42 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.204 2009/01/02 20:42:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1791,6 +1791,7 @@ _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI,
17911791
snap = InvalidSnapshot;
17921792

17931793
qdesc = CreateQueryDesc((PlannedStmt *) stmt,
1794+
plansource->query_string,
17941795
snap, crosscheck_snapshot,
17951796
dest,
17961797
paramLI, false);

src/backend/tcop/pquery.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.128 2009/01/01 17:23:48 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.129 2009/01/02 20:42:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -36,6 +36,7 @@ Portal ActivePortal = NULL;
3636

3737

3838
static void ProcessQuery(PlannedStmt *plan,
39+
const char *sourceText,
3940
ParamListInfo params,
4041
DestReceiver *dest,
4142
char *completionTag);
@@ -61,6 +62,7 @@ static void DoPortalRewind(Portal portal);
6162
*/
6263
QueryDesc *
6364
CreateQueryDesc(PlannedStmt *plannedstmt,
65+
const char *sourceText,
6466
Snapshot snapshot,
6567
Snapshot crosscheck_snapshot,
6668
DestReceiver *dest,
@@ -72,6 +74,7 @@ CreateQueryDesc(PlannedStmt *plannedstmt,
7274
qd->operation = plannedstmt->commandType; /* operation */
7375
qd->plannedstmt = plannedstmt; /* plan */
7476
qd->utilitystmt = plannedstmt->utilityStmt; /* in case DECLARE CURSOR */
77+
qd->sourceText = sourceText; /* query text */
7578
qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
7679
/* RI check snapshot */
7780
qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
@@ -93,6 +96,7 @@ CreateQueryDesc(PlannedStmt *plannedstmt,
9396
*/
9497
QueryDesc *
9598
CreateUtilityQueryDesc(Node *utilitystmt,
99+
const char *sourceText,
96100
Snapshot snapshot,
97101
DestReceiver *dest,
98102
ParamListInfo params)
@@ -102,6 +106,7 @@ CreateUtilityQueryDesc(Node *utilitystmt,
102106
qd->operation = CMD_UTILITY; /* operation */
103107
qd->plannedstmt = NULL;
104108
qd->utilitystmt = utilitystmt; /* utility command */
109+
qd->sourceText = sourceText; /* query text */
105110
qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
106111
qd->crosscheck_snapshot = InvalidSnapshot; /* RI check snapshot */
107112
qd->dest = dest; /* output dest */
@@ -141,6 +146,7 @@ FreeQueryDesc(QueryDesc *qdesc)
141146
* or PORTAL_ONE_RETURNING portal
142147
*
143148
* plan: the plan tree for the query
149+
* sourceText: the source text of the query
144150
* params: any parameters needed
145151
* dest: where to send results
146152
* completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
@@ -153,6 +159,7 @@ FreeQueryDesc(QueryDesc *qdesc)
153159
*/
154160
static void
155161
ProcessQuery(PlannedStmt *plan,
162+
const char *sourceText,
156163
ParamListInfo params,
157164
DestReceiver *dest,
158165
char *completionTag)
@@ -169,7 +176,7 @@ ProcessQuery(PlannedStmt *plan,
169176
/*
170177
* Create the QueryDesc object
171178
*/
172-
queryDesc = CreateQueryDesc(plan,
179+
queryDesc = CreateQueryDesc(plan, sourceText,
173180
GetActiveSnapshot(), InvalidSnapshot,
174181
dest, params, false);
175182

@@ -503,6 +510,7 @@ PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
503510
* the destination to DestNone.
504511
*/
505512
queryDesc = CreateQueryDesc((PlannedStmt *) linitial(portal->stmts),
513+
portal->sourceText,
506514
GetActiveSnapshot(),
507515
InvalidSnapshot,
508516
None_Receiver,
@@ -1258,13 +1266,15 @@ PortalRunMulti(Portal portal, bool isTopLevel,
12581266
{
12591267
/* statement can set tag string */
12601268
ProcessQuery(pstmt,
1269+
portal->sourceText,
12611270
portal->portalParams,
12621271
dest, completionTag);
12631272
}
12641273
else
12651274
{
12661275
/* stmt added by rewrite cannot set tag */
12671276
ProcessQuery(pstmt,
1277+
portal->sourceText,
12681278
portal->portalParams,
12691279
altdest, NULL);
12701280
}

src/include/commands/explain.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994-5, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.37 2009/01/01 17:23:58 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/commands/explain.h,v 1.38 2009/01/02 20:42:00 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -38,8 +38,10 @@ extern void ExplainOneUtility(Node *utilityStmt, ExplainStmt *stmt,
3838
ParamListInfo params,
3939
TupOutputState *tstate);
4040

41-
extern void ExplainOnePlan(PlannedStmt *plannedstmt, ParamListInfo params,
42-
ExplainStmt *stmt, TupOutputState *tstate);
41+
extern void ExplainOnePlan(PlannedStmt *plannedstmt, ExplainStmt *stmt,
42+
const char *queryString,
43+
ParamListInfo params,
44+
TupOutputState *tstate);
4345

4446
extern void ExplainPrintPlan(StringInfo str, QueryDesc *queryDesc,
4547
bool analyze, bool verbose);

src/include/executor/execdesc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/include/executor/execdesc.h,v 1.39 2009/01/01 17:23:59 momjian Exp $
11+
* $PostgreSQL: pgsql/src/include/executor/execdesc.h,v 1.40 2009/01/02 20:42:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -37,6 +37,7 @@ typedef struct QueryDesc
3737
CmdType operation; /* CMD_SELECT, CMD_UPDATE, etc. */
3838
PlannedStmt *plannedstmt; /* planner's output, or null if utility */
3939
Node *utilitystmt; /* utility statement, or null */
40+
const char *sourceText; /* source text of the query */
4041
Snapshot snapshot; /* snapshot to use for query */
4142
Snapshot crosscheck_snapshot; /* crosscheck for RI update/delete */
4243
DestReceiver *dest; /* the destination for tuple output */
@@ -54,13 +55,15 @@ typedef struct QueryDesc
5455

5556
/* in pquery.c */
5657
extern QueryDesc *CreateQueryDesc(PlannedStmt *plannedstmt,
58+
const char *sourceText,
5759
Snapshot snapshot,
5860
Snapshot crosscheck_snapshot,
5961
DestReceiver *dest,
6062
ParamListInfo params,
6163
bool doInstrument);
6264

6365
extern QueryDesc *CreateUtilityQueryDesc(Node *utilitystmt,
66+
const char *sourceText,
6467
Snapshot snapshot,
6568
DestReceiver *dest,
6669
ParamListInfo params);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy