1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* nodeUnique.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to handle unique'ing of queries where appropriate
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2008-08-05 23:28:29 +02:00
|
|
|
* Unique is a very simple node type that just filters out duplicate
|
2014-05-06 18:12:18 +02:00
|
|
|
* tuples from a stream of sorted tuples from its subplan. It's essentially
|
2008-08-05 23:28:29 +02:00
|
|
|
* a dumbed-down form of Group: the duplicate-removal functionality is
|
|
|
|
* identical. However, Unique doesn't do projection nor qual checking,
|
|
|
|
* so it's marginally more efficient for cases where neither is needed.
|
|
|
|
* (It's debatable whether the savings justifies carrying two plan node
|
|
|
|
* types, though.)
|
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, 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/nodeUnique.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* INTERFACE ROUTINES
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecUnique - generate a unique'd temporary relation
|
2008-08-05 23:28:29 +02:00
|
|
|
* ExecInitUnique - initialize node and subnodes
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecEndUnique - shutdown node and subnodes
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* Assumes tuples returned from subplan arrive in
|
|
|
|
* sorted order.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-01-10 21:19:49 +01:00
|
|
|
|
1996-10-31 11:12:26 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/nodeUnique.h"
|
2017-07-26 02:37:17 +02:00
|
|
|
#include "miscadmin.h"
|
2005-05-06 19:24:55 +02:00
|
|
|
#include "utils/memutils.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-01-11 00:54:24 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecUnique
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2017-07-17 09:33:49 +02:00
|
|
|
static TupleTableSlot * /* return: a tuple or NULL */
|
|
|
|
ExecUnique(PlanState *pstate)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2017-07-17 09:33:49 +02:00
|
|
|
UniqueState *node = castNode(UniqueState, pstate);
|
2018-02-16 06:55:31 +01:00
|
|
|
ExprContext *econtext = node->ps.ps_ExprContext;
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleTableSlot *resultTupleSlot;
|
|
|
|
TupleTableSlot *slot;
|
2002-12-05 16:50:39 +01:00
|
|
|
PlanState *outerPlan;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2017-07-26 02:37:17 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* get information from the node
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
outerPlan = outerPlanState(node);
|
|
|
|
resultTupleSlot = node->ps.ps_ResultTupleSlot;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* now loop, returning only non-duplicate tuples. We assume that the
|
2009-06-11 16:49:15 +02:00
|
|
|
* tuples arrive in sorted order so we can detect duplicates easily. The
|
|
|
|
* first tuple of each group is returned.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* fetch a tuple from the outer subplan
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
slot = ExecProcNode(outerPlan);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (TupIsNull(slot))
|
2003-02-02 20:08:57 +01:00
|
|
|
{
|
2008-08-05 23:28:29 +02:00
|
|
|
/* end of subplan, so we're done */
|
2005-03-16 22:38:10 +01:00
|
|
|
ExecClearTuple(resultTupleSlot);
|
1997-09-07 07:04:48 +02:00
|
|
|
return NULL;
|
2003-02-02 20:08:57 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2008-08-05 23:28:29 +02:00
|
|
|
* Always return the first tuple from the subplan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
if (TupIsNull(resultTupleSlot))
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Else test if the new tuple and the previously returned tuple match.
|
|
|
|
* If so then we loop back and fetch another new tuple from the
|
|
|
|
* subplan.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2018-02-16 06:55:31 +01:00
|
|
|
econtext->ecxt_innertuple = slot;
|
|
|
|
econtext->ecxt_outertuple = resultTupleSlot;
|
|
|
|
if (!ExecQualAndReset(node->eqfunction, econtext))
|
2000-01-27 19:11:50 +01:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We have a new tuple different from the previous saved tuple (if any).
|
|
|
|
* Save it and return it. We must copy it because the source subplan
|
|
|
|
* won't guarantee that this source tuple is still accessible after
|
|
|
|
* fetching the next source tuple.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
return ExecCopySlot(resultTupleSlot, slot);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecInitUnique
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This initializes the unique node state structures and
|
|
|
|
* the node's subplan.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
UniqueState *
|
2006-02-28 05:10:28 +01:00
|
|
|
ExecInitUnique(Unique *node, EState *estate, int eflags)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
UniqueState *uniquestate;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2006-02-28 05:10:28 +01:00
|
|
|
/* check for unsupported flags */
|
2008-08-05 23:28:29 +02:00
|
|
|
Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
|
2006-02-28 05:10:28 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-12-05 16:50:39 +01:00
|
|
|
* create state structure
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
uniquestate = makeNode(UniqueState);
|
2002-12-05 16:50:39 +01:00
|
|
|
uniquestate->ps.plan = (Plan *) node;
|
|
|
|
uniquestate->ps.state = estate;
|
2017-07-17 09:33:49 +02:00
|
|
|
uniquestate->ps.ExecProcNode = ExecUnique;
|
2002-12-05 16:50:39 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2018-02-16 06:55:31 +01:00
|
|
|
* create expression context
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2018-02-16 06:55:31 +01:00
|
|
|
ExecAssignExprContext(estate, &uniquestate->ps);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* then initialize outer plan
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2006-02-28 05:10:28 +01:00
|
|
|
outerPlanState(uniquestate) = ExecInitNode(outerPlan(node), estate, eflags);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2018-02-17 06:17:38 +01:00
|
|
|
* Initialize result slot and type. Unique nodes do no projections, so
|
|
|
|
* initialize projection info for this node appropriately.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
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
|
|
|
ExecInitResultTupleSlotTL(&uniquestate->ps, &TTSOpsMinimalTuple);
|
2002-12-05 16:50:39 +01:00
|
|
|
uniquestate->ps.ps_ProjInfo = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-01-27 19:11:50 +01:00
|
|
|
/*
|
|
|
|
* Precompute fmgr lookup data for inner loop
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2018-02-16 06:55:31 +01:00
|
|
|
uniquestate->eqfunction =
|
|
|
|
execTuplesMatchPrepare(ExecGetResultType(outerPlanState(uniquestate)),
|
|
|
|
node->numCols,
|
|
|
|
node->uniqColIdx,
|
|
|
|
node->uniqOperators,
|
2019-03-22 12:09:32 +01:00
|
|
|
node->uniqCollations,
|
2018-02-16 06:55:31 +01:00
|
|
|
&uniquestate->ps);
|
2000-01-27 19:11:50 +01:00
|
|
|
|
2002-12-05 16:50:39 +01:00
|
|
|
return uniquestate;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ExecEndUnique
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This shuts down the subplan and frees resources allocated
|
|
|
|
* to this node.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecEndUnique(UniqueState *node)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-01-27 19:11:50 +01:00
|
|
|
/* clean up tuple table */
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecClearTuple(node->ps.ps_ResultTupleSlot);
|
|
|
|
|
2018-02-16 06:55:31 +01:00
|
|
|
ExecFreeExprContext(&node->ps);
|
2002-12-15 17:17:59 +01:00
|
|
|
|
|
|
|
ExecEndNode(outerPlanState(node));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-02-23 07:28:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-07-12 19:01:06 +02:00
|
|
|
ExecReScanUnique(UniqueState *node)
|
1998-02-23 07:28:16 +01:00
|
|
|
{
|
2005-03-16 22:38:10 +01:00
|
|
|
/* must clear result tuple so first input tuple is returned */
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecClearTuple(node->ps.ps_ResultTupleSlot);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if chgParam of subnode is not null then plan will be re-scanned by
|
|
|
|
* first ExecProcNode.
|
1998-02-23 07:28:16 +01:00
|
|
|
*/
|
2010-07-12 19:01:06 +02:00
|
|
|
if (node->ps.lefttree->chgParam == NULL)
|
|
|
|
ExecReScan(node->ps.lefttree);
|
1998-02-23 07:28:16 +01:00
|
|
|
}
|