PostgreSQL Source Code  git master
plancache.h File Reference
#include "access/tupdesc.h"
#include "nodes/params.h"
#include "utils/queryenvironment.h"
Include dependency graph for plancache.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  CachedPlanSource
 
struct  CachedPlan
 

Macros

#define CACHEDPLANSOURCE_MAGIC   195726186
 
#define CACHEDPLAN_MAGIC   953717834
 

Typedefs

typedef struct CachedPlanSource CachedPlanSource
 
typedef struct CachedPlan CachedPlan
 

Functions

void InitPlanCache (void)
 
void ResetPlanCache (void)
 
CachedPlanSourceCreateCachedPlan (struct RawStmt *raw_parse_tree, const char *query_string, const char *commandTag)
 
CachedPlanSourceCreateOneShotCachedPlan (struct RawStmt *raw_parse_tree, const char *query_string, const char *commandTag)
 
void CompleteCachedPlan (CachedPlanSource *plansource, List *querytree_list, MemoryContext querytree_context, Oid *param_types, int num_params, ParserSetupHook parserSetup, void *parserSetupArg, int cursor_options, bool fixed_result)
 
void SaveCachedPlan (CachedPlanSource *plansource)
 
void DropCachedPlan (CachedPlanSource *plansource)
 
void CachedPlanSetParentContext (CachedPlanSource *plansource, MemoryContext newcontext)
 
CachedPlanSourceCopyCachedPlan (CachedPlanSource *plansource)
 
bool CachedPlanIsValid (CachedPlanSource *plansource)
 
ListCachedPlanGetTargetList (CachedPlanSource *plansource, QueryEnvironment *queryEnv)
 
CachedPlanGetCachedPlan (CachedPlanSource *plansource, ParamListInfo boundParams, bool useResOwner, QueryEnvironment *queryEnv)
 
void ReleaseCachedPlan (CachedPlan *plan, bool useResOwner)
 

Macro Definition Documentation

◆ CACHEDPLAN_MAGIC

#define CACHEDPLAN_MAGIC   953717834

◆ CACHEDPLANSOURCE_MAGIC

Typedef Documentation

◆ CachedPlan

◆ CachedPlanSource

Function Documentation

◆ CachedPlanGetTargetList()

List* CachedPlanGetTargetList ( CachedPlanSource plansource,
QueryEnvironment queryEnv 
)

Definition at line 1428 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, FetchStatementTargetList(), CachedPlanSource::is_complete, CachedPlanSource::magic, NIL, CachedPlanSource::query_list, QueryListGetPrimaryStmt(), CachedPlanSource::resultDesc, and RevalidateCachedQuery().

Referenced by exec_describe_statement_message(), and FetchPreparedStatementTargetList().

