1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* nodeAppend.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines to handle append nodes.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/executor/nodeAppend.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/* INTERFACE ROUTINES
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitAppend - initialize the append node
|
2004-09-24 03:36:37 +02:00
|
|
|
* ExecAppend - retrieve the next tuple from the node
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecEndAppend - shut down the append node
|
1998-07-16 00:16:21 +02:00
|
|
|
* ExecReScanAppend - rescan the append node
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02: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 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02: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 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* ...
|
|
|
|
* /
|
|
|
|
* Append -------+------+------+--- nil
|
|
|
|
* / \ | | |
|
2014-05-06 18:12:18 +02:00
|
|
|
* nil nil ... ... ...
|
1997-09-07 07:04:48 +02:00
|
|
|
* subplans
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-11-12 01:37:02 +01:00
|
|
|
* Append nodes are currently used for unions, and to support
|
|
|
|
* inheritance queries, where several relations need to be scanned.
|
1997-09-07 07:04:48 +02: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 08:22:35 +02:00
|
|
|
*
|
2005-04-24 13:46:21 +02:00
|
|
|
* select name from person
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* generates the plan:
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* |
|
|
|
|
* Append -------+-------+--------+--------+
|
|
|
|
* / \ | | | |
|
|
|
|
* nil nil Scan Scan Scan Scan
|
|
|
|
* | | | |
|
|
|
|
* person employee student student-emp
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1996-10-31 11:12:26 +01:00
|
|
|
|
2000-11-12 01:37:02 +01:00
|
|
|
#include "postgres.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-11-06 07:52:23 +01:00
|
|
|
#include "executor/execdebug.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "executor/nodeAppend.h"
|
2017-07-26 02:37:17 +02:00
|
|
|
#include "miscadmin.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot *ExecAppend(PlanState *pstate);
|
2002-12-05 16:50:39 +01:00
|
|
|
static bool exec_append_initialize_next(AppendState *appendstate);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
2001-05-15 02:33:36 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1999-02-14 00:22:53 +01:00
|
|
|
* exec_append_initialize_next
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2002-12-05 16:50:39 +01:00
|
|
|
* Sets up the append state node for the "next" scan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Returns t iff there is a "next" scan to process.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static bool
|
2002-12-05 16:50:39 +01:00
|
|
|
exec_append_initialize_next(AppendState *appendstate)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int whichplan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get information from the append node
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
whichplan = appendstate->as_whichplan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2009-10-10 03:43:50 +02:00
|
|
|
if (whichplan < 0)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02: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
|
2017-08-16 06:22:32 +02:00
|
|
|
* that we are at the end of the line by returning false
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2009-10-10 03:43:50 +02:00
|
|
|
appendstate->as_whichplan = 0;
|
2017-08-16 06:22:32 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2009-10-10 03:43:50 +02:00
|
|
|
else if (whichplan >= appendstate->as_nplans)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* as above, end the scan if we go beyond the last scan in our list..
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2009-10-10 03:43:50 +02:00
|
|
|
appendstate->as_whichplan = appendstate->as_nplans - 1;
|
2017-08-16 06:22:32 +02:00
|
|
|
return false;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-16 06:22:32 +02:00
|
|
|
return true;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitAppend
|
|
|
|
*
|
2002-12-05 16:50:39 +01:00
|
|
|
* Begin all of the subscans of the append node.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02: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-24 03:36:37 +02:00
|
|
|
* block instead of that of the call to ExecAppend.)
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
AppendState *
|
2006-02-28 05:10:28 +01:00
|
|
|
ExecInitAppend(Append *node, EState *estate, int eflags)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
AppendState *appendstate = makeNode(AppendState);
|
|
|
|
PlanState **appendplanstates;
|
1997-09-08 04:41:22 +02:00
|
|
|
int nplans;
|
|
|
|
int i;
|
2009-10-10 03:43:50 +02:00
|
|
|
ListCell *lc;
|
2000-06-10 07:16:38 +02:00
|
|
|
|
2006-02-28 05:10:28 +01:00
|
|
|
/* check for unsupported flags */
|
|
|
|
Assert(!(eflags & EXEC_FLAG_MARK));
|
|
|
|
|
2017-03-21 14:48:04 +01:00
|
|
|
/*
|
2017-05-17 22:31:56 +02: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 14:48:04 +01:00
|
|
|
*/
|
|
|
|
ExecLockNonLeafAppendTables(node->partitioned_rels, estate);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* Set up empty vector of subplan states
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2004-05-31 01:40:41 +02:00
|
|
|
nplans = list_length(node->appendplans);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
appendplanstates = (PlanState **) palloc0(nplans * sizeof(PlanState *));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* create new AppendState for our append node
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
appendstate->ps.plan = (Plan *) node;
|
|
|
|
appendstate->ps.state = estate;
|
2017-07-17 09:33:49 +02:00
|
|
|
appendstate->ps.ExecProcNode = ExecAppend;
|
2002-12-05 16:50:39 +01:00
|
|
|
appendstate->appendplans = appendplanstates;
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->as_nplans = nplans;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Miscellaneous initialization
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2001-03-22 07:16:21 +01:00
|
|
|
* Append plans don't have expression contexts because they never call
|
|
|
|
* ExecQual or ExecProject.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* append nodes still have Result slots, which hold pointers to tuples, so
|
|
|
|
* we have to initialize them.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecInitResultTupleSlot(estate, &appendstate->ps);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-05-15 02:33:36 +02:00
|
|
|
* call ExecInitNode on each of the plans to be executed and save the
|
2009-10-10 03:43:50 +02:00
|
|
|
* results into the array "appendplans".
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2009-10-10 03:43:50 +02:00
|
|
|
i = 0;
|
|
|
|
foreach(lc, node->appendplans)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2009-10-10 03:43:50 +02:00
|
|
|
Plan *initNode = (Plan *) lfirst(lc);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-02-28 05:10:28 +01:00
|
|
|
appendplanstates[i] = ExecInitNode(initNode, estate, eflags);
|
2009-10-10 03:43:50 +02:00
|
|
|
i++;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2009-10-10 03:43:50 +02:00
|
|
|
* initialize output tuple type
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecAssignResultTypeFromTL(&appendstate->ps);
|
|
|
|
appendstate->ps.ps_ProjInfo = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2009-10-10 03:43:50 +02:00
|
|
|
* initialize to scan first subplan
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2009-10-10 03:43:50 +02:00
|
|
|
appendstate->as_whichplan = 0;
|
2002-12-05 16:50:39 +01:00
|
|
|
exec_append_initialize_next(appendstate);
|
1999-10-31 01:13:30 +02:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
return appendstate;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2004-09-24 03:36:37 +02:00
|
|
|
* ExecAppend
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2005-05-23 00:30:20 +02:00
|
|
|
* Handles iteration over multiple subplans.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot *
|
|
|
|
ExecAppend(PlanState *pstate)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
AppendState *node = castNode(AppendState, pstate);
|
|
|
|
|
2005-05-23 00:30:20 +02:00
|
|
|
for (;;)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-05-23 00:30:20 +02:00
|
|
|
PlanState *subnode;
|
|
|
|
TupleTableSlot *result;
|
|
|
|
|
2017-07-26 02:37:17 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-05-23 00:30:20 +02:00
|
|
|
* figure out which subplan we are currently processing
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-05-23 00:30:20 +02:00
|
|
|
subnode = node->appendplans[node->as_whichplan];
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-05-23 00:30:20 +02:00
|
|
|
* get a tuple from the subplan
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-05-23 00:30:20 +02:00
|
|
|
result = ExecProcNode(subnode);
|
|
|
|
|
|
|
|
if (!TupIsNull(result))
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02: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-10 03:43:50 +02:00
|
|
|
* ExecInitAppend; there's no need for it.
|
2005-05-23 00:30:20 +02:00
|
|
|
*/
|
|
|
|
return result;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02: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 07:04:48 +02:00
|
|
|
*/
|
2005-05-23 00:30:20 +02:00
|
|
|
if (ScanDirectionIsForward(node->ps.state->es_direction))
|
|
|
|
node->as_whichplan++;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2005-05-23 00:30:20 +02: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 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecEndAppend
|
|
|
|
*
|
|
|
|
* Shuts down the subscans of the append node.
|
|
|
|
*
|
|
|
|
* Returns nothing of interest.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndAppend(AppendState *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-12-05 16:50:39 +01:00
|
|
|
PlanState **appendplans;
|
1997-09-08 04:41:22 +02:00
|
|
|
int nplans;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get information from the node
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendplans = node->appendplans;
|
2002-12-05 16:50:39 +01:00
|
|
|
nplans = node->as_nplans;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2009-10-10 03:43:50 +02:00
|
|
|
* shut down each of the subscans
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < nplans; i++)
|
2009-10-10 03:43:50 +02:00
|
|
|
ExecEndNode(appendplans[i]);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-11-12 01:37:02 +01:00
|
|
|
|
1998-07-16 00:16:21 +02:00
|
|
|
void
|
2010-07-12 19:01:06 +02:00
|
|
|
ExecReScanAppend(AppendState *node)
|
1998-07-16 00:16:21 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
1998-07-16 00:16:21 +02:00
|
|
|
|
2009-10-10 03:43:50 +02:00
|
|
|
for (i = 0; i < node->as_nplans; i++)
|
1998-07-16 00:16:21 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
PlanState *subnode = node->appendplans[i];
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-05-08 21:47:02 +02:00
|
|
|
/*
|
|
|
|
* ExecReScan doesn't know about my subplans, so I have to do
|
|
|
|
* changed-parameter signaling myself.
|
|
|
|
*/
|
2003-02-09 01:30:41 +01:00
|
|
|
if (node->ps.chgParam != NULL)
|
|
|
|
UpdateChangedParamSet(subnode, node->ps.chgParam);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2001-05-08 21:47:02 +02:00
|
|
|
/*
|
2006-02-05 03:59:17 +01:00
|
|
|
* If chgParam of subnode is not null then plan will be re-scanned by
|
2010-07-12 19:01:06 +02:00
|
|
|
* first ExecProcNode.
|
2001-05-08 21:47:02 +02:00
|
|
|
*/
|
2010-07-12 19:01:06 +02:00
|
|
|
if (subnode->chgParam == NULL)
|
|
|
|
ExecReScan(subnode);
|
1998-07-16 00:16:21 +02:00
|
|
|
}
|
2009-10-10 03:43:50 +02:00
|
|
|
node->as_whichplan = 0;
|
1998-07-16 00:16:21 +02:00
|
|
|
exec_append_initialize_next(node);
|
|
|
|
}
|