PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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;
35 
36  Assert(node->numCols > 0);
37  Assert(node->numGroups > 0);
38 
39  rustate->hashtable = BuildTupleHashTable(node->numCols,
40  node->dupColIdx,
41  rustate->eqfunctions,
42  rustate->hashfunctions,
43  node->numGroups,
44  0,
45  rustate->tableContext,
46  rustate->tempContext,
47  false);
48 }
49 
50 
51 /* ----------------------------------------------------------------
52  * ExecRecursiveUnion(node)
53  *
54  * Scans the recursive query sequentially and returns the next
55  * qualifying tuple.
56  *
57  * 1. evaluate non recursive term and assign the result to RT
58  *
59  * 2. execute recursive terms
60  *
61  * 2.1 WT := RT
62  * 2.2 while WT is not empty repeat 2.3 to 2.6. if WT is empty returns RT
63  * 2.3 replace the name of recursive term with WT
64  * 2.4 evaluate the recursive term and store into WT
65  * 2.5 append WT to RT
66  * 2.6 go back to 2.2
67  * ----------------------------------------------------------------
68  */
69 static TupleTableSlot *
71 {
75  RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
76  TupleTableSlot *slot;
77  bool isnew;
78 
80 
81  /* 1. Evaluate non-recursive term */
82  if (!node->recursing)
83  {
84  for (;;)
85  {
86  slot = ExecProcNode(outerPlan);
87  if (TupIsNull(slot))
88  break;
89  if (plan->numCols > 0)
90  {
91  /* Find or build hashtable entry for this tuple's group */
92  LookupTupleHashEntry(node->hashtable, slot, &isnew);
93  /* Must reset temp context after each hashtable lookup */
95  /* Ignore tuple if already seen */
96  if (!isnew)
97  continue;
98  }
99  /* Each non-duplicate tuple goes to the working table ... */
101  /* ... and to the caller */
102  return slot;
103  }
104  node->recursing = true;
105  }
106 
107  /* 2. Execute recursive term */
108  for (;;)
109  {
110  slot = ExecProcNode(innerPlan);
111  if (TupIsNull(slot))
112  {
113  /* Done if there's nothing in the intermediate table */
114  if (node->intermediate_empty)
115  break;
116 
117  /* done with old working table ... */
119 
120  /* intermediate table becomes working table */
121  node->working_table = node->intermediate_table;
122 
123  /* create new empty intermediate table */
124  node->intermediate_table = tuplestore_begin_heap(false, false,
125  work_mem);
126  node->intermediate_empty = true;
127 
128  /* reset the recursive term */
129  innerPlan->chgParam = bms_add_member(innerPlan->chgParam,
130  plan->wtParam);
131 
132  /* and continue fetching from recursive term */
133  continue;
134  }
135 
136  if (plan->numCols > 0)
137  {
138  /* Find or build hashtable entry for this tuple's group */
139  LookupTupleHashEntry(node->hashtable, slot, &isnew);
140  /* Must reset temp context after each hashtable lookup */
142  /* Ignore tuple if already seen */
143  if (!isnew)
144  continue;
145  }
146 
147  /* Else, tuple is good; stash it in intermediate table ... */
148  node->intermediate_empty = false;
150  /* ... and return it */
151  return slot;
152  }
153 
154  return NULL;
155 }
156 
157 /* ----------------------------------------------------------------
158  * ExecInitRecursiveUnionScan
159  * ----------------------------------------------------------------
160  */
162 ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
163 {
164  RecursiveUnionState *rustate;
165  ParamExecData *prmdata;
166 
167  /* check for unsupported flags */
168  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
169 
170  /*
171  * create state structure
172  */
173  rustate = makeNode(RecursiveUnionState);
174  rustate->ps.plan = (Plan *) node;
175  rustate->ps.state = estate;
177 
178  rustate->eqfunctions = NULL;
179  rustate->hashfunctions = NULL;
180  rustate->hashtable = NULL;
181  rustate->tempContext = NULL;
182  rustate->tableContext = NULL;
183 
184  /* initialize processing state */
185  rustate->recursing = false;
186  rustate->intermediate_empty = true;
187  rustate->working_table = tuplestore_begin_heap(false, false, work_mem);
188  rustate->intermediate_table = tuplestore_begin_heap(false, false, work_mem);
189 
190  /*
191  * If hashing, we need a per-tuple memory context for comparisons, and a
192  * longer-lived context to store the hash table. The table can't just be
193  * kept in the per-query context because we want to be able to throw it
194  * away when rescanning.
195  */
196  if (node->numCols > 0)
197  {
198  rustate->tempContext =
200  "RecursiveUnion",
202  rustate->tableContext =
204  "RecursiveUnion hash table",
206  }
207 
208  /*
209  * Make the state structure available to descendant WorkTableScan nodes
210  * via the Param slot reserved for it.
211  */
212  prmdata = &(estate->es_param_exec_vals[node->wtParam]);
213  Assert(prmdata->execPlan == NULL);
214  prmdata->value = PointerGetDatum(rustate);
215  prmdata->isnull = false;
216 
217  /*
218  * Miscellaneous initialization
219  *
220  * RecursiveUnion plans don't have expression contexts because they never
221  * call ExecQual or ExecProject.
222  */
223  Assert(node->plan.qual == NIL);
224 
225  /*
226  * RecursiveUnion nodes still have Result slots, which hold pointers to
227  * tuples, so we have to initialize them.
228  */
229  ExecInitResultTupleSlot(estate, &rustate->ps);
230 
231  /*
232  * Initialize result tuple type and projection info. (Note: we have to
233  * set up the result type before initializing child nodes, because
234  * nodeWorktablescan.c expects it to be valid.)
235  */
236  ExecAssignResultTypeFromTL(&rustate->ps);
237  rustate->ps.ps_ProjInfo = NULL;
238 
239  /*
240  * initialize child nodes
241  */
242  outerPlanState(rustate) = ExecInitNode(outerPlan(node), estate, eflags);
243  innerPlanState(rustate) = ExecInitNode(innerPlan(node), estate, eflags);
244 
245  /*
246  * If hashing, precompute fmgr lookup data for inner loop, and create the
247  * hash table.
248  */
249  if (node->numCols > 0)
250  {
252  node->dupOperators,
253  &rustate->eqfunctions,
254  &rustate->hashfunctions);
255  build_hash_table(rustate);
256  }
257 
258  return rustate;
259 }
260 
261 /* ----------------------------------------------------------------
262  * ExecEndRecursiveUnionScan
263  *
264  * frees any storage allocated through C routines.
265  * ----------------------------------------------------------------
266  */
267 void
269 {
270  /* Release tuplestores */
273 
274  /* free subsidiary stuff including hashtable */
275  if (node->tempContext)
277  if (node->tableContext)
279 
280  /*
281  * clean out the upper tuple table
282  */
284 
285  /*
286  * close down subplans
287  */
290 }
291 
292 /* ----------------------------------------------------------------
293  * ExecReScanRecursiveUnion
294  *
295  * Rescans the relation.
296  * ----------------------------------------------------------------
297  */
298 void
300 {
303  RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
304 
305  /*
306  * Set recursive term's chgParam to tell it that we'll modify the working
307  * table and therefore it has to rescan.
308  */
309  innerPlan->chgParam = bms_add_member(innerPlan->chgParam, plan->wtParam);
310 
311  /*
312  * if chgParam of subnode is not null then plan will be re-scanned by
313  * first ExecProcNode. Because of above, we only have to do this to the
314  * non-recursive term.
315  */
316  if (outerPlan->chgParam == NULL)
317  ExecReScan(outerPlan);
318 
319  /* Release any hashtable storage */
320  if (node->tableContext)
322 
323  /* And rebuild empty hashtable if needed */
324  if (plan->numCols > 0)
325  build_hash_table(node);
326 
327  /* reset processing state */
328  node->recursing = false;
329  node->intermediate_empty = true;
332 }
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:708
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
void * execPlan
Definition: params.h:99
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:882
Tuplestorestate * intermediate_table
Definition: execnodes.h:1048
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
#define PointerGetDatum(X)
Definition: postgres.h:562
RecursiveUnionState * ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
FmgrInfo * hashfunctions
Definition: execnodes.h:1051
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
EState * state
Definition: execnodes.h:849
void ExecEndRecursiveUnion(RecursiveUnionState *node)
void execTuplesHashPrepare(int numCols, Oid *eqOperators, FmgrInfo **eqFunctions, FmgrInfo **hashFunctions)
Definition: execGrouping.c:233
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
ParamExecData * es_param_exec_vals
Definition: execnodes.h:466
bool isnull
Definition: params.h:101
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
AttrNumber * dupColIdx
Definition: plannodes.h:287
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:418
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
MemoryContext tempContext
Definition: execnodes.h:1052
#define outerPlanState(node)
Definition: execnodes.h:893
#define innerPlan(node)
Definition: plannodes.h:173
#define TupIsNull(slot)
Definition: tuptable.h:138
FmgrInfo * eqfunctions
Definition: execnodes.h:1050
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TupleHashEntry LookupTupleHashEntry(TupleHashTable hashtable, TupleTableSlot *slot, bool *isnew)
Definition: execGrouping.c:351
TupleHashTable hashtable
Definition: execnodes.h:1053
static TupleTableSlot * ExecRecursiveUnion(PlanState *pstate)
Bitmapset * chgParam
Definition: execnodes.h:875
Tuplestorestate * working_table
Definition: execnodes.h:1047
#define outerPlan(node)
Definition: plannodes.h:174
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:246
int work_mem
Definition: globals.c:113
Plan * plan
Definition: execnodes.h:847
void ExecReScanRecursiveUnion(RecursiveUnionState *node)
#define makeNode(_type_)
Definition: nodes.h:557
MemoryContext tableContext
Definition: execnodes.h:1054
#define Assert(condition)
Definition: c.h:664
#define EXEC_FLAG_MARK
Definition: executor.h:61
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
Oid * dupOperators
Definition: plannodes.h:288
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx, FmgrInfo *eqfunctions, FmgrInfo *hashfunctions, long nbuckets, Size additionalsize, MemoryContext tablecxt, MemoryContext tempcxt, bool use_variable_hash_iv)
Definition: execGrouping.c:290
static void build_hash_table(RecursiveUnionState *rustate)
Datum value
Definition: params.h:100
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define innerPlanState(node)
Definition: execnodes.h:892
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139