1996-07-09 02:22:35 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* nodeAppend.c
|
1997-09-07 01:04:48 -04:00
|
|
|
* routines to handle append nodes.
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2017-01-03 13:48:53 -05:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2000-01-26 00:58:53 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/backend/executor/nodeAppend.c
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
/* INTERFACE ROUTINES
|
1997-09-07 01:04:48 -04:00
|
|
|
* ExecInitAppend - initialize the append node
|
2004-09-23 21:36:37 -04:00
|
|
|
* ExecAppend - retrieve the next tuple from the node
|
1997-09-07 01:04:48 -04:00
|
|
|
* ExecEndAppend - shut down the append node
|
1998-07-15 18:16:21 -04:00
|
|
|
* ExecReScanAppend - rescan the append node
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* NOTES
|
|
|
|
|
* Each append node contains a list of one or more subplans which
|
|
|
|
|
* must be iteratively processed (forwards or backwards).
|
|
|
|
|
* Tuples are retrieved by executing the 'whichplan'th subplan
|
|
|
|
|
* until the subplan stops returning tuples, at which point that
|
|
|
|
|
* plan is shut down and the next started up.
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* Append nodes don't make use of their left and right
|
|
|
|
|
* subtrees, rather they maintain a list of subplans so
|
|
|
|
|
* a typical append node looks like this in the plan tree:
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* ...
|
|
|
|
|
* /
|
|
|
|
|
* Append -------+------+------+--- nil
|
|
|
|
|
* / \ | | |
|
2014-05-06 12:12:18 -04:00
|
|
|
* nil nil ... ... ...
|
1997-09-07 01:04:48 -04:00
|
|
|
* subplans
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2000-11-11 19:37:02 -05:00
|
|
|
* Append nodes are currently used for unions, and to support
|
|
|
|
|
* inheritance queries, where several relations need to be scanned.
|
1997-09-07 01:04:48 -04:00
|
|
|
* For example, in our standard person/student/employee/student-emp
|
|
|
|
|
* example, where student and employee inherit from person
|
|
|
|
|
* and student-emp inherits from student and employee, the
|
|
|
|
|
* query:
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2005-04-24 07:46:21 -04:00
|
|
|
* select name from person
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* generates the plan:
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* |
|
|
|
|
|
* Append -------+-------+--------+--------+
|
|
|
|
|
* / \ | | | |
|
|
|
|
|
* nil nil Scan Scan Scan Scan
|
|
|
|
|
* | | | |
|
|
|
|
|
* person employee student student-emp
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1996-10-31 05:12:26 -05:00
|
|
|
|
2000-11-11 19:37:02 -05:00
|
|
|
#include "postgres.h"
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1996-11-06 01:52:23 -05:00
|
|
|
#include "executor/execdebug.h"
|
1996-07-09 02:22:35 -04:00
|
|
|
#include "executor/nodeAppend.h"
|
2017-07-25 20:37:17 -04:00
|
|
|
#include "miscadmin.h"
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2017-07-17 03:33:49 -04:00
|
|
|
static TupleTableSlot *ExecAppend(PlanState *pstate);
|
2002-12-05 10:50:39 -05:00
|
|
|
static bool exec_append_initialize_next(AppendState *appendstate);
|
1997-08-19 17:40:56 -04:00
|
|
|
|
2001-05-14 20:33:36 -04:00
|
|
|
|
1996-07-09 02:22:35 -04:00
|
|
|
/* ----------------------------------------------------------------
|
1999-02-13 18:22:53 -05:00
|
|
|
* exec_append_initialize_next
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2002-12-05 10:50:39 -05:00
|
|
|
* Sets up the append state node for the "next" scan.
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
|
|
|
|
* Returns t iff there is a "next" scan to process.
|
1996-07-09 02:22:35 -04:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
1997-09-07 22:41:22 -04:00
|
|
|
static bool
|
2002-12-05 10:50:39 -05:00
|
|
|
exec_append_initialize_next(AppendState *appendstate)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
1997-09-07 22:41:22 -04:00
|
|
|
int whichplan;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* get information from the append node
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
1998-07-15 10:54:39 -04:00
|
|
|
whichplan = appendstate->as_whichplan;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2009-10-09 21:43:50 -04:00
|
|
|
if (whichplan < 0)
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* if scanning in reverse, we start at the last scan in the list and
|
|
|
|
|
* then proceed back to the first.. in any case we inform ExecAppend
|
|
|
|
|
* that we are at the end of the line by returning FALSE
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2009-10-09 21:43:50 -04:00
|
|
|
appendstate->as_whichplan = 0;
|
1997-09-07 01:04:48 -04:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2009-10-09 21:43:50 -04:00
|
|
|
else if (whichplan >= appendstate->as_nplans)
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* as above, end the scan if we go beyond the last scan in our list..
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2009-10-09 21:43:50 -04:00
|
|
|
appendstate->as_whichplan = appendstate->as_nplans - 1;
|
1997-09-07 01:04:48 -04:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return TRUE;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 01:04:48 -04:00
|
|
|
* ExecInitAppend
|
|
|
|
|
*
|
2002-12-05 10:50:39 -05:00
|
|
|
* Begin all of the subscans of the append node.
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
1997-09-07 01:04:48 -04:00
|
|
|
* (This is potentially wasteful, since the entire result of the
|
|
|
|
|
* append node may not be scanned, but this way all of the
|
|
|
|
|
* structures get allocated in the executor's top level memory
|
2004-09-23 21:36:37 -04:00
|
|
|
* block instead of that of the call to ExecAppend.)
|
1996-07-09 02:22:35 -04:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
AppendState *
|
2006-02-27 23:10:28 -05:00
|
|
|
ExecInitAppend(Append *node, EState *estate, int eflags)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2002-12-05 10:50:39 -05:00
|
|
|
AppendState *appendstate = makeNode(AppendState);
|
|
|
|
|
PlanState **appendplanstates;
|
1997-09-07 22:41:22 -04:00
|
|
|
int nplans;
|
|
|
|
|
int i;
|
2009-10-09 21:43:50 -04:00
|
|
|
ListCell *lc;
|
2000-06-10 01:16:38 -04:00
|
|
|
|
2006-02-27 23:10:28 -05:00
|
|
|
/* check for unsupported flags */
|
|
|
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
|
|
2017-03-21 09:48:04 -04:00
|
|
|
/*
|
2017-05-17 16:31:56 -04:00
|
|
|
* Lock the non-leaf tables in the partition tree controlled by this node.
|
|
|
|
|
* It's a no-op for non-partitioned parent tables.
|
2017-03-21 09:48:04 -04:00
|
|
|
*/
|
|
|
|
|
ExecLockNonLeafAppendTables(node->partitioned_rels, estate);
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2002-12-05 10:50:39 -05:00
|
|
|
* Set up empty vector of subplan states
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2004-05-30 19:40:41 -04:00
|
|
|
nplans = list_length(node->appendplans);
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* create new AppendState for our append node
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
appendstate->ps.plan = (Plan *) node;
|
|
|
|
|
appendstate->ps.state = estate;
|
2017-07-17 03:33:49 -04:00
|
|
|
appendstate->ps.ExecProcNode = ExecAppend;
|
2002-12-05 10:50:39 -05:00
|
|
|
appendstate->appendplans = appendplanstates;
|
1998-07-15 10:54:39 -04:00
|
|
|
appendstate->as_nplans = nplans;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* Miscellaneous initialization
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2001-03-22 01:16:21 -05:00
|
|
|
* Append plans don't have expression contexts because they never call
|
|
|
|
|
* ExecQual or ExecProject.
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* append nodes still have Result slots, which hold pointers to tuples, so
|
|
|
|
|
* we have to initialize them.
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecInitResultTupleSlot(estate, &appendstate->ps);
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2001-05-14 20:33:36 -04:00
|
|
|
* call ExecInitNode on each of the plans to be executed and save the
|
2009-10-09 21:43:50 -04:00
|
|
|
* results into the array "appendplans".
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
2009-10-09 21:43:50 -04:00
|
|
|
i = 0;
|
|
|
|
|
foreach(lc, node->appendplans)
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
2009-10-09 21:43:50 -04:00
|
|
|
Plan *initNode = (Plan *) lfirst(lc);
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2006-02-27 23:10:28 -05:00
|
|
|
appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
|
2009-10-09 21:43:50 -04:00
|
|
|
i++;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2009-10-09 21:43:50 -04:00
|
|
|
* initialize output tuple type
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecAssignResultTypeFromTL(&appendstate->ps);
|
|
|
|
|
appendstate->ps.ps_ProjInfo = NULL;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2009-10-09 21:43:50 -04:00
|
|
|
* initialize to scan first subplan
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2009-10-09 21:43:50 -04:00
|
|
|
appendstate->as_whichplan = 0;
|
2002-12-05 10:50:39 -05:00
|
|
|
exec_append_initialize_next(appendstate);
|
1999-10-30 19:13:30 -04:00
|
|
|
|
2002-12-05 10:50:39 -05:00
|
|
|
return appendstate;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2004-09-23 21:36:37 -04:00
|
|
|
* ExecAppend
|
1997-09-07 01:04:48 -04:00
|
|
|
*
|
2005-05-22 18:30:20 -04:00
|
|
|
* Handles iteration over multiple subplans.
|
1996-07-09 02:22:35 -04:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
2017-07-17 03:33:49 -04:00
|
|
|
static TupleTableSlot *
|
|
|
|
|
ExecAppend(PlanState *pstate)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2017-07-17 03:33:49 -04:00
|
|
|
AppendState *node = castNode(AppendState, pstate);
|
|
|
|
|
|
2005-05-22 18:30:20 -04:00
|
|
|
for (;;)
|
1997-09-07 01:04:48 -04:00
|
|
|
{
|
2005-05-22 18:30:20 -04:00
|
|
|
PlanState *subnode;
|
|
|
|
|
TupleTableSlot *result;
|
|
|
|
|
|
2017-07-25 20:37:17 -04:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-05-22 18:30:20 -04:00
|
|
|
* figure out which subplan we are currently processing
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2005-05-22 18:30:20 -04:00
|
|
|
subnode = node->appendplans[node->as_whichplan];
|
|
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-05-22 18:30:20 -04:00
|
|
|
* get a tuple from the subplan
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2005-05-22 18:30:20 -04:00
|
|
|
result = ExecProcNode(subnode);
|
|
|
|
|
|
|
|
|
|
if (!TupIsNull(result))
|
|
|
|
|
{
|
|
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* If the subplan gave us something then return it as-is. We do
|
|
|
|
|
* NOT make use of the result slot that was set up in
|
2009-10-09 21:43:50 -04:00
|
|
|
* ExecInitAppend; there's no need for it.
|
2005-05-22 18:30:20 -04:00
|
|
|
*/
|
|
|
|
|
return result;
|
|
|
|
|
}
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2005-10-14 22:49:52 -04:00
|
|
|
* Go on to the "next" subplan in the appropriate direction. If no
|
|
|
|
|
* more subplans, return the empty slot set up for us by
|
|
|
|
|
* ExecInitAppend.
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
2005-05-22 18:30:20 -04:00
|
|
|
if (ScanDirectionIsForward(node->ps.state->es_direction))
|
|
|
|
|
node->as_whichplan++;
|
1997-09-07 01:04:48 -04:00
|
|
|
else
|
2005-05-22 18:30:20 -04:00
|
|
|
node->as_whichplan--;
|
|
|
|
|
if (!exec_append_initialize_next(node))
|
|
|
|
|
return ExecClearTuple(node->ps.ps_ResultTupleSlot);
|
|
|
|
|
|
|
|
|
|
/* Else loop back and try to get a tuple from the new subplan */
|
1997-09-07 01:04:48 -04:00
|
|
|
}
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 01:04:48 -04:00
|
|
|
* ExecEndAppend
|
|
|
|
|
*
|
|
|
|
|
* Shuts down the subscans of the append node.
|
|
|
|
|
*
|
|
|
|
|
* Returns nothing of interest.
|
1996-07-09 02:22:35 -04:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
void
|
2002-12-05 10:50:39 -05:00
|
|
|
ExecEndAppend(AppendState *node)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
2002-12-05 10:50:39 -05:00
|
|
|
PlanState **appendplans;
|
1997-09-07 22:41:22 -04:00
|
|
|
int nplans;
|
|
|
|
|
int i;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
|
|
|
|
* get information from the node
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
1998-07-15 10:54:39 -04:00
|
|
|
appendplans = node->appendplans;
|
2002-12-05 10:50:39 -05:00
|
|
|
nplans = node->as_nplans;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2001-03-22 01:16:21 -05:00
|
|
|
/*
|
2009-10-09 21:43:50 -04:00
|
|
|
* shut down each of the subscans
|
1997-09-07 01:04:48 -04:00
|
|
|
*/
|
|
|
|
|
for (i = 0; i < nplans; i++)
|
2009-10-09 21:43:50 -04:00
|
|
|
ExecEndNode(appendplans[i]);
|
1997-09-07 01:04:48 -04:00
|
|
|
}
|
2000-11-11 19:37:02 -05:00
|
|
|
|
1998-07-15 18:16:21 -04:00
|
|
|
void
|
2010-07-12 13:01:06 -04:00
|
|
|
ExecReScanAppend(AppendState *node)
|
1998-07-15 18:16:21 -04:00
|
|
|
{
|
1998-09-01 00:40:42 -04:00
|
|
|
int i;
|
1998-07-15 18:16:21 -04:00
|
|
|
|
2009-10-09 21:43:50 -04:00
|
|
|
for (i = 0; i < node->as_nplans; i++)
|
1998-07-15 18:16:21 -04:00
|
|
|
{
|
2003-08-03 20:43:34 -04:00
|
|
|
PlanState *subnode = node->appendplans[i];
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-08 15:47:02 -04:00
|
|
|
/*
|
|
|
|
|
* ExecReScan doesn't know about my subplans, so I have to do
|
|
|
|
|
* changed-parameter signaling myself.
|
|
|
|
|
*/
|
2003-02-08 19:30:41 -05:00
|
|
|
if (node->ps.chgParam != NULL)
|
|
|
|
|
UpdateChangedParamSet(subnode, node->ps.chgParam);
|
2001-10-25 01:50:21 -04:00
|
|
|
|
2001-05-08 15:47:02 -04:00
|
|
|
/*
|
2006-02-04 21:59:17 -05:00
|
|
|
* If chgParam of subnode is not null then plan will be re-scanned by
|
2010-07-12 13:01:06 -04:00
|
|
|
* first ExecProcNode.
|
2001-05-08 15:47:02 -04:00
|
|
|
*/
|
2010-07-12 13:01:06 -04:00
|
|
|
if (subnode->chgParam == NULL)
|
|
|
|
|
ExecReScan(subnode);
|
1998-07-15 18:16:21 -04:00
|
|
|
}
|
2009-10-09 21:43:50 -04:00
|
|
|
node->as_whichplan = 0;
|
1998-07-15 18:16:21 -04:00
|
|
|
exec_append_initialize_next(node);
|
|
|
|
|
}
|