/Users/deen/code/yugabyte-db/src/postgres/src/backend/executor/nodeFunctionscan.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * nodeFunctionscan.c |
4 | | * Support routines for scanning RangeFunctions (functions in rangetable). |
5 | | * |
6 | | * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group |
7 | | * Portions Copyright (c) 1994, Regents of the University of California |
8 | | * |
9 | | * |
10 | | * IDENTIFICATION |
11 | | * src/backend/executor/nodeFunctionscan.c |
12 | | * |
13 | | *------------------------------------------------------------------------- |
14 | | */ |
15 | | /* |
16 | | * INTERFACE ROUTINES |
17 | | * ExecFunctionScan scans a function. |
18 | | * ExecFunctionNext retrieve next tuple in sequential order. |
19 | | * ExecInitFunctionScan creates and initializes a functionscan node. |
20 | | * ExecEndFunctionScan releases any storage allocated. |
21 | | * ExecReScanFunctionScan rescans the function |
22 | | */ |
23 | | #include "postgres.h" |
24 | | |
25 | | #include "catalog/pg_type.h" |
26 | | #include "executor/nodeFunctionscan.h" |
27 | | #include "funcapi.h" |
28 | | #include "nodes/nodeFuncs.h" |
29 | | #include "utils/builtins.h" |
30 | | #include "utils/memutils.h" |
31 | | |
32 | | |
33 | | /* |
34 | | * Runtime data for each function being scanned. |
35 | | */ |
36 | | typedef struct FunctionScanPerFuncState |
37 | | { |
38 | | SetExprState *setexpr; /* state of the expression being evaluated */ |
39 | | TupleDesc tupdesc; /* desc of the function result type */ |
40 | | int colcount; /* expected number of result columns */ |
41 | | Tuplestorestate *tstore; /* holds the function result set */ |
42 | | int64 rowcount; /* # of rows in result set, -1 if not known */ |
43 | | TupleTableSlot *func_slot; /* function result slot (or NULL) */ |
44 | | } FunctionScanPerFuncState; |
45 | | |
46 | | static TupleTableSlot *FunctionNext(FunctionScanState *node); |
47 | | |
48 | | |
49 | | /* ---------------------------------------------------------------- |
50 | | * Scan Support |
51 | | * ---------------------------------------------------------------- |
52 | | */ |
53 | | /* ---------------------------------------------------------------- |
54 | | * FunctionNext |
55 | | * |
56 | | * This is a workhorse for ExecFunctionScan |
57 | | * ---------------------------------------------------------------- |
58 | | */ |
59 | | static TupleTableSlot * |
60 | | FunctionNext(FunctionScanState *node) |
61 | 1.89M | { |
62 | 1.89M | EState *estate; |
63 | 1.89M | ScanDirection direction; |
64 | 1.89M | TupleTableSlot *scanslot; |
65 | 1.89M | bool alldone; |
66 | 1.89M | int64 oldpos; |
67 | 1.89M | int funcno; |
68 | 1.89M | int att; |
69 | | |
70 | | /* |
71 | | * get information from the estate and scan state |
72 | | */ |
73 | 1.89M | estate = node->ss.ps.state; |
74 | 1.89M | direction = estate->es_direction; |
75 | 1.89M | scanslot = node->ss.ss_ScanTupleSlot; |
76 | | |
77 | 1.89M | if (node->simple) |
78 | 1.85M | { |
79 | | /* |
80 | | * Fast path for the trivial case: the function return type and scan |
81 | | * result type are the same, so we fetch the function result straight |
82 | | * into the scan result slot. No need to update ordinality or |
83 | | * rowcounts either. |
84 | | */ |
85 | 1.85M | Tuplestorestate *tstore = node->funcstates[0].tstore; |
86 | | |
87 | | /* |
88 | | * If first time through, read all tuples from function and put them |
89 | | * in a tuplestore. Subsequent calls just fetch tuples from |
90 | | * tuplestore. |
91 | | */ |
92 | 1.85M | if (tstore == NULL) |
93 | 13.4k | { |
94 | 13.4k | node->funcstates[0].tstore = tstore = |
95 | 13.4k | ExecMakeTableFunctionResult(node->funcstates[0].setexpr, |
96 | 13.4k | node->ss.ps.ps_ExprContext, |
97 | 13.4k | node->argcontext, |
98 | 13.4k | node->funcstates[0].tupdesc, |
99 | 13.4k | node->eflags & EXEC_FLAG_BACKWARD); |
100 | | |
101 | | /* |
102 | | * paranoia - cope if the function, which may have constructed the |
103 | | * tuplestore itself, didn't leave it pointing at the start. This |
104 | | * call is fast, so the overhead shouldn't be an issue. |
105 | | */ |
106 | 13.4k | tuplestore_rescan(tstore); |
107 | 13.4k | } |
108 | | |
109 | | /* |
110 | | * Get the next tuple from tuplestore. |
111 | | */ |
112 | 1.85M | (void) tuplestore_gettupleslot(tstore, |
113 | 1.85M | ScanDirectionIsForward(direction), |
114 | 1.85M | false, |
115 | 1.85M | scanslot); |
116 | 1.85M | return scanslot; |
117 | 1.85M | } |
118 | | |
119 | | /* |
120 | | * Increment or decrement ordinal counter before checking for end-of-data, |
121 | | * so that we can move off either end of the result by 1 (and no more than |
122 | | * 1) without losing correct count. See PortalRunSelect for why we can |
123 | | * assume that we won't be called repeatedly in the end-of-data state. |
124 | | */ |
125 | 40.4k | oldpos = node->ordinal; |
126 | 40.4k | if (ScanDirectionIsForward(direction)) |
127 | 40.4k | node->ordinal++; |
128 | 18.4E | else |
129 | 18.4E | node->ordinal--; |
130 | | |
131 | | /* |
132 | | * Main loop over functions. |
133 | | * |
134 | | * We fetch the function results into func_slots (which match the function |
135 | | * return types), and then copy the values to scanslot (which matches the |
136 | | * scan result type), setting the ordinal column (if any) as well. |
137 | | */ |
138 | 40.4k | ExecClearTuple(scanslot); |
139 | 40.4k | att = 0; |
140 | 40.4k | alldone = true; |
141 | 80.9k | for (funcno = 0; funcno < node->nfuncs; funcno++40.4k ) |
142 | 40.4k | { |
143 | 40.4k | FunctionScanPerFuncState *fs = &node->funcstates[funcno]; |
144 | 40.4k | int i; |
145 | | |
146 | | /* |
147 | | * If first time through, read all tuples from function and put them |
148 | | * in a tuplestore. Subsequent calls just fetch tuples from |
149 | | * tuplestore. |
150 | | */ |
151 | 40.4k | if (fs->tstore == NULL) |
152 | 29.0k | { |
153 | 29.0k | fs->tstore = |
154 | 29.0k | ExecMakeTableFunctionResult(fs->setexpr, |
155 | 29.0k | node->ss.ps.ps_ExprContext, |
156 | 29.0k | node->argcontext, |
157 | 29.0k | fs->tupdesc, |
158 | 29.0k | node->eflags & EXEC_FLAG_BACKWARD); |
159 | | |
160 | | /* |
161 | | * paranoia - cope if the function, which may have constructed the |
162 | | * tuplestore itself, didn't leave it pointing at the start. This |
163 | | * call is fast, so the overhead shouldn't be an issue. |
164 | | */ |
165 | 29.0k | tuplestore_rescan(fs->tstore); |
166 | 29.0k | } |
167 | | |
168 | | /* |
169 | | * Get the next tuple from tuplestore. |
170 | | * |
171 | | * If we have a rowcount for the function, and we know the previous |
172 | | * read position was out of bounds, don't try the read. This allows |
173 | | * backward scan to work when there are mixed row counts present. |
174 | | */ |
175 | 40.4k | if (fs->rowcount != -1 && fs->rowcount < oldpos0 ) |
176 | 0 | ExecClearTuple(fs->func_slot); |
177 | 40.4k | else |
178 | 40.4k | (void) tuplestore_gettupleslot(fs->tstore, |
179 | 40.4k | ScanDirectionIsForward(direction), |
180 | 40.4k | false, |
181 | 40.4k | fs->func_slot); |
182 | | |
183 | 40.4k | if (TupIsNull(fs->func_slot)) |
184 | 29.0k | { |
185 | | /* |
186 | | * If we ran out of data for this function in the forward |
187 | | * direction then we now know how many rows it returned. We need |
188 | | * to know this in order to handle backwards scans. The row count |
189 | | * we store is actually 1+ the actual number, because we have to |
190 | | * position the tuplestore 1 off its end sometimes. |
191 | | */ |
192 | 29.0k | if (ScanDirectionIsForward(direction) && fs->rowcount == -1) |
193 | 29.0k | fs->rowcount = node->ordinal; |
194 | | |
195 | | /* |
196 | | * populate the result cols with nulls |
197 | | */ |
198 | 58.0k | for (i = 0; i < fs->colcount; i++29.0k ) |
199 | 29.0k | { |
200 | 29.0k | scanslot->tts_values[att] = (Datum) 0; |
201 | 29.0k | scanslot->tts_isnull[att] = true; |
202 | 29.0k | att++; |
203 | 29.0k | } |
204 | 29.0k | } |
205 | 11.4k | else |
206 | 11.4k | { |
207 | | /* |
208 | | * we have a result, so just copy it to the result cols. |
209 | | */ |
210 | 11.4k | slot_getallattrs(fs->func_slot); |
211 | | |
212 | 22.9k | for (i = 0; i < fs->colcount; i++11.4k ) |
213 | 11.4k | { |
214 | 11.4k | scanslot->tts_values[att] = fs->func_slot->tts_values[i]; |
215 | 11.4k | scanslot->tts_isnull[att] = fs->func_slot->tts_isnull[i]; |
216 | 11.4k | att++; |
217 | 11.4k | } |
218 | | |
219 | | /* |
220 | | * We're not done until every function result is exhausted; we pad |
221 | | * the shorter results with nulls until then. |
222 | | */ |
223 | 11.4k | alldone = false; |
224 | 11.4k | } |
225 | 40.4k | } |
226 | | |
227 | | /* |
228 | | * ordinal col is always last, per spec. |
229 | | */ |
230 | 40.4k | if (node->ordinality) |
231 | 40.4k | { |
232 | 40.4k | scanslot->tts_values[att] = Int64GetDatumFast(node->ordinal); |
233 | 40.4k | scanslot->tts_isnull[att] = false; |
234 | 40.4k | } |
235 | | |
236 | | /* |
237 | | * If alldone, we just return the previously-cleared scanslot. Otherwise, |
238 | | * finish creating the virtual tuple. |
239 | | */ |
240 | 40.4k | if (!alldone) |
241 | 11.4k | ExecStoreVirtualTuple(scanslot); |
242 | | |
243 | 40.4k | return scanslot; |
244 | 1.89M | } |
245 | | |
246 | | /* |
247 | | * FunctionRecheck -- access method routine to recheck a tuple in EvalPlanQual |
248 | | */ |
249 | | static bool |
250 | | FunctionRecheck(FunctionScanState *node, TupleTableSlot *slot) |
251 | 0 | { |
252 | | /* nothing to check */ |
253 | 0 | return true; |
254 | 0 | } |
255 | | |
256 | | /* ---------------------------------------------------------------- |
257 | | * ExecFunctionScan(node) |
258 | | * |
259 | | * Scans the function sequentially and returns the next qualifying |
260 | | * tuple. |
261 | | * We call the ExecScan() routine and pass it the appropriate |
262 | | * access method functions. |
263 | | * ---------------------------------------------------------------- |
264 | | */ |
265 | | static TupleTableSlot * |
266 | | ExecFunctionScan(PlanState *pstate) |
267 | 1.87M | { |
268 | 1.87M | FunctionScanState *node = castNode(FunctionScanState, pstate); |
269 | | |
270 | 1.87M | return ExecScan(&node->ss, |
271 | 1.87M | (ExecScanAccessMtd) FunctionNext, |
272 | 1.87M | (ExecScanRecheckMtd) FunctionRecheck); |
273 | 1.87M | } |
274 | | |
275 | | /* ---------------------------------------------------------------- |
276 | | * ExecInitFunctionScan |
277 | | * ---------------------------------------------------------------- |
278 | | */ |
279 | | FunctionScanState * |
280 | | ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags) |
281 | 4.35k | { |
282 | 4.35k | FunctionScanState *scanstate; |
283 | 4.35k | int nfuncs = list_length(node->functions); |
284 | 4.35k | TupleDesc scan_tupdesc; |
285 | 4.35k | int i, |
286 | 4.35k | natts; |
287 | 4.35k | ListCell *lc; |
288 | | |
289 | | /* check for unsupported flags */ |
290 | 4.35k | Assert(!(eflags & EXEC_FLAG_MARK)); |
291 | | |
292 | | /* |
293 | | * FunctionScan should not have any children. |
294 | | */ |
295 | 4.35k | Assert(outerPlan(node) == NULL); |
296 | 4.35k | Assert(innerPlan(node) == NULL); |
297 | | |
298 | | /* |
299 | | * create new ScanState for node |
300 | | */ |
301 | 4.35k | scanstate = makeNode(FunctionScanState); |
302 | 0 | scanstate->ss.ps.plan = (Plan *) node; |
303 | 4.35k | scanstate->ss.ps.state = estate; |
304 | 4.35k | scanstate->ss.ps.ExecProcNode = ExecFunctionScan; |
305 | 4.35k | scanstate->eflags = eflags; |
306 | | |
307 | | /* |
308 | | * are we adding an ordinality column? |
309 | | */ |
310 | 4.35k | scanstate->ordinality = node->funcordinality; |
311 | | |
312 | 4.35k | scanstate->nfuncs = nfuncs; |
313 | 4.35k | if (nfuncs == 1 && !node->funcordinality) |
314 | 4.22k | scanstate->simple = true; |
315 | 129 | else |
316 | 129 | scanstate->simple = false; |
317 | | |
318 | | /* |
319 | | * Ordinal 0 represents the "before the first row" position. |
320 | | * |
321 | | * We need to track ordinal position even when not adding an ordinality |
322 | | * column to the result, in order to handle backwards scanning properly |
323 | | * with multiple functions with different result sizes. (We can't position |
324 | | * any individual function's tuplestore any more than 1 place beyond its |
325 | | * end, so when scanning backwards, we need to know when to start |
326 | | * including the function in the scan again.) |
327 | | */ |
328 | 4.35k | scanstate->ordinal = 0; |
329 | | |
330 | | /* |
331 | | * Miscellaneous initialization |
332 | | * |
333 | | * create expression context for node |
334 | | */ |
335 | 4.35k | ExecAssignExprContext(estate, &scanstate->ss.ps); |
336 | | |
337 | 4.35k | scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState)); |
338 | | |
339 | 4.35k | natts = 0; |
340 | 4.35k | i = 0; |
341 | 4.35k | foreach(lc, node->functions) |
342 | 4.35k | { |
343 | 4.35k | RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc); |
344 | 4.35k | Node *funcexpr = rtfunc->funcexpr; |
345 | 4.35k | int colcount = rtfunc->funccolcount; |
346 | 4.35k | FunctionScanPerFuncState *fs = &scanstate->funcstates[i]; |
347 | 4.35k | TypeFuncClass functypclass; |
348 | 4.35k | Oid funcrettype; |
349 | 4.35k | TupleDesc tupdesc; |
350 | | |
351 | 4.35k | fs->setexpr = |
352 | 4.35k | ExecInitTableFunctionResult((Expr *) funcexpr, |
353 | 4.35k | scanstate->ss.ps.ps_ExprContext, |
354 | 4.35k | &scanstate->ss.ps); |
355 | | |
356 | | /* |
357 | | * Don't allocate the tuplestores; the actual calls to the functions |
358 | | * do that. NULL means that we have not called the function yet (or |
359 | | * need to call it again after a rescan). |
360 | | */ |
361 | 4.35k | fs->tstore = NULL; |
362 | 4.35k | fs->rowcount = -1; |
363 | | |
364 | | /* |
365 | | * Now determine if the function returns a simple or composite type, |
366 | | * and build an appropriate tupdesc. Note that in the composite case, |
367 | | * the function may now return more columns than it did when the plan |
368 | | * was made; we have to ignore any columns beyond "colcount". |
369 | | */ |
370 | 4.35k | functypclass = get_expr_result_type(funcexpr, |
371 | 4.35k | &funcrettype, |
372 | 4.35k | &tupdesc); |
373 | | |
374 | 4.35k | if (functypclass == TYPEFUNC_COMPOSITE || |
375 | 4.35k | functypclass == TYPEFUNC_COMPOSITE_DOMAIN3.19k ) |
376 | 1.15k | { |
377 | | /* Composite data type, e.g. a table's row type */ |
378 | 1.15k | Assert(tupdesc); |
379 | 1.15k | Assert(tupdesc->natts >= colcount); |
380 | | /* Must copy it out of typcache for safety */ |
381 | 1.15k | tupdesc = CreateTupleDescCopy(tupdesc); |
382 | 1.15k | } |
383 | 3.19k | else if (functypclass == TYPEFUNC_SCALAR) |
384 | 2.91k | { |
385 | | /* Base data type, i.e. scalar */ |
386 | 2.91k | tupdesc = CreateTemplateTupleDesc(1, false); |
387 | 2.91k | TupleDescInitEntry(tupdesc, |
388 | 2.91k | (AttrNumber) 1, |
389 | 2.91k | NULL, /* don't care about the name here */ |
390 | 2.91k | funcrettype, |
391 | 2.91k | -1, |
392 | 2.91k | 0); |
393 | 2.91k | TupleDescInitEntryCollation(tupdesc, |
394 | 2.91k | (AttrNumber) 1, |
395 | 2.91k | exprCollation(funcexpr)); |
396 | 2.91k | } |
397 | 276 | else if (functypclass == TYPEFUNC_RECORD) |
398 | 278 | { |
399 | 278 | tupdesc = BuildDescFromLists(rtfunc->funccolnames, |
400 | 278 | rtfunc->funccoltypes, |
401 | 278 | rtfunc->funccoltypmods, |
402 | 278 | rtfunc->funccolcollations); |
403 | | |
404 | | /* |
405 | | * For RECORD results, make sure a typmod has been assigned. (The |
406 | | * function should do this for itself, but let's cover things in |
407 | | * case it doesn't.) |
408 | | */ |
409 | 278 | BlessTupleDesc(tupdesc); |
410 | 278 | } |
411 | 18.4E | else |
412 | 18.4E | { |
413 | | /* crummy error message, but parser should have caught this */ |
414 | 18.4E | elog(ERROR, "function in FROM has unsupported return type"); |
415 | 18.4E | } |
416 | | |
417 | 4.35k | fs->tupdesc = tupdesc; |
418 | 4.35k | fs->colcount = colcount; |
419 | | |
420 | | /* |
421 | | * We only need separate slots for the function results if we are |
422 | | * doing ordinality or multiple functions; otherwise, we'll fetch |
423 | | * function results directly into the scan slot. |
424 | | */ |
425 | 4.35k | if (!scanstate->simple) |
426 | 129 | { |
427 | 129 | fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc); |
428 | 129 | } |
429 | 4.22k | else |
430 | 4.22k | fs->func_slot = NULL; |
431 | | |
432 | 4.35k | natts += colcount; |
433 | 4.35k | i++; |
434 | 4.35k | } |
435 | | |
436 | | /* |
437 | | * Create the combined TupleDesc |
438 | | * |
439 | | * If there is just one function without ordinality, the scan result |
440 | | * tupdesc is the same as the function result tupdesc --- except that we |
441 | | * may stuff new names into it below, so drop any rowtype label. |
442 | | */ |
443 | 4.35k | if (scanstate->simple) |
444 | 4.22k | { |
445 | 4.22k | scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc); |
446 | 4.22k | scan_tupdesc->tdtypeid = RECORDOID; |
447 | 4.22k | scan_tupdesc->tdtypmod = -1; |
448 | 4.22k | } |
449 | 128 | else |
450 | 128 | { |
451 | 128 | AttrNumber attno = 0; |
452 | | |
453 | 128 | if (node->funcordinality) |
454 | 129 | natts++; |
455 | | |
456 | 128 | scan_tupdesc = CreateTemplateTupleDesc(natts, false); |
457 | | |
458 | 257 | for (i = 0; i < nfuncs; i++129 ) |
459 | 129 | { |
460 | 129 | TupleDesc tupdesc = scanstate->funcstates[i].tupdesc; |
461 | 129 | int colcount = scanstate->funcstates[i].colcount; |
462 | 129 | int j; |
463 | | |
464 | 258 | for (j = 1; j <= colcount; j++129 ) |
465 | 129 | TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j); |
466 | 129 | } |
467 | | |
468 | | /* If doing ordinality, add a column of type "bigint" at the end */ |
469 | 128 | if (node->funcordinality) |
470 | 129 | { |
471 | 129 | TupleDescInitEntry(scan_tupdesc, |
472 | 129 | ++attno, |
473 | 129 | NULL, /* don't care about the name here */ |
474 | 129 | INT8OID, |
475 | 129 | -1, |
476 | 129 | 0); |
477 | 129 | } |
478 | | |
479 | 128 | Assert(attno == natts); |
480 | 128 | } |
481 | | |
482 | | /* |
483 | | * Initialize scan slot and type. |
484 | | */ |
485 | 4.35k | ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc); |
486 | | |
487 | | /* |
488 | | * Initialize result slot, type and projection. |
489 | | */ |
490 | 4.35k | ExecInitResultTypeTL(&scanstate->ss.ps); |
491 | 4.35k | ExecAssignScanProjectionInfo(&scanstate->ss); |
492 | | |
493 | | /* |
494 | | * initialize child expressions |
495 | | */ |
496 | 4.35k | scanstate->ss.ps.qual = |
497 | 4.35k | ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate); |
498 | | |
499 | | /* |
500 | | * Create a memory context that ExecMakeTableFunctionResult can use to |
501 | | * evaluate function arguments in. We can't use the per-tuple context for |
502 | | * this because it gets reset too often; but we don't want to leak |
503 | | * evaluation results into the query-lifespan context either. We just |
504 | | * need one context, because we evaluate each function separately. |
505 | | */ |
506 | 4.35k | scanstate->argcontext = AllocSetContextCreate(GetCurrentMemoryContext(), |
507 | 4.35k | "Table function arguments", |
508 | 4.35k | ALLOCSET_DEFAULT_SIZES); |
509 | | |
510 | 4.35k | return scanstate; |
511 | 4.35k | } |
512 | | |
513 | | /* ---------------------------------------------------------------- |
514 | | * ExecEndFunctionScan |
515 | | * |
516 | | * frees any storage allocated through C routines. |
517 | | * ---------------------------------------------------------------- |
518 | | */ |
519 | | void |
520 | | ExecEndFunctionScan(FunctionScanState *node) |
521 | 3.54k | { |
522 | 3.54k | int i; |
523 | | |
524 | | /* |
525 | | * Free the exprcontext |
526 | | */ |
527 | 3.54k | ExecFreeExprContext(&node->ss.ps); |
528 | | |
529 | | /* |
530 | | * clean out the tuple table |
531 | | */ |
532 | 3.54k | if (node->ss.ps.ps_ResultTupleSlot) |
533 | 929 | ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); |
534 | 3.54k | ExecClearTuple(node->ss.ss_ScanTupleSlot); |
535 | | |
536 | | /* |
537 | | * Release slots and tuplestore resources |
538 | | */ |
539 | 7.09k | for (i = 0; i < node->nfuncs; i++3.54k ) |
540 | 3.54k | { |
541 | 3.54k | FunctionScanPerFuncState *fs = &node->funcstates[i]; |
542 | | |
543 | 3.54k | if (fs->func_slot) |
544 | 129 | ExecClearTuple(fs->func_slot); |
545 | | |
546 | 3.54k | if (fs->tstore != NULL) |
547 | 2.97k | { |
548 | 2.97k | tuplestore_end(node->funcstates[i].tstore); |
549 | 2.97k | fs->tstore = NULL; |
550 | 2.97k | } |
551 | 3.54k | } |
552 | 3.54k | } |
553 | | |
554 | | /* ---------------------------------------------------------------- |
555 | | * ExecReScanFunctionScan |
556 | | * |
557 | | * Rescans the relation. |
558 | | * ---------------------------------------------------------------- |
559 | | */ |
560 | | void |
561 | | ExecReScanFunctionScan(FunctionScanState *node) |
562 | 40.8k | { |
563 | 40.8k | FunctionScan *scan = (FunctionScan *) node->ss.ps.plan; |
564 | 40.8k | int i; |
565 | 40.8k | Bitmapset *chgparam = node->ss.ps.chgParam; |
566 | | |
567 | 40.8k | if (node->ss.ps.ps_ResultTupleSlot) |
568 | 633 | ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); |
569 | 81.6k | for (i = 0; i < node->nfuncs; i++40.8k ) |
570 | 40.8k | { |
571 | 40.8k | FunctionScanPerFuncState *fs = &node->funcstates[i]; |
572 | | |
573 | 40.8k | if (fs->func_slot) |
574 | 29.0k | ExecClearTuple(fs->func_slot); |
575 | 40.8k | } |
576 | | |
577 | 40.8k | ExecScanReScan(&node->ss); |
578 | | |
579 | | /* |
580 | | * Here we have a choice whether to drop the tuplestores (and recompute |
581 | | * the function outputs) or just rescan them. We must recompute if an |
582 | | * expression contains changed parameters, else we rescan. |
583 | | * |
584 | | * XXX maybe we should recompute if the function is volatile? But in |
585 | | * general the executor doesn't conditionalize its actions on that. |
586 | | */ |
587 | 40.8k | if (chgparam) |
588 | 40.0k | { |
589 | 40.0k | ListCell *lc; |
590 | | |
591 | 40.0k | i = 0; |
592 | 40.0k | foreach(lc, scan->functions) |
593 | 40.0k | { |
594 | 40.0k | RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc); |
595 | | |
596 | 40.0k | if (bms_overlap(chgparam, rtfunc->funcparams)) |
597 | 39.9k | { |
598 | 39.9k | if (node->funcstates[i].tstore != NULL) |
599 | 39.3k | { |
600 | 39.3k | tuplestore_end(node->funcstates[i].tstore); |
601 | 39.3k | node->funcstates[i].tstore = NULL; |
602 | 39.3k | } |
603 | 39.9k | node->funcstates[i].rowcount = -1; |
604 | 39.9k | } |
605 | 40.0k | i++; |
606 | 40.0k | } |
607 | 40.0k | } |
608 | | |
609 | | /* Reset ordinality counter */ |
610 | 40.8k | node->ordinal = 0; |
611 | | |
612 | | /* Make sure we rewind any remaining tuplestores */ |
613 | 81.6k | for (i = 0; i < node->nfuncs; i++40.8k ) |
614 | 40.8k | { |
615 | 40.8k | if (node->funcstates[i].tstore != NULL) |
616 | 792 | tuplestore_rescan(node->funcstates[i].tstore); |
617 | 40.8k | } |
618 | 40.8k | } |