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
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
2001-01-24 20:43:33 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.39 2001/01/24 19:42:54 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/* INTERFACE ROUTINES
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitAppend - initialize the append node
|
|
|
|
* ExecProcAppend - retrieve the next tuple from the node
|
|
|
|
* 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
|
|
|
|
* / \ | | |
|
|
|
|
* nil nil ... ... ...
|
|
|
|
* 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
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* retrieve (e.name) from e in 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 "access/heapam.h"
|
|
|
|
#include "executor/execdebug.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "executor/nodeAppend.h"
|
1999-07-16 05:14:30 +02:00
|
|
|
#include "parser/parsetree.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-08 22:59:27 +02:00
|
|
|
static bool exec_append_initialize_next(Append *node);
|
1997-08-19 23:40:56 +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
|
|
|
*
|
|
|
|
* Sets up the append node state (i.e. the append state node)
|
|
|
|
* for the "next" scan.
|
|
|
|
*
|
|
|
|
* 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
|
1997-09-08 22:59:27 +02:00
|
|
|
exec_append_initialize_next(Append *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
EState *estate;
|
1998-07-15 16:54:39 +02:00
|
|
|
AppendState *appendstate;
|
1997-09-08 04:41:22 +02:00
|
|
|
int whichplan;
|
|
|
|
int nplans;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* get information from the append node
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
estate = node->plan.state;
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate = node->appendstate;
|
|
|
|
whichplan = appendstate->as_whichplan;
|
|
|
|
nplans = appendstate->as_nplans;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (whichplan < 0)
|
|
|
|
{
|
|
|
|
/* ----------------
|
|
|
|
* 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 ExecProcAppend that we are
|
|
|
|
* at the end of the line by returning FALSE
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->as_whichplan = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (whichplan >= nplans)
|
|
|
|
{
|
|
|
|
/* ----------------
|
|
|
|
* as above, end the scan if we go beyond
|
|
|
|
* the last scan in our list..
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->as_whichplan = nplans - 1;
|
1997-09-07 07:04:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ----------------
|
|
|
|
* initialize the scan
|
2000-10-05 21:11:39 +02:00
|
|
|
*
|
2000-11-12 01:37:02 +01:00
|
|
|
* If we are controlling the target relation, select the proper
|
|
|
|
* active ResultRelInfo and junk filter for this target.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-12 01:37:02 +01:00
|
|
|
if (node->isTarget)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-11-12 01:37:02 +01:00
|
|
|
Assert(whichplan < estate->es_num_result_relations);
|
|
|
|
estate->es_result_relation_info =
|
|
|
|
estate->es_result_relations + whichplan;
|
|
|
|
estate->es_junkFilter =
|
|
|
|
estate->es_result_relation_info->ri_junkFilter;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-02-26 05:46:47 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return TRUE;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitAppend
|
|
|
|
*
|
|
|
|
* Begins all of the subscans of the append node, storing the
|
|
|
|
* scan structures in the 'initialized' vector of the append-state
|
|
|
|
* structure.
|
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
|
|
|
|
* block instead of that of the call to ExecProcAppend.)
|
|
|
|
*
|
|
|
|
* Returns the scan result of the first scan.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
bool
|
1997-09-08 23:56:23 +02:00
|
|
|
ExecInitAppend(Append *node, EState *estate, Plan *parent)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-07-15 16:54:39 +02:00
|
|
|
AppendState *appendstate;
|
1997-09-08 04:41:22 +02:00
|
|
|
int nplans;
|
1998-07-15 16:54:39 +02:00
|
|
|
List *appendplans;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool *initialized;
|
|
|
|
int i;
|
|
|
|
Plan *initNode;
|
2000-06-10 07:16:38 +02:00
|
|
|
|
|
|
|
CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* assign execution state to node and get information
|
|
|
|
* for append state
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
node->plan.state = estate;
|
|
|
|
|
1998-07-15 16:54:39 +02:00
|
|
|
appendplans = node->appendplans;
|
|
|
|
nplans = length(appendplans);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
initialized = (bool *) palloc(nplans * sizeof(bool));
|
2000-06-10 07:16:38 +02:00
|
|
|
MemSet(initialized, 0, nplans * sizeof(bool));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* create new AppendState for our append node
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate = makeNode(AppendState);
|
|
|
|
appendstate->as_whichplan = 0;
|
|
|
|
appendstate->as_nplans = nplans;
|
|
|
|
appendstate->as_initialized = initialized;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-07-15 16:54:39 +02:00
|
|
|
node->appendstate = appendstate;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
2000-07-12 04:37:39 +02:00
|
|
|
* Miscellaneous initialization
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* Append plans don't have expression contexts because they
|
2000-07-12 04:37:39 +02:00
|
|
|
* never call ExecQual or ExecProject.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#define APPEND_NSLOTS 1
|
1997-09-07 07:04:48 +02:00
|
|
|
/* ----------------
|
|
|
|
* append nodes still have Result slots, which hold pointers
|
2000-06-10 07:16:38 +02:00
|
|
|
* to tuples, so we have to initialize them.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
ExecInitResultTupleSlot(estate, &appendstate->cstate);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* call ExecInitNode on each of the plans in our list
|
|
|
|
* and save the results into the array "initialized"
|
|
|
|
* ----------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < nplans; i++)
|
|
|
|
{
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->as_whichplan = i;
|
1997-09-07 07:04:48 +02:00
|
|
|
exec_append_initialize_next(node);
|
|
|
|
|
1998-07-15 16:54:39 +02:00
|
|
|
initNode = (Plan *) nth(i, appendplans);
|
1997-09-07 07:04:48 +02:00
|
|
|
initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
2000-11-12 01:37:02 +01:00
|
|
|
* initialize tuple type
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-11-12 01:37:02 +01:00
|
|
|
ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->cstate.cs_ProjInfo = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* return the result from the first subplan's initialization
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate->as_whichplan = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
exec_append_initialize_next(node);
|
1999-10-31 01:13:30 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return TRUE;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-09-08 22:59:27 +02:00
|
|
|
ExecCountSlotsAppend(Append *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
List *plan;
|
|
|
|
int nSlots = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-12 01:37:02 +01:00
|
|
|
foreach(plan, node->appendplans)
|
1997-09-07 07:04:48 +02:00
|
|
|
nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
|
|
|
|
return nSlots + APPEND_NSLOTS;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecProcAppend
|
|
|
|
*
|
|
|
|
* Handles the iteration over the multiple scans.
|
|
|
|
*
|
2000-07-12 04:37:39 +02:00
|
|
|
* NOTE: Can't call this ExecAppend, that name is used in execMain.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
TupleTableSlot *
|
1997-09-08 22:59:27 +02:00
|
|
|
ExecProcAppend(Append *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
EState *estate;
|
1998-07-15 16:54:39 +02:00
|
|
|
AppendState *appendstate;
|
1997-09-08 04:41:22 +02:00
|
|
|
int whichplan;
|
1998-07-15 16:54:39 +02:00
|
|
|
List *appendplans;
|
1997-09-08 04:41:22 +02:00
|
|
|
Plan *subnode;
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleTableSlot *result;
|
|
|
|
TupleTableSlot *result_slot;
|
1997-09-08 04:41:22 +02:00
|
|
|
ScanDirection direction;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* get information from the node
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate = node->appendstate;
|
1997-09-07 07:04:48 +02:00
|
|
|
estate = node->plan.state;
|
|
|
|
direction = estate->es_direction;
|
1998-07-15 16:54:39 +02:00
|
|
|
appendplans = node->appendplans;
|
|
|
|
whichplan = appendstate->as_whichplan;
|
|
|
|
result_slot = appendstate->cstate.cs_ResultTupleSlot;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* figure out which subplan we are currently processing
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
subnode = (Plan *) nth(whichplan, appendplans);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (subnode == NULL)
|
|
|
|
elog(DEBUG, "ExecProcAppend: subnode is NULL");
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* get a tuple from the subplan
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
result = ExecProcNode(subnode, (Plan *) node);
|
|
|
|
|
|
|
|
if (!TupIsNull(result))
|
|
|
|
{
|
|
|
|
/* ----------------
|
|
|
|
* if the subplan gave us something then place a copy of
|
1999-09-24 02:25:33 +02:00
|
|
|
* whatever we get into our result slot and return it.
|
|
|
|
*
|
|
|
|
* Note we rely on the subplan to retain ownership of the
|
|
|
|
* tuple for as long as we need it --- we don't copy it.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1999-09-24 02:25:33 +02:00
|
|
|
return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* ----------------
|
|
|
|
* .. go on to the "next" subplan in the appropriate
|
|
|
|
* direction and try processing again (recursively)
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (ScanDirectionIsForward(direction))
|
2000-11-12 01:37:02 +01:00
|
|
|
appendstate->as_whichplan++;
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
2000-11-12 01:37:02 +01:00
|
|
|
appendstate->as_whichplan--;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* return something from next node or an empty slot
|
2000-11-12 01:37:02 +01:00
|
|
|
* if all of our subplans have been exhausted.
|
1997-09-07 07:04:48 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (exec_append_initialize_next(node))
|
|
|
|
{
|
|
|
|
ExecSetSlotDescriptorIsNew(result_slot, true);
|
1997-12-27 07:41:41 +01:00
|
|
|
return ExecProcAppend(node);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return ExecClearTuple(result_slot);
|
|
|
|
}
|
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
|
1997-09-08 22:59:27 +02:00
|
|
|
ExecEndAppend(Append *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-10 07:16:38 +02:00
|
|
|
EState *estate;
|
1998-07-15 16:54:39 +02:00
|
|
|
AppendState *appendstate;
|
1997-09-08 04:41:22 +02:00
|
|
|
int nplans;
|
1998-07-15 16:54:39 +02:00
|
|
|
List *appendplans;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool *initialized;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* get information from the node
|
|
|
|
* ----------------
|
|
|
|
*/
|
1998-07-15 16:54:39 +02:00
|
|
|
appendstate = node->appendstate;
|
2000-06-10 07:16:38 +02:00
|
|
|
estate = node->plan.state;
|
1998-07-15 16:54:39 +02:00
|
|
|
appendplans = node->appendplans;
|
|
|
|
nplans = appendstate->as_nplans;
|
|
|
|
initialized = appendstate->as_initialized;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* shut down each of the subscans
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nplans; i++)
|
|
|
|
{
|
2000-06-10 07:16:38 +02:00
|
|
|
if (initialized[i])
|
1998-07-15 16:54:39 +02:00
|
|
|
ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
|
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
|
|
|
|
ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
|
|
|
|
{
|
|
|
|
AppendState *appendstate = node->appendstate;
|
1998-09-01 06:40:42 +02:00
|
|
|
int nplans = length(node->appendplans);
|
|
|
|
int i;
|
1998-07-16 00:16:21 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nplans; i++)
|
|
|
|
{
|
|
|
|
Plan *rescanNode;
|
|
|
|
|
|
|
|
appendstate->as_whichplan = i;
|
|
|
|
rescanNode = (Plan *) nth(i, node->appendplans);
|
|
|
|
if (rescanNode->chgParam == NULL)
|
|
|
|
{
|
|
|
|
exec_append_initialize_next(node);
|
1998-09-01 06:40:42 +02:00
|
|
|
ExecReScan((Plan *) rescanNode, exprCtxt, (Plan *) node);
|
1998-07-16 00:16:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
appendstate->as_whichplan = 0;
|
|
|
|
exec_append_initialize_next(node);
|
|
|
|
}
|