2017-03-08 16:39:37 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* nodeTableFuncscan.c
|
|
|
|
* Support routines for scanning RangeTableFunc (XMLTABLE like functions).
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2017-03-08 16:39:37 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/executor/nodeTableFuncscan.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecTableFuncScan scans a function.
|
2017-03-08 16:39:37 +01:00
|
|
|
* ExecFunctionNext retrieve next tuple in sequential order.
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecInitTableFuncScan creates and initializes a TableFuncscan node.
|
|
|
|
* ExecEndTableFuncScan releases any storage allocated.
|
|
|
|
* ExecReScanTableFuncScan rescans the function
|
2017-03-08 16:39:37 +01:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeTableFuncscan.h"
|
|
|
|
#include "executor/tablefunc.h"
|
|
|
|
#include "miscadmin.h"
|
2019-11-12 04:00:16 +01:00
|
|
|
#include "nodes/execnodes.h"
|
2017-03-08 16:39:37 +01:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/xml.h"
|
|
|
|
|
|
|
|
static TupleTableSlot *TableFuncNext(TableFuncScanState *node);
|
|
|
|
static bool TableFuncRecheck(TableFuncScanState *node, TupleTableSlot *slot);
|
|
|
|
|
|
|
|
static void tfuncFetchRows(TableFuncScanState *tstate, ExprContext *econtext);
|
|
|
|
static void tfuncInitialize(TableFuncScanState *tstate, ExprContext *econtext, Datum doc);
|
|
|
|
static void tfuncLoadRows(TableFuncScanState *tstate, ExprContext *econtext);
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* Scan Support
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* TableFuncNext
|
|
|
|
*
|
2021-09-27 07:21:28 +02:00
|
|
|
* This is a workhorse for ExecTableFuncScan
|
2017-03-08 16:39:37 +01:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static TupleTableSlot *
|
|
|
|
TableFuncNext(TableFuncScanState *node)
|
|
|
|
{
|
|
|
|
TupleTableSlot *scanslot;
|
|
|
|
|
|
|
|
scanslot = node->ss.ss_ScanTupleSlot;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If first time through, read all tuples from function and put them in a
|
|
|
|
* tuplestore. Subsequent calls just fetch tuples from tuplestore.
|
|
|
|
*/
|
|
|
|
if (node->tupstore == NULL)
|
|
|
|
tfuncFetchRows(node, node->ss.ps.ps_ExprContext);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next tuple from tuplestore.
|
|
|
|
*/
|
|
|
|
(void) tuplestore_gettupleslot(node->tupstore,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
scanslot);
|
|
|
|
return scanslot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TableFuncRecheck -- access method routine to recheck a tuple in EvalPlanQual
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
TableFuncRecheck(TableFuncScanState *node, TupleTableSlot *slot)
|
|
|
|
{
|
|
|
|
/* nothing to check */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecTableFuncScan(node)
|
2017-03-08 16:39:37 +01:00
|
|
|
*
|
|
|
|
* Scans the function sequentially and returns the next qualifying
|
|
|
|
* tuple.
|
|
|
|
* We call the ExecScan() routine and pass it the appropriate
|
|
|
|
* access method functions.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot *
|
|
|
|
ExecTableFuncScan(PlanState *pstate)
|
2017-03-08 16:39:37 +01:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
TableFuncScanState *node = castNode(TableFuncScanState, pstate);
|
|
|
|
|
2017-03-08 16:39:37 +01:00
|
|
|
return ExecScan(&node->ss,
|
|
|
|
(ExecScanAccessMtd) TableFuncNext,
|
|
|
|
(ExecScanRecheckMtd) TableFuncRecheck);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecInitTableFuncScan
|
2017-03-08 16:39:37 +01:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
TableFuncScanState *
|
|
|
|
ExecInitTableFuncScan(TableFuncScan *node, EState *estate, int eflags)
|
|
|
|
{
|
|
|
|
TableFuncScanState *scanstate;
|
|
|
|
TableFunc *tf = node->tablefunc;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* check for unsupported flags */
|
|
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TableFuncscan should not have any children.
|
|
|
|
*/
|
|
|
|
Assert(outerPlan(node) == NULL);
|
|
|
|
Assert(innerPlan(node) == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create new ScanState for node
|
|
|
|
*/
|
|
|
|
scanstate = makeNode(TableFuncScanState);
|
|
|
|
scanstate->ss.ps.plan = (Plan *) node;
|
|
|
|
scanstate->ss.ps.state = estate;
|
2017-07-17 09:33:49 +02:00
|
|
|
scanstate->ss.ps.ExecProcNode = ExecTableFuncScan;
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Miscellaneous initialization
|
|
|
|
*
|
|
|
|
* create expression context for node
|
|
|
|
*/
|
|
|
|
ExecAssignExprContext(estate, &scanstate->ss.ps);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize source tuple type
|
|
|
|
*/
|
|
|
|
tupdesc = BuildDescFromLists(tf->colnames,
|
|
|
|
tf->coltypes,
|
|
|
|
tf->coltypmods,
|
|
|
|
tf->colcollations);
|
2018-02-17 06:17:38 +01:00
|
|
|
/* and the corresponding scan slot */
|
Introduce notion of different types of slots (without implementing them).
Upcoming work intends to allow pluggable ways to introduce new ways of
storing table data. Accessing those table access methods from the
executor requires TupleTableSlots to be carry tuples in the native
format of such storage methods; otherwise there'll be a significant
conversion overhead.
Different access methods will require different data to store tuples
efficiently (just like virtual, minimal, heap already require fields
in TupleTableSlot). To allow that without requiring additional pointer
indirections, we want to have different structs (embedding
TupleTableSlot) for different types of slots. Thus different types of
slots are needed, which requires adapting creators of slots.
The slot that most efficiently can represent a type of tuple in an
executor node will often depend on the type of slot a child node
uses. Therefore we need to track the type of slot is returned by
nodes, so parent slots can create slots based on that.
Relatedly, JIT compilation of tuple deforming needs to know which type
of slot a certain expression refers to, so it can create an
appropriate deforming function for the type of tuple in the slot.
But not all nodes will only return one type of slot, e.g. an append
node will potentially return different types of slots for each of its
subplans.
Therefore add function that allows to query the type of a node's
result slot, and whether it'll always be the same type (whether it's
fixed). This can be queried using ExecGetResultSlotOps().
The scan, result, inner, outer type of slots are automatically
inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(),
left/right subtrees respectively. If that's not correct for a node,
that can be overwritten using new fields in PlanState.
This commit does not introduce the actually abstracted implementation
of different kind of TupleTableSlots, that will be left for a followup
commit. The different types of slots introduced will, for now, still
use the same backing implementation.
While this already partially invalidates the big comment in
tuptable.h, it seems to make more sense to update it later, when the
different TupleTableSlot implementations actually exist.
Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-16 07:00:30 +01:00
|
|
|
ExecInitScanTupleSlot(estate, &scanstate->ss, tupdesc,
|
|
|
|
&TTSOpsMinimalTuple);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
* Initialize result type and projection.
|
2017-03-08 16:39:37 +01:00
|
|
|
*/
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
ExecInitResultTypeTL(&scanstate->ss.ps);
|
2017-03-08 16:39:37 +01:00
|
|
|
ExecAssignScanProjectionInfo(&scanstate->ss);
|
|
|
|
|
2018-02-17 06:17:38 +01:00
|
|
|
/*
|
|
|
|
* initialize child expressions
|
|
|
|
*/
|
|
|
|
scanstate->ss.ps.qual =
|
|
|
|
ExecInitQual(node->scan.plan.qual, &scanstate->ss.ps);
|
|
|
|
|
2022-09-01 23:07:14 +02:00
|
|
|
/* Only XMLTABLE is supported currently */
|
|
|
|
scanstate->routine = &XmlTableRoutine;
|
2017-03-08 16:39:37 +01:00
|
|
|
|
2018-08-13 02:45:35 +02:00
|
|
|
scanstate->perTableCxt =
|
2017-03-08 16:39:37 +01:00
|
|
|
AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"TableFunc per value context",
|
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
|
|
|
scanstate->opaque = NULL; /* initialized at runtime */
|
|
|
|
|
|
|
|
scanstate->ns_names = tf->ns_names;
|
|
|
|
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
scanstate->ns_uris =
|
|
|
|
ExecInitExprList(tf->ns_uris, (PlanState *) scanstate);
|
2017-03-08 16:39:37 +01:00
|
|
|
scanstate->docexpr =
|
|
|
|
ExecInitExpr((Expr *) tf->docexpr, (PlanState *) scanstate);
|
|
|
|
scanstate->rowexpr =
|
|
|
|
ExecInitExpr((Expr *) tf->rowexpr, (PlanState *) scanstate);
|
Faster expression evaluation and targetlist projection.
This replaces the old, recursive tree-walk based evaluation, with
non-recursive, opcode dispatch based, expression evaluation.
Projection is now implemented as part of expression evaluation.
This both leads to significant performance improvements, and makes
future just-in-time compilation of expressions easier.
The speed gains primarily come from:
- non-recursive implementation reduces stack usage / overhead
- simple sub-expressions are implemented with a single jump, without
function calls
- sharing some state between different sub-expressions
- reduced amount of indirect/hard to predict memory accesses by laying
out operation metadata sequentially; including the avoidance of
nearly all of the previously used linked lists
- more code has been moved to expression initialization, avoiding
constant re-checks at evaluation time
Future just-in-time compilation (JIT) has become easier, as
demonstrated by released patches intended to be merged in a later
release, for primarily two reasons: Firstly, due to a stricter split
between expression initialization and evaluation, less code has to be
handled by the JIT. Secondly, due to the non-recursive nature of the
generated "instructions", less performance-critical code-paths can
easily be shared between interpreted and compiled evaluation.
The new framework allows for significant future optimizations. E.g.:
- basic infrastructure for to later reduce the per executor-startup
overhead of expression evaluation, by caching state in prepared
statements. That'd be helpful in OLTPish scenarios where
initialization overhead is measurable.
- optimizing the generated "code". A number of proposals for potential
work has already been made.
- optimizing the interpreter. Similarly a number of proposals have
been made here too.
The move of logic into the expression initialization step leads to some
backward-incompatible changes:
- Function permission checks are now done during expression
initialization, whereas previously they were done during
execution. In edge cases this can lead to errors being raised that
previously wouldn't have been, e.g. a NULL array being coerced to a
different array type previously didn't perform checks.
- The set of domain constraints to be checked, is now evaluated once
during expression initialization, previously it was re-built
every time a domain check was evaluated. For normal queries this
doesn't change much, but e.g. for plpgsql functions, which caches
ExprStates, the old set could stick around longer. The behavior
around might still change.
Author: Andres Freund, with significant changes by Tom Lane,
changes by Heikki Linnakangas
Reviewed-By: Tom Lane, Heikki Linnakangas
Discussion: https://postgr.es/m/20161206034955.bh33paeralxbtluv@alap3.anarazel.de
2017-03-14 23:45:36 +01:00
|
|
|
scanstate->colexprs =
|
|
|
|
ExecInitExprList(tf->colexprs, (PlanState *) scanstate);
|
|
|
|
scanstate->coldefexprs =
|
|
|
|
ExecInitExprList(tf->coldefexprs, (PlanState *) scanstate);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
scanstate->notnulls = tf->notnulls;
|
|
|
|
|
|
|
|
/* these are allocated now and initialized later */
|
|
|
|
scanstate->in_functions = palloc(sizeof(FmgrInfo) * tupdesc->natts);
|
|
|
|
scanstate->typioparams = palloc(sizeof(Oid) * tupdesc->natts);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in the necessary fmgr infos.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < tupdesc->natts; i++)
|
|
|
|
{
|
|
|
|
Oid in_funcid;
|
|
|
|
|
2017-08-20 20:19:07 +02:00
|
|
|
getTypeInputInfo(TupleDescAttr(tupdesc, i)->atttypid,
|
2017-03-08 16:39:37 +01:00
|
|
|
&in_funcid, &scanstate->typioparams[i]);
|
|
|
|
fmgr_info(in_funcid, &scanstate->in_functions[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return scanstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecEndTableFuncScan
|
2017-03-08 16:39:37 +01:00
|
|
|
*
|
|
|
|
* frees any storage allocated through C routines.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecEndTableFuncScan(TableFuncScanState *node)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Free the exprcontext
|
|
|
|
*/
|
|
|
|
ExecFreeExprContext(&node->ss.ps);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean out the tuple table
|
|
|
|
*/
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
if (node->ss.ps.ps_ResultTupleSlot)
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
2017-03-08 16:39:37 +01:00
|
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release tuplestore resources
|
|
|
|
*/
|
|
|
|
if (node->tupstore != NULL)
|
|
|
|
tuplestore_end(node->tupstore);
|
|
|
|
node->tupstore = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2021-09-27 07:21:28 +02:00
|
|
|
* ExecReScanTableFuncScan
|
2017-03-08 16:39:37 +01:00
|
|
|
*
|
|
|
|
* Rescans the relation.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecReScanTableFuncScan(TableFuncScanState *node)
|
|
|
|
{
|
|
|
|
Bitmapset *chgparam = node->ss.ps.chgParam;
|
|
|
|
|
Don't require return slots for nodes without projection.
In a lot of nodes the return slot is not required. That can either be
because the node doesn't do any projection (say an Append node), or
because the node does perform projections but the projection is
optimized away because the projection would yield an identical row.
Slots aren't that small, especially for wide rows, so it's worthwhile
to avoid creating them. It's not possible to just skip creating the
slot - it's currently used to determine the tuple descriptor returned
by ExecGetResultType(). So separate the determination of the result
type from the slot creation. The work previously done internally
ExecInitResultTupleSlotTL() can now also be done separately with
ExecInitResultTypeTL() and ExecInitResultSlot(). That way nodes that
aren't guaranteed to need a result slot, can use
ExecInitResultTypeTL() to determine the result type of the node, and
ExecAssignScanProjectionInfo() (via
ExecConditionalAssignProjectionInfo()) determines that a result slot
is needed, it is created with ExecInitResultSlot().
Besides the advantage of avoiding to create slots that then are
unused, this is necessary preparation for later patches around tuple
table slot abstraction. In particular separating the return descriptor
and slot is a prerequisite to allow JITing of tuple deforming with
knowledge of the underlying tuple format, and to avoid unnecessarily
creating JITed tuple deforming for virtual slots.
This commit removes a redundant argument from
ExecInitResultTupleSlotTL(). While this commit touches a lot of the
relevant lines anyway, it'd normally still not worthwhile to cause
breakage, except that aforementioned later commits will touch *all*
ExecInitResultTupleSlotTL() callers anyway (but fits worse
thematically).
Author: Andres Freund
Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
2018-11-10 02:19:39 +01:00
|
|
|
if (node->ss.ps.ps_ResultTupleSlot)
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
2017-03-08 16:39:37 +01:00
|
|
|
ExecScanReScan(&node->ss);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recompute when parameters are changed.
|
|
|
|
*/
|
|
|
|
if (chgparam)
|
|
|
|
{
|
|
|
|
if (node->tupstore != NULL)
|
|
|
|
{
|
|
|
|
tuplestore_end(node->tupstore);
|
|
|
|
node->tupstore = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->tupstore != NULL)
|
|
|
|
tuplestore_rescan(node->tupstore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* tfuncFetchRows
|
|
|
|
*
|
|
|
|
* Read rows from a TableFunc producer
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tfuncFetchRows(TableFuncScanState *tstate, ExprContext *econtext)
|
|
|
|
{
|
|
|
|
const TableFuncRoutine *routine = tstate->routine;
|
|
|
|
MemoryContext oldcxt;
|
|
|
|
Datum value;
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
Assert(tstate->opaque == NULL);
|
|
|
|
|
|
|
|
/* build tuplestore for the result */
|
|
|
|
oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
|
|
|
|
tstate->tupstore = tuplestore_begin_heap(false, false, work_mem);
|
|
|
|
|
2018-08-13 02:45:35 +02:00
|
|
|
/*
|
|
|
|
* Each call to fetch a new set of rows - of which there may be very many
|
|
|
|
* if XMLTABLE is being used in a lateral join - will allocate a possibly
|
|
|
|
* substantial amount of memory, so we cannot use the per-query context
|
|
|
|
* here. perTableCxt now serves the same function as "argcontext" does in
|
|
|
|
* FunctionScan - a place to store per-one-call (i.e. one result table)
|
|
|
|
* lifetime data (as opposed to per-query or per-result-tuple).
|
|
|
|
*/
|
|
|
|
MemoryContextSwitchTo(tstate->perTableCxt);
|
|
|
|
|
2017-03-08 16:39:37 +01:00
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
routine->InitOpaque(tstate,
|
|
|
|
tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor->natts);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If evaluating the document expression returns NULL, the table
|
|
|
|
* expression is empty and we return immediately.
|
|
|
|
*/
|
|
|
|
value = ExecEvalExpr(tstate->docexpr, econtext, &isnull);
|
|
|
|
|
|
|
|
if (!isnull)
|
|
|
|
{
|
|
|
|
/* otherwise, pass the document value to the table builder */
|
|
|
|
tfuncInitialize(tstate, econtext, value);
|
|
|
|
|
|
|
|
/* initialize ordinality counter */
|
|
|
|
tstate->ordinal = 1;
|
|
|
|
|
|
|
|
/* Load all rows into the tuplestore, and we're done */
|
|
|
|
tfuncLoadRows(tstate, econtext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
if (tstate->opaque != NULL)
|
|
|
|
routine->DestroyOpaque(tstate);
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
2018-08-13 02:45:35 +02:00
|
|
|
/* clean up and return to original memory context */
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
if (tstate->opaque != NULL)
|
|
|
|
{
|
|
|
|
routine->DestroyOpaque(tstate);
|
|
|
|
tstate->opaque = NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-13 02:45:35 +02:00
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
MemoryContextReset(tstate->perTableCxt);
|
2017-03-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill in namespace declarations, the row filter, and column filters in a
|
|
|
|
* table expression builder context.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tfuncInitialize(TableFuncScanState *tstate, ExprContext *econtext, Datum doc)
|
|
|
|
{
|
|
|
|
const TableFuncRoutine *routine = tstate->routine;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
ListCell *lc1,
|
|
|
|
*lc2;
|
|
|
|
bool isnull;
|
|
|
|
int colno;
|
|
|
|
Datum value;
|
|
|
|
int ordinalitycol =
|
|
|
|
((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the document as a possibly-toasted Datum into the tablefunc
|
|
|
|
* context.
|
|
|
|
*/
|
|
|
|
routine->SetDocument(tstate, doc);
|
|
|
|
|
|
|
|
/* Evaluate namespace specifications */
|
|
|
|
forboth(lc1, tstate->ns_uris, lc2, tstate->ns_names)
|
|
|
|
{
|
|
|
|
ExprState *expr = (ExprState *) lfirst(lc1);
|
Remove Value node struct
The Value node struct is a weird construct. It is its own node type,
but most of the time, it actually has a node type of Integer, Float,
String, or BitString. As a consequence, the struct name and the node
type don't match most of the time, and so it has to be treated
specially a lot. There doesn't seem to be any value in the special
construct. There is very little code that wants to accept all Value
variants but nothing else (and even if it did, this doesn't provide
any convenient way to check it), and most code wants either just one
particular node type (usually String), or it accepts a broader set of
node types besides just Value.
This change removes the Value struct and node type and replaces them
by separate Integer, Float, String, and BitString node types that are
proper node types and structs of their own and behave mostly like
normal node types.
Also, this removes the T_Null node tag, which was previously also a
possible variant of Value but wasn't actually used outside of the
Value contained in A_Const. Replace that by an isnull field in
A_Const.
Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/5ba6bc5b-3f95-04f2-2419-f8ddb4c046fb@enterprisedb.com
2021-09-09 07:58:12 +02:00
|
|
|
String *ns_node = lfirst_node(String, lc2);
|
2017-03-08 16:39:37 +01:00
|
|
|
char *ns_uri;
|
2018-09-17 19:16:32 +02:00
|
|
|
char *ns_name;
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
value = ExecEvalExpr((ExprState *) expr, econtext, &isnull);
|
|
|
|
if (isnull)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("namespace URI must not be null")));
|
|
|
|
ns_uri = TextDatumGetCString(value);
|
|
|
|
|
2018-09-17 19:16:32 +02:00
|
|
|
/* DEFAULT is passed down to SetNamespace as NULL */
|
|
|
|
ns_name = ns_node ? strVal(ns_node) : NULL;
|
|
|
|
|
2017-03-08 16:39:37 +01:00
|
|
|
routine->SetNamespace(tstate, ns_name, ns_uri);
|
|
|
|
}
|
|
|
|
|
2022-09-01 23:07:14 +02:00
|
|
|
/* Install the row filter expression into the table builder context */
|
|
|
|
value = ExecEvalExpr(tstate->rowexpr, econtext, &isnull);
|
|
|
|
if (isnull)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("row filter expression must not be null")));
|
2017-03-08 16:39:37 +01:00
|
|
|
|
2022-09-01 23:07:14 +02:00
|
|
|
routine->SetRowFilter(tstate, TextDatumGetCString(value));
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the column filter expressions into the table builder context.
|
|
|
|
* If an expression is given, use that; otherwise the column name itself
|
|
|
|
* is the column filter.
|
|
|
|
*/
|
|
|
|
colno = 0;
|
|
|
|
tupdesc = tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor;
|
|
|
|
foreach(lc1, tstate->colexprs)
|
|
|
|
{
|
|
|
|
char *colfilter;
|
2017-08-20 20:19:07 +02:00
|
|
|
Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
if (colno != ordinalitycol)
|
|
|
|
{
|
|
|
|
ExprState *colexpr = lfirst(lc1);
|
|
|
|
|
|
|
|
if (colexpr != NULL)
|
|
|
|
{
|
|
|
|
value = ExecEvalExpr(colexpr, econtext, &isnull);
|
|
|
|
if (isnull)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("column filter expression must not be null"),
|
|
|
|
errdetail("Filter for column \"%s\" is null.",
|
2017-08-20 20:19:07 +02:00
|
|
|
NameStr(att->attname))));
|
2017-03-08 16:39:37 +01:00
|
|
|
colfilter = TextDatumGetCString(value);
|
|
|
|
}
|
|
|
|
else
|
2017-08-20 20:19:07 +02:00
|
|
|
colfilter = NameStr(att->attname);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
routine->SetColumnFilter(tstate, colfilter, colno);
|
|
|
|
}
|
|
|
|
|
|
|
|
colno++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load all the rows from the TableFunc table builder into a tuplestore.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tfuncLoadRows(TableFuncScanState *tstate, ExprContext *econtext)
|
|
|
|
{
|
|
|
|
const TableFuncRoutine *routine = tstate->routine;
|
|
|
|
TupleTableSlot *slot = tstate->ss.ss_ScanTupleSlot;
|
|
|
|
TupleDesc tupdesc = slot->tts_tupleDescriptor;
|
|
|
|
Datum *values = slot->tts_values;
|
|
|
|
bool *nulls = slot->tts_isnull;
|
|
|
|
int natts = tupdesc->natts;
|
|
|
|
MemoryContext oldcxt;
|
|
|
|
int ordinalitycol;
|
|
|
|
|
|
|
|
ordinalitycol =
|
|
|
|
((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
|
2018-08-13 02:45:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need a short-lived memory context that we can clean up each time
|
|
|
|
* around the loop, to avoid wasting space. Our default per-tuple context
|
|
|
|
* is fine for the job, since we won't have used it for anything yet in
|
|
|
|
* this tuple cycle.
|
|
|
|
*/
|
|
|
|
oldcxt = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep requesting rows from the table builder until there aren't any.
|
|
|
|
*/
|
|
|
|
while (routine->FetchRow(tstate))
|
|
|
|
{
|
|
|
|
ListCell *cell = list_head(tstate->coldefexprs);
|
|
|
|
int colno;
|
|
|
|
|
2017-07-26 02:37:17 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2017-03-08 16:39:37 +01:00
|
|
|
ExecClearTuple(tstate->ss.ss_ScanTupleSlot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtain the value of each column for this row, installing them into
|
|
|
|
* the slot; then add the tuple to the tuplestore.
|
|
|
|
*/
|
|
|
|
for (colno = 0; colno < natts; colno++)
|
|
|
|
{
|
2017-08-20 20:19:07 +02:00
|
|
|
Form_pg_attribute att = TupleDescAttr(tupdesc, colno);
|
|
|
|
|
2017-03-08 16:39:37 +01:00
|
|
|
if (colno == ordinalitycol)
|
|
|
|
{
|
|
|
|
/* Fast path for ordinality column */
|
|
|
|
values[colno] = Int32GetDatum(tstate->ordinal++);
|
|
|
|
nulls[colno] = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
values[colno] = routine->GetValue(tstate,
|
|
|
|
colno,
|
2017-08-20 20:19:07 +02:00
|
|
|
att->atttypid,
|
|
|
|
att->atttypmod,
|
2017-03-08 16:39:37 +01:00
|
|
|
&isnull);
|
|
|
|
|
|
|
|
/* No value? Evaluate and apply the default, if any */
|
|
|
|
if (isnull && cell != NULL)
|
|
|
|
{
|
|
|
|
ExprState *coldefexpr = (ExprState *) lfirst(cell);
|
|
|
|
|
|
|
|
if (coldefexpr != NULL)
|
|
|
|
values[colno] = ExecEvalExpr(coldefexpr, econtext,
|
|
|
|
&isnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify a possible NOT NULL constraint */
|
|
|
|
if (isnull && bms_is_member(colno, tstate->notnulls))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("null is not allowed in column \"%s\"",
|
2017-08-20 20:19:07 +02:00
|
|
|
NameStr(att->attname))));
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
nulls[colno] = isnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* advance list of default expressions */
|
|
|
|
if (cell != NULL)
|
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation of
Lisp lists, which consist of chains of separately-allocated cons cells,
each having a value and a next-cell link. We'd hacked that once before
(commit d0b4399d8) to add a separate List header, but the data was still
in cons cells. That makes some operations -- notably list_nth() -- O(N),
and it's bulky because of the next-cell pointers and per-cell palloc
overhead, and it's very cache-unfriendly if the cons cells end up
scattered around rather than being adjacent.
In this rewrite, we still have List headers, but the data is in a
resizable array of values, with no next-cell links. Now we need at
most two palloc's per List, and often only one, since we can allocate
some values in the same palloc call as the List header. (Of course,
extending an existing List may require repalloc's to enlarge the array.
But this involves just O(log N) allocations not O(N).)
Of course this is not without downsides. The key difficulty is that
addition or deletion of a list entry may now cause other entries to
move, which it did not before.
For example, that breaks foreach() and sister macros, which historically
used a pointer to the current cons-cell as loop state. We can repair
those macros transparently by making their actual loop state be an
integer list index; the exposed "ListCell *" pointer is no longer state
carried across loop iterations, but is just a derived value. (In
practice, modern compilers can optimize things back to having just one
loop state value, at least for simple cases with inline loop bodies.)
In principle, this is a semantics change for cases where the loop body
inserts or deletes list entries ahead of the current loop index; but
I found no such cases in the Postgres code.
The change is not at all transparent for code that doesn't use foreach()
but chases lists "by hand" using lnext(). The largest share of such
code in the backend is in loops that were maintaining "prev" and "next"
variables in addition to the current-cell pointer, in order to delete
list cells efficiently using list_delete_cell(). However, we no longer
need a previous-cell pointer to delete a list cell efficiently. Keeping
a next-cell pointer doesn't work, as explained above, but we can improve
matters by changing such code to use a regular foreach() loop and then
using the new macro foreach_delete_current() to delete the current cell.
(This macro knows how to update the associated foreach loop's state so
that no cells will be missed in the traversal.)
There remains a nontrivial risk of code assuming that a ListCell *
pointer will remain good over an operation that could now move the list
contents. To help catch such errors, list.c can be compiled with a new
define symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contents
whenever that could possibly happen. This makes list operations
significantly more expensive so it's not normally turned on (though it
is on by default if USE_VALGRIND is on).
There are two notable API differences from the previous code:
* lnext() now requires the List's header pointer in addition to the
current cell's address.
* list_delete_cell() no longer requires a previous-cell argument.
These changes are somewhat unfortunate, but on the other hand code using
either function needs inspection to see if it is assuming anything
it shouldn't, so it's not all bad.
Programmers should be aware of these significant performance changes:
* list_nth() and related functions are now O(1); so there's no
major access-speed difference between a list and an array.
* Inserting or deleting a list element now takes time proportional to
the distance to the end of the list, due to moving the array elements.
(However, it typically *doesn't* require palloc or pfree, so except in
long lists it's probably still faster than before.) Notably, lcons()
used to be about the same cost as lappend(), but that's no longer true
if the list is long. Code that uses lcons() and list_delete_first()
to maintain a stack might usefully be rewritten to push and pop at the
end of the list rather than the beginning.
* There are now list_insert_nth...() and list_delete_nth...() functions
that add or remove a list cell identified by index. These have the
data-movement penalty explained above, but there's no search penalty.
* list_concat() and variants now copy the second list's data into
storage belonging to the first list, so there is no longer any
sharing of cells between the input lists. The second argument is
now declared "const List *" to reflect that it isn't changed.
This patch just does the minimum needed to get the new implementation
in place and fix bugs exposed by the regression tests. As suggested
by the foregoing, there's a fair amount of followup work remaining to
do.
Also, the ENABLE_LIST_COMPAT macros are finally removed in this
commit. Code using those should have been gone a dozen years ago.
Patch by me; thanks to David Rowley, Jesper Pedersen, and others
for review.
Discussion: https://postgr.es/m/11587.1550975080@sss.pgh.pa.us
2019-07-15 19:41:58 +02:00
|
|
|
cell = lnext(tstate->coldefexprs, cell);
|
2017-03-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tuplestore_putvalues(tstate->tupstore, tupdesc, values, nulls);
|
|
|
|
|
2018-08-13 02:45:35 +02:00
|
|
|
MemoryContextReset(econtext->ecxt_per_tuple_memory);
|
2017-03-08 16:39:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
}
|