1430 {
1431  Query *pstmt;
1432 
1433  /* Assert caller is doing things in a sane order */
1434  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1435  Assert(plansource->is_complete);
1436 
1437  /*
1438  * No work needed if statement doesn't return tuples (we assume this
1439  * feature cannot be changed by an invalidation)
1440  */
1441  if (plansource->resultDesc == NULL)
1442  return NIL;
1443 
1444  /* Make sure the querytree list is valid and we have parse-time locks */
1445  RevalidateCachedQuery(plansource, queryEnv);
1446 
1447  /* Get the primary statement and find out what it returns */
1448  pstmt = QueryListGetPrimaryStmt(plansource->query_list);
1449 
1450  return FetchStatementTargetList((Node *) pstmt);
1451 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:517
TupleDesc resultDesc
Definition: plancache.h:92
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:360
static Query * QueryListGetPrimaryStmt(List *stmts)
Definition: plancache.c:1462
static List * RevalidateCachedQuery(CachedPlanSource *plansource, QueryEnvironment *queryEnv)
Definition: plancache.c:558
#define Assert(condition)
Definition: c.h:699
List * query_list
Definition: plancache.h:95
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25

◆ CachedPlanIsValid()

bool CachedPlanIsValid ( CachedPlanSource plansource)

Definition at line 1415 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::is_valid, and CachedPlanSource::magic.

Referenced by SPI_plan_is_valid().

1416 {
1417  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1418  return plansource->is_valid;
1419 }
#define Assert(condition)
Definition: c.h:699
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25

◆ CachedPlanSetParentContext()

void CachedPlanSetParentContext ( CachedPlanSource plansource,
MemoryContext  newcontext 
)

Definition at line 1286 of file plancache.c.

References Assert, CACHEDPLAN_MAGIC, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::context, CachedPlan::context, elog, ERROR, CachedPlanSource::gplan, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::magic, CachedPlan::magic, and MemoryContextSetParent().

Referenced by _SPI_make_plan_non_temp().

1288 {
1289  /* Assert caller is doing things in a sane order */
1290  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1291  Assert(plansource->is_complete);
1292 
1293  /* These seem worth real tests, though */
1294  if (plansource->is_saved)
1295  elog(ERROR, "cannot move a saved cached plan to another context");
1296  if (plansource->is_oneshot)
1297  elog(ERROR, "cannot move a one-shot cached plan to another context");
1298 
1299  /* OK, let the caller keep the plan where he wishes */
1300  MemoryContextSetParent(plansource->context, newcontext);
1301 
1302  /*
1303  * The query_context needs no special handling, since it's a child of
1304  * plansource->context. But if there's a generic plan, it should be
1305  * maintained as a sibling of plansource->context.
1306  */
1307  if (plansource->gplan)
1308  {
1309  Assert(plansource->gplan->magic == CACHEDPLAN_MAGIC);
1310  MemoryContextSetParent(plansource->gplan->context, newcontext);
1311  }
1312 }
#define CACHEDPLAN_MAGIC
Definition: plancache.h:26
MemoryContext context
Definition: plancache.h:93
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:354
MemoryContext context
Definition: plancache.h:143
struct CachedPlan * gplan
Definition: plancache.h:105
#define ERROR
Definition: elog.h:43
#define Assert(condition)
Definition: c.h:699
#define elog
Definition: elog.h:219
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25

◆ CompleteCachedPlan()

void CompleteCachedPlan ( CachedPlanSource plansource,
List querytree_list,
MemoryContext  querytree_context,
Oid param_types,
int  num_params,
ParserSetupHook  parserSetup,
void *  parserSetupArg,
int  cursor_options,
bool  fixed_result 
)

Definition at line 326 of file plancache.c.

References ALLOCSET_START_SMALL_SIZES, AllocSetContextCreate, Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::context, copyObject, CurrentMemoryContext, CachedPlanSource::cursor_options, CachedPlanSource::dependsOnRLS, extract_query_dependencies(), CachedPlanSource::fixed_result, GetOverrideSearchPath(), GetUserId(), CachedPlanSource::invalItems, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_valid, IsTransactionStmtPlan, CachedPlanSource::magic, MemoryContextSetParent(), MemoryContextSwitchTo(), CachedPlanSource::num_params, palloc(), CachedPlanSource::param_types, CachedPlanSource::parserSetup, CachedPlanSource::parserSetupArg, PlanCacheComputeResultDesc(), CachedPlanSource::query_context, CachedPlanSource::query_list, CachedPlanSource::relationOids, CachedPlanSource::resultDesc, CachedPlanSource::rewriteRoleId, CachedPlanSource::rewriteRowSecurity, row_security, and CachedPlanSource::search_path.

Referenced by _SPI_execute_plan(), _SPI_prepare_plan(), exec_parse_message(), and PrepareQuery().

335 {
336  MemoryContext source_context = plansource->context;
338 
339  /* Assert caller is doing things in a sane order */
340  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
341  Assert(!plansource->is_complete);
342 
343  /*
344  * If caller supplied a querytree_context, reparent it underneath the
345  * CachedPlanSource's context; otherwise, create a suitable context and
346  * copy the querytree_list into it. But no data copying should be done
347  * for one-shot plans; for those, assume the passed querytree_list is
348  * sufficiently long-lived.
349  */
350  if (plansource->is_oneshot)
351  {
352  querytree_context = CurrentMemoryContext;
353  }
354  else if (querytree_context != NULL)
355  {
356  MemoryContextSetParent(querytree_context, source_context);
357  MemoryContextSwitchTo(querytree_context);
358  }
359  else
360  {
361  /* Again, it's a good bet the querytree_context can be small */
362  querytree_context = AllocSetContextCreate(source_context,
363  "CachedPlanQuery",
365  MemoryContextSwitchTo(querytree_context);
366  querytree_list = copyObject(querytree_list);
367  }
368 
369  plansource->query_context = querytree_context;
370  plansource->query_list = querytree_list;
371 
372  if (!plansource->is_oneshot && !IsTransactionStmtPlan(plansource))
373  {
374  /*
375  * Use the planner machinery to extract dependencies. Data is saved
376  * in query_context. (We assume that not a lot of extra cruft is
377  * created by this call.) We can skip this for one-shot plans, and
378  * transaction control commands have no such dependencies anyway.
379  */
380  extract_query_dependencies((Node *) querytree_list,
381  &plansource->relationOids,
382  &plansource->invalItems,
383  &plansource->dependsOnRLS);
384 
385  /* Update RLS info as well. */
386  plansource->rewriteRoleId = GetUserId();
387  plansource->rewriteRowSecurity = row_security;
388 
389  /*
390  * Also save the current search_path in the query_context. (This
391  * should not generate much extra cruft either, since almost certainly
392  * the path is already valid.) Again, we don't really need this for
393  * one-shot plans; and we *must* skip this for transaction control
394  * commands, because this could result in catalog accesses.
395  */
396  plansource->search_path = GetOverrideSearchPath(querytree_context);
397  }
398 
399  /*
400  * Save the final parameter types (or other parameter specification data)
401  * into the source_context, as well as our other parameters. Also save
402  * the result tuple descriptor.
403  */
404  MemoryContextSwitchTo(source_context);
405 
406  if (num_params > 0)
407  {
408  plansource->param_types = (Oid *) palloc(num_params * sizeof(Oid));
409  memcpy(plansource->param_types, param_types, num_params * sizeof(Oid));
410  }
411  else
412  plansource->param_types = NULL;
413  plansource->num_params = num_params;
414  plansource->parserSetup = parserSetup;
415  plansource->parserSetupArg = parserSetupArg;
416  plansource->cursor_options = cursor_options;
417  plansource->fixed_result = fixed_result;
418  plansource->resultDesc = PlanCacheComputeResultDesc(querytree_list);
419 
420  MemoryContextSwitchTo(oldcxt);
421 
422  plansource->is_complete = true;
423  plansource->is_valid = true;
424 }
MemoryContext context
Definition: plancache.h:93
bool rewriteRowSecurity
Definition: plancache.h:102
Oid GetUserId(void)
Definition: miscinit.c:379
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:354
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:517
unsigned int Oid
Definition: postgres_ext.h:31
TupleDesc resultDesc
Definition: plancache.h:92
List * invalItems
Definition: plancache.h:97
OverrideSearchPath * GetOverrideSearchPath(MemoryContext context)
Definition: namespace.c:3300
Oid * param_types
Definition: plancache.h:86
List * relationOids
Definition: plancache.h:96
void * parserSetupArg
Definition: plancache.h:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ParserSetupHook parserSetup
Definition: plancache.h:88
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
bool row_security
Definition: guc.c:448
#define Assert(condition)
Definition: c.h:699
void extract_query_dependencies(Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
Definition: setrefs.c:2596
void * palloc(Size size)
Definition: mcxt.c:924
MemoryContext query_context
Definition: plancache.h:100
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:209
List * query_list
Definition: plancache.h:95
#define IsTransactionStmtPlan(plansource)
Definition: plancache.c:78
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
#define copyObject(obj)
Definition: nodes.h:630
static TupleDesc PlanCacheComputeResultDesc(List *stmt_list)
Definition: plancache.c:1674
struct OverrideSearchPath * search_path
Definition: plancache.h:98

◆ CopyCachedPlan()

CachedPlanSource* CopyCachedPlan ( CachedPlanSource plansource)

Definition at line 1324 of file plancache.c.

References ALLOCSET_START_SMALL_SIZES, AllocSetContextCreate, Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::commandTag, CachedPlanSource::context, copyObject, CopyOverrideSearchPath(), CreateTupleDescCopy(), CurrentMemoryContext, CachedPlanSource::cursor_options, CachedPlanSource::dependsOnRLS, elog, ERROR, CachedPlanSource::fixed_result, CachedPlanSource::generation, CachedPlanSource::generic_cost, CachedPlanSource::gplan, CachedPlanSource::invalItems, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::is_valid, CachedPlanSource::magic, MemoryContextSetIdentifier(), MemoryContextSwitchTo(), CachedPlanSource::next_saved, CachedPlanSource::num_custom_plans, CachedPlanSource::num_params, palloc(), palloc0(), CachedPlanSource::param_types, CachedPlanSource::parserSetup, CachedPlanSource::parserSetupArg, pstrdup(), CachedPlanSource::query_context, CachedPlanSource::query_list, CachedPlanSource::query_string, CachedPlanSource::raw_parse_tree, CachedPlanSource::relationOids, CachedPlanSource::resultDesc, CachedPlanSource::rewriteRoleId, CachedPlanSource::rewriteRowSecurity, CachedPlanSource::search_path, and CachedPlanSource::total_custom_cost.

Referenced by _SPI_save_plan().

1325 {
1326  CachedPlanSource *newsource;
1327  MemoryContext source_context;
1328  MemoryContext querytree_context;
1329  MemoryContext oldcxt;
1330 
1331  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1332  Assert(plansource->is_complete);
1333 
1334  /*
1335  * One-shot plans can't be copied, because we haven't taken care that
1336  * parsing/planning didn't scribble on the raw parse tree or querytrees.
1337  */
1338  if (plansource->is_oneshot)
1339  elog(ERROR, "cannot copy a one-shot cached plan");
1340 
1342  "CachedPlanSource",
1344 
1345  oldcxt = MemoryContextSwitchTo(source_context);
1346 
1347  newsource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
1348  newsource->magic = CACHEDPLANSOURCE_MAGIC;
1349  newsource->raw_parse_tree = copyObject(plansource->raw_parse_tree);
1350  newsource->query_string = pstrdup(plansource->query_string);
1351  MemoryContextSetIdentifier(source_context, newsource->query_string);
1352  newsource->commandTag = plansource->commandTag;
1353  if (plansource->num_params > 0)
1354  {
1355  newsource->param_types = (Oid *)
1356  palloc(plansource->num_params * sizeof(Oid));
1357  memcpy(newsource->param_types, plansource->param_types,
1358  plansource->num_params * sizeof(Oid));
1359  }
1360  else
1361  newsource->param_types = NULL;
1362  newsource->num_params = plansource->num_params;
1363  newsource->parserSetup = plansource->parserSetup;
1364  newsource->parserSetupArg = plansource->parserSetupArg;
1365  newsource->cursor_options = plansource->cursor_options;
1366  newsource->fixed_result = plansource->fixed_result;
1367  if (plansource->resultDesc)
1368  newsource->resultDesc = CreateTupleDescCopy(plansource->resultDesc);
1369  else
1370  newsource->resultDesc = NULL;
1371  newsource->context = source_context;
1372 
1373  querytree_context = AllocSetContextCreate(source_context,
1374  "CachedPlanQuery",
1376  MemoryContextSwitchTo(querytree_context);
1377  newsource->query_list = copyObject(plansource->query_list);
1378  newsource->relationOids = copyObject(plansource->relationOids);
1379  newsource->invalItems = copyObject(plansource->invalItems);
1380  if (plansource->search_path)
1381  newsource->search_path = CopyOverrideSearchPath(plansource->search_path);
1382  newsource->query_context = querytree_context;
1383  newsource->rewriteRoleId = plansource->rewriteRoleId;
1384  newsource->rewriteRowSecurity = plansource->rewriteRowSecurity;
1385  newsource->dependsOnRLS = plansource->dependsOnRLS;
1386 
1387  newsource->gplan = NULL;
1388 
1389  newsource->is_oneshot = false;
1390  newsource->is_complete = true;
1391  newsource->is_saved = false;
1392  newsource->is_valid = plansource->is_valid;
1393  newsource->generation = plansource->generation;
1394  newsource->next_saved = NULL;
1395 
1396  /* We may as well copy any acquired cost knowledge */
1397  newsource->generic_cost = plansource->generic_cost;
1398  newsource->total_custom_cost = plansource->total_custom_cost;
1399  newsource->num_custom_plans = plansource->num_custom_plans;
1400 
1401  MemoryContextSwitchTo(oldcxt);
1402 
1403  return newsource;
1404 }
MemoryContext context
Definition: plancache.h:93
bool rewriteRowSecurity
Definition: plancache.h:102
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:112
const char * commandTag
Definition: plancache.h:85
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
double total_custom_cost
Definition: plancache.h:116
struct CachedPlan * gplan
Definition: plancache.h:105
TupleDesc resultDesc
Definition: plancache.h:92
struct CachedPlanSource * next_saved
Definition: plancache.h:113
#define ERROR
Definition: elog.h:43
OverrideSearchPath * CopyOverrideSearchPath(OverrideSearchPath *path)
Definition: namespace.c:3336
List * invalItems
Definition: plancache.h:97
Oid * param_types
Definition: plancache.h:86
List * relationOids
Definition: plancache.h:96
void * parserSetupArg
Definition: plancache.h:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ParserSetupHook parserSetup
Definition: plancache.h:88
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
void * palloc0(Size size)
Definition: mcxt.c:955
double generic_cost
Definition: plancache.h:115
#define Assert(condition)
Definition: c.h:699
const char * query_string
Definition: plancache.h:84
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:329
struct RawStmt * raw_parse_tree
Definition: plancache.h:83
void * palloc(Size size)
Definition: mcxt.c:924
MemoryContext query_context
Definition: plancache.h:100
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:209
List * query_list
Definition: plancache.h:95
#define elog
Definition: elog.h:219
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
#define copyObject(obj)
Definition: nodes.h:630
struct OverrideSearchPath * search_path
Definition: plancache.h:98

◆ CreateCachedPlan()

CachedPlanSource* CreateCachedPlan ( struct RawStmt raw_parse_tree,
const char *  query_string,
const char *  commandTag 
)

Definition at line 152 of file plancache.c.

References ALLOCSET_START_SMALL_SIZES, AllocSetContextCreate, Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::commandTag, CachedPlanSource::context, copyObject, CurrentMemoryContext, CachedPlanSource::cursor_options, CachedPlanSource::dependsOnRLS, CachedPlanSource::fixed_result, CachedPlanSource::generation, CachedPlanSource::generic_cost, CachedPlanSource::gplan, InvalidOid, CachedPlanSource::invalItems, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::is_valid, CachedPlanSource::magic, MemoryContextSetIdentifier(), MemoryContextSwitchTo(), CachedPlanSource::next_saved, NIL, CachedPlanSource::num_custom_plans, CachedPlanSource::num_params, palloc0(), CachedPlanSource::param_types, CachedPlanSource::parserSetup, CachedPlanSource::parserSetupArg, pstrdup(), CachedPlanSource::query_context, CachedPlanSource::query_list, CachedPlanSource::query_string, CachedPlanSource::raw_parse_tree, CachedPlanSource::relationOids, CachedPlanSource::resultDesc, CachedPlanSource::rewriteRoleId, CachedPlanSource::rewriteRowSecurity, CachedPlanSource::search_path, and CachedPlanSource::total_custom_cost.

Referenced by _SPI_prepare_plan(), exec_parse_message(), and PrepareQuery().

155 {
156  CachedPlanSource *plansource;
157  MemoryContext source_context;
158  MemoryContext oldcxt;
159 
160  Assert(query_string != NULL); /* required as of 8.4 */
161 
162  /*
163  * Make a dedicated memory context for the CachedPlanSource and its
164  * permanent subsidiary data. It's probably not going to be large, but
165  * just in case, allow it to grow large. Initially it's a child of the
166  * caller's context (which we assume to be transient), so that it will be
167  * cleaned up on error.
168  */
170  "CachedPlanSource",
172 
173  /*
174  * Create and fill the CachedPlanSource struct within the new context.
175  * Most fields are just left empty for the moment.
176  */
177  oldcxt = MemoryContextSwitchTo(source_context);
178 
179  plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
180  plansource->magic = CACHEDPLANSOURCE_MAGIC;
181  plansource->raw_parse_tree = copyObject(raw_parse_tree);
182  plansource->query_string = pstrdup(query_string);
183  MemoryContextSetIdentifier(source_context, plansource->query_string);
184  plansource->commandTag = commandTag;
185  plansource->param_types = NULL;
186  plansource->num_params = 0;
187  plansource->parserSetup = NULL;
188  plansource->parserSetupArg = NULL;
189  plansource->cursor_options = 0;
190  plansource->fixed_result = false;
191  plansource->resultDesc = NULL;
192  plansource->context = source_context;
193  plansource->query_list = NIL;
194  plansource->relationOids = NIL;
195  plansource->invalItems = NIL;
196  plansource->search_path = NULL;
197  plansource->query_context = NULL;
198  plansource->rewriteRoleId = InvalidOid;
199  plansource->rewriteRowSecurity = false;
200  plansource->dependsOnRLS = false;
201  plansource->gplan = NULL;
202  plansource->is_oneshot = false;
203  plansource->is_complete = false;
204  plansource->is_saved = false;
205  plansource->is_valid = false;
206  plansource->generation = 0;
207  plansource->next_saved = NULL;
208  plansource->generic_cost = -1;
209  plansource->total_custom_cost = 0;
210  plansource->num_custom_plans = 0;
211 
212  MemoryContextSwitchTo(oldcxt);
213 
214  return plansource;
215 }
MemoryContext context
Definition: plancache.h:93
#define NIL
Definition: pg_list.h:69
bool rewriteRowSecurity
Definition: plancache.h:102
const char * commandTag
Definition: plancache.h:85
char * pstrdup(const char *in)
Definition: mcxt.c:1161
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
double total_custom_cost
Definition: plancache.h:116
struct CachedPlan * gplan
Definition: plancache.h:105
TupleDesc resultDesc
Definition: plancache.h:92
struct CachedPlanSource * next_saved
Definition: plancache.h:113
List * invalItems
Definition: plancache.h:97
Oid * param_types
Definition: plancache.h:86
List * relationOids
Definition: plancache.h:96
void * parserSetupArg
Definition: plancache.h:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ParserSetupHook parserSetup
Definition: plancache.h:88
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
void * palloc0(Size size)
Definition: mcxt.c:955
#define InvalidOid
Definition: postgres_ext.h:36
double generic_cost
Definition: plancache.h:115
#define Assert(condition)
Definition: c.h:699
const char * query_string
Definition: plancache.h:84
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:329
struct RawStmt * raw_parse_tree
Definition: plancache.h:83
MemoryContext query_context
Definition: plancache.h:100
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:209
List * query_list
Definition: plancache.h:95
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
#define copyObject(obj)
Definition: nodes.h:630
struct OverrideSearchPath * search_path
Definition: plancache.h:98

◆ CreateOneShotCachedPlan()

CachedPlanSource* CreateOneShotCachedPlan ( struct RawStmt raw_parse_tree,
const char *  query_string,
const char *  commandTag 
)

Definition at line 236 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::commandTag, CachedPlanSource::context, CurrentMemoryContext, CachedPlanSource::cursor_options, CachedPlanSource::dependsOnRLS, CachedPlanSource::fixed_result, CachedPlanSource::generation, CachedPlanSource::generic_cost, CachedPlanSource::gplan, InvalidOid, CachedPlanSource::invalItems, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::is_valid, CachedPlanSource::magic, CachedPlanSource::next_saved, NIL, CachedPlanSource::num_custom_plans, CachedPlanSource::num_params, palloc0(), CachedPlanSource::param_types, CachedPlanSource::parserSetup, CachedPlanSource::parserSetupArg, CachedPlanSource::query_context, CachedPlanSource::query_list, CachedPlanSource::query_string, CachedPlanSource::raw_parse_tree, CachedPlanSource::relationOids, CachedPlanSource::resultDesc, CachedPlanSource::rewriteRoleId, CachedPlanSource::rewriteRowSecurity, CachedPlanSource::search_path, and CachedPlanSource::total_custom_cost.

Referenced by _SPI_prepare_oneshot_plan().

239 {
240  CachedPlanSource *plansource;
241 
242  Assert(query_string != NULL); /* required as of 8.4 */
243 
244  /*
245  * Create and fill the CachedPlanSource struct within the caller's memory
246  * context. Most fields are just left empty for the moment.
247  */
248  plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
249  plansource->magic = CACHEDPLANSOURCE_MAGIC;
250  plansource->raw_parse_tree = raw_parse_tree;
251  plansource->query_string = query_string;
252  plansource->commandTag = commandTag;
253  plansource->param_types = NULL;
254  plansource->num_params = 0;
255  plansource->parserSetup = NULL;
256  plansource->parserSetupArg = NULL;
257  plansource->cursor_options = 0;
258  plansource->fixed_result = false;
259  plansource->resultDesc = NULL;
260  plansource->context = CurrentMemoryContext;
261  plansource->query_list = NIL;
262  plansource->relationOids = NIL;
263  plansource->invalItems = NIL;
264  plansource->search_path = NULL;
265  plansource->query_context = NULL;
266  plansource->rewriteRoleId = InvalidOid;
267  plansource->rewriteRowSecurity = false;
268  plansource->dependsOnRLS = false;
269  plansource->gplan = NULL;
270  plansource->is_oneshot = true;
271  plansource->is_complete = false;
272  plansource->is_saved = false;
273  plansource->is_valid = false;
274  plansource->generation = 0;
275  plansource->next_saved = NULL;
276  plansource->generic_cost = -1;
277  plansource->total_custom_cost = 0;
278  plansource->num_custom_plans = 0;
279 
280  return plansource;
281 }
MemoryContext context
Definition: plancache.h:93
#define NIL
Definition: pg_list.h:69
bool rewriteRowSecurity
Definition: plancache.h:102
const char * commandTag
Definition: plancache.h:85
double total_custom_cost
Definition: plancache.h:116
struct CachedPlan * gplan
Definition: plancache.h:105
TupleDesc resultDesc
Definition: plancache.h:92
struct CachedPlanSource * next_saved
Definition: plancache.h:113
List * invalItems
Definition: plancache.h:97
Oid * param_types
Definition: plancache.h:86
List * relationOids
Definition: plancache.h:96
void * parserSetupArg
Definition: plancache.h:89
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
ParserSetupHook parserSetup
Definition: plancache.h:88
void * palloc0(Size size)
Definition: mcxt.c:955
#define InvalidOid
Definition: postgres_ext.h:36
double generic_cost
Definition: plancache.h:115
#define Assert(condition)
Definition: c.h:699
const char * query_string
Definition: plancache.h:84
struct RawStmt * raw_parse_tree
Definition: plancache.h:83
MemoryContext query_context
Definition: plancache.h:100
List * query_list
Definition: plancache.h:95
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
struct OverrideSearchPath * search_path
Definition: plancache.h:98

◆ DropCachedPlan()

void DropCachedPlan ( CachedPlanSource plansource)

Definition at line 487 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::context, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::magic, MemoryContextDelete(), CachedPlanSource::next_saved, and ReleaseGenericPlan().

Referenced by drop_unnamed_stmt(), DropAllPreparedStatements(), DropPreparedStatement(), and SPI_freeplan().

488 {
489  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
490 
491  /* If it's been saved, remove it from the list */
492  if (plansource->is_saved)
493  {
494  if (first_saved_plan == plansource)
495  first_saved_plan = plansource->next_saved;
496  else
497  {
498  CachedPlanSource *psrc;
499 
500  for (psrc = first_saved_plan; psrc; psrc = psrc->next_saved)
501  {
502  if (psrc->next_saved == plansource)
503  {
504  psrc->next_saved = plansource->next_saved;
505  break;
506  }
507  }
508  }
509  plansource->is_saved = false;
510  }
511 
512  /* Decrement generic CachePlan's refcount and drop if no longer needed */
513  ReleaseGenericPlan(plansource);
514 
515  /* Mark it no longer valid */
516  plansource->magic = 0;
517 
518  /*
519  * Remove the CachedPlanSource and all subsidiary data (including the
520  * query_context if any). But if it's a one-shot we can't free anything.
521  */
522  if (!plansource->is_oneshot)
523  MemoryContextDelete(plansource->context);
524 }
MemoryContext context
Definition: plancache.h:93
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
static void ReleaseGenericPlan(CachedPlanSource *plansource)
Definition: plancache.c:530
struct CachedPlanSource * next_saved
Definition: plancache.h:113
static CachedPlanSource * first_saved_plan
Definition: plancache.c:88
#define Assert(condition)
Definition: c.h:699
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25

◆ GetCachedPlan()

CachedPlan* GetCachedPlan ( CachedPlanSource plansource,
ParamListInfo  boundParams,
bool  useResOwner,
QueryEnvironment queryEnv 
)

Definition at line 1137 of file plancache.c.

References Assert, BuildCachedPlan(), cached_plan_cost(), CACHEDPLAN_MAGIC, CACHEDPLANSOURCE_MAGIC, CacheMemoryContext, CheckCachedPlan(), choose_custom_plan(), CachedPlanSource::context, CachedPlan::context, CurrentResourceOwner, elog, ERROR, CachedPlanSource::generic_cost, CachedPlanSource::gplan, CachedPlanSource::is_complete, CachedPlanSource::is_saved, CachedPlan::is_saved, CachedPlanSource::magic, CachedPlan::magic, MemoryContextGetParent(), MemoryContextSetParent(), NIL, CachedPlanSource::num_custom_plans, CachedPlan::refcount, ReleaseGenericPlan(), ResourceOwnerEnlargePlanCacheRefs(), ResourceOwnerRememberPlanCacheRef(), RevalidateCachedQuery(), and CachedPlanSource::total_custom_cost.

Referenced by _SPI_execute_plan(), exec_bind_message(), ExecuteQuery(), ExplainExecuteQuery(), SPI_cursor_open_internal(), and SPI_plan_get_cached_plan().

1139 {
1140  CachedPlan *plan = NULL;
1141  List *qlist;
1142  bool customplan;
1143 
1144  /* Assert caller is doing things in a sane order */
1145  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1146  Assert(plansource->is_complete);
1147  /* This seems worth a real test, though */
1148  if (useResOwner && !plansource->is_saved)
1149  elog(ERROR, "cannot apply ResourceOwner to non-saved cached plan");
1150 
1151  /* Make sure the querytree list is valid and we have parse-time locks */
1152  qlist = RevalidateCachedQuery(plansource, queryEnv);
1153 
1154  /* Decide whether to use a custom plan */
1155  customplan = choose_custom_plan(plansource, boundParams);
1156 
1157  if (!customplan)
1158  {
1159  if (CheckCachedPlan(plansource))
1160  {
1161  /* We want a generic plan, and we already have a valid one */
1162  plan = plansource->gplan;
1163  Assert(plan->magic == CACHEDPLAN_MAGIC);
1164  }
1165  else
1166  {
1167  /* Build a new generic plan */
1168  plan = BuildCachedPlan(plansource, qlist, NULL, queryEnv);
1169  /* Just make real sure plansource->gplan is clear */
1170  ReleaseGenericPlan(plansource);
1171  /* Link the new generic plan into the plansource */
1172  plansource->gplan = plan;
1173  plan->refcount++;
1174  /* Immediately reparent into appropriate context */
1175  if (plansource->is_saved)
1176  {
1177  /* saved plans all live under CacheMemoryContext */
1179  plan->is_saved = true;
1180  }
1181  else
1182  {
1183  /* otherwise, it should be a sibling of the plansource */
1185  MemoryContextGetParent(plansource->context));
1186  }
1187  /* Update generic_cost whenever we make a new generic plan */
1188  plansource->generic_cost = cached_plan_cost(plan, false);
1189 
1190  /*
1191  * If, based on the now-known value of generic_cost, we'd not have
1192  * chosen to use a generic plan, then forget it and make a custom
1193  * plan. This is a bit of a wart but is necessary to avoid a
1194  * glitch in behavior when the custom plans are consistently big
1195  * winners; at some point we'll experiment with a generic plan and
1196  * find it's a loser, but we don't want to actually execute that
1197  * plan.
1198  */
1199  customplan = choose_custom_plan(plansource, boundParams);
1200 
1201  /*
1202  * If we choose to plan again, we need to re-copy the query_list,
1203  * since the planner probably scribbled on it. We can force
1204  * BuildCachedPlan to do that by passing NIL.
1205  */
1206  qlist = NIL;
1207  }
1208  }
1209 
1210  if (customplan)
1211  {
1212  /* Build a custom plan */
1213  plan = BuildCachedPlan(plansource, qlist, boundParams, queryEnv);
1214  /* Accumulate total costs of custom plans, but 'ware overflow */
1215  if (plansource->num_custom_plans < INT_MAX)
1216  {
1217  plansource->total_custom_cost += cached_plan_cost(plan, true);
1218  plansource->num_custom_plans++;
1219  }
1220  }
1221 
1222  Assert(plan != NULL);
1223 
1224  /* Flag the plan as in use by caller */
1225  if (useResOwner)
1227  plan->refcount++;
1228  if (useResOwner)
1230 
1231  /*
1232  * Saved plans should be under CacheMemoryContext so they will not go away
1233  * until their reference count goes to zero. In the generic-plan cases we
1234  * already took care of that, but for a custom plan, do it as soon as we
1235  * have created a reference-counted link.
1236  */
1237  if (customplan && plansource->is_saved)
1238  {
1240  plan->is_saved = true;
1241  }
1242 
1243  return plan;
1244 }
#define CACHEDPLAN_MAGIC
Definition: plancache.h:26
MemoryContext context
Definition: plancache.h:93
static CachedPlan * BuildCachedPlan(CachedPlanSource *plansource, List *qlist, ParamListInfo boundParams, QueryEnvironment *queryEnv)
Definition: plancache.c:882
#define NIL
Definition: pg_list.h:69
static void ReleaseGenericPlan(CachedPlanSource *plansource)
Definition: plancache.c:530
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:439
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:354
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
int refcount
Definition: plancache.h:142
void ResourceOwnerRememberPlanCacheRef(ResourceOwner owner, CachedPlan *plan)
Definition: resowner.c:1063
void ResourceOwnerEnlargePlanCacheRefs(ResourceOwner owner)
Definition: resowner.c:1052
MemoryContext context
Definition: plancache.h:143
double total_custom_cost
Definition: plancache.h:116
struct CachedPlan * gplan
Definition: plancache.h:105
#define ERROR
Definition: elog.h:43
static List * RevalidateCachedQuery(CachedPlanSource *plansource, QueryEnvironment *queryEnv)
Definition: plancache.c:558
bool is_saved
Definition: plancache.h:135
double generic_cost
Definition: plancache.h:115
#define Assert(condition)
Definition: c.h:699
static bool CheckCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:798
#define elog
Definition: elog.h:219
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
Definition: pg_list.h:45
static bool choose_custom_plan(CachedPlanSource *plansource, ParamListInfo boundParams)
Definition: plancache.c:1021
MemoryContext CacheMemoryContext
Definition: mcxt.c:47
static double cached_plan_cost(CachedPlan *plan, bool include_planner)
Definition: plancache.c:1072

◆ InitPlanCache()

void InitPlanCache ( void  )

Definition at line 116 of file plancache.c.

References AMOPOPID, CacheRegisterRelcacheCallback(), CacheRegisterSyscacheCallback(), FOREIGNDATAWRAPPEROID, FOREIGNSERVEROID, NAMESPACEOID, OPEROID, PlanCacheFuncCallback(), PlanCacheRelCallback(), PlanCacheSysCallback(), and PROCOID.

Referenced by InitPostgres().

117 {
125 }
static void PlanCacheFuncCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: plancache.c:1775
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1431
static void PlanCacheRelCallback(Datum arg, Oid relid)
Definition: plancache.c:1710
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1389
uintptr_t Datum
Definition: postgres.h:365
static void PlanCacheSysCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: plancache.c:1854

◆ ReleaseCachedPlan()

void ReleaseCachedPlan ( CachedPlan plan,
bool  useResOwner 
)

Definition at line 1258 of file plancache.c.

References Assert, CACHEDPLAN_MAGIC, CachedPlan::context, CurrentResourceOwner, CachedPlan::is_oneshot, CachedPlan::is_saved, CachedPlan::magic, MemoryContextDelete(), CachedPlan::refcount, and ResourceOwnerForgetPlanCacheRef().

Referenced by _SPI_execute_plan(), exec_eval_simple_expr(), exec_simple_check_plan(), ExplainExecuteQuery(), PortalReleaseCachedPlan(), ReleaseGenericPlan(), ResourceOwnerReleaseInternal(), and SPI_cursor_open_internal().

1259 {
1260  Assert(plan->magic == CACHEDPLAN_MAGIC);
1261  if (useResOwner)
1262  {
1263  Assert(plan->is_saved);
1265  }
1266  Assert(plan->refcount > 0);
1267  plan->refcount--;
1268  if (plan->refcount == 0)
1269  {
1270  /* Mark it no longer valid */
1271  plan->magic = 0;
1272 
1273  /* One-shot plans do not own their context, so we can't free them */
1274  if (!plan->is_oneshot)
1276  }
1277 }
#define CACHEDPLAN_MAGIC
Definition: plancache.h:26
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
int refcount
Definition: plancache.h:142
MemoryContext context
Definition: plancache.h:143
void ResourceOwnerForgetPlanCacheRef(ResourceOwner owner, CachedPlan *plan)
Definition: resowner.c:1072
bool is_saved
Definition: plancache.h:135
#define Assert(condition)
Definition: c.h:699
bool is_oneshot
Definition: plancache.h:134

◆ ResetPlanCache()

void ResetPlanCache ( void  )

Definition at line 1863 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, CMD_UTILITY, Query::commandType, CachedPlanSource::gplan, CachedPlanSource::is_valid, CachedPlan::is_valid, IsTransactionStmtPlan, lfirst_node, CachedPlanSource::magic, CachedPlanSource::next_saved, CachedPlanSource::query_list, UtilityContainsQuery(), and Query::utilityStmt.

Referenced by assign_session_replication_role(), DiscardAll(), DiscardCommand(), and PlanCacheSysCallback().

1864 {
1865  CachedPlanSource *plansource;
1866 
1867  for (plansource = first_saved_plan; plansource; plansource = plansource->next_saved)
1868  {
1869  ListCell *lc;
1870 
1871  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1872 
1873  /* No work if it's already invalidated */
1874  if (!plansource->is_valid)
1875  continue;
1876 
1877  /*
1878  * We *must not* mark transaction control statements as invalid,
1879  * particularly not ROLLBACK, because they may need to be executed in
1880  * aborted transactions when we can't revalidate them (cf bug #5269).
1881  */
1882  if (IsTransactionStmtPlan(plansource))
1883  continue;
1884 
1885  /*
1886  * In general there is no point in invalidating utility statements
1887  * since they have no plans anyway. So invalidate it only if it
1888  * contains at least one non-utility statement, or contains a utility
1889  * statement that contains a pre-analyzed query (which could have
1890  * dependencies.)
1891  */
1892  foreach(lc, plansource->query_list)
1893  {
1894  Query *query = lfirst_node(Query, lc);
1895 
1896  if (query->commandType != CMD_UTILITY ||
1898  {
1899  /* non-utility statement, so invalidate */
1900  plansource->is_valid = false;
1901  if (plansource->gplan)
1902  plansource->gplan->is_valid = false;
1903  /* no need to look further */
1904  break;
1905  }
1906  }
1907  }
1908 }
bool is_valid
Definition: plancache.h:136
Node * utilityStmt
Definition: parsenodes.h:120
struct CachedPlan * gplan
Definition: plancache.h:105
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:1870
struct CachedPlanSource * next_saved
Definition: plancache.h:113
#define lfirst_node(type, lc)
Definition: pg_list.h:109
static CachedPlanSource * first_saved_plan
Definition: plancache.c:88
CmdType commandType
Definition: parsenodes.h:112
#define Assert(condition)
Definition: c.h:699
List * query_list
Definition: plancache.h:95
#define IsTransactionStmtPlan(plansource)
Definition: plancache.c:78
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25

◆ SaveCachedPlan()

void SaveCachedPlan ( CachedPlanSource plansource)

Definition at line 442 of file plancache.c.

References Assert, CACHEDPLANSOURCE_MAGIC, CacheMemoryContext, CachedPlanSource::context, elog, ERROR, first_saved_plan, CachedPlanSource::is_complete, CachedPlanSource::is_oneshot, CachedPlanSource::is_saved, CachedPlanSource::magic, MemoryContextSetParent(), CachedPlanSource::next_saved, and ReleaseGenericPlan().

Referenced by _SPI_save_plan(), exec_parse_message(), SPI_keepplan(), and StorePreparedStatement().

443 {
444  /* Assert caller is doing things in a sane order */
445  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
446  Assert(plansource->is_complete);
447  Assert(!plansource->is_saved);
448 
449  /* This seems worth a real test, though */
450  if (plansource->is_oneshot)
451  elog(ERROR, "cannot save one-shot cached plan");
452 
453  /*
454  * In typical use, this function would be called before generating any
455  * plans from the CachedPlanSource. If there is a generic plan, moving it
456  * into CacheMemoryContext would be pretty risky since it's unclear
457  * whether the caller has taken suitable care with making references
458  * long-lived. Best thing to do seems to be to discard the plan.
459  */
460  ReleaseGenericPlan(plansource);
461 
462  /*
463  * Reparent the source memory context under CacheMemoryContext so that it
464  * will live indefinitely. The query_context follows along since it's
465  * already a child of the other one.
466  */
468 
469  /*
470  * Add the entry to the global list of cached plans.
471  */
472  plansource->next_saved = first_saved_plan;
473  first_saved_plan = plansource;
474 
475  plansource->is_saved = true;
476 }
MemoryContext context
Definition: plancache.h:93
static void ReleaseGenericPlan(CachedPlanSource *plansource)
Definition: plancache.c:530
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:354
struct CachedPlanSource * next_saved
Definition: plancache.h:113
#define ERROR
Definition: elog.h:43
static CachedPlanSource * first_saved_plan
Definition: plancache.c:88
#define Assert(condition)
Definition: c.h:699
#define elog
Definition: elog.h:219
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:25
MemoryContext CacheMemoryContext
Definition: mcxt.c:47