PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeUnique.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeUnique.c
4  * Routines to handle unique'ing of queries where appropriate
5  *
6  * Unique is a very simple node type that just filters out duplicate
7  * tuples from a stream of sorted tuples from its subplan. It's essentially
8  * a dumbed-down form of Group: the duplicate-removal functionality is
9  * identical. However, Unique doesn't do projection nor qual checking,
10  * so it's marginally more efficient for cases where neither is needed.
11  * (It's debatable whether the savings justifies carrying two plan node
12  * types, though.)
13  *
14  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  *
18  * IDENTIFICATION
19  * src/backend/executor/nodeUnique.c
20  *
21  *-------------------------------------------------------------------------
22  */
23 /*
24  * INTERFACE ROUTINES
25  * ExecUnique - generate a unique'd temporary relation
26  * ExecInitUnique - initialize node and subnodes
27  * ExecEndUnique - shutdown node and subnodes
28  *
29  * NOTES
30  * Assumes tuples returned from subplan arrive in
31  * sorted order.
32  */
33 
34 #include "postgres.h"
35 
36 #include "executor/executor.h"
37 #include "executor/nodeUnique.h"
38 #include "utils/memutils.h"
39 
40 
41 /* ----------------------------------------------------------------
42  * ExecUnique
43  * ----------------------------------------------------------------
44  */
45 TupleTableSlot * /* return: a tuple or NULL */
47 {
48  Unique *plannode = (Unique *) node->ps.plan;
49  TupleTableSlot *resultTupleSlot;
50  TupleTableSlot *slot;
52 
53  /*
54  * get information from the node
55  */
56  outerPlan = outerPlanState(node);
57  resultTupleSlot = node->ps.ps_ResultTupleSlot;
58 
59  /*
60  * now loop, returning only non-duplicate tuples. We assume that the
61  * tuples arrive in sorted order so we can detect duplicates easily. The
62  * first tuple of each group is returned.
63  */
64  for (;;)
65  {
66  /*
67  * fetch a tuple from the outer subplan
68  */
69  slot = ExecProcNode(outerPlan);
70  if (TupIsNull(slot))
71  {
72  /* end of subplan, so we're done */
73  ExecClearTuple(resultTupleSlot);
74  return NULL;
75  }
76 
77  /*
78  * Always return the first tuple from the subplan.
79  */
80  if (TupIsNull(resultTupleSlot))
81  break;
82 
83  /*
84  * Else test if the new tuple and the previously returned tuple match.
85  * If so then we loop back and fetch another new tuple from the
86  * subplan.
87  */
88  if (!execTuplesMatch(slot, resultTupleSlot,
89  plannode->numCols, plannode->uniqColIdx,
90  node->eqfunctions,
91  node->tempContext))
92  break;
93  }
94 
95  /*
96  * We have a new tuple different from the previous saved tuple (if any).
97  * Save it and return it. We must copy it because the source subplan
98  * won't guarantee that this source tuple is still accessible after
99  * fetching the next source tuple.
100  */
101  return ExecCopySlot(resultTupleSlot, slot);
102 }
103 
104 /* ----------------------------------------------------------------
105  * ExecInitUnique
106  *
107  * This initializes the unique node state structures and
108  * the node's subplan.
109  * ----------------------------------------------------------------
110  */
111 UniqueState *
112 ExecInitUnique(Unique *node, EState *estate, int eflags)
113 {
114  UniqueState *uniquestate;
115 
116  /* check for unsupported flags */
117  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
118 
119  /*
120  * create state structure
121  */
122  uniquestate = makeNode(UniqueState);
123  uniquestate->ps.plan = (Plan *) node;
124  uniquestate->ps.state = estate;
125 
126  /*
127  * Miscellaneous initialization
128  *
129  * Unique nodes have no ExprContext initialization because they never call
130  * ExecQual or ExecProject. But they do need a per-tuple memory context
131  * anyway for calling execTuplesMatch.
132  */
133  uniquestate->tempContext =
135  "Unique",
137 
138  /*
139  * Tuple table initialization
140  */
141  ExecInitResultTupleSlot(estate, &uniquestate->ps);
142 
143  /*
144  * then initialize outer plan
145  */
146  outerPlanState(uniquestate) = ExecInitNode(outerPlan(node), estate, eflags);
147 
148  /*
149  * unique nodes do no projections, so initialize projection info for this
150  * node appropriately
151  */
152  ExecAssignResultTypeFromTL(&uniquestate->ps);
153  uniquestate->ps.ps_ProjInfo = NULL;
154 
155  /*
156  * Precompute fmgr lookup data for inner loop
157  */
158  uniquestate->eqfunctions =
160  node->uniqOperators);
161 
162  return uniquestate;
163 }
164 
165 /* ----------------------------------------------------------------
166  * ExecEndUnique
167  *
168  * This shuts down the subplan and frees resources allocated
169  * to this node.
170  * ----------------------------------------------------------------
171  */
172 void
174 {
175  /* clean up tuple table */
177 
179 
181 }
182 
183 
184 void
186 {
187  /* must clear result tuple so first input tuple is returned */
189 
190  /*
191  * if chgParam of subnode is not null then plan will be re-scanned by
192  * first ExecProcNode.
193  */
194  if (node->ps.lefttree->chgParam == NULL)
195  ExecReScan(node->ps.lefttree);
196 }
int numCols
Definition: plannodes.h:771
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:380
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1078
void ExecEndUnique(UniqueState *node)
Definition: nodeUnique.c:173
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
void ExecReScan(PlanState *node)
Definition: execAmi.c:73
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
bool execTuplesMatch(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, MemoryContext evalContext)
Definition: execGrouping.c:69
EState * state
Definition: execnodes.h:1048
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:429
struct PlanState * lefttree
Definition: execnodes.h:1062
Oid * uniqOperators
Definition: plannodes.h:773
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1076
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1089
PlanState ps
Definition: execnodes.h:1989
void ExecReScanUnique(UniqueState *node)
Definition: nodeUnique.c:185
#define TupIsNull(slot)
Definition: tuptable.h:138
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
Bitmapset * chgParam
Definition: execnodes.h:1071
#define outerPlan(node)
Definition: plannodes.h:159
AttrNumber * uniqColIdx
Definition: plannodes.h:772
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
UniqueState * ExecInitUnique(Unique *node, EState *estate, int eflags)
Definition: nodeUnique.c:112
FmgrInfo * eqfunctions
Definition: execnodes.h:1990
Plan * plan
Definition: execnodes.h:1046
MemoryContext tempContext
Definition: execnodes.h:1991
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
#define EXEC_FLAG_MARK
Definition: executor.h:61
TupleTableSlot * ExecUnique(UniqueState *node)
Definition: nodeUnique.c:46
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
Definition: execGrouping.c:204
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137