PostgreSQL Source Code  git master
plancache.h File Reference
#include "access/tupdesc.h"
#include "lib/ilist.h"
#include "nodes/params.h"
#include "tcop/cmdtag.h"
#include "utils/queryenvironment.h"
#include "utils/resowner.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
 
struct  CachedExpression
 

Macros

#define CACHEDPLANSOURCE_MAGIC   195726186
 
#define CACHEDPLAN_MAGIC   953717834
 
#define CACHEDEXPR_MAGIC   838275847
 

Typedefs

typedef struct CachedPlanSource CachedPlanSource
 
typedef struct CachedPlan CachedPlan
 
typedef struct CachedExpression CachedExpression
 

Enumerations

enum  PlanCacheMode { PLAN_CACHE_MODE_AUTO , PLAN_CACHE_MODE_FORCE_GENERIC_PLAN , PLAN_CACHE_MODE_FORCE_CUSTOM_PLAN }
 

Functions

void InitPlanCache (void)
 
void ResetPlanCache (void)
 
CachedPlanSourceCreateCachedPlan (struct RawStmt *raw_parse_tree, const char *query_string, CommandTag commandTag)
 
CachedPlanSourceCreateOneShotCachedPlan (struct RawStmt *raw_parse_tree, const char *query_string, CommandTag 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, ResourceOwner owner, QueryEnvironment *queryEnv)
 
void ReleaseCachedPlan (CachedPlan *plan, ResourceOwner owner)
 
bool CachedPlanAllowsSimpleValidityCheck (CachedPlanSource *plansource, CachedPlan *plan, ResourceOwner owner)
 
bool CachedPlanIsSimplyValid (CachedPlanSource *plansource, CachedPlan *plan, ResourceOwner owner)
 
CachedExpressionGetCachedExpression (Node *expr)
 
void FreeCachedExpression (CachedExpression *cexpr)
 

Variables

PGDLLIMPORT int plan_cache_mode
 

Macro Definition Documentation

◆ CACHEDEXPR_MAGIC

#define CACHEDEXPR_MAGIC   838275847

Definition at line 42 of file plancache.h.

◆ CACHEDPLAN_MAGIC

#define CACHEDPLAN_MAGIC   953717834

Definition at line 41 of file plancache.h.

◆ CACHEDPLANSOURCE_MAGIC

#define CACHEDPLANSOURCE_MAGIC   195726186

Definition at line 40 of file plancache.h.

Typedef Documentation

◆ CachedExpression

◆ CachedPlan

typedef struct CachedPlan CachedPlan

◆ CachedPlanSource

Enumeration Type Documentation

◆ PlanCacheMode

Enumerator
PLAN_CACHE_MODE_AUTO 
PLAN_CACHE_MODE_FORCE_GENERIC_PLAN 
PLAN_CACHE_MODE_FORCE_CUSTOM_PLAN 

Definition at line 30 of file plancache.h.

