PostgreSQL Source Code git master
nodeRecursiveunion.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/nodeRecursiveunion.h"
#include "miscadmin.h"
#include "utils/memutils.h"
Include dependency graph for nodeRecursiveunion.c:

Go to the source code of this file.

Functions

static void build_hash_table (RecursiveUnionState *rustate)
 
static TupleTableSlotExecRecursiveUnion (PlanState *pstate)
 
RecursiveUnionStateExecInitRecursiveUnion (RecursiveUnion *node, EState *estate, int eflags)
 
void ExecEndRecursiveUnion (RecursiveUnionState *node)
 
void ExecReScanRecursiveUnion (RecursiveUnionState *node)
 

Function Documentation

◆ build_hash_table()

static void build_hash_table ( RecursiveUnionState rustate)
static

Definition at line 32 of file nodeRecursiveunion.c.

33{
34 RecursiveUnion *node = (RecursiveUnion *) rustate->ps.plan;
36
37 Assert(node->numCols > 0);
38
39 /*
40 * If both child plans deliver the same fixed tuple slot type, we can tell
41 * BuildTupleHashTable to expect that slot type as input. Otherwise,
42 * we'll pass NULL denoting that any slot type is possible.
43 */
44 rustate->hashtable = BuildTupleHashTable(&rustate->ps,
45 desc,
47 node->numCols,
48 node->dupColIdx,
49 rustate->eqfuncoids,
50 rustate->hashfunctions,
51 node->dupCollations,
52 node->numGroups,
53 0,
54 rustate->ps.state->es_query_cxt,
55 rustate->tuplesContext,
56 rustate->tempContext,
57 false);
58}
TupleHashTable BuildTupleHashTable(PlanState *parent, TupleDesc inputDesc, const TupleTableSlotOps *inputOps, int numCols, AttrNumber *keyColIdx, const Oid *eqfuncoids, FmgrInfo *hashfunctions, Oid *collations, double nelements, Size additionalsize, MemoryContext metacxt, MemoryContext tuplescxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:184
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:495
const TupleTableSlotOps * ExecGetCommonChildSlotOps(PlanState *ps)
Definition: execUtils.c:563
#define outerPlanState(node)
Definition: execnodes.h:1261
Assert(PointerIsAligned(start, uint64))
MemoryContext es_query_cxt
Definition: execnodes.h:710
Plan * plan
Definition: execnodes.h:1165
EState * state
Definition: execnodes.h:1167
MemoryContext tempContext
Definition: execnodes.h:1573
MemoryContext tuplesContext
Definition: execnodes.h:1575
FmgrInfo * hashfunctions
Definition: execnodes.h:1572
TupleHashTable hashtable
Definition: execnodes.h:1574
Cardinality numGroups
Definition: plannodes.h:478

References Assert(), BuildTupleHashTable(), RecursiveUnionState::eqfuncoids, EState::es_query_cxt, ExecGetCommonChildSlotOps(), ExecGetResultType(), RecursiveUnionState::hashfunctions, RecursiveUnionState::hashtable, RecursiveUnion::numCols, RecursiveUnion::numGroups, outerPlanState, PlanState::plan, RecursiveUnionState::ps, PlanState::state, RecursiveUnionState::tempContext, and RecursiveUnionState::tuplesContext.

Referenced by ExecInitRecursiveUnion().

◆ ExecEndRecursiveUnion()

void ExecEndRecursiveUnion ( RecursiveUnionState node)

Definition at line 285 of file nodeRecursiveunion.c.

286{
287 /* Release tuplestores */
290
291 /* free subsidiary stuff including hashtable data */
292 if (node->tempContext)
294 if (node->tuplesContext)
296
297 /*
298 * close down subplans
299 */
302}
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:562
#define innerPlanState(node)
Definition: execnodes.h:1260
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
Tuplestorestate * working_table
Definition: execnodes.h:1568
Tuplestorestate * intermediate_table
Definition: execnodes.h:1569
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:492

References ExecEndNode(), innerPlanState, RecursiveUnionState::intermediate_table, MemoryContextDelete(), outerPlanState, RecursiveUnionState::tempContext, RecursiveUnionState::tuplesContext, tuplestore_end(), and RecursiveUnionState::working_table.

Referenced by ExecEndNode().

◆ ExecInitRecursiveUnion()

RecursiveUnionState * ExecInitRecursiveUnion ( RecursiveUnion node,
EState estate,
int  eflags 
)

Definition at line 179 of file nodeRecursiveunion.c.

180{
181 RecursiveUnionState *rustate;
182 ParamExecData *prmdata;
183
184 /* check for unsupported flags */
186
187 /*
188 * create state structure
189 */
190 rustate = makeNode(RecursiveUnionState);
191 rustate->ps.plan = (Plan *) node;
192 rustate->ps.state = estate;
194
195 rustate->eqfuncoids = NULL;
196 rustate->hashfunctions = NULL;
197 rustate->hashtable = NULL;
198 rustate->tempContext = NULL;
199 rustate->tuplesContext = NULL;
200
201 /* initialize processing state */
202 rustate->recursing = false;
203 rustate->intermediate_empty = true;
204 rustate->working_table = tuplestore_begin_heap(false, false, work_mem);
205 rustate->intermediate_table = tuplestore_begin_heap(false, false, work_mem);
206
207 /*
208 * If hashing, we need a per-tuple memory context for comparisons, and a
209 * longer-lived context to store the hash table. The table can't just be
210 * kept in the per-query context because we want to be able to throw it
211 * away when rescanning. We can use a BumpContext to save storage,
212 * because we will have no need to delete individual table entries.
213 */
214 if (node->numCols > 0)
215 {
216 rustate->tempContext =
218 "RecursiveUnion",
220 rustate->tuplesContext =
222 "RecursiveUnion hashed tuples",
224 }
225
226 /*
227 * Make the state structure available to descendant WorkTableScan nodes
228 * via the Param slot reserved for it.
229 */
230 prmdata = &(estate->es_param_exec_vals[node->wtParam]);
231 Assert(prmdata->execPlan == NULL);
232 prmdata->value = PointerGetDatum(rustate);
233 prmdata->isnull = false;
234
235 /*
236 * Miscellaneous initialization
237 *
238 * RecursiveUnion plans don't have expression contexts because they never
239 * call ExecQual or ExecProject.
240 */
241 Assert(node->plan.qual == NIL);
242
243 /*
244 * RecursiveUnion nodes still have Result slots, which hold pointers to
245 * tuples, so we have to initialize them.
246 */
247 ExecInitResultTypeTL(&rustate->ps);
248
249 /*
250 * Initialize result tuple type. (Note: we have to set up the result type
251 * before initializing child nodes, because nodeWorktablescan.c expects it
252 * to be valid.)
253 */
254 rustate->ps.ps_ProjInfo = NULL;
255
256 /*
257 * initialize child nodes
258 */
259 outerPlanState(rustate) = ExecInitNode(outerPlan(node), estate, eflags);
260 innerPlanState(rustate) = ExecInitNode(innerPlan(node), estate, eflags);
261
262 /*
263 * If hashing, precompute fmgr lookup data for inner loop, and create the
264 * hash table.
265 */
266 if (node->numCols > 0)
267 {
269 node->dupOperators,
270 &rustate->eqfuncoids,
271 &rustate->hashfunctions);
272 build_hash_table(rustate);
273 }
274
275 return rustate;
276}
MemoryContext BumpContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: bump.c:133
void execTuplesHashPrepare(int numCols, const Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:100
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1944
#define EXEC_FLAG_BACKWARD
Definition: executor.h:69
#define EXEC_FLAG_MARK
Definition: executor.h:70
int work_mem
Definition: globals.c:131
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static void build_hash_table(RecursiveUnionState *rustate)
static TupleTableSlot * ExecRecursiveUnion(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:161
#define NIL
Definition: pg_list.h:68
#define innerPlan(node)
Definition: plannodes.h:260
#define outerPlan(node)
Definition: plannodes.h:261
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
ParamExecData * es_param_exec_vals
Definition: execnodes.h:705
bool isnull
Definition: params.h:149
Datum value
Definition: params.h:148
void * execPlan
Definition: params.h:147
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1205
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1171
List * qual
Definition: plannodes.h:231
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:330

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), build_hash_table(), BumpContextCreate(), CurrentMemoryContext, RecursiveUnionState::eqfuncoids, EState::es_param_exec_vals, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecInitNode(), ExecInitResultTypeTL(), ParamExecData::execPlan, PlanState::ExecProcNode, ExecRecursiveUnion(), execTuplesHashPrepare(), RecursiveUnionState::hashfunctions, RecursiveUnionState::hashtable, innerPlan, innerPlanState, RecursiveUnionState::intermediate_empty, RecursiveUnionState::intermediate_table, ParamExecData::isnull, makeNode, NIL, RecursiveUnion::numCols, outerPlan, outerPlanState, PlanState::plan, RecursiveUnion::plan, PointerGetDatum(), RecursiveUnionState::ps, PlanState::ps_ProjInfo, Plan::qual, RecursiveUnionState::recursing, PlanState::state, RecursiveUnionState::tempContext, RecursiveUnionState::tuplesContext, tuplestore_begin_heap(), ParamExecData::value, work_mem, RecursiveUnionState::working_table, and RecursiveUnion::wtParam.

Referenced by ExecInitNode().

◆ ExecRecursiveUnion()

static TupleTableSlot * ExecRecursiveUnion ( PlanState pstate)
static

Definition at line 80 of file nodeRecursiveunion.c.

81{
86 TupleTableSlot *slot;
87 bool isnew;
88
90
91 /* 1. Evaluate non-recursive term */
92 if (!node->recursing)
93 {
94 for (;;)
95 {
96 slot = ExecProcNode(outerPlan);
97 if (TupIsNull(slot))
98 break;
99 if (plan->numCols > 0)
100 {
101 /* Find or build hashtable entry for this tuple's group */
102 LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL);
103 /* Must reset temp context after each hashtable lookup */
105 /* Ignore tuple if already seen */
106 if (!isnew)
107 continue;
108 }
109 /* Each non-duplicate tuple goes to the working table ... */
111 /* ... and to the caller */
112 return slot;
113 }
114 node->recursing = true;
115 }
116
117 /* 2. Execute recursive term */
118 for (;;)
119 {
120 slot = ExecProcNode(innerPlan);
121 if (TupIsNull(slot))
122 {
123 Tuplestorestate *swaptemp;
124
125 /* Done if there's nothing in the intermediate table */
126 if (node->intermediate_empty)
127 break;
128
129 /*
130 * Now we let the intermediate table become the work table. We
131 * need a fresh intermediate table, so delete the tuples from the
132 * current working table and use that as the new intermediate
133 * table. This saves a round of free/malloc from creating a new
134 * tuple store.
135 */
137
138 swaptemp = node->working_table;
139 node->working_table = node->intermediate_table;
140 node->intermediate_table = swaptemp;
141
142 /* mark the intermediate table as empty */
143 node->intermediate_empty = true;
144
145 /* reset the recursive term */
146 innerPlan->chgParam = bms_add_member(innerPlan->chgParam,
147 plan->wtParam);
148
149 /* and continue fetching from recursive term */
150 continue;
151 }
152
153 if (plan->numCols > 0)
154 {
155 /* Find or build hashtable entry for this tuple's group */
156 LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL);
157 /* Must reset temp context after each hashtable lookup */
159 /* Ignore tuple if already seen */
160 if (!isnew)
161 continue;
162 }
163
164 /* Else, tuple is good; stash it in intermediate table ... */
165 node->intermediate_empty = false;
167 /* ... and return it */
168 return slot;
169 }
170
171 return NULL;
172}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew, uint32 *hash)
Definition: execGrouping.c:382
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:314
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:400
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
#define plan(x)
Definition: pg_regress.c:161
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:742
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:430
#define TupIsNull(slot)
Definition: tuptable.h:309

