PostgreSQL Source Code  git master
nodeRecursiveunion.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeRecursiveunion.c
4  * routines to handle RecursiveUnion nodes.
5  *
6  * To implement UNION (without ALL), we need a hashtable that stores tuples
7  * already seen. The hash key is computed from the grouping columns.
8  *
9  *
10  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/executor/nodeRecursiveunion.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 #include "postgres.h"
20 
21 #include "executor/execdebug.h"
23 #include "miscadmin.h"
24 #include "utils/memutils.h"
25 
26 
27 
28 /*
29  * Initialize the hash table to empty.
30  */
31 static void
33 {
34  RecursiveUnion *node = (RecursiveUnion *) rustate->ps.plan;
36 
37  Assert(node->numCols > 0);
38  Assert(node->numGroups > 0);
39 
40  rustate->hashtable = BuildTupleHashTable(&rustate->ps,
41  desc,
42  node->numCols,
43  node->dupColIdx,
44  rustate->eqfuncoids,
45  rustate->hashfunctions,
46  node->numGroups,
47  0,
48  rustate->tableContext,
49  rustate->tempContext,
50  false);
51 }
52 
53 
54 /* ----------------------------------------------------------------
55  * ExecRecursiveUnion(node)
56  *
57  * Scans the recursive query sequentially and returns the next
58  * qualifying tuple.
59  *
60  * 1. evaluate non recursive term and assign the result to RT
61  *
62  * 2. execute recursive terms
63  *
64  * 2.1 WT := RT
65  * 2.2 while WT is not empty repeat 2.3 to 2.6. if WT is empty returns RT
66  * 2.3 replace the name of recursive term with WT
67  * 2.4 evaluate the recursive term and store into WT
68  * 2.5 append WT to RT
69  * 2.6 go back to 2.2
70  * ----------------------------------------------------------------
71  */
72 static TupleTableSlot *
74 {
78  RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
79  TupleTableSlot *slot;
80  bool isnew;
81 
83 
84  /* 1. Evaluate non-recursive term */
85  if (!node->recursing)
86  {
87  for (;;)
88  {
89  slot = ExecProcNode(outerPlan);
90  if (TupIsNull(slot))
91  break;
92  if (plan->numCols > 0)
93  {
94  /* Find or build hashtable entry for this tuple's group */
95  LookupTupleHashEntry(node->hashtable, slot, &isnew);
96  /* Must reset temp context after each hashtable lookup */
98  /* Ignore tuple if already seen */
99  if (!isnew)
100  continue;
101  }
102  /* Each non-duplicate tuple goes to the working table ... */
104  /* ... and to the caller */
105  return slot;
106  }
107  node->recursing = true;
108  }
109 
110  /* 2. Execute recursive term */
111  for (;;)
112  {
113  slot = ExecProcNode(innerPlan);
114  if (TupIsNull(slot))
115  {
116  /* Done if there's nothing in the intermediate table */
117  if (node->intermediate_empty)
118  break;
119 
120  /* done with old working table ... */
122 
123  /* intermediate table becomes working table */
124  node->working_table = node->intermediate_table;
125 
126  /* create new empty intermediate table */
127  node->intermediate_table = tuplestore_begin_heap(false, false,
128  work_mem);
129  node->intermediate_empty = true;
130 
131  /* reset the recursive term */
132  innerPlan->chgParam = bms_add_member(innerPlan->chgParam,
133  plan->wtParam);
134 
135  /* and continue fetching from recursive term */
136  continue;
137  }
138 
139  if (plan->numCols > 0)
140  {
141  /* Find or build hashtable entry for this tuple's group */
142  LookupTupleHashEntry(node->hashtable, slot, &isnew);
143  /* Must reset temp context after each hashtable lookup */
145  /* Ignore tuple if already seen */
146  if (!isnew)
147  continue;
148  }
149 
150  /* Else, tuple is good; stash it in intermediate table ... */
151  node->intermediate_empty = false;
153  /* ... and return it */
154  return slot;
155  }
156 
157  return NULL;
158 }
159 
160 /* ----------------------------------------------------------------
161  * ExecInitRecursiveUnionScan
162  * ----------------------------------------------------------------
163  */
165 ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
166 {
167  RecursiveUnionState *rustate;
168  ParamExecData *prmdata;
169 
170  /* check for unsupported flags */
171  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
172 
173  /*
174  * create state structure
175  */
176  rustate = makeNode(RecursiveUnionState);
177  rustate->ps.plan = (Plan *) node;
178  rustate->ps.state = estate;
180 
181  rustate->eqfuncoids = NULL;
182  rustate->hashfunctions = NULL;
183  rustate->hashtable = NULL;
184  rustate->tempContext = NULL;
185  rustate->tableContext = NULL;
186 
187  /* initialize processing state */
188  rustate->recursing = false;
189  rustate->intermediate_empty = true;
190  rustate->working_table = tuplestore_begin_heap(false, false, work_mem);
191  rustate->intermediate_table = tuplestore_begin_heap(false, false, work_mem);
192 
193  /*
194  * If hashing, we need a per-tuple memory context for comparisons, and a
195  * longer-lived context to store the hash table. The table can't just be
196  * kept in the per-query context because we want to be able to throw it
197  * away when rescanning.
198  */
199  if (node->numCols > 0)
200  {
201  rustate->tempContext =
203  "RecursiveUnion",
205  rustate->tableContext =
207  "RecursiveUnion hash table",
209  }
210 
211  /*
212  * Make the state structure available to descendant WorkTableScan nodes
213  * via the Param slot reserved for it.
214  */
215  prmdata = &(estate->es_param_exec_vals[node->wtParam]);
216  Assert(prmdata->execPlan == NULL);
217  prmdata->value = PointerGetDatum(rustate);
218  prmdata->isnull = false;
219 
220  /*
221  * Miscellaneous initialization
222  *
223  * RecursiveUnion plans don't have expression contexts because they never
224  * call ExecQual or ExecProject.
225  */
226  Assert(node->plan.qual == NIL);
227 
228  /*
229  * RecursiveUnion nodes still have Result slots, which hold pointers to
230  * tuples, so we have to initialize them.
231  */
232  ExecInitResultTupleSlotTL(estate, &rustate->ps);
233 
234  /*
235  * Initialize result tuple type. (Note: we have to set up the result type
236  * before initializing child nodes, because nodeWorktablescan.c expects it
237  * to be valid.)
238  */
239  rustate->ps.ps_ProjInfo = NULL;
240 
241  /*
242  * initialize child nodes
243  */
244  outerPlanState(rustate) = ExecInitNode(outerPlan(node), estate, eflags);
245  innerPlanState(rustate) = ExecInitNode(innerPlan(node), estate, eflags);
246 
247  /*
248  * If hashing, precompute fmgr lookup data for inner loop, and create the
249  * hash table.
250  */
251  if (node->numCols > 0)
252  {
254  node->dupOperators,
255  &rustate->eqfuncoids,
256  &rustate->hashfunctions);
257  build_hash_table(rustate);
258  }
259 
260  return rustate;
261 }
262 
263 /* ----------------------------------------------------------------
264  * ExecEndRecursiveUnionScan
265  *
266  * frees any storage allocated through C routines.
267  * ----------------------------------------------------------------
268  */
269 void
271 {
272  /* Release tuplestores */
275 
276  /* free subsidiary stuff including hashtable */
277  if (node->tempContext)
279  if (node->tableContext)
281 
282  /*
283  * clean out the upper tuple table
284  */
286 
287  /*
288  * close down subplans
289  */
292 }
293 
294 /* ----------------------------------------------------------------
295  * ExecReScanRecursiveUnion
296  *
297  * Rescans the relation.
298  * ----------------------------------------------------------------
299  */
300 void
302 {
305  RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
306 
307  /*
308  * Set recursive term's chgParam to tell it that we'll modify the working
309  * table and therefore it has to rescan.
310  */
311  innerPlan->chgParam = bms_add_member(innerPlan->chgParam, plan->wtParam);
312 
313  /*
314  * if chgParam of subnode is not null then plan will be re-scanned by
315  * first ExecProcNode. Because of above, we only have to do this to the
316  * non-recursive term.
317  */
318  if (outerPlan->chgParam == NULL)
319  ExecReScan(outerPlan);
320 
321  /* Release any hashtable storage */
322  if (node->tableContext)
324 
325  /* And rebuild empty hashtable if needed */
326  if (plan->numCols > 0)
327  build_hash_table(node);
328 
329  /* reset processing state */
330  node->recursing = false;
331  node->intermediate_empty = true;
334 }
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:708
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:148
void execTuplesHashPrepare(int numCols, Oid *eqOperators, Oid **eqFuncOids, FmgrInfo **hashFunctions)
Definition: execGrouping.c:95
void * execPlan
Definition: params.h:147
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:948
Tuplestorestate * intermediate_table
Definition: execnodes.h:1140
#define castNode(_type_, nodeptr)
Definition: nodes.h:585
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:538
#define PointerGetDatum(X)
Definition: postgres.h:541
RecursiveUnionState * ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
FmgrInfo * hashfunctions
Definition: execnodes.h:1143
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
EState * state
Definition: execnodes.h:914
void ExecEndRecursiveUnion(RecursiveUnionState *node)
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:946
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
bool isnull
Definition: params.h:149
AttrNumber * dupColIdx
Definition: plannodes.h:301
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:418
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
MemoryContext tempContext
Definition: execnodes.h:1144
#define outerPlanState(node)
Definition: execnodes.h:966
#define innerPlan(node)
Definition: plannodes.h:176
#define TupIsNull(slot)
Definition: tuptable.h:146
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew)
Definition: execGrouping.c:233
TupleHashTable hashtable
Definition: execnodes.h:1145
static TupleTableSlot * ExecRecursiveUnion(PlanState *pstate)
Bitmapset * chgParam
Definition: execnodes.h:941
Tuplestorestate * working_table
Definition: execnodes.h:1139
#define outerPlan(node)
Definition: plannodes.h:177
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:918
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:232
int work_mem
Definition: globals.c:120
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:890
Plan * plan
Definition: execnodes.h:912
void ExecReScanRecursiveUnion(RecursiveUnionState *node)
#define makeNode(_type_)
Definition: nodes.h:564
MemoryContext tableContext
Definition: execnodes.h:1146
#define Assert(condition)
Definition: c.h:699
#define EXEC_FLAG_MARK
Definition: executor.h:61
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
Oid * dupOperators
Definition: plannodes.h:302
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:438
TupleHashTable BuildTupleHashTable(PlanState *parent, TupleDesc inputDesc, int numCols, AttrNumber *keyColIdx, Oid *eqfuncoids, FmgrInfo *hashfunctions, long nbuckets, Size additionalsize, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:152
static void build_hash_table(RecursiveUnionState *rustate)
Datum value
Definition: params.h:148
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define innerPlanState(node)
Definition: execnodes.h:965
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139