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 1041 of file nodeMemoize.c.

1043 {
1044 #ifdef USE_ASSERT_CHECKING
1045  /* Validate the memory accounting code is correct in assert builds. */
1046  {
1047  int count;
1048  uint64 mem = 0;
1049  memoize_iterator i;
1050  MemoizeEntry *entry;
1051 
1052  memoize_start_iterate(node->hashtable, &i);
1053 
1054  count = 0;
1055  while ((entry = memoize_iterate(node->hashtable, &i)) != NULL)
1056  {
1057  MemoizeTuple *tuple = entry->tuplehead;
1058 
1059  mem += EMPTY_ENTRY_MEMORY_BYTES(entry);
1060  while (tuple != NULL)
1061  {
1062  mem += CACHE_TUPLE_BYTES(tuple);
1063  tuple = tuple->next;
1064  }
1065  count++;
1066  }
1067 
1068  Assert(count == node->hashtable->members);
1069  Assert(mem == node->mem_used);
1070  }
1071 #endif
1072 
1073  /*
1074  * When ending a parallel worker, copy the statistics gathered by the
1075  * worker back into shared memory so that it can be picked up by the main
1076  * process to report in EXPLAIN ANALYZE.
1077  */
1078  if (node->shared_info != NULL && IsParallelWorker())
1079  {
1081 
1082  /* Make mem_peak available for EXPLAIN */
1083  if (node->stats.mem_peak == 0)
1084  node->stats.mem_peak = node->mem_used;
1085 
1086  Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
1088  memcpy(si, &node->stats, sizeof(MemoizeInstrumentation));
1089  }
1090 
1091  /* Remove the cache context */
1093 
1094  /*
1095  * shut down the subplan
1096  */
1097  ExecEndNode(outerPlanState(node));
int ParallelWorkerNumber
Definition: parallel.c:114
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
#define outerPlanState(node)
Definition: execnodes.h:1133
#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:403
#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:2198
SharedMemoizeInfo * shared_info
Definition: execnodes.h:2213
MemoizeInstrumentation stats
Definition: execnodes.h:2212
MemoryContext tableContext
Definition: execnodes.h:2200
struct memoize_hash * hashtable
Definition: execnodes.h:2189
struct MemoizeTuple * next
Definition: nodeMemoize.c:96
MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2174

References Assert(), CACHE_TUPLE_BYTES, EMPTY_ENTRY_MEMORY_BYTES, ExecEndNode(), MemoizeState::hashtable, i, IsParallelWorker, MemoizeInstrumentation::mem_peak, MemoizeState::mem_used, MemoryContextDelete(), MemoizeTuple::next, outerPlanState, ParallelWorkerNumber, MemoizeState::shared_info, SharedMemoizeInfo::sinstrument, MemoizeState::stats, MemoizeState::tableContext, and MemoizeEntry::tuplehead.

Referenced by ExecEndNode().

◆ ExecEstimateCacheEntryOverheadBytes()

double ExecEstimateCacheEntryOverheadBytes ( double  ntuples)

Definition at line 1132 of file nodeMemoize.c.

1134 {
1135  return sizeof(MemoizeEntry) + sizeof(MemoizeKey) + sizeof(MemoizeTuple) *
1136  ntuples;
struct MemoizeTuple MemoizeTuple
struct MemoizeEntry MemoizeEntry

Referenced by cost_memoize_rescan().

◆ ExecInitMemoize()

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

Definition at line 916 of file nodeMemoize.c.

918 {
920  Plan *outerNode;
921  int i;
922  int nkeys;
923  Oid *eqfuncoids;
924 
925  /* check for unsupported flags */
926  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
927 
928  mstate->ss.ps.plan = (Plan *) node;
929  mstate->ss.ps.state = estate;
930  mstate->ss.ps.ExecProcNode = ExecMemoize;
931 
932  /*
933  * Miscellaneous initialization
934  *
935  * create expression context for node
936  */
937  ExecAssignExprContext(estate, &mstate->ss.ps);
938 
939  outerNode = outerPlan(node);
940  outerPlanState(mstate) = ExecInitNode(outerNode, estate, eflags);
941 
942  /*
943  * Initialize return slot and type. No need to initialize projection info
944  * because this node doesn't do projections.
945  */
947  mstate->ss.ps.ps_ProjInfo = NULL;
948 
949  /*
950  * Initialize scan slot and type.
951  */
953 
954  /*
955  * Set the state machine to lookup the cache. We won't find anything
956  * until we cache something, but this saves a special case to create the
957  * first entry.
958  */
959  mstate->mstatus = MEMO_CACHE_LOOKUP;
960 
961  mstate->nkeys = nkeys = node->numKeys;
966  &TTSOpsVirtual);
967 
968  mstate->param_exprs = (ExprState **) palloc(nkeys * sizeof(ExprState *));
969  mstate->collations = node->collations; /* Just point directly to the plan
970  * data */
971  mstate->hashfunctions = (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
972 
973  eqfuncoids = palloc(nkeys * sizeof(Oid));
974 
975  for (i = 0; i < nkeys; i++)
976  {
977  Oid hashop = node->hashOperators[i];
978  Oid left_hashfn;
979  Oid right_hashfn;
980  Expr *param_expr = (Expr *) list_nth(node->param_exprs, i);
981 
982  if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
983  elog(ERROR, "could not find hash function for hash operator %u",
984  hashop);
985 
986  fmgr_info(left_hashfn, &mstate->hashfunctions[i]);
987 
988  mstate->param_exprs[i] = ExecInitExpr(param_expr, (PlanState *) mstate);
989  eqfuncoids[i] = get_opcode(hashop);
990  }
991 
994  &TTSOpsVirtual,
995  eqfuncoids,
996  node->collations,
997  node->param_exprs,
998  (PlanState *) mstate);
999 
1000  pfree(eqfuncoids);
1001  mstate->mem_used = 0;
1002 
1003  /* Limit the total memory consumed by the cache to this */
1004  mstate->mem_limit = get_hash_memory_limit();
1005 
1006  /* A memory context dedicated for the cache */
1008  "MemoizeHashTable",
1010 
1011  dlist_init(&mstate->lru_list);
1012  mstate->last_tuple = NULL;
1013  mstate->entry = NULL;
1014 
1015  /*
1016  * Mark if we can assume the cache entry is completed after we get the
1017  * first record for it. Some callers might not call us again after
1018  * getting the first match. e.g. A join operator performing a unique join
1019  * is able to skip to the next outer tuple after getting the first
1020  * matching inner tuple. In this case, the cache entry is complete after
1021  * getting the first tuple. This allows us to mark it as so.
1022  */
1023  mstate->singlerow = node->singlerow;
1024  mstate->keyparamids = node->keyparamids;
1025 
1026  /*
1027  * Record if the cache keys should be compared bit by bit, or logically
1028  * using the type's hash equality operator
1029  */
1030  mstate->binary_mode = node->binary_mode;
1031 
1032  /* Zero the statistics counters */
1033  memset(&mstate->stats, 0, sizeof(MemoizeInstrumentation));
1034 
1035  /* Allocate and set up the actual cache */
1036  build_hash_table(mstate, node->est_entries);
1037 
1038  return mstate;
#define ERROR
Definition: elog.h:39
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:128
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:4064
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1800
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleDesc ExecTypeFromExprList(List *exprList)
Definition: execTuples.c:1998
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1239
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:664
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:488
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_MARK
Definition: executor.h:69
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1289
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:509
void pfree(void *pointer)
Definition: mcxt.c:1456
MemoryContext CurrentMemoryContext
Definition: mcxt.c:135
void * palloc(Size size)
Definition: mcxt.c:1226
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3587
#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:674
#define makeNode(_type_)
Definition: nodes.h:176
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define outerPlan(node)
Definition: plannodes.h:183
unsigned int Oid
Definition: postgres_ext.h:31
Definition: fmgr.h:57
TupleDesc hashkeydesc
Definition: execnodes.h:2190
FmgrInfo * hashfunctions
Definition: execnodes.h:2196
Oid * collations
Definition: execnodes.h:2197
TupleTableSlot * probeslot
Definition: execnodes.h:2192
struct MemoizeEntry * entry
Definition: execnodes.h:2206
ExprState * cache_eq_expr
Definition: execnodes.h:2193
bool singlerow
Definition: execnodes.h:2208
dlist_head lru_list
Definition: execnodes.h:2201
bool binary_mode
Definition: execnodes.h:2210
Bitmapset * keyparamids
Definition: execnodes.h:2214
ScanState ss
Definition: execnodes.h:2186
uint64 mem_limit
Definition: execnodes.h:2199
ExprState ** param_exprs
Definition: execnodes.h:2194
TupleTableSlot * tableslot
Definition: execnodes.h:2191
struct MemoizeTuple * last_tuple
Definition: execnodes.h:2202
bool singlerow
Definition: plannodes.h:907
Bitmapset * keyparamids
Definition: plannodes.h:922
bool binary_mode
Definition: plannodes.h:913
int numKeys
Definition: plannodes.h:892
List * param_exprs
Definition: plannodes.h:901
uint32 est_entries
Definition: plannodes.h:919
Plan * plan
Definition: execnodes.h:1037
EState * state
Definition: execnodes.h:1039
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1077
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1043
PlanState ps
Definition: execnodes.h:1474

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), MemoizeState::binary_mode, Memoize::binary_mode, build_hash_table(), MemoizeState::cache_eq_expr, MemoizeState::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, 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 1150 of file nodeMemoize.c.

1152 {
1153  Size size;
1154 
1155  /* don't need this if not instrumenting or no workers */
1156  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1157  return;
1158 
1159  size = mul_size(pcxt->nworkers, sizeof(MemoizeInstrumentation));
1160  size = add_size(size, offsetof(SharedMemoizeInfo, sinstrument));
1161  shm_toc_estimate_chunk(&pcxt->estimator, size);
1162  shm_toc_estimate_keys(&pcxt->estimator, 1);
size_t Size
Definition: c.h:594
#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:1047

References add_size(), ParallelContext::estimator, PlanState::instrument, mul_size(), ParallelContext::nworkers, 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 1171 of file nodeMemoize.c.

1173 {
1174  Size size;
1175 
1176  /* don't need this if not instrumenting or no workers */
1177  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1178  return;
1179 
1180  size = offsetof(SharedMemoizeInfo, sinstrument)
1181  + pcxt->nworkers * sizeof(MemoizeInstrumentation);
1182  node->shared_info = shm_toc_allocate(pcxt->toc, size);
1183  /* ensure any unfilled slots will contain zeroes */
1184  memset(node->shared_info, 0, size);
1185  node->shared_info->num_workers = pcxt->nworkers;
1186  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1187  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:152

References PlanState::instrument, SharedMemoizeInfo::num_workers, ParallelContext::nworkers, 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 1196 of file nodeMemoize.c.

1198 {
1199  node->shared_info =
1200  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 1209 of file nodeMemoize.c.

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

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecReScanMemoize()

void ExecReScanMemoize ( MemoizeState node)

Definition at line 1100 of file nodeMemoize.c.

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

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