PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
extern

Definition at line 533 of file nodeFunctionscan.c.

534{
535 int i;
536
537 /*
538 * Release slots and tuplestore resources
539 */
540 for (i = 0; i < node->nfuncs; i++)
541 {
543
544 if (fs->tstore != NULL)
545 {
547 fs->tstore = NULL;
548 }
549 }
550}
int i
Definition isn.c:77
static int fb(int x)
struct FunctionScanPerFuncState * funcstates
Definition execnodes.h:1939
void tuplestore_end(Tuplestorestate *state)
Definition tuplestore.c:493

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

Referenced by ExecEndNode().

◆ ExecInitFunctionScan()

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

Definition at line 280 of file nodeFunctionscan.c.

281{
283 int nfuncs = list_length(node->functions);
285 int i,
286 natts;
287 ListCell *lc;
288
289 /* check for unsupported flags */
290 Assert(!(eflags & EXEC_FLAG_MARK));
291
292 /*
293 * FunctionScan should not have any children.
294 */
295 Assert(outerPlan(node) == NULL);
296 Assert(innerPlan(node) == NULL);
297
298 /*
299 * create new ScanState for node
300 */
302 scanstate->ss.ps.plan = (Plan *) node;
303 scanstate->ss.ps.state = estate;
304 scanstate->ss.ps.ExecProcNode = ExecFunctionScan;
305 scanstate->eflags = eflags;
306
307 /*
308 * are we adding an ordinality column?
309 */
310 scanstate->ordinality = node->funcordinality;
311
312 scanstate->nfuncs = nfuncs;
313 if (nfuncs == 1 && !node->funcordinality)
314 scanstate->simple = true;
315 else
316 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 scanstate->ordinal = 0;
329
330 /*
331 * Miscellaneous initialization
332 *
333 * create expression context for node
334 */
335 ExecAssignExprContext(estate, &scanstate->ss.ps);
336
337 scanstate->funcstates = palloc_array(FunctionScanPerFuncState, nfuncs);
338
339 natts = 0;
340 i = 0;
341 foreach(lc, node->functions)
342 {
344 Node *funcexpr = rtfunc->funcexpr;
345 int colcount = rtfunc->funccolcount;
346 FunctionScanPerFuncState *fs = &scanstate->funcstates[i];
347 TupleDesc tupdesc;
348
349 fs->setexpr =
351 scanstate->ss.ps.ps_ExprContext,
352 &scanstate->ss.ps);
353
354 /*
355 * Don't allocate the tuplestores; the actual calls to the functions
356 * do that. NULL means that we have not called the function yet (or
357 * need to call it again after a rescan).
358 */
359 fs->tstore = NULL;
360 fs->rowcount = -1;
361
362 /*
363 * Now build a tupdesc showing the result type we expect from the
364 * function. If we have a coldeflist then that takes priority (note
365 * the parser enforces that there is one if the function's nominal
366 * output type is RECORD). Otherwise use get_expr_result_type.
367 *
368 * Note that if the function returns a named composite type, that may
369 * now contain more or different columns than it did when the plan was
370 * made. For both that and the RECORD case, we need to check tuple
371 * compatibility. ExecMakeTableFunctionResult handles some of this,
372 * and CheckVarSlotCompatibility provides a backstop.
373 */
374 if (rtfunc->funccolnames != NIL)
375 {
376 tupdesc = BuildDescFromLists(rtfunc->funccolnames,
377 rtfunc->funccoltypes,
378 rtfunc->funccoltypmods,
379 rtfunc->funccolcollations);
380
381 /*
382 * For RECORD results, make sure a typmod has been assigned. (The
383 * function should do this for itself, but let's cover things in
384 * case it doesn't.)
385 */
386 BlessTupleDesc(tupdesc);
387 }
388 else
389 {
392
395 &tupdesc);
396
399 {
400 /* Composite data type, e.g. a table's row type */
401 Assert(tupdesc);
402 /* Must copy it out of typcache for safety */
403 tupdesc = CreateTupleDescCopy(tupdesc);
404 }
405 else if (functypclass == TYPEFUNC_SCALAR)
406 {
407 /* Base data type, i.e. scalar */
408 tupdesc = CreateTemplateTupleDesc(1);
409 TupleDescInitEntry(tupdesc,
410 (AttrNumber) 1,
411 NULL, /* don't care about the name here */
413 -1,
414 0);
416 (AttrNumber) 1,
417 exprCollation(funcexpr));
418 TupleDescFinalize(tupdesc);
419 }
420 else
421 {
422 /* crummy error message, but parser should have caught this */
423 elog(ERROR, "function in FROM has unsupported return type");
424 }
425 }
426
427 fs->tupdesc = tupdesc;
428 fs->colcount = colcount;
429
430 /*
431 * We only need separate slots for the function results if we are
432 * doing ordinality or multiple functions; otherwise, we'll fetch
433 * function results directly into the scan slot.
434 */
435 if (!scanstate->simple)
436 {
437 fs->func_slot = ExecInitExtraTupleSlot(estate, fs->tupdesc,
439 }
440 else
441 fs->func_slot = NULL;
442
443 natts += colcount;
444 i++;
445 }
446
447 /*
448 * Create the combined TupleDesc
449 *
450 * If there is just one function without ordinality, the scan result
451 * tupdesc is the same as the function result tupdesc --- except that we
452 * may stuff new names into it below, so drop any rowtype label.
453 */
454 if (scanstate->simple)
455 {
456 scan_tupdesc = CreateTupleDescCopy(scanstate->funcstates[0].tupdesc);
457 scan_tupdesc->tdtypeid = RECORDOID;
458 scan_tupdesc->tdtypmod = -1;
459 }
460 else
461 {
462 AttrNumber attno = 0;
463
464 if (node->funcordinality)
465 natts++;
466
468
469 for (i = 0; i < nfuncs; i++)
470 {
471 TupleDesc tupdesc = scanstate->funcstates[i].tupdesc;
472 int colcount = scanstate->funcstates[i].colcount;
473 int j;
474
475 for (j = 1; j <= colcount; j++)
476 TupleDescCopyEntry(scan_tupdesc, ++attno, tupdesc, j);
477 }
478
479 /* If doing ordinality, add a column of type "bigint" at the end */
480 if (node->funcordinality)
481 {
483 ++attno,
484 NULL, /* don't care about the name here */
485 INT8OID,
486 -1,
487 0);
488 }
489
491 Assert(attno == natts);
492 }
493
494 /*
495 * Initialize scan slot and type.
496 */
499
500 /*
501 * Initialize result slot, type and projection.
502 */
505
506 /*
507 * initialize child expressions
508 */
509 scanstate->ss.ps.qual =
510 ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
511
512 /*
513 * Create a memory context that ExecMakeTableFunctionResult can use to
514 * evaluate function arguments in. We can't use the per-tuple context for
515 * this because it gets reset too often; but we don't want to leak
516 * evaluation results into the query-lifespan context either. We just
517 * need one context, because we evaluate each function separately.
518 */
520 "Table function arguments",
522
523 return scanstate;
524}
int16 AttrNumber
Definition attnum.h:21
#define Assert(condition)
Definition c.h:945
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition execExpr.c:250
SetExprState * ExecInitTableFunctionResult(Expr *expr, ExprContext *econtext, PlanState *parent)
Definition execSRF.c:57
void ExecAssignScanProjectionInfo(ScanState *node)
Definition execScan.c:81
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops, uint16 flags)
void ExecInitResultTypeTL(PlanState *planstate)
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsMinimalTuple
Definition execTuples.c:86
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition execUtils.c:490
#define EXEC_FLAG_MARK
Definition executor.h:71
#define palloc_array(type, count)
Definition fe_memutils.h:76
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
int j
Definition isn.c:78
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
static TupleTableSlot * ExecFunctionScan(PlanState *pstate)
#define makeNode(_type_)
Definition nodes.h:161
#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:264
#define outerPlan(node)
Definition plannodes.h:265
unsigned int Oid
List * functions
Definition plannodes.h:784
bool funcordinality
Definition plannodes.h:786
Definition nodes.h:135
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition tupdesc.c:242
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition tupdesc.c:1106
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1081
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition tupdesc.c:469

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, BlessTupleDesc(), BuildDescFromLists(), FunctionScanPerFuncState::colcount, CreateTemplateTupleDesc(), CreateTupleDescCopy(), CurrentMemoryContext, elog, ERROR, EXEC_FLAG_MARK, ExecAssignExprContext(), ExecAssignScanProjectionInfo(), ExecFunctionScan(), ExecInitExtraTupleSlot(), ExecInitQual(), ExecInitResultTypeTL(), ExecInitScanTupleSlot(), ExecInitTableFunctionResult(), exprCollation(), fb(), FunctionScanPerFuncState::func_slot, RangeTblFunction::funcexpr, FunctionScan::funcordinality, FunctionScan::functions, get_expr_result_type(), i, innerPlan, j, lfirst, list_length(), makeNode, NIL, outerPlan, palloc_array, FunctionScanPerFuncState::rowcount, FunctionScan::scan, FunctionScanPerFuncState::setexpr, FunctionScanPerFuncState::tstore, TTSOpsMinimalTuple, FunctionScanPerFuncState::tupdesc, TupleDescCopyEntry(), TupleDescFinalize(), TupleDescInitEntry(), TupleDescInitEntryCollation(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_SCALAR.

Referenced by ExecInitNode().

◆ ExecReScanFunctionScan()

void ExecReScanFunctionScan ( FunctionScanState node)
extern

Definition at line 559 of file nodeFunctionscan.c.

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

References bms_overlap(), PlanState::chgParam, ExecClearTuple(), ExecScanReScan(), fb(), FunctionScanPerFuncState::func_slot, FunctionScanState::funcstates, FunctionScan::functions, i, 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().