References bms_add_member(), castNode, CHECK_FOR_INTERRUPTS, ExecProcNode(), RecursiveUnionState::hashtable, innerPlan, innerPlanState, RecursiveUnionState::intermediate_empty, RecursiveUnionState::intermediate_table, LookupTupleHashEntry(), MemoryContextReset(), outerPlan, outerPlanState, PlanState::plan, plan, RecursiveUnionState::ps, RecursiveUnionState::recursing, RecursiveUnionState::tempContext, TupIsNull, tuplestore_clear(), tuplestore_puttupleslot(), and RecursiveUnionState::working_table.

Referenced by ExecInitRecursiveUnion().

◆ ExecReScanRecursiveUnion()

void ExecReScanRecursiveUnion ( RecursiveUnionState node)

Definition at line 311 of file nodeRecursiveunion.c.

312{
316
317 /*
318 * Set recursive term's chgParam to tell it that we'll modify the working
319 * table and therefore it has to rescan.
320 */
321 innerPlan->chgParam = bms_add_member(innerPlan->chgParam, plan->wtParam);
322
323 /*
324 * if chgParam of subnode is not null then plan will be re-scanned by
325 * first ExecProcNode. Because of above, we only have to do this to the
326 * non-recursive term.
327 */
328 if (outerPlan->chgParam == NULL)
330
331 /* Empty hashtable if needed */
332 if (plan->numCols > 0)
334
335 /* reset processing state */
336 node->recursing = false;
337 node->intermediate_empty = true;
340}
void ExecReScan(PlanState *node)
Definition: execAmi.c:77
void ResetTupleHashTable(TupleHashTable hashtable)
Definition: execGrouping.c:302

References bms_add_member(), ExecReScan(), RecursiveUnionState::hashtable, innerPlan, innerPlanState, RecursiveUnionState::intermediate_empty, RecursiveUnionState::intermediate_table, outerPlan, outerPlanState, PlanState::plan, plan, RecursiveUnionState::ps, RecursiveUnionState::recursing, ResetTupleHashTable(), tuplestore_clear(), and RecursiveUnionState::working_table.

Referenced by ExecReScan().