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.)
|
|
|
|
*
|
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/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);
|
2002-12-05 16:50:39 +01:00
|
|
|
Unique *plannode = (Unique *) node->ps.plan;
|
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
|
|
|
*/
|
2005-03-16 22:38:10 +01:00
|
|
|
if (!execTuplesMatch(slot, resultTupleSlot,
|
2002-12-05 16:50:39 +01:00
|
|
|
plannode->numCols, plannode->uniqColIdx,
|
|
|
|
node->eqfunctions,
|
|
|
|
node->tempContext))
|
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
|
|
|
/*
|
|
|
|
* Miscellaneous initialization
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Unique nodes have no ExprContext initialization because they never call
|
|
|
|
* ExecQual or ExecProject. But they do need a per-tuple memory context
|
|
|
|
* anyway for calling execTuplesMatch.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-07-12 04:37:39 +02:00
|
|
|
uniquestate->tempContext =
|
|
|
|
AllocSetContextCreate(CurrentMemoryContext,
|
|
|
|
"Unique",
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 23:50:38 +02:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Tuple table initialization
|
|
|
|
*/
|
2002-12-05 16:50:39 +01:00
|
|
|
ExecInitResultTupleSlot(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
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* unique nodes do no projections, so initialize projection info for this
|
|
|
|
* node appropriately
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2005-11-23 21:27:58 +01:00
|
|
|
ExecAssignResultTypeFromTL(&uniquestate->ps);
|
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
|
|
|
*/
|
2000-01-27 19:11:50 +01:00
|
|
|
uniquestate->eqfunctions =
|
2007-01-10 19:06:05 +01:00
|
|
|
execTuplesMatchPrepare(node->numCols,
|
|
|
|
node->uniqOperators);
|
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);
|
|
|
|
|
|
|
|
MemoryContextDelete(node->tempContext);
|
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
|
|
|
}
|