PostgreSQL Source Code git master
nodeFunctionscan.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeFunctionscan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

FunctionScanStateExecInitFunctionScan (FunctionScan *node, EState *estate, int eflags)
 
void ExecEndFunctionScan (FunctionScanState *node)
 
void ExecReScanFunctionScan (FunctionScanState *node)
 

Function Documentation

◆ ExecEndFunctionScan()

void ExecEndFunctionScan ( FunctionScanState node)

Definition at line 530 of file nodeFunctionscan.c.

531{
532 int i;
533
534 /*
535 * Release slots and tuplestore resources
536 */
537 for (i = 0; i < node->nfuncs; i++)
538 {
540
541 if (fs->tstore != NULL)
542 {
544 fs->tstore = NULL;
545 }
546 }
547}
int i
Definition: isn.c:74
Tuplestorestate * tstore
struct FunctionScanPerFuncState * funcstates
Definition: execnodes.h:1971
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:492

References FunctionScanState::funcstates, i, FunctionScanState::nfuncs, FunctionScanPerFuncState::tstore, and tuplestore_end().

Referenced by ExecEndNode().

◆ ExecInitFunctionScan()

FunctionScanState * ExecInitFunctionScan ( FunctionScan node,
EState estate,
int  eflags 
)

Definition at line 279 of file nodeFunctionscan.c.