31 {
PlanCacheMode
Definition: plancache.h:31
@ PLAN_CACHE_MODE_FORCE_CUSTOM_PLAN
Definition: plancache.h:34
@ PLAN_CACHE_MODE_FORCE_GENERIC_PLAN
Definition: plancache.h:33
@ PLAN_CACHE_MODE_AUTO
Definition: plancache.h:32

Function Documentation

◆ CachedPlanAllowsSimpleValidityCheck()

bool CachedPlanAllowsSimpleValidityCheck ( CachedPlanSource plansource,
CachedPlan plan,
ResourceOwner  owner 
)

Definition at line 1309 of file plancache.c.

1311 {
1312  ListCell *lc;
1313 
1314  /*
1315  * Sanity-check that the caller gave us a validated generic plan. Notice
1316  * that we *don't* assert plansource->is_valid as you might expect; that's
1317  * because it's possible that that's already false when GetCachedPlan
1318  * returns, e.g. because ResetPlanCache happened partway through. We
1319  * should accept the plan as long as plan->is_valid is true, and expect to
1320  * replan after the next CachedPlanIsSimplyValid call.
1321  */
1322  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1323  Assert(plan->magic == CACHEDPLAN_MAGIC);
1324  Assert(plan->is_valid);
1325  Assert(plan == plansource->gplan);
1326  Assert(plansource->search_path != NULL);
1328 
1329  /* We don't support oneshot plans here. */
1330  if (plansource->is_oneshot)
1331  return false;
1332  Assert(!plan->is_oneshot);
1333 
1334  /*
1335  * If the plan is dependent on RLS considerations, or it's transient,
1336  * reject. These things probably can't ever happen for table-free
1337  * queries, but for safety's sake let's check.
1338  */
1339  if (plansource->dependsOnRLS)
1340  return false;
1341  if (plan->dependsOnRole)
1342  return false;
1343  if (TransactionIdIsValid(plan->saved_xmin))
1344  return false;
1345 
1346  /*
1347  * Reject if AcquirePlannerLocks would have anything to do. This is
1348  * simplistic, but there's no need to inquire any more carefully; indeed,
1349  * for current callers it shouldn't even be possible to hit any of these
1350  * checks.
1351  */
1352  foreach(lc, plansource->query_list)
1353  {
1354  Query *query = lfirst_node(Query, lc);
1355 
1356  if (query->commandType == CMD_UTILITY)
1357  return false;
1358  if (query->rtable || query->cteList || query->hasSubLinks)
1359  return false;
1360  }
1361 
1362  /*
1363  * Reject if AcquireExecutorLocks would have anything to do. This is
1364  * probably unnecessary given the previous check, but let's be safe.
1365  */
1366  foreach(lc, plan->stmt_list)
1367  {
1368  PlannedStmt *plannedstmt = lfirst_node(PlannedStmt, lc);
1369  ListCell *lc2;
1370 
1371  if (plannedstmt->commandType == CMD_UTILITY)
1372  return false;
1373 
1374  /*
1375  * We have to grovel through the rtable because it's likely to contain
1376  * an RTE_RESULT relation, rather than being totally empty.
1377  */
1378  foreach(lc2, plannedstmt->rtable)
1379  {
1380  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc2);
1381 
1382  if (rte->rtekind == RTE_RELATION)
1383  return false;
1384  }
1385  }
1386 
1387  /*
1388  * Okay, it's simple. Note that what we've primarily established here is
1389  * that no locks need be taken before checking the plan's is_valid flag.
1390  */
1391 
1392  /* Bump refcount if requested. */
1393  if (owner)
1394  {
1396  plan->refcount++;
1398  }
1399 
1400  return true;
1401 }
Assert(fmt[strlen(fmt) - 1] !='\n')
bool OverrideSearchPathMatchesCurrent(OverrideSearchPath *path)
Definition: namespace.c:3462
@ CMD_UTILITY
Definition: nodes.h:281
@ RTE_RELATION
Definition: parsenodes.h:1014
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
#define plan(x)
Definition: pg_regress.c:154
#define CACHEDPLAN_MAGIC
Definition: plancache.h:41
#define CACHEDPLANSOURCE_MAGIC
Definition: plancache.h:40
void ResourceOwnerRememberPlanCacheRef(ResourceOwner owner, CachedPlan *plan)
Definition: resowner.c:1225
void ResourceOwnerEnlargePlanCacheRefs(ResourceOwner owner)
Definition: resowner.c:1214
struct CachedPlan * gplan
Definition: plancache.h:121
struct OverrideSearchPath * search_path
Definition: plancache.h:114
List * query_list
Definition: plancache.h:111
CmdType commandType
Definition: plannodes.h:53
List * rtable
Definition: plannodes.h:73
List * cteList
Definition: parsenodes.h:173
List * rtable
Definition: parsenodes.h:175
CmdType commandType
Definition: parsenodes.h:128
RTEKind rtekind
Definition: parsenodes.h:1033
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References Assert(), CACHEDPLAN_MAGIC, CACHEDPLANSOURCE_MAGIC, CMD_UTILITY, Query::commandType, PlannedStmt::commandType, Query::cteList, CachedPlanSource::dependsOnRLS, CachedPlanSource::gplan, CachedPlanSource::is_oneshot, lfirst, lfirst_node, CachedPlanSource::magic, OverrideSearchPathMatchesCurrent(), plan, CachedPlanSource::query_list, ResourceOwnerEnlargePlanCacheRefs(), ResourceOwnerRememberPlanCacheRef(), Query::rtable, PlannedStmt::rtable, RTE_RELATION, RangeTblEntry::rtekind, CachedPlanSource::search_path, and TransactionIdIsValid.

Referenced by exec_eval_simple_expr(), and exec_simple_check_plan().

◆ CachedPlanGetTargetList()

List* CachedPlanGetTargetList ( CachedPlanSource plansource,
QueryEnvironment queryEnv 
)

Definition at line 1611 of file plancache.c.

1613 {
1614  Query *pstmt;
1615 
1616  /* Assert caller is doing things in a sane order */
1617  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1618  Assert(plansource->is_complete);
1619 
1620  /*
1621  * No work needed if statement doesn't return tuples (we assume this
1622  * feature cannot be changed by an invalidation)
1623  */
1624  if (plansource->resultDesc == NULL)
1625  return NIL;
1626 
1627  /* Make sure the querytree list is valid and we have parse-time locks */
1628  RevalidateCachedQuery(plansource, queryEnv);
1629 
1630  /* Get the primary statement and find out what it returns */
1631  pstmt = QueryListGetPrimaryStmt(plansource->query_list);
1632 
1633  return FetchStatementTargetList((Node *) pstmt);
1634 }
#define NIL
Definition: pg_list.h:68
static List * RevalidateCachedQuery(CachedPlanSource *plansource, QueryEnvironment *queryEnv)
Definition: plancache.c:555
static Query * QueryListGetPrimaryStmt(List *stmts)
Definition: plancache.c:1724
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:348
TupleDesc resultDesc
Definition: plancache.h:108
Definition: nodes.h:129

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().

