2017-03-08 16:39:37 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* nodeTableFuncscan.c
|
|
|
|
* Support routines for scanning RangeTableFunc (XMLTABLE like functions).
|
|
|
|
*
|
2018-01-03 05:30:12 +01:00
|
|
|
* Portions Copyright (c) 1996-2018, 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
|
|
|
|
* ExecTableFuncscan scans a function.
|
|
|
|
* ExecFunctionNext retrieve next tuple in sequential order.
|
|
|
|
* ExecInitTableFuncscan creates and initializes a TableFuncscan node.
|
|
|
|
* ExecEndTableFuncscan releases any storage allocated.
|
|
|
|
* ExecReScanTableFuncscan rescans the function
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "nodes/execnodes.h"
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeTableFuncscan.h"
|
|
|
|
#include "executor/tablefunc.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#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
|
|
|
|
*
|
|
|
|
* This is a workhorse for ExecTableFuncscan
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecTableFuncscan(node)
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecInitTableFuncscan
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
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 child expressions
|
|
|
|
*/
|
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->ss.ps.qual =
|
|
|
|
ExecInitQual(node->scan.plan.qual, &scanstate->ss.ps);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* tuple table initialization
|
|
|
|
*/
|
|
|
|
ExecInitResultTupleSlot(estate, &scanstate->ss.ps);
|
|
|
|
ExecInitScanTupleSlot(estate, &scanstate->ss);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize source tuple type
|
|
|
|
*/
|
|
|
|
tupdesc = BuildDescFromLists(tf->colnames,
|
|
|
|
tf->coltypes,
|
|
|
|
tf->coltypmods,
|
|
|
|
tf->colcollations);
|
|
|
|
|
|
|
|
ExecAssignScanType(&scanstate->ss, tupdesc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize result tuple type and projection info.
|
|
|
|
*/
|
|
|
|
ExecAssignResultTypeFromTL(&scanstate->ss.ps);
|
|
|
|
ExecAssignScanProjectionInfo(&scanstate->ss);
|
|
|
|
|
|
|
|
/* Only XMLTABLE is supported currently */
|
|
|
|
scanstate->routine = &XmlTableRoutine;
|
|
|
|
|
|
|
|
scanstate->perValueCxt =
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecEndTableFuncscan
|
|
|
|
*
|
|
|
|
* frees any storage allocated through C routines.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecEndTableFuncScan(TableFuncScanState *node)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Free the exprcontext
|
|
|
|
*/
|
|
|
|
ExecFreeExprContext(&node->ss.ps);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean out the tuple table
|
|
|
|
*/
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
|
|
|
ExecClearTuple(node->ss.ss_ScanTupleSlot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release tuplestore resources
|
|
|
|
*/
|
|
|
|
if (node->tupstore != NULL)
|
|
|
|
tuplestore_end(node->tupstore);
|
|
|
|
node->tupstore = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* ExecReScanTableFuncscan
|
|
|
|
*
|
|
|
|
* Rescans the relation.
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ExecReScanTableFuncScan(TableFuncScanState *node)
|
|
|
|
{
|
|
|
|
Bitmapset *chgparam = node->ss.ps.chgParam;
|
|
|
|
|
|
|
|
ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
|
|
|
|
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);
|
|
|
|
|
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
routine->InitOpaque(tstate,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
tstate->ss.ss_ScanTupleSlot->tts_tupleDescriptor->natts);
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
/* return to original memory context, and clean up */
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
|
|
|
|
if (tstate->opaque != NULL)
|
|
|
|
{
|
|
|
|
routine->DestroyOpaque(tstate);
|
|
|
|
tstate->opaque = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 =
|
2017-05-17 22:31:56 +02:00
|
|
|
((TableFuncScan *) (tstate->ss.ps.plan))->tablefunc->ordinalitycol;
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
char *ns_name = strVal(lfirst(lc2));
|
|
|
|
char *ns_uri;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
routine->SetNamespace(tstate, ns_name, ns_uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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")));
|
|
|
|
|
|
|
|
routine->SetRowFilter(tstate, TextDatumGetCString(value));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("column filter expression must not be null"),
|
2017-03-08 16:39:37 +01:00
|
|
|
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;
|
|
|
|
oldcxt = MemoryContextSwitchTo(tstate->perValueCxt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/*
|
2017-05-17 22:31:56 +02:00
|
|
|
* Obtain the value of each column for this row, installing them into
|
|
|
|
* the slot; then add the tuple to the tuplestore.
|
2017-03-08 16:39:37 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
{
|
2017-05-17 22:31:56 +02:00
|
|
|
bool isnull;
|
2017-03-08 16:39:37 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
cell = lnext(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
tuplestore_putvalues(tstate->tupstore, tupdesc, values, nulls);
|
|
|
|
|
|
|
|
MemoryContextReset(tstate->perValueCxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcxt);
|
|
|
|
}
|