280{
281 FunctionScanState *scanstate;
282 int nfuncs = list_length(node->functions);
283 TupleDesc scan_tupdesc;
284 int i,
285 natts;
286 ListCell *lc;
287
288 /* check for unsupported flags */
289 Assert(!(eflags & EXEC_FLAG_MARK));
290
291 /*
292 * FunctionScan should not have any children.
293 */
294 Assert(outerPlan(node) == NULL);
295 Assert(innerPlan(node) == NULL);
296
297 /*
298 * create new ScanState for node
299 */
300 scanstate = makeNode(FunctionScanState);
301 scanstate->ss.ps.plan = (Plan *) node;
302 scanstate->ss.ps.state = estate;
303 scanstate->ss.ps.ExecProcNode = ExecFunctionScan;
304 scanstate->eflags = eflags;
305
306 /*
307 * are we adding an ordinality column?
308 */
309 scanstate->ordinality = node->funcordinality;
310
311 scanstate->nfuncs = nfuncs;
312 if (nfuncs == 1 && !node->funcordinality)
313 scanstate->simple = true;
314 else
315 scanstate->simple = false;
316
317 /*
318 * Ordinal 0 represents the "before the first row" position.
319 *
320 * We need to track ordinal position even when not adding an ordinality
321 * column to the result, in order to handle backwards scanning properly
322 * with multiple functions with different result sizes. (We can't position
323 * any individual function's tuplestore any more than 1 place beyond its
324 * end, so when scanning backwards, we need to know when to start
325 * including the function in the scan again.)
326 */
327 scanstate->ordinal = 0;
328
329 /*
330 * Miscellaneous initialization
331 *
332 * create expression context for node
333 */
334 ExecAssignExprContext(estate, &scanstate->ss.ps);
335
336 scanstate->funcstates = palloc(nfuncs * sizeof(FunctionScanPerFuncState));
337
338 natts = 0;
339 i = 0;
340 foreach(lc, node->functions)
341 {
342 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
343 Node *funcexpr = rtfunc->funcexpr;
344 int colcount = rtfunc->funccolcount;
345 FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
346 TupleDesc tupdesc;
347
348 fs->setexpr =
350 scanstate->ss.ps.ps_ExprContext,
351 &scanstate->ss.ps);
352
353 /*
354 * Don't allocate the tuplestores; the actual calls to the functions
355 * do that. NULL means that we have not called the function yet (or
356 * need to call it again after a rescan).
357 */
358 fs->tstore = NULL;
359 fs->rowcount = -1;
360
361 /*
362 * Now build a tupdesc showing the result type we expect from the
363 * function. If we have a coldeflist then that takes priority (note
364 * the parser enforces that there is one if the function's nominal
365 * output type is RECORD). Otherwise use get_expr_result_type.
366 *
367 * Note that if the function returns a named composite type, that may
368 * now contain more or different columns than it did when the plan was
369 * made. For both that and the RECORD case, we need to check tuple
370 * compatibility. ExecMakeTableFunctionResult handles some of this,
371 * and CheckVarSlotCompatibility provides a backstop.
372 */
373 if (rtfunc->funccolnames != NIL)
374 {
375 tupdesc = BuildDescFromLists(rtfunc->funccolnames,
376 rtfunc->funccoltypes,
377 rtfunc->funccoltypmods,
378 rtfunc->funccolcollations);
379
380 /*
381 * For RECORD results, make sure a typmod has been assigned. (The
382 * function should do this for itself, but let's cover things in
383 * case it doesn't.)
384 */
385 BlessTupleDesc(tupdesc);
386 }
387 else
388 {
389 TypeFuncClass functypclass;
390 Oid funcrettype;
391
392 functypclass = get_expr_result_type(funcexpr,
393 &funcrettype,
394 &tupdesc);
395
396 if (functypclass == TYPEFUNC_COMPOSITE ||
397 functypclass == TYPEFUNC_COMPOSITE_DOMAIN)
398 {
399 /* Composite data type, e.g. a table's row type */
400 Assert(tupdesc);
401 /* Must copy it out of typcache for safety */
402 tupdesc = CreateTupleDescCopy(tupdesc);
403 }
404 else if (functypclass == TYPEFUNC_SCALAR)
405 {
406 /* Base data type, i.e. scalar */
407 tupdesc = CreateTemplateTupleDesc(1);
408 TupleDescInitEntry(tupdesc,
409 (AttrNumber) 1,
410 NULL, /* don't care about the name here */
411 funcrettype,
412 -1,
413 0);
415 (AttrNumber) 1,
416 exprCollation(funcexpr));
417 }
418 else
419 {
420 /* crummy error message, but parser should have caught this */
421 elog(ERROR, "function in FROM has unsupported return type");
422 }
423 }
424
425 fs->tupdesc = tupdesc;
426 fs->colcount = colcount;
427
428 /*
429 * We only need separate slots for the function results if we are
430 * doing ordinality or multiple functions; otherwise, we'll fetch
431 * function results directly into the scan slot.
432 */
433 if (!scanstate->simple)
434 {
435 fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc,
437 }
438 else
439 fs->func_slot = NULL;
440
441 natts += colcount;
442 i++;
443 }
444
445 /*
446 * Create the combined TupleDesc
447 *
448 * If there is just one function without ordinality, the scan result
449 * tupdesc is the same as the function result tupdesc --- except that we
450 * may stuff new names into it below, so drop any rowtype label.
451 */
452 if (scanstate->simple)
453 {
454 scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
455 scan_tupdesc->tdtypeid = RECORDOID;
456 scan_tupdesc->tdtypmod = -1;
457 }
458 else
459 {
460 AttrNumber attno = 0;
461
462 if (node->funcordinality)
463 natts++;
464
465 scan_tupdesc = CreateTemplateTupleDesc(natts);
466
467 for (i = 0; i < nfuncs; i++)
468 {
469 TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
470 int colcount = scanstate->funcstates[i].colcount;
471 int j;
472
473 for (j = 1; j <= colcount; j++)
474 TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
475 }
476
477 /* If doing ordinality, add a column of type "bigint" at the end */
478 if (node->funcordinality)
479 {
480 TupleDescInitEntry(scan_tupdesc,
481 ++attno,
482 NULL, /* don't care about the name here */
483 INT8OID,
484 -1,
485 0);
486 }
487
488 Assert(attno == natts);
489 }
490
491 /*
492 * Initialize scan slot and type.
493 */
494 ExecInitScanTupleSlot(estate, &scanstate->ss, scan_tupdesc,
496
497 /*
498 * Initialize result slot, type and projection.
499 */
500 ExecInitResultTypeTL(&scanstate->ss.ps);
501 ExecAssignScanProjectionInfo(&scanstate->ss);
502
503 /*
504 * initialize child expressions
505 */
506 scanstate->ss.ps.qual =
507 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
508
509 /*
510 * Create a memory context that ExecMakeTableFunctionResult can use to
511 * evaluate function arguments in. We can't use the per-tuple context for
512 * this because it gets reset too often; but we don't want to leak
513 * evaluation results into the query-lifespan context either. We just
514 * need one context, because we evaluate each function separately.
515 */
517 "Table function arguments",
519
520 return scanstate;
521}
int16 AttrNumber
Definition: attnum.h:21
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:229
SetExprState * ExecInitTableFunctionResult(Expr *expr, ExprContext *econtext, PlanState *parent)
Definition: execSRF.c:56
void ExecAssignScanProjectionInfo(ScanState *node)
Definition: execScan.c:81
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2258
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1998
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1942
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2018
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:487
#define EXEC_FLAG_MARK
Definition: executor.h:70
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:299
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
Assert(PointerIsAligned(start, uint64))
int j
Definition: isn.c:75
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
static TupleTableSlot * ExecFunctionScan(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:155
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define innerPlan(node)
Definition: plannodes.h:230
#define outerPlan(node)
Definition: plannodes.h:231
unsigned int Oid
Definition: postgres_ext.h:32
TupleTableSlot * func_slot
MemoryContext argcontext
Definition: execnodes.h:1972
List * functions
Definition: plannodes.h:717
bool funcordinality
Definition: plannodes.h:719
Definition: nodes.h:129
ExprState * qual
Definition: execnodes.h:1174
Plan * plan
Definition: execnodes.h:1153
EState * state
Definition: execnodes.h:1155
ExprContext * ps_ExprContext
Definition: execnodes.h:1192
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1159
PlanState ps
Definition: execnodes.h:1609
int32 tdtypmod
Definition: tupdesc.h:133
Oid tdtypeid
Definition: tupdesc.h:132
TupleDesc CreateTemplateTupleDesc(int natts)
Definition: tupdesc.c:164
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:234
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition: tupdesc.c:1010
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:985
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:801
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:445

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, FunctionScanState::argcontext, Assert(), BlessTupleDesc(), BuildDescFromLists(), FunctionScanPerFuncState::colcount, CreateTemplateTupleDesc(), CreateTupleDescCopy(), CurrentMemoryContext, FunctionScanState::eflags, elog, ERROR, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecFunctionScan(), ExecInitExtraTupleSlot(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecInitTableFunctionResult(), PlanState::ExecProcNode, exprCollation(), FunctionScanPerFuncState::func_slot, RangeTblFunction::funcexpr, FunctionScan::funcordinality, FunctionScanState::funcstates, FunctionScan::functions, get_expr_result_type(), i, innerPlan, j, lfirst, list_length(), makeNode, FunctionScanState::nfuncs, NIL, FunctionScanState::ordinal, FunctionScanState::ordinality, outerPlan, palloc(), PlanState::plan, ScanState::ps, PlanState::ps_ExprContext, PlanState::qual, FunctionScanPerFuncState::rowcount, FunctionScan::scan, FunctionScanPerFuncState::setexpr, FunctionScanState::simple, FunctionScanState::ss, PlanState::state, TupleDescData::tdtypeid, TupleDescData::tdtypmod, FunctionScanPerFuncState::tstore, TTSOpsMinimalTuple, FunctionScanPerFuncState::tupdesc, TupleDescCopyEntry(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_SCALAR.

Referenced by ExecInitNode().

◆ ExecReScanFunctionScan()

void ExecReScanFunctionScan ( FunctionScanState node)

Definition at line 556 of file nodeFunctionscan.c.

557{
558 FunctionScan *scan = (FunctionScan *) node->ss.ps.plan;
559 int i;
560 Bitmapset *chgparam = node->ss.ps.chgParam;
561
562 if (node->ss.ps.ps_ResultTupleSlot)
564 for (i = 0; i < node->nfuncs; i++)
565 {
567
568 if (fs->func_slot)
570 }
571
572 ExecScanReScan(&node->ss);
573
574 /*
575 * Here we have a choice whether to drop the tuplestores (and recompute
576 * the function outputs) or just rescan them. We must recompute if an
577 * expression contains changed parameters, else we rescan.
578 *
579 * XXX maybe we should recompute if the function is volatile? But in
580 * general the executor doesn't conditionalize its actions on that.
581 */
582 if (chgparam)
583 {
584 ListCell *lc;
585
586 i = 0;
587 foreach(lc, scan->functions)
588 {
589 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
590
591 if (bms_overlap(chgparam, rtfunc->funcparams))
592 {
593 if (node->funcstates[i].tstore != NULL)
594 {
596 node->funcstates[i].tstore = NULL;
597 }
598 node->funcstates[i].rowcount = -1;
599 }
600 i++;
601 }
602 }
603
604 /* Reset ordinality counter */
605 node->ordinal = 0;
606
607 /* Make sure we rewind any remaining tuplestores */
608 for (i = 0; i < node->nfuncs; i++)
609 {
610 if (node->funcstates[i].tstore != NULL)
612 }
613}
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
void ExecScanReScan(ScanState *node)
Definition: execScan.c:108
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:78
Bitmapset * chgParam
Definition: execnodes.h:1185
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1191
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1285
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454

References bms_overlap(), PlanState::chgParam, ExecClearTuple(), ExecScanReScan(), FunctionScanPerFuncState::func_slot, FunctionScanState::funcstates, FunctionScan::functions, i, if(), lfirst, FunctionScanState::nfuncs, FunctionScanState::ordinal, PlanState::plan, ScanState::ps, PlanState::ps_ResultTupleSlot, FunctionScanPerFuncState::rowcount, FunctionScanState::ss, FunctionScanPerFuncState::tstore, tuplestore_end(), and tuplestore_rescan().

Referenced by ExecReScan().