◆ CachedPlanIsSimplyValid()

bool CachedPlanIsSimplyValid ( CachedPlanSource plansource,
CachedPlan plan,
ResourceOwner  owner 
)

Definition at line 1424 of file plancache.c.

1426 {
1427  /*
1428  * Careful here: since the caller doesn't necessarily hold a refcount on
1429  * the plan to start with, it's possible that "plan" is a dangling
1430  * pointer. Don't dereference it until we've verified that it still
1431  * matches the plansource's gplan (which is either valid or NULL).
1432  */
1433  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1434 
1435  /*
1436  * Has cache invalidation fired on this plan? We can check this right
1437  * away since there are no locks that we'd need to acquire first. Note
1438  * that here we *do* check plansource->is_valid, so as to force plan
1439  * rebuild if that's become false.
1440  */
1441  if (!plansource->is_valid || plan != plansource->gplan || !plan->is_valid)
1442  return false;
1443 
1444  Assert(plan->magic == CACHEDPLAN_MAGIC);
1445 
1446  /* Is the search_path still the same as when we made it? */
1447  Assert(plansource->search_path != NULL);
1449  return false;
1450 
1451  /* It's still good. Bump refcount if requested. */
1452  if (owner)
1453  {
1455  plan->refcount++;
1457  }
1458 
1459  return true;
1460 }

References Assert(), CACHEDPLAN_MAGIC, CACHEDPLANSOURCE_MAGIC, CachedPlanSource::gplan, CachedPlanSource::is_valid, CachedPlanSource::magic, OverrideSearchPathMatchesCurrent(), plan, ResourceOwnerEnlargePlanCacheRefs(), ResourceOwnerRememberPlanCacheRef(), and CachedPlanSource::search_path.

Referenced by exec_eval_simple_expr().

◆ CachedPlanIsValid()

bool CachedPlanIsValid ( CachedPlanSource plansource)

Definition at line 1598 of file plancache.c.

1599 {
1600  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1601  return plansource->is_valid;
1602 }

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

Referenced by SPI_plan_is_valid().

◆ CachedPlanSetParentContext()

void CachedPlanSetParentContext ( CachedPlanSource plansource,
MemoryContext  newcontext 
)

Definition at line 1469 of file plancache.c.

1471 {
1472  /* Assert caller is doing things in a sane order */
1473  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1474  Assert(plansource->is_complete);
1475 
1476  /* These seem worth real tests, though */
1477  if (plansource->is_saved)
1478  elog(ERROR, "cannot move a saved cached plan to another context");
1479  if (plansource->is_oneshot)
1480  elog(ERROR, "cannot move a one-shot cached plan to another context");
1481 
1482  /* OK, let the caller keep the plan where he wishes */
1483  MemoryContextSetParent(plansource->context, newcontext);
1484 
1485  /*
1486  * The query_context needs no special handling, since it's a child of
1487  * plansource->context. But if there's a generic plan, it should be
1488  * maintained as a sibling of plansource->context.
1489  */
1490  if (plansource->gplan)
1491  {
1492  Assert(plansource->gplan->magic == CACHEDPLAN_MAGIC);
1493  MemoryContextSetParent(plansource->gplan->context, newcontext);
1494  }
1495 }
#define ERROR
Definition: elog.h:39
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:546
MemoryContext context
Definition: plancache.h:109
MemoryContext context
Definition: plancache.h:160

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().

◆ 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 338 of file plancache.c.

347 {
348  MemoryContext source_context = plansource->context;
350 
351  /* Assert caller is doing things in a sane order */
352  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
353  Assert(!plansource->is_complete);
354 
355  /*
356  * If caller supplied a querytree_context, reparent it underneath the
357  * CachedPlanSource's context; otherwise, create a suitable context and
358  * copy the querytree_list into it. But no data copying should be done
359  * for one-shot plans; for those, assume the passed querytree_list is
360  * sufficiently long-lived.
361  */
362  if (plansource->is_oneshot)
363  {
364  querytree_context = CurrentMemoryContext;
365  }
366  else if (querytree_context != NULL)
367  {
368  MemoryContextSetParent(querytree_context, source_context);
369  MemoryContextSwitchTo(querytree_context);
370  }
371  else
372  {
373  /* Again, it's a good bet the querytree_context can be small */
374  querytree_context = AllocSetContextCreate(source_context,
375  "CachedPlanQuery",
377  MemoryContextSwitchTo(querytree_context);
378  querytree_list = copyObject(querytree_list);
379  }
380 
381  plansource->query_context = querytree_context;
382  plansource->query_list = querytree_list;
383 
384  if (!plansource->is_oneshot && !IsTransactionStmtPlan(plansource))
385  {
386  /*
387  * Use the planner machinery to extract dependencies. Data is saved
388  * in query_context. (We assume that not a lot of extra cruft is
389  * created by this call.) We can skip this for one-shot plans, and
390  * transaction control commands have no such dependencies anyway.
391  */
392  extract_query_dependencies((Node *) querytree_list,
393  &plansource->relationOids,
394  &plansource->invalItems,
395  &plansource->dependsOnRLS);
396 
397  /* Update RLS info as well. */
398  plansource->rewriteRoleId = GetUserId();
399  plansource->rewriteRowSecurity = row_security;
400 
401  /*
402  * Also save the current search_path in the query_context. (This
403  * should not generate much extra cruft either, since almost certainly
404  * the path is already valid.) Again, we don't really need this for
405  * one-shot plans; and we *must* skip this for transaction control
406  * commands, because this could result in catalog accesses.
407  */
408  plansource->search_path = GetOverrideSearchPath(querytree_context);
409  }
410 
411  /*
412  * Save the final parameter types (or other parameter specification data)
413  * into the source_context, as well as our other parameters. Also save
414  * the result tuple descriptor.
415  */
416  MemoryContextSwitchTo(source_context);
417 
418  if (num_params > 0)
419  {
420  plansource->param_types = (Oid *) palloc(num_params * sizeof(Oid));
421  memcpy(plansource->param_types, param_types, num_params * sizeof(Oid));
422  }
423  else
424  plansource->param_types = NULL;
425  plansource->num_params = num_params;
426  plansource->parserSetup = parserSetup;
427  plansource->parserSetupArg = parserSetupArg;
428  plansource->cursor_options = cursor_options;
429  plansource->fixed_result = fixed_result;
430  plansource->resultDesc = PlanCacheComputeResultDesc(querytree_list);
431 
432  MemoryContextSwitchTo(oldcxt);
433 
434  plansource->is_complete = true;
435  plansource->is_valid = true;
436 }
bool row_security
Definition: guc_tables.c:500
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void * palloc(Size size)
Definition: mcxt.c:1226
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_START_SMALL_SIZES
Definition: memutils.h:170
Oid GetUserId(void)
Definition: miscinit.c:510
OverrideSearchPath * GetOverrideSearchPath(MemoryContext context)
Definition: namespace.c:3403
#define copyObject(obj)
Definition: nodes.h:244
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
static TupleDesc PlanCacheComputeResultDesc(List *stmt_list)
Definition: plancache.c:1920
#define IsTransactionStmtPlan(plansource)
Definition: plancache.c:82
unsigned int Oid
Definition: postgres_ext.h:31
void extract_query_dependencies(Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
Definition: setrefs.c:3489
MemoryContext query_context
Definition: plancache.h:116
List * invalItems
Definition: plancache.h:113
ParserSetupHook parserSetup
Definition: plancache.h:104
bool rewriteRowSecurity
Definition: plancache.h:118
List * relationOids
Definition: plancache.h:112
void * parserSetupArg
Definition: plancache.h:105

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().

◆ CopyCachedPlan()

CachedPlanSource* CopyCachedPlan ( CachedPlanSource plansource)

Definition at line 1507 of file plancache.c.

1508 {
1509  CachedPlanSource *newsource;
1510  MemoryContext source_context;
1511  MemoryContext querytree_context;
1512  MemoryContext oldcxt;
1513 
1514  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
1515  Assert(plansource->is_complete);
1516 
1517  /*
1518  * One-shot plans can't be copied, because we haven't taken care that
1519  * parsing/planning didn't scribble on the raw parse tree or querytrees.
1520  */
1521  if (plansource->is_oneshot)
1522  elog(ERROR, "cannot copy a one-shot cached plan");
1523 
1525  "CachedPlanSource",
1527 
1528  oldcxt = MemoryContextSwitchTo(source_context);
1529 
1530  newsource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
1531  newsource->magic = CACHEDPLANSOURCE_MAGIC;
1532  newsource->raw_parse_tree = copyObject(plansource->raw_parse_tree);
1533  newsource->query_string = pstrdup(plansource->query_string);
1534  MemoryContextSetIdentifier(source_context, newsource->query_string);
1535  newsource->commandTag = plansource->commandTag;
1536  if (plansource->num_params > 0)
1537  {
1538  newsource->param_types = (Oid *)
1539  palloc(plansource->num_params * sizeof(Oid));
1540  memcpy(newsource->param_types, plansource->param_types,
1541  plansource->num_params * sizeof(Oid));
1542  }
1543  else
1544  newsource->param_types = NULL;
1545  newsource->num_params = plansource->num_params;
1546  newsource->parserSetup = plansource->parserSetup;
1547  newsource->parserSetupArg = plansource->parserSetupArg;
1548  newsource->cursor_options = plansource->cursor_options;
1549  newsource->fixed_result = plansource->fixed_result;
1550  if (plansource->resultDesc)
1551  newsource->resultDesc = CreateTupleDescCopy(plansource->resultDesc);
1552  else
1553  newsource->resultDesc = NULL;
1554  newsource->context = source_context;
1555 
1556  querytree_context = AllocSetContextCreate(source_context,
1557  "CachedPlanQuery",
1559  MemoryContextSwitchTo(querytree_context);
1560  newsource->query_list = copyObject(plansource->query_list);
1561  newsource->relationOids = copyObject(plansource->relationOids);
1562  newsource->invalItems = copyObject(plansource->invalItems);
1563  if (plansource->search_path)
1564  newsource->search_path = CopyOverrideSearchPath(plansource->search_path);
1565  newsource->query_context = querytree_context;
1566  newsource->rewriteRoleId = plansource->rewriteRoleId;
1567  newsource->rewriteRowSecurity = plansource->rewriteRowSecurity;
1568  newsource->dependsOnRLS = plansource->dependsOnRLS;
1569 
1570  newsource->gplan = NULL;
1571 
1572  newsource->is_oneshot = false;
1573  newsource->is_complete = true;
1574  newsource->is_saved = false;
1575  newsource->is_valid = plansource->is_valid;
1576  newsource->generation = plansource->generation;
1577 
1578  /* We may as well copy any acquired cost knowledge */
1579  newsource->generic_cost = plansource->generic_cost;
1580  newsource->total_custom_cost = plansource->total_custom_cost;
1581  newsource->num_generic_plans = plansource->num_generic_plans;
1582  newsource->num_custom_plans = plansource->num_custom_plans;
1583 
1584  MemoryContextSwitchTo(oldcxt);
1585 
1586  return newsource;
1587 }
char * pstrdup(const char *in)
Definition: mcxt.c:1644
void * palloc0(Size size)
Definition: mcxt.c:1257
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:521
OverrideSearchPath * CopyOverrideSearchPath(OverrideSearchPath *path)
Definition: namespace.c:3440
CommandTag commandTag
Definition: plancache.h:101
double total_custom_cost
Definition: plancache.h:132
const char * query_string
Definition: plancache.h:100
struct RawStmt * raw_parse_tree
Definition: plancache.h:99
int64 num_custom_plans
Definition: plancache.h:133
int64 num_generic_plans
Definition: plancache.h:134
double generic_cost
Definition: plancache.h:131
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111

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::num_custom_plans, CachedPlanSource::num_generic_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().

◆ CreateCachedPlan()

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

Definition at line 164 of file plancache.c.

167 {
168  CachedPlanSource *plansource;
169  MemoryContext source_context;
170  MemoryContext oldcxt;
171 
172  Assert(query_string != NULL); /* required as of 8.4 */
173 
174  /*
175  * Make a dedicated memory context for the CachedPlanSource and its
176  * permanent subsidiary data. It's probably not going to be large, but
177  * just in case, allow it to grow large. Initially it's a child of the
178  * caller's context (which we assume to be transient), so that it will be
179  * cleaned up on error.
180  */
182  "CachedPlanSource",
184 
185  /*
186  * Create and fill the CachedPlanSource struct within the new context.
187  * Most fields are just left empty for the moment.
188  */
189  oldcxt = MemoryContextSwitchTo(source_context);
190 
191  plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
192  plansource->magic = CACHEDPLANSOURCE_MAGIC;
193  plansource->raw_parse_tree = copyObject(raw_parse_tree);
194  plansource->query_string = pstrdup(query_string);
195  MemoryContextSetIdentifier(source_context, plansource->query_string);
196  plansource->commandTag = commandTag;
197  plansource->param_types = NULL;
198  plansource->num_params = 0;
199  plansource->parserSetup = NULL;
200  plansource->parserSetupArg = NULL;
201  plansource->cursor_options = 0;
202  plansource->fixed_result = false;
203  plansource->resultDesc = NULL;
204  plansource->context = source_context;
205  plansource->query_list = NIL;
206  plansource->relationOids = NIL;
207  plansource->invalItems = NIL;
208  plansource->search_path = NULL;
209  plansource->query_context = NULL;
210  plansource->rewriteRoleId = InvalidOid;
211  plansource->rewriteRowSecurity = false;
212  plansource->dependsOnRLS = false;
213  plansource->gplan = NULL;
214  plansource->is_oneshot = false;
215  plansource->is_complete = false;
216  plansource->is_saved = false;
217  plansource->is_valid = false;
218  plansource->generation = 0;
219  plansource->generic_cost = -1;
220  plansource->total_custom_cost = 0;
221  plansource->num_generic_plans = 0;
222  plansource->num_custom_plans = 0;
223 
224  MemoryContextSwitchTo(oldcxt);
225 
226  return plansource;
227 }
#define InvalidOid
Definition: postgres_ext.h:36

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(), NIL, CachedPlanSource::num_custom_plans, CachedPlanSource::num_generic_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().

◆ CreateOneShotCachedPlan()

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

Definition at line 248 of file plancache.c.

251 {
252  CachedPlanSource *plansource;
253 
254  Assert(query_string != NULL); /* required as of 8.4 */
255 
256  /*
257  * Create and fill the CachedPlanSource struct within the caller's memory
258  * context. Most fields are just left empty for the moment.
259  */
260  plansource = (CachedPlanSource *) palloc0(sizeof(CachedPlanSource));
261  plansource->magic = CACHEDPLANSOURCE_MAGIC;
262  plansource->raw_parse_tree = raw_parse_tree;
263  plansource->query_string = query_string;
264  plansource->commandTag = commandTag;
265  plansource->param_types = NULL;
266  plansource->num_params = 0;
267  plansource->parserSetup = NULL;
268  plansource->parserSetupArg = NULL;
269  plansource->cursor_options = 0;
270  plansource->fixed_result = false;
271  plansource->resultDesc = NULL;
272  plansource->context = CurrentMemoryContext;
273  plansource->query_list = NIL;
274  plansource->relationOids = NIL;
275  plansource->invalItems = NIL;
276  plansource->search_path = NULL;
277  plansource->query_context = NULL;
278  plansource->rewriteRoleId = InvalidOid;
279  plansource->rewriteRowSecurity = false;
280  plansource->dependsOnRLS = false;
281  plansource->gplan = NULL;
282  plansource->is_oneshot = true;
283  plansource->is_complete = false;
284  plansource->is_saved = false;
285  plansource->is_valid = false;
286  plansource->generation = 0;
287  plansource->generic_cost = -1;
288  plansource->total_custom_cost = 0;
289  plansource->num_generic_plans = 0;
290  plansource->num_custom_plans = 0;
291 
292  return plansource;
293 }

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, NIL, CachedPlanSource::num_custom_plans, CachedPlanSource::num_generic_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().

◆ DropCachedPlan()

void DropCachedPlan ( CachedPlanSource plansource)

Definition at line 498 of file plancache.c.

499 {
500  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
501 
502  /* If it's been saved, remove it from the list */
503  if (plansource->is_saved)
504  {
505  dlist_delete(&plansource->node);
506  plansource->is_saved = false;
507  }
508 
509  /* Decrement generic CachedPlan's refcount and drop if no longer needed */
510  ReleaseGenericPlan(plansource);
511 
512  /* Mark it no longer valid */
513  plansource->magic = 0;
514 
515  /*
516  * Remove the CachedPlanSource and all subsidiary data (including the
517  * query_context if any). But if it's a one-shot we can't free anything.
518  */
519  if (!plansource->is_oneshot)
520  MemoryContextDelete(plansource->context);
521 }
static void dlist_delete(dlist_node *node)
Definition: ilist.h:405
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
static void ReleaseGenericPlan(CachedPlanSource *plansource)
Definition: plancache.c:527
dlist_node node
Definition: plancache.h:129

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

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

◆ FreeCachedExpression()

void FreeCachedExpression ( CachedExpression cexpr)

Definition at line 1705 of file plancache.c.

1706 {
1707  /* Sanity check */
1708  Assert(cexpr->magic == CACHEDEXPR_MAGIC);
1709  /* Unlink from global list */
1710  dlist_delete(&cexpr->node);
1711  /* Free all storage associated with CachedExpression */
1712  MemoryContextDelete(cexpr->context);
1713 }
#define CACHEDEXPR_MAGIC
Definition: plancache.h:42
MemoryContext context
Definition: plancache.h:183
dlist_node node
Definition: plancache.h:184

References Assert(), CACHEDEXPR_MAGIC, CachedExpression::context, dlist_delete(), CachedExpression::magic, MemoryContextDelete(), and CachedExpression::node.

Referenced by get_cast_hashentry().

◆ GetCachedExpression()

CachedExpression* GetCachedExpression ( Node expr)

Definition at line 1648 of file plancache.c.

1649 {
1650  CachedExpression *cexpr;
1651  List *relationOids;
1652  List *invalItems;
1653  MemoryContext cexpr_context;
1654  MemoryContext oldcxt;
1655 
1656  /*
1657  * Pass the expression through the planner, and collect dependencies.
1658  * Everything built here is leaked in the caller's context; that's
1659  * intentional to minimize the size of the permanent data structure.
1660  */
1661  expr = (Node *) expression_planner_with_deps((Expr *) expr,
1662  &relationOids,
1663  &invalItems);
1664 
1665  /*
1666  * Make a private memory context, and copy what we need into that. To
1667  * avoid leaking a long-lived context if we fail while copying data, we
1668  * initially make the context under the caller's context.
1669  */
1671  "CachedExpression",
1673 
1674  oldcxt = MemoryContextSwitchTo(cexpr_context);
1675 
1676  cexpr = (CachedExpression *) palloc(sizeof(CachedExpression));
1677  cexpr->magic = CACHEDEXPR_MAGIC;
1678  cexpr->expr = copyObject(expr);
1679  cexpr->is_valid = true;
1680  cexpr->relationOids = copyObject(relationOids);
1681  cexpr->invalItems = copyObject(invalItems);
1682  cexpr->context = cexpr_context;
1683 
1684  MemoryContextSwitchTo(oldcxt);
1685 
1686  /*
1687  * Reparent the expr's memory context under CacheMemoryContext so that it
1688  * will live indefinitely.
1689  */
1691 
1692  /*
1693  * Add the entry to the global list of cached expressions.
1694  */
1696 
1697  return cexpr;
1698 }
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:364
MemoryContext CacheMemoryContext
Definition: mcxt.c:144
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static dlist_head cached_expression_list
Definition: plancache.c:97
Expr * expression_planner_with_deps(Expr *expr, List **relationOids, List **invalItems)
Definition: planner.c:6458
List * relationOids
Definition: plancache.h:181
List * invalItems
Definition: plancache.h:182
Definition: pg_list.h:54

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, cached_expression_list, CACHEDEXPR_MAGIC, CacheMemoryContext, CachedExpression::context, copyObject, CurrentMemoryContext, dlist_push_tail(), CachedExpression::expr, expression_planner_with_deps(), CachedExpression::invalItems, CachedExpression::is_valid, CachedExpression::magic, MemoryContextSetParent(), MemoryContextSwitchTo(), CachedExpression::node, palloc(), and CachedExpression::relationOids.

Referenced by get_cast_hashentry().

◆ GetCachedPlan()

CachedPlan* GetCachedPlan ( CachedPlanSource plansource,
ParamListInfo  boundParams,
ResourceOwner  owner,
QueryEnvironment queryEnv 
)

Definition at line 1141 of file plancache.c.

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

References Assert(), BuildCachedPlan(), cached_plan_cost(), CACHEDPLAN_MAGIC, CACHEDPLANSOURCE_MAGIC, CacheMemoryContext, CheckCachedPlan(), choose_custom_plan(), CachedPlanSource::context, elog(), ERROR, CachedPlanSource::generic_cost, CachedPlanSource::gplan, CachedPlanSource::is_complete, CachedPlanSource::is_saved, CachedPlanSource::magic, MemoryContextGetParent(), MemoryContextSetParent(), NIL, CachedPlanSource::num_custom_plans, CachedPlanSource::num_generic_plans, plan, 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().

◆ InitPlanCache()

void InitPlanCache ( void  )

Definition at line 127 of file plancache.c.

128 {
137 }
void CacheRegisterRelcacheCallback(RelcacheCallbackFunction func, Datum arg)
Definition: inval.c:1561
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1519
static void PlanCacheSysCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: plancache.c:2149
static void PlanCacheObjectCallback(Datum arg, int cacheid, uint32 hashvalue)
Definition: plancache.c:2040
static void PlanCacheRelCallback(Datum arg, Oid relid)
Definition: plancache.c:1956
uintptr_t Datum
Definition: postgres.h:64
@ FOREIGNSERVEROID
Definition: syscache.h:64
@ TYPEOID
Definition: syscache.h:114
@ OPEROID
Definition: syscache.h:72
@ PROCOID
Definition: syscache.h:79
@ FOREIGNDATAWRAPPEROID
Definition: syscache.h:62
@ AMOPOPID
Definition: syscache.h:37
@ NAMESPACEOID
Definition: syscache.h:70

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

Referenced by InitPostgres().

◆ ReleaseCachedPlan()

void ReleaseCachedPlan ( CachedPlan plan,
ResourceOwner  owner 
)

Definition at line 1264 of file plancache.c.

1265 {
1266  Assert(plan->magic == CACHEDPLAN_MAGIC);
1267  if (owner)
1268  {
1269  Assert(plan->is_saved);
1271  }
1272  Assert(plan->refcount > 0);
1273  plan->refcount--;
1274  if (plan->refcount == 0)
1275  {
1276  /* Mark it no longer valid */
1277  plan->magic = 0;
1278 
1279  /* One-shot plans do not own their context, so we can't free them */
1280  if (!plan->is_oneshot)
1281  MemoryContextDelete(plan->context);
1282  }
1283 }
void ResourceOwnerForgetPlanCacheRef(ResourceOwner owner, CachedPlan *plan)
Definition: resowner.c:1234

References Assert(), CACHEDPLAN_MAGIC, MemoryContextDelete(), plan, and ResourceOwnerForgetPlanCacheRef().

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

◆ ResetPlanCache()

void ResetPlanCache ( void  )

Definition at line 2158 of file plancache.c.

2159 {
2160  dlist_iter iter;
2161 
2163  {
2165  node, iter.cur);
2166  ListCell *lc;
2167 
2168  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
2169 
2170  /* No work if it's already invalidated */
2171  if (!plansource->is_valid)
2172  continue;
2173 
2174  /*
2175  * We *must not* mark transaction control statements as invalid,
2176  * particularly not ROLLBACK, because they may need to be executed in
2177  * aborted transactions when we can't revalidate them (cf bug #5269).
2178  */
2179  if (IsTransactionStmtPlan(plansource))
2180  continue;
2181 
2182  /*
2183  * In general there is no point in invalidating utility statements
2184  * since they have no plans anyway. So invalidate it only if it
2185  * contains at least one non-utility statement, or contains a utility
2186  * statement that contains a pre-analyzed query (which could have
2187  * dependencies.)
2188  */
2189  foreach(lc, plansource->query_list)
2190  {
2191  Query *query = lfirst_node(Query, lc);
2192 
2193  if (query->commandType != CMD_UTILITY ||
2195  {
2196  /* non-utility statement, so invalidate */
2197  plansource->is_valid = false;
2198  if (plansource->gplan)
2199  plansource->gplan->is_valid = false;
2200  /* no need to look further */
2201  break;
2202  }
2203  }
2204  }
2205 
2206  /* Likewise invalidate cached expressions */
2208  {
2210  node, iter.cur);
2211 
2212  Assert(cexpr->magic == CACHEDEXPR_MAGIC);
2213 
2214  cexpr->is_valid = false;
2215  }
2216 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:623
#define dlist_container(type, membername, ptr)
Definition: ilist.h:593
static dlist_head saved_plan_list
Definition: plancache.c:92
bool is_valid
Definition: plancache.h:153
Node * utilityStmt
Definition: parsenodes.h:143
dlist_node * cur
Definition: ilist.h:179
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:2180

References Assert(), cached_expression_list, CACHEDEXPR_MAGIC, CACHEDPLANSOURCE_MAGIC, CMD_UTILITY, Query::commandType, dlist_iter::cur, dlist_container, dlist_foreach, CachedPlanSource::gplan, CachedPlanSource::is_valid, CachedPlan::is_valid, CachedExpression::is_valid, IsTransactionStmtPlan, lfirst_node, CachedPlanSource::magic, CachedExpression::magic, CachedPlanSource::query_list, saved_plan_list, UtilityContainsQuery(), and Query::utilityStmt.

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

◆ SaveCachedPlan()

void SaveCachedPlan ( CachedPlanSource plansource)

Definition at line 454 of file plancache.c.

455 {
456  /* Assert caller is doing things in a sane order */
457  Assert(plansource->magic == CACHEDPLANSOURCE_MAGIC);
458  Assert(plansource->is_complete);
459  Assert(!plansource->is_saved);
460 
461  /* This seems worth a real test, though */
462  if (plansource->is_oneshot)
463  elog(ERROR, "cannot save one-shot cached plan");
464 
465  /*
466  * In typical use, this function would be called before generating any
467  * plans from the CachedPlanSource. If there is a generic plan, moving it
468  * into CacheMemoryContext would be pretty risky since it's unclear
469  * whether the caller has taken suitable care with making references
470  * long-lived. Best thing to do seems to be to discard the plan.
471  */
472  ReleaseGenericPlan(plansource);
473 
474  /*
475  * Reparent the source memory context under CacheMemoryContext so that it
476  * will live indefinitely. The query_context follows along since it's
477  * already a child of the other one.
478  */
480 
481  /*
482  * Add the entry to the global list of cached plans.
483  */
484  dlist_push_tail(&saved_plan_list, &plansource->node);
485 
486  plansource->is_saved = true;
487 }

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

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

Variable Documentation

◆ plan_cache_mode

PGDLLIMPORT int plan_cache_mode
extern

Definition at line 119 of file plancache.c.

Referenced by choose_custom_plan().