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

1082{
1083#ifdef USE_ASSERT_CHECKING
1084 /* Validate the memory accounting code is correct in assert builds. */
1085 if (node->hashtable != NULL)
1086 {
1087 int count;
1088 uint64 mem = 0;
1089 memoize_iterator i;
1090 MemoizeEntry *entry;
1091
1092 memoize_start_iterate(node->hashtable, &i);
1093
1094 count = 0;
1095 while ((entry = memoize_iterate(node->hashtable, &i)) != NULL)
1096 {
1097 MemoizeTuple *tuple = entry->tuplehead;
1098
1099 mem += EMPTY_ENTRY_MEMORY_BYTES(entry);
1100 while (tuple != NULL)
1101 {
1102 mem += CACHE_TUPLE_BYTES(tuple);
1103 tuple = tuple->next;
1104 }
1105 count++;
1106 }
1107
1108 Assert(count == node->hashtable->members);
1109 Assert(mem == node->mem_used);
1110 }
1111#endif
1112
1113 /*
1114 * When ending a parallel worker, copy the statistics gathered by the
1115 * worker back into shared memory so that it can be picked up by the main
1116 * process to report in EXPLAIN ANALYZE.
1117 */
1118 if (node->shared_info != NULL && IsParallelWorker())
1119 {
1121
1122 /* Make mem_peak available for EXPLAIN */
1123 if (node->stats.mem_peak == 0)
1124 node->stats.mem_peak = node->mem_used;
1125
1126 Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
1128 memcpy(si, &node->stats, sizeof(MemoizeInstrumentation));
1129 }
1130
1131 /* Remove the cache context */
1133
1134 /*
1135 * shut down the subplan
1136 */
int ParallelWorkerNumber
Definition: parallel.c:115
uint64_t uint64
Definition: c.h:544
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:562
#define outerPlanState(node)
Definition: execnodes.h:1261
Assert(PointerIsAligned(start, uint64))
#define IsParallelWorker()
Definition: parallel.h:60
int i
Definition: isn.c:77
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
#define CACHE_TUPLE_BYTES(t)
Definition: nodeMemoize.c:90
#define EMPTY_ENTRY_MEMORY_BYTES(e)
Definition: nodeMemoize.c:87
MemoizeTuple * tuplehead
Definition: nodeMemoize.c:118
uint64 mem_used
Definition: execnodes.h:2352
SharedMemoizeInfo * shared_info
Definition: execnodes.h:2367
MemoizeInstrumentation stats
Definition: execnodes.h:2366
MemoryContext tableContext
Definition: execnodes.h:2354
struct memoize_hash * hashtable
Definition: execnodes.h:2343
struct MemoizeTuple * next
Definition: nodeMemoize.c:97
MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2328

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

1174{
1175 return sizeof(MemoizeEntry) + sizeof(MemoizeKey) + sizeof(MemoizeTuple) *
1176 ntuples;
struct MemoizeTuple MemoizeTuple
struct MemoizeEntry MemoizeEntry

Referenced by cost_memoize_rescan().

◆ ExecInitMemoize()

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

Definition at line 952 of file nodeMemoize.c.

954{
956 Plan *outerNode;
957 int i;
958 int nkeys;
959 Oid *eqfuncoids;
960
961 /* check for unsupported flags */
963
964 mstate->ss.ps.plan = (Plan *) node;
965 mstate->ss.ps.state = estate;
966 mstate->ss.ps.ExecProcNode = ExecMemoize;
967
968 /*
969 * Miscellaneous initialization
970 *
971 * create expression context for node
972 */
973 ExecAssignExprContext(estate, &mstate->ss.ps);
974
975 outerNode = outerPlan(node);
976 outerPlanState(mstate) = ExecInitNode(outerNode, estate, eflags);
977
978 /*
979 * Initialize return slot and type. No need to initialize projection info
980 * because this node doesn't do projections.
981 */
983 mstate->ss.ps.ps_ProjInfo = NULL;
984
985 /*
986 * Initialize scan slot and type.
987 */
989
990 /*
991 * Set the state machine to lookup the cache. We won't find anything
992 * until we cache something, but this saves a special case to create the
993 * first entry.
994 */
995 mstate->mstatus = MEMO_CACHE_LOOKUP;
996
997 mstate->nkeys = nkeys = node->numKeys;
1002 &TTSOpsVirtual);
1003
1004 mstate->param_exprs = (ExprState **) palloc(nkeys * sizeof(ExprState *));
1005 mstate->collations = node->collations; /* Just point directly to the plan
1006 * data */
1007 mstate->hashfunctions = (FmgrInfo *) palloc(nkeys * sizeof(FmgrInfo));
1008
1009 eqfuncoids = palloc(nkeys * sizeof(Oid));
1010
1011 for (i = 0; i < nkeys; i++)
1012 {
1013 Oid hashop = node->hashOperators[i];
1014 Oid left_hashfn;
1015 Oid right_hashfn;
1016 Expr *param_expr = (Expr *) list_nth(node->param_exprs, i);
1017
1018 if (!get_op_hash_functions(hashop, &left_hashfn, &right_hashfn))
1019 elog(ERROR, "could not find hash function for hash operator %u",
1020 hashop);
1021
1022 fmgr_info(left_hashfn, &mstate->hashfunctions[i]);
1023
1024 mstate->param_exprs[i] = ExecInitExpr(param_expr, (PlanState *) mstate);
1025 eqfuncoids[i] = get_opcode(hashop);
1026 }
1027
1031 eqfuncoids,
1032 node->collations,
1033 node->param_exprs,
1034 (PlanState *) mstate);
1035
1036 pfree(eqfuncoids);
1037 mstate->mem_used = 0;
1038
1039 /* Limit the total memory consumed by the cache to this */
1040 mstate->mem_limit = get_hash_memory_limit();
1041
1042 /* A memory context dedicated for the cache */
1044 "MemoizeHashTable",
1046
1047 dlist_init(&mstate->lru_list);
1048 mstate->last_tuple = NULL;
1049 mstate->entry = NULL;
1050
1051 /*
1052 * Mark if we can assume the cache entry is completed after we get the
1053 * first record for it. Some callers might not call us again after
1054 * getting the first match. e.g. A join operator performing a unique join
1055 * is able to skip to the next outer tuple after getting the first
1056 * matching inner tuple. In this case, the cache entry is complete after
1057 * getting the first tuple. This allows us to mark it as so.
1058 */
1059 mstate->singlerow = node->singlerow;
1060 mstate->keyparamids = node->keyparamids;
1061
1062 /*
1063 * Record if the cache keys should be compared bit by bit, or logically
1064 * using the type's hash equality operator
1065 */
1066 mstate->binary_mode = node->binary_mode;
1067
1068 /* Zero the statistics counters */
1069 memset(&mstate->stats, 0, sizeof(MemoizeInstrumentation));
1070
1071 /*
1072 * Because it may require a large allocation, we delay building of the
1073 * hash table until executor run.
1074 */
1075 mstate->hashtable = NULL;
1076
1077 return mstate;
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:143
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:4624
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1427
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1988
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
TupleDesc ExecTypeFromExprList(List *exprList)
Definition: execTuples.c:2186
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:704
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:485
#define EXEC_FLAG_BACKWARD
Definition: executor.h:69
#define EXEC_FLAG_MARK
Definition: executor.h:70
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:128
static void dlist_init(dlist_head *head)
Definition: ilist.h:314
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1452
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:582
void pfree(void *pointer)
Definition: mcxt.c:1594
void * palloc(Size size)
Definition: mcxt.c:1365
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3621
#define MEMO_CACHE_LOOKUP
Definition: nodeMemoize.c:79
static TupleTableSlot * ExecMemoize(PlanState *pstate)
Definition: nodeMemoize.c:697
#define makeNode(_type_)
Definition: nodes.h:161
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define outerPlan(node)
Definition: plannodes.h:261
unsigned int Oid
Definition: postgres_ext.h:32
Definition: fmgr.h:57
TupleDesc hashkeydesc
Definition: execnodes.h:2344
FmgrInfo * hashfunctions
Definition: execnodes.h:2350
Oid * collations
Definition: execnodes.h:2351
TupleTableSlot * probeslot
Definition: execnodes.h:2346
struct MemoizeEntry * entry
Definition: execnodes.h:2360
ExprState * cache_eq_expr
Definition: execnodes.h:2347
bool singlerow
Definition: execnodes.h:2362
dlist_head lru_list
Definition: execnodes.h:2355
bool binary_mode
Definition: execnodes.h:2364
Bitmapset * keyparamids
Definition: execnodes.h:2368
ScanState ss
Definition: execnodes.h:2340
uint64 mem_limit
Definition: execnodes.h:2353
ExprState ** param_exprs
Definition: execnodes.h:2348
TupleTableSlot * tableslot
Definition: execnodes.h:2345
struct MemoizeTuple * last_tuple
Definition: execnodes.h:2356
bool singlerow
Definition: plannodes.h:1090
Bitmapset * keyparamids
Definition: plannodes.h:1105
bool binary_mode
Definition: plannodes.h:1096
int numKeys
Definition: plannodes.h:1075
List * param_exprs
Definition: plannodes.h:1084
Plan * plan
Definition: execnodes.h:1165
EState * state
Definition: execnodes.h:1167
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1205
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1171
PlanState ps
Definition: execnodes.h:1621

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), MemoizeState::binary_mode, Memoize::binary_mode, MemoizeState::cache_eq_expr, MemoizeState::collations, CurrentMemoryContext, dlist_init(), elog, MemoizeState::entry, ERROR, 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, MemoizeState::hashtable, 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 1190 of file nodeMemoize.c.

