PostgreSQL Source Code  git master
nodeMemoize.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
Include dependency graph for nodeMemoize.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

MemoizeStateExecInitMemoize (Memoize *node, EState *estate, int eflags)
 
void ExecEndMemoize (MemoizeState *node)
 
void ExecReScanMemoize (MemoizeState *node)
 
double ExecEstimateCacheEntryOverheadBytes (double ntuples)
 
void ExecMemoizeEstimate (MemoizeState *node, ParallelContext *pcxt)
 
void ExecMemoizeInitializeDSM (MemoizeState *node, ParallelContext *pcxt)
 
void ExecMemoizeInitializeWorker (MemoizeState *node, ParallelWorkerContext *pwcxt)
 
void ExecMemoizeRetrieveInstrumentation (MemoizeState *node)
 

Function Documentation

◆ ExecEndMemoize()

void ExecEndMemoize ( MemoizeState node)

Definition at line 1030 of file nodeMemoize.c.

1032 {
1033 #ifdef USE_ASSERT_CHECKING
1034  /* Validate the memory accounting code is correct in assert builds. */
1035  {
1036  int count;
1037  uint64 mem = 0;
1038  memoize_iterator i;
1039  MemoizeEntry *entry;
1040 
1041  memoize_start_iterate(node->hashtable, &i);
1042 
1043  count = 0;
1044  while ((entry = memoize_iterate(node->hashtable, &i)) != NULL)
1045  {
1046  MemoizeTuple *tuple = entry->tuplehead;
1047 
1048  mem += EMPTY_ENTRY_MEMORY_BYTES(entry);
1049  while (tuple != NULL)
1050  {
1051  mem += CACHE_TUPLE_BYTES(tuple);
1052  tuple = tuple->next;
1053  }
1054  count++;
1055  }
1056 
1057  Assert(count == node->hashtable->members);
1058  Assert(mem == node->mem_used);
1059  }
1060 #endif
1061 
1062  /*
1063  * When ending a parallel worker, copy the statistics gathered by the
1064  * worker back into shared memory so that it can be picked up by the main
1065  * process to report in EXPLAIN ANALYZE.
1066  */
1067  if (node->shared_info != NULL && IsParallelWorker())
1068  {
1070 
1071  /* Make mem_peak available for EXPLAIN */
1072  if (node->stats.mem_peak == 0)
1073  node->stats.mem_peak = node->mem_used;
1074 
1075  Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
1077  memcpy(si, &node->stats, sizeof(MemoizeInstrumentation));
1078  }
1079 
1080  /* Remove the cache context */
1082 
1084  /* must drop pointer to cache result tuple */
1086 
1087  /*
1088  * free exprcontext
1089  */
1090  ExecFreeExprContext(&node->ss.ps);
1091 
1092  /*
1093  * shut down the subplan
1094  */
1095  ExecEndNode(outerPlanState(node));
int ParallelWorkerNumber
Definition: parallel.c:112
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:556
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:650
#define outerPlanState(node)
Definition: execnodes.h:1067
#define IsParallelWorker()
Definition: parallel.h:61
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define CACHE_TUPLE_BYTES(t)
Definition: nodeMemoize.c:89
#define EMPTY_ENTRY_MEMORY_BYTES(e)
Definition: nodeMemoize.c:86
MemoizeTuple * tuplehead
Definition: nodeMemoize.c:117
uint64 mem_used
Definition: execnodes.h:2104
SharedMemoizeInfo * shared_info
Definition: execnodes.h:2119
MemoizeInstrumentation stats
Definition: execnodes.h:2118
MemoryContext tableContext
Definition: execnodes.h:2106
ScanState ss
Definition: execnodes.h:2092
struct memoize_hash * hashtable
Definition: execnodes.h:2095
struct MemoizeTuple * next
Definition: nodeMemoize.c:96
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1009
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1385
PlanState ps
Definition: execnodes.h:1382
MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2080
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425

References Assert(), CACHE_TUPLE_BYTES, EMPTY_ENTRY_MEMORY_BYTES, ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), MemoizeState::hashtable, i, IsParallelWorker, MemoizeInstrumentation::mem_peak, MemoizeState::mem_used, MemoryContextDelete(), MemoizeTuple::next, outerPlanState, ParallelWorkerNumber, ScanState::ps, PlanState::ps_ResultTupleSlot, MemoizeState::shared_info, SharedMemoizeInfo::sinstrument, MemoizeState::ss, ScanState::ss_ScanTupleSlot, MemoizeState::stats, MemoizeState::tableContext, and MemoizeEntry::tuplehead.

