Age Owner Branch data TLA Line data Source code
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-2025, 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/executor.h"
22 : : #include "executor/nodeRecursiveunion.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
6178 tgl@sss.pgh.pa.us 32 :CBC 189 : build_hash_table(RecursiveUnionState *rustate)
33 : : {
5931 bruce@momjian.us 34 : 189 : RecursiveUnion *node = (RecursiveUnion *) rustate->ps.plan;
2760 andres@anarazel.de 35 : 189 : TupleDesc desc = ExecGetResultType(outerPlanState(rustate));
36 : :
6178 tgl@sss.pgh.pa.us 37 [ - + ]: 189 : Assert(node->numCols > 0);
38 [ - + ]: 189 : Assert(node->numGroups > 0);
39 : :
40 : : /*
41 : : * If both child plans deliver the same fixed tuple slot type, we can tell
42 : : * BuildTupleHashTable to expect that slot type as input. Otherwise,
43 : : * we'll pass NULL denoting that any slot type is possible.
44 : : */
261 45 : 189 : rustate->hashtable = BuildTupleHashTable(&rustate->ps,
46 : : desc,
47 : : ExecGetCommonChildSlotOps(&rustate->ps),
48 : : node->numCols,
49 : : node->dupColIdx,
50 : 189 : rustate->eqfuncoids,
51 : : rustate->hashfunctions,
52 : : node->dupCollations,
53 : : node->numGroups,
54 : : 0,
55 : 189 : rustate->ps.state->es_query_cxt,
56 : : rustate->tableContext,
57 : : rustate->tempContext,
58 : : false);
6178 59 : 189 : }
60 : :
61 : :
62 : : /* ----------------------------------------------------------------
63 : : * ExecRecursiveUnion(node)
64 : : *
65 : : * Scans the recursive query sequentially and returns the next
66 : : * qualifying tuple.
67 : : *
68 : : * 1. evaluate non recursive term and assign the result to RT
69 : : *
70 : : * 2. execute recursive terms
71 : : *
72 : : * 2.1 WT := RT
73 : : * 2.2 while WT is not empty repeat 2.3 to 2.6. if WT is empty returns RT
74 : : * 2.3 replace the name of recursive term with WT
75 : : * 2.4 evaluate the recursive term and store into WT
76 : : * 2.5 append WT to RT
77 : : * 2.6 go back to 2.2
78 : : * ----------------------------------------------------------------
79 : : */
80 : : static TupleTableSlot *
2973 andres@anarazel.de 81 : 35027 : ExecRecursiveUnion(PlanState *pstate)
82 : : {
83 : 35027 : RecursiveUnionState *node = castNode(RecursiveUnionState, pstate);
6181 tgl@sss.pgh.pa.us 84 : 35027 : PlanState *outerPlan = outerPlanState(node);
85 : 35027 : PlanState *innerPlan = innerPlanState(node);
86 : 35027 : RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
87 : : TupleTableSlot *slot;
88 : : bool isnew;
89 : :
2965 andres@anarazel.de 90 [ - + ]: 35027 : CHECK_FOR_INTERRUPTS();
91 : :
92 : : /* 1. Evaluate non-recursive term */
6181 tgl@sss.pgh.pa.us 93 [ + + ]: 35027 : if (!node->recursing)
94 : : {
95 : : for (;;)
96 : : {
6178 97 : 11072 : slot = ExecProcNode(outerPlan);
98 [ + + + + ]: 11072 : if (TupIsNull(slot))
99 : : break;
100 [ + + ]: 10633 : if (plan->numCols > 0)
101 : : {
102 : : /* Find or build hashtable entry for this tuple's group */
1868 jdavis@postgresql.or 103 : 279 : LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL);
104 : : /* Must reset temp context after each hashtable lookup */
6178 tgl@sss.pgh.pa.us 105 : 279 : MemoryContextReset(node->tempContext);
106 : : /* Ignore tuple if already seen */
107 [ + + ]: 279 : if (!isnew)
108 : 9 : continue;
109 : : }
110 : : /* Each non-duplicate tuple goes to the working table ... */
6181 111 : 10624 : tuplestore_puttupleslot(node->working_table, slot);
112 : : /* ... and to the caller */
113 : 10624 : return slot;
114 : : }
115 : 439 : node->recursing = true;
116 : : }
117 : :
118 : : /* 2. Execute recursive term */
119 : : for (;;)
120 : : {
6178 121 : 27728 : slot = ExecProcNode(innerPlan);
122 [ + + + + ]: 27728 : if (TupIsNull(slot))
123 : 3286 : {
124 : : Tuplestorestate *swaptemp;
125 : :
126 : : /* Done if there's nothing in the intermediate table */
127 [ + + ]: 3704 : if (node->intermediate_empty)
128 : 418 : break;
129 : :
130 : : /*
131 : : * Now we let the intermediate table become the work table. We
132 : : * need a fresh intermediate table, so delete the tuples from the
133 : : * current working table and use that as the new intermediate
134 : : * table. This saves a round of free/malloc from creating a new
135 : : * tuple store.
136 : : */
352 drowley@postgresql.o 137 : 3286 : tuplestore_clear(node->working_table);
138 : :
139 : 3286 : swaptemp = node->working_table;
6178 tgl@sss.pgh.pa.us 140 : 3286 : node->working_table = node->intermediate_table;
352 drowley@postgresql.o 141 : 3286 : node->intermediate_table = swaptemp;
142 : :
143 : : /* mark the intermediate table as empty */
6178 tgl@sss.pgh.pa.us 144 : 3286 : node->intermediate_empty = true;
145 : :
146 : : /* reset the recursive term */
147 : 3286 : innerPlan->chgParam = bms_add_member(innerPlan->chgParam,
148 : : plan->wtParam);
149 : :
150 : : /* and continue fetching from recursive term */
151 : 3286 : continue;
152 : : }
153 : :
154 [ + + ]: 24024 : if (plan->numCols > 0)
155 : : {
156 : : /* Find or build hashtable entry for this tuple's group */
1868 jdavis@postgresql.or 157 : 480 : LookupTupleHashEntry(node->hashtable, slot, &isnew, NULL);
158 : : /* Must reset temp context after each hashtable lookup */
6178 tgl@sss.pgh.pa.us 159 : 480 : MemoryContextReset(node->tempContext);
160 : : /* Ignore tuple if already seen */
161 [ + + ]: 480 : if (!isnew)
162 : 39 : continue;
163 : : }
164 : :
165 : : /* Else, tuple is good; stash it in intermediate table ... */
5931 bruce@momjian.us 166 : 23985 : node->intermediate_empty = false;
167 : 23985 : tuplestore_puttupleslot(node->intermediate_table, slot);
168 : : /* ... and return it */
6178 tgl@sss.pgh.pa.us 169 : 23985 : return slot;
170 : : }
171 : :
172 : 418 : return NULL;
173 : : }
174 : :
175 : : /* ----------------------------------------------------------------
176 : : * ExecInitRecursiveUnion
177 : : * ----------------------------------------------------------------
178 : : */
179 : : RecursiveUnionState *
6181 180 : 463 : ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
181 : : {
182 : : RecursiveUnionState *rustate;
183 : : ParamExecData *prmdata;
184 : :
185 : : /* check for unsupported flags */
186 [ - + ]: 463 : Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
187 : :
188 : : /*
189 : : * create state structure
190 : : */
191 : 463 : rustate = makeNode(RecursiveUnionState);
192 : 463 : rustate->ps.plan = (Plan *) node;
193 : 463 : rustate->ps.state = estate;
2973 andres@anarazel.de 194 : 463 : rustate->ps.ExecProcNode = ExecRecursiveUnion;
195 : :
2760 196 : 463 : rustate->eqfuncoids = NULL;
6178 tgl@sss.pgh.pa.us 197 : 463 : rustate->hashfunctions = NULL;
198 : 463 : rustate->hashtable = NULL;
199 : 463 : rustate->tempContext = NULL;
200 : 463 : rustate->tableContext = NULL;
201 : :
202 : : /* initialize processing state */
6181 203 : 463 : rustate->recursing = false;
204 : 463 : rustate->intermediate_empty = true;
205 : 463 : rustate->working_table = tuplestore_begin_heap(false, false, work_mem);
206 : 463 : rustate->intermediate_table = tuplestore_begin_heap(false, false, work_mem);
207 : :
208 : : /*
209 : : * If hashing, we need a per-tuple memory context for comparisons, and a
210 : : * longer-lived context to store the hash table. The table can't just be
211 : : * kept in the per-query context because we want to be able to throw it
212 : : * away when rescanning.
213 : : */
6178 214 [ + + ]: 463 : if (node->numCols > 0)
215 : : {
216 : 189 : rustate->tempContext =
217 : 189 : AllocSetContextCreate(CurrentMemoryContext,
218 : : "RecursiveUnion",
219 : : ALLOCSET_DEFAULT_SIZES);
220 : 189 : rustate->tableContext =
221 : 189 : AllocSetContextCreate(CurrentMemoryContext,
222 : : "RecursiveUnion hash table",
223 : : ALLOCSET_DEFAULT_SIZES);
224 : : }
225 : :
226 : : /*
227 : : * Make the state structure available to descendant WorkTableScan nodes
228 : : * via the Param slot reserved for it.
229 : : */
6181 230 : 463 : prmdata = &(estate->es_param_exec_vals[node->wtParam]);
231 [ - + ]: 463 : Assert(prmdata->execPlan == NULL);
232 : 463 : prmdata->value = PointerGetDatum(rustate);
233 : 463 : 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 [ - + ]: 463 : 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 : : */
2493 andres@anarazel.de 247 : 463 : 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 : : */
6181 tgl@sss.pgh.pa.us 254 : 463 : rustate->ps.ps_ProjInfo = NULL;
255 : :
256 : : /*
257 : : * initialize child nodes
258 : : */
259 : 463 : outerPlanState(rustate) = ExecInitNode(outerPlan(node), estate, eflags);
260 : 463 : 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 : : */
6178 266 [ + + ]: 463 : if (node->numCols > 0)
267 : : {
268 : 189 : execTuplesHashPrepare(node->numCols,
269 : 189 : node->dupOperators,
270 : : &rustate->eqfuncoids,
271 : : &rustate->hashfunctions);
272 : 189 : build_hash_table(rustate);
273 : : }
274 : :
6181 275 : 463 : return rustate;
276 : : }
277 : :
278 : : /* ----------------------------------------------------------------
279 : : * ExecEndRecursiveUnion
280 : : *
281 : : * frees any storage allocated through C routines.
282 : : * ----------------------------------------------------------------
283 : : */
284 : : void
285 : 463 : ExecEndRecursiveUnion(RecursiveUnionState *node)
286 : : {
287 : : /* Release tuplestores */
288 : 463 : tuplestore_end(node->working_table);
289 : 463 : tuplestore_end(node->intermediate_table);
290 : :
291 : : /* free subsidiary stuff including hashtable */
6178 292 [ + + ]: 463 : if (node->tempContext)
293 : 189 : MemoryContextDelete(node->tempContext);
294 [ + + ]: 463 : if (node->tableContext)
295 : 189 : MemoryContextDelete(node->tableContext);
296 : :
297 : : /*
298 : : * close down subplans
299 : : */
6181 300 : 463 : ExecEndNode(outerPlanState(node));
301 : 463 : ExecEndNode(innerPlanState(node));
302 : 463 : }
303 : :
304 : : /* ----------------------------------------------------------------
305 : : * ExecReScanRecursiveUnion
306 : : *
307 : : * Rescans the relation.
308 : : * ----------------------------------------------------------------
309 : : */
310 : : void
5535 311 : 6 : ExecReScanRecursiveUnion(RecursiveUnionState *node)
312 : : {
6181 313 : 6 : PlanState *outerPlan = outerPlanState(node);
314 : 6 : PlanState *innerPlan = innerPlanState(node);
315 : 6 : RecursiveUnion *plan = (RecursiveUnion *) node->ps.plan;
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 : 6 : 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 [ - + ]: 6 : if (outerPlan->chgParam == NULL)
5535 tgl@sss.pgh.pa.us 329 :UBC 0 : ExecReScan(outerPlan);
330 : :
331 : : /* Release any hashtable storage */
6178 tgl@sss.pgh.pa.us 332 [ + - ]:CBC 6 : if (node->tableContext)
661 nathan@postgresql.or 333 : 6 : MemoryContextReset(node->tableContext);
334 : :
335 : : /* Empty hashtable if needed */
6178 tgl@sss.pgh.pa.us 336 [ + - ]: 6 : if (plan->numCols > 0)
2401 andres@anarazel.de 337 : 6 : ResetTupleHashTable(node->hashtable);
338 : :
339 : : /* reset processing state */
6181 tgl@sss.pgh.pa.us 340 : 6 : node->recursing = false;
341 : 6 : node->intermediate_empty = true;
342 : 6 : tuplestore_clear(node->working_table);
343 : 6 : tuplestore_clear(node->intermediate_table);
344 : 6 : }
|