1192{
1193 Size size;
1194
1195 /* don't need this if not instrumenting or no workers */
1196 if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1197 return;
1198
1199 size = mul_size(pcxt->nworkers, sizeof(MemoizeInstrumentation));
1200 size = add_size(size, offsetof(SharedMemoizeInfo, sinstrument));
1201 shm_toc_estimate_chunk(&pcxt->estimator, size);
size_t Size
Definition: c.h:615
#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:494
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511
shm_toc_estimator estimator
Definition: parallel.h:41
Instrumentation * instrument
Definition: execnodes.h:1175

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

1213{
1214 Size size;
1215
1216 /* don't need this if not instrumenting or no workers */
1217 if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1218 return;
1219
1220 size = offsetof(SharedMemoizeInfo, sinstrument)
1221 + pcxt->nworkers * sizeof(MemoizeInstrumentation);
1222 node->shared_info = shm_toc_allocate(pcxt->toc, size);
1223 /* ensure any unfilled slots will contain zeroes */
1224 memset(node->shared_info, 0, size);
1225 node->shared_info->num_workers = pcxt->nworkers;
1226 shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1227 node->shared_info);
struct MemoizeInstrumentation MemoizeInstrumentation
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
shm_toc * toc
Definition: parallel.h:44
int plan_node_id
Definition: plannodes.h:227

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

1238{
1239 node->shared_info =
1240 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 1249 of file nodeMemoize.c.

1251{
1252 Size size;
1254
1255 if (node->shared_info == NULL)
1256 return;
1257
1258 size = offsetof(SharedMemoizeInfo, sinstrument)
1260 si = palloc(size);
1261 memcpy(si, node->shared_info, size);
1262 node->shared_info = si;

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecReScanMemoize()

void ExecReScanMemoize ( MemoizeState node)

Definition at line 1140 of file nodeMemoize.c.

1142{
1144
1145 /* Mark that we must lookup the cache for a new set of parameters */
1146 node->mstatus = MEMO_CACHE_LOOKUP;
1147
1148 /* nullify pointers used for the last scan */
1149 node->entry = NULL;
1150 node->last_tuple = NULL;
1151
1152 /*
1153 * if chgParam of subnode is not null then plan will be re-scanned by
1154 * first ExecProcNode.
1155 */
1156 if (outerPlan->chgParam == NULL)
1158
1159 /*
1160 * Purge the entire cache if a parameter changed that is not part of the
1161 * cache key.
1162 */
1163 if (bms_nonempty_difference(outerPlan->chgParam, node->keyparamids))
1164 cache_purge_all(node);
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:641
void ExecReScan(PlanState *node)
Definition: execAmi.c:77
static void cache_purge_all(MemoizeState *mstate)
Definition: nodeMemoize.c:402

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