Skip to content

Commit 719dcf3

Browse files
committed
Introduce field tracking cached plan type in PlannedStmt
PlannedStmt gains a new field, called CachedPlanType, able to track if a given plan tree originates from the cache and if we are dealing with a generic or custom cached plan. This field can be used for monitoring or statistical purposes, in the executor hooks, for example, based on the planned statement attached to a QueryDesc. A patch is under discussion for pg_stat_statements to provide an equivalent of the counters in pg_prepared_statements for custom and generic plans, to provide a more global view of such data, as this data is now restricted to the current session. The concept introduced in this commit is useful on its own, and has been extracted from a larger patch by the same author. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Andrei Lepikhov <lepihov@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/CAA5RZ0uFw8Y9GCFvafhC=OA8NnMqVZyzXPfv_EePOt+iv1T-qQ@mail.gmail.com
1 parent df33561 commit 719dcf3

File tree

9 files changed

+33
-0
lines changed

9 files changed

+33
-0
lines changed

src/backend/commands/foreigncmds.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ ImportForeignSchema(ImportForeignSchemaStmt *stmt)
15881588
pstmt->utilityStmt = (Node *) cstmt;
15891589
pstmt->stmt_location = rs->stmt_location;
15901590
pstmt->stmt_len = rs->stmt_len;
1591+
pstmt->cached_plan_type = PLAN_CACHE_NONE;
15911592

15921593
/* Execute statement */
15931594
ProcessUtility(pstmt, cmd, false,

src/backend/commands/schemacmds.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ CreateSchemaCommand(CreateSchemaStmt *stmt, const char *queryString,
215215
wrapper->utilityStmt = stmt;
216216
wrapper->stmt_location = stmt_location;
217217
wrapper->stmt_len = stmt_len;
218+
wrapper->cached_plan_type = PLAN_CACHE_NONE;
218219

219220
/* do this step */
220221
ProcessUtility(wrapper,

src/backend/executor/execParallel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ ExecSerializePlan(Plan *plan, EState *estate)
189189
pstmt->permInfos = estate->es_rteperminfos;
190190
pstmt->resultRelations = NIL;
191191
pstmt->appendRelations = NIL;
192+
pstmt->cached_plan_type = PLAN_CACHE_NONE;
192193

193194
/*
194195
* Transfer only parallel-safe subplans, leaving a NULL "hole" in the list

src/backend/optimizer/plan/planner.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
582582
result->utilityStmt = parse->utilityStmt;
583583
result->stmt_location = parse->stmt_location;
584584
result->stmt_len = parse->stmt_len;
585+
result->cached_plan_type = PLAN_CACHE_NONE;
585586

586587
result->jitFlags = PGJIT_NONE;
587588
if (jit_enabled && jit_above_cost >= 0 &&

src/backend/tcop/postgres.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ pg_plan_queries(List *querytrees, const char *query_string, int cursorOptions,
988988
stmt->stmt_location = query->stmt_location;
989989
stmt->stmt_len = query->stmt_len;
990990
stmt->queryId = query->queryId;
991+
stmt->cached_plan_type = PLAN_CACHE_NONE;
991992
}
992993
else
993994
{

src/backend/tcop/utility.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@ ProcessUtilitySlow(ParseState *pstate,
12341234
wrapper->utilityStmt = stmt;
12351235
wrapper->stmt_location = pstmt->stmt_location;
12361236
wrapper->stmt_len = pstmt->stmt_len;
1237+
wrapper->cached_plan_type = PLAN_CACHE_NONE;
12371238

12381239
ProcessUtility(wrapper,
12391240
queryString,
@@ -1964,6 +1965,7 @@ ProcessUtilityForAlterTable(Node *stmt, AlterTableUtilityContext *context)
19641965
wrapper->utilityStmt = stmt;
19651966
wrapper->stmt_location = context->pstmt->stmt_location;
19661967
wrapper->stmt_len = context->pstmt->stmt_len;
1968+
wrapper->cached_plan_type = PLAN_CACHE_NONE;
19671969

19681970
ProcessUtility(wrapper,
19691971
context->queryString,

src/backend/utils/cache/plancache.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
12831283
CachedPlan *plan = NULL;
12841284
List *qlist;
12851285
bool customplan;
1286+
ListCell *lc;
12861287

12871288
/* Assert caller is doing things in a sane order */
12881289
Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
@@ -1385,6 +1386,13 @@ GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams,
13851386
plan->is_saved = true;
13861387
}
13871388

1389+
foreach(lc, plan->stmt_list)
1390+
{
1391+
PlannedStmt *pstmt = (PlannedStmt *) lfirst(lc);
1392+
1393+
pstmt->cached_plan_type = customplan ? PLAN_CACHE_CUSTOM : PLAN_CACHE_GENERIC;
1394+
}
1395+
13881396
return plan;
13891397
}
13901398

src/include/nodes/plannodes.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@
2828
* ----------------------------------------------------------------
2929
*/
3030

31+
/* ----------------
32+
* CachedPlanType
33+
*
34+
* CachedPlanType identifies whether a PlannedStmt is a cached plan, and if
35+
* so, whether it is generic or custom.
36+
* ----------------
37+
*/
38+
typedef enum CachedPlanType
39+
{
40+
PLAN_CACHE_NONE = 0, /* Not a cached plan */
41+
PLAN_CACHE_GENERIC, /* Generic cached plan */
42+
PLAN_CACHE_CUSTOM, /* Custom cached plan */
43+
} CachedPlanType;
44+
3145
/* ----------------
3246
* PlannedStmt node
3347
*
@@ -58,6 +72,9 @@ typedef struct PlannedStmt
5872
/* plan identifier (can be set by plugins) */
5973
int64 planId;
6074

75+
/* type of cached plan */
76+
CachedPlanType cached_plan_type;
77+
6178
/* is it insert|update|delete|merge RETURNING? */
6279
bool hasReturning;
6380

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ CachedFunctionHashEntry
391391
CachedFunctionHashKey
392392
CachedPlan
393393
CachedPlanSource
394+
CachedPlanType
394395
CallContext
395396
CallStmt
396397
CancelRequestPacket

0 commit comments

Comments
 (0)