Referenced by ExecEndNode().

◆ ExecEstimateCacheEntryOverheadBytes()

double ExecEstimateCacheEntryOverheadBytes ( double  ntuples)

Definition at line 1130 of file nodeMemoize.c.

1132 {
1133  return sizeof(MemoizeEntry) + sizeof(MemoizeKey) + sizeof(MemoizeTuple) *
1134  ntuples;
struct MemoizeTuple MemoizeTuple
struct MemoizeEntry MemoizeEntry

Referenced by cost_memoize_rescan().

◆ ExecInitMemoize()

MemoizeState* ExecInitMemoize ( Memoize node,
EState estate,
int  eflags 
)

Definition at line 905 of file nodeMemoize.c.

907 {
909  Plan *outerNode;
910  int i;
911  int nkeys;
912  Oid *eqfuncoids;
913 
914  /* check for unsupported flags */
915  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
916 
917  mstate->ss.ps.plan = (Plan *) node;
918  mstate->ss.ps.state = estate;
919  mstate->ss.ps.ExecProcNode = ExecMemoize;
920 
921  /*
922  * Miscellaneous initialization
923  *
924  * create expression context for node
925  */
926  ExecAssignExprContext(estate, &mstate->ss.ps);
927 
928  outerNode = outerPlan(node);
929  outerPlanState(mstate) = ExecInitNode(outerNode, estate, eflags);
930 
931  /*
932  * Initialize return slot and type. No need to initialize projection info
933  * because this node doesn't do projections.
934  */
936  mstate->ss.ps.ps_ProjInfo = NULL;
937 
938  /*
939  * Initialize scan slot and type.
940  */
942 
943  /*
944  * Set the state machine to lookup the cache. We won't find anything
945  * until we cache something, but this saves a special case to create the
946  * first entry.
947  */
948  mstate->mstatus = MEMO_CACHE_LOOKUP;
949 
950  mstate->nkeys = nkeys = node->numKeys;
955  &TTSOpsVirtual);
956 
957  mstate->param_exprs = (ExprState **) palloc(nkeys * sizeof(ExprState *));
958  mstate->collations = node->collations; /* Just point directly to the plan
959  * data */
960  mstate->hashfunctions = (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
961 
962  eqfuncoids = palloc(nkeys * sizeof(Oid));
963 
964  for (i = 0; i < nkeys; i++)
965  {
966  Oid hashop = node->hashOperators[i];
967  Oid left_hashfn;
968  Oid right_hashfn;
969  Expr *param_expr = (Expr *) list_nth(node->param_exprs, i);
970 
971  if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
972  elog(ERROR, "could not find hash function for hash operator %u",
973  hashop);
974 
975  fmgr_info(left_hashfn, &mstate->hashfunctions[i]);
976 
977  mstate->param_exprs[i] = ExecInitExpr(param_expr, (PlanState *) mstate);
978  eqfuncoids[i] = get_opcode(hashop);
979  }
980 
983  &TTSOpsVirtual,
984  eqfuncoids,
985  node->collations,
986  node->param_exprs,
987  (PlanState *) mstate);
988 
989  pfree(eqfuncoids);
990  mstate->mem_used = 0;
991 
992  /* Limit the total memory consumed by the cache to this */
993  mstate->mem_limit = get_hash_memory_limit();
994 
995  /* A memory context dedicated for the cache */
997  "MemoizeHashTable",
999 
1000  dlist_init(&mstate->lru_list);
1001  mstate->last_tuple = NULL;
1002  mstate->entry = NULL;
1003 
1004  /*
1005  * Mark if we can assume the cache entry is completed after we get the
1006  * first record for it. Some callers might not call us again after
1007  * getting the first match. e.g. A join operator performing a unique join
1008  * is able to skip to the next outer tuple after getting the first
1009  * matching inner tuple. In this case, the cache entry is complete after
1010  * getting the first tuple. This allows us to mark it as so.
1011  */
1012  mstate->singlerow = node->singlerow;
1013  mstate->keyparamids = node->keyparamids;
1014 
1015  /*
1016  * Record if the cache keys should be compared bit by bit, or logically
1017  * using the type's hash equality operator
1018  */
1019  mstate->binary_mode = node->binary_mode;
1020 
1021  /* Zero the statistics counters */
1022  memset(&mstate->stats, 0, sizeof(MemoizeInstrumentation));
1023 
1024  /* Allocate and set up the actual cache */
1025  build_hash_table(mstate, node->est_entries);
1026 
1027  return mstate;
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
ExprState * ExecBuildParamSetEqual(TupleDesc desc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, const Oid *eqfunctions, const Oid *collations, const List *param_exprs, PlanState *parent)
Definition: execExpr.c:3865
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:141
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1799
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleDesc ExecTypeFromExprList(List *exprList)
Definition: execTuples.c:1997
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:682
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:480
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define EXEC_FLAG_MARK
Definition: executor.h:59
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:508
void pfree(void *pointer)
Definition: mcxt.c:1169
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void * palloc(Size size)
Definition: mcxt.c:1062
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3401
#define MEMO_CACHE_LOOKUP
Definition: nodeMemoize.c:78
static void build_hash_table(MemoizeState *mstate, uint32 size)
Definition: nodeMemoize.c:264
static TupleTableSlot * ExecMemoize(PlanState *pstate)
Definition: nodeMemoize.c:663
#define makeNode(_type_)
Definition: nodes.h:586
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
#define outerPlan(node)
Definition: plannodes.h:171
unsigned int Oid
Definition: postgres_ext.h:31
Definition: fmgr.h:57
TupleDesc hashkeydesc
Definition: execnodes.h:2096
FmgrInfo * hashfunctions
Definition: execnodes.h:2102
Oid * collations
Definition: execnodes.h:2103
TupleTableSlot * probeslot
Definition: execnodes.h:2098
struct MemoizeEntry * entry
Definition: execnodes.h:2112
ExprState * cache_eq_expr
Definition: execnodes.h:2099
bool singlerow
Definition: execnodes.h:2114
dlist_head lru_list
Definition: execnodes.h:2107
bool binary_mode
Definition: execnodes.h:2116
Bitmapset * keyparamids
Definition: execnodes.h:2120
uint64 mem_limit
Definition: execnodes.h:2105
ExprState ** param_exprs
Definition: execnodes.h:2100
TupleTableSlot * tableslot
Definition: execnodes.h:2097
struct MemoizeTuple * last_tuple
Definition: execnodes.h:2108
bool singlerow
Definition: plannodes.h:814
Bitmapset * keyparamids
Definition: plannodes.h:822
bool binary_mode
Definition: plannodes.h:817
int numKeys
Definition: plannodes.h:809
List * param_exprs
Definition: plannodes.h:813
Oid * hashOperators
Definition: plannodes.h:811
uint32 est_entries
Definition: plannodes.h:819
Oid * collations
Definition: plannodes.h:812
Plan * plan
Definition: execnodes.h:971
EState * state
Definition: execnodes.h:973
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1011
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:977

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), MemoizeState::binary_mode, Memoize::binary_mode, build_hash_table(), MemoizeState::cache_eq_expr, MemoizeState::collations, Memoize::collations, CurrentMemoryContext, dlist_init(), elog, MemoizeState::entry, ERROR, Memoize::est_entries, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecBuildParamSetEqual(), ExecCreateScanSlotFromOuterPlan(), ExecInitExpr(), ExecInitNode(), ExecInitResultTupleSlotTL(), ExecMemoize(), PlanState::ExecProcNode, ExecTypeFromExprList(), fmgr_info(), get_hash_memory_limit(), get_op_hash_functions(), get_opcode(), MemoizeState::hashfunctions, MemoizeState::hashkeydesc, Memoize::hashOperators, i, MemoizeState::keyparamids, Memoize::keyparamids, MemoizeState::last_tuple, list_nth(), MemoizeState::lru_list, makeNode, MakeSingleTupleTableSlot(), MemoizeState::mem_limit, MemoizeState::mem_used, MEMO_CACHE_LOOKUP, MemoizeState::mstatus, MemoizeState::nkeys, Memoize::numKeys, outerPlan, outerPlanState, palloc(), MemoizeState::param_exprs, Memoize::param_exprs, pfree(), PlanState::plan, MemoizeState::probeslot, ScanState::ps, PlanState::ps_ProjInfo, MemoizeState::singlerow, Memoize::singlerow, MemoizeState::ss, PlanState::state, MemoizeState::stats, MemoizeState::tableContext, MemoizeState::tableslot, TTSOpsMinimalTuple, and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecMemoizeEstimate()

void ExecMemoizeEstimate ( MemoizeState node,
ParallelContext pcxt 
)

Definition at line 1148 of file nodeMemoize.c.

1150 {
1151  Size size;
1152 
1153  /* don't need this if not instrumenting or no workers */
1154  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1155  return;
1156 
1157  size = mul_size(pcxt->nworkers, sizeof(MemoizeInstrumentation));
1158  size = add_size(size, offsetof(SharedMemoizeInfo, sinstrument));
1159  shm_toc_estimate_chunk(&pcxt->estimator, size);
1160  shm_toc_estimate_keys(&pcxt->estimator, 1);
#define offsetof(type, field)
Definition: c.h:727
size_t Size
Definition: c.h:540
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
shm_toc_estimator estimator
Definition: parallel.h:42
Instrumentation * instrument
Definition: execnodes.h:981

References add_size(), ParallelContext::estimator, PlanState::instrument, mul_size(), ParallelContext::nworkers, offsetof, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, and MemoizeState::ss.

Referenced by ExecParallelEstimate().

◆ ExecMemoizeInitializeDSM()

void ExecMemoizeInitializeDSM ( MemoizeState node,
ParallelContext pcxt 
)

Definition at line 1169 of file nodeMemoize.c.

1171 {
1172  Size size;
1173 
1174  /* don't need this if not instrumenting or no workers */
1175  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1176  return;
1177 
1178  size = offsetof(SharedMemoizeInfo, sinstrument)
1179  + pcxt->nworkers * sizeof(MemoizeInstrumentation);
1180  node->shared_info = shm_toc_allocate(pcxt->toc, size);
1181  /* ensure any unfilled slots will contain zeroes */
1182  memset(node->shared_info, 0, size);
1183  node->shared_info->num_workers = pcxt->nworkers;
1184  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1185  node->shared_info);
struct MemoizeInstrumentation MemoizeInstrumentation
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
shm_toc * toc
Definition: parallel.h:45
int plan_node_id
Definition: plannodes.h:140

References PlanState::instrument, SharedMemoizeInfo::num_workers, ParallelContext::nworkers, offsetof, PlanState::plan, Plan::plan_node_id, ScanState::ps, MemoizeState::shared_info, shm_toc_allocate(), shm_toc_insert(), MemoizeState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

◆ ExecMemoizeInitializeWorker()

void ExecMemoizeInitializeWorker ( MemoizeState node,
ParallelWorkerContext pwcxt 
)

Definition at line 1194 of file nodeMemoize.c.

1196 {
1197  node->shared_info =
1198  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

References PlanState::plan, Plan::plan_node_id, ScanState::ps, MemoizeState::shared_info, shm_toc_lookup(), MemoizeState::ss, and ParallelWorkerContext::toc.

Referenced by ExecParallelInitializeWorker().

◆ ExecMemoizeRetrieveInstrumentation()

void ExecMemoizeRetrieveInstrumentation ( MemoizeState node)

Definition at line 1207 of file nodeMemoize.c.

1209 {
1210  Size size;
1211  SharedMemoizeInfo *si;
1212 
1213  if (node->shared_info == NULL)
1214  return;
1215 
1216  size = offsetof(SharedMemoizeInfo, sinstrument)
1217  + node->shared_info->num_workers * sizeof(MemoizeInstrumentation);
1218  si = palloc(size);
1219  memcpy(si, node->shared_info, size);
1220  node->shared_info = si;

References SharedMemoizeInfo::num_workers, offsetof, palloc(), and MemoizeState::shared_info.

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecReScanMemoize()

void ExecReScanMemoize ( MemoizeState node)

Definition at line 1098 of file nodeMemoize.c.

1100 {
1102 
1103  /* Mark that we must lookup the cache for a new set of parameters */
1104  node->mstatus = MEMO_CACHE_LOOKUP;
1105 
1106  /* nullify pointers used for the last scan */
1107  node->entry = NULL;
1108  node->last_tuple = NULL;
1109 
1110  /*
1111  * if chgParam of subnode is not null then plan will be re-scanned by
1112  * first ExecProcNode.
1113  */
1114  if (outerPlan->chgParam == NULL)
1116 
1117  /*
1118  * Purge the entire cache if a parameter changed that is not part of the
1119  * cache key.
1120  */
1121  if (bms_nonempty_difference(outerPlan->chgParam, node->keyparamids))
1122  cache_purge_all(node);
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:547
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
static void cache_purge_all(MemoizeState *mstate)
Definition: nodeMemoize.c:374

References bms_nonempty_difference(), cache_purge_all(), MemoizeState::entry, ExecReScan(), MemoizeState::keyparamids, MemoizeState::last_tuple, MEMO_CACHE_LOOKUP, MemoizeState::mstatus, outerPlan, and outerPlanState.

Referenced by ExecReScan().