PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeUnique.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/nodeUnique.h"
#include "miscadmin.h"
#include "utils/memutils.h"
Include dependency graph for nodeUnique.c:

Go to the source code of this file.

Functions

static TupleTableSlotExecUnique (PlanState *pstate)
 
UniqueStateExecInitUnique (Unique *node, EState *estate, int eflags)
 
void ExecEndUnique (UniqueState *node)
 
void ExecReScanUnique (UniqueState *node)
 

Function Documentation

void ExecEndUnique ( UniqueState node)

Definition at line 178 of file nodeUnique.c.

References ExecClearTuple(), ExecEndNode(), MemoryContextDelete(), outerPlanState, UniqueState::ps, PlanState::ps_ResultTupleSlot, and UniqueState::tempContext.

Referenced by ExecEndNode().

179 {
180  /* clean up tuple table */
182 
184 
186 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define outerPlanState(node)
Definition: execnodes.h:893
PlanState ps
Definition: execnodes.h:1913
MemoryContext tempContext
Definition: execnodes.h:1915
UniqueState* ExecInitUnique ( Unique node,
EState estate,
int  eflags 
)

Definition at line 116 of file nodeUnique.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentMemoryContext, UniqueState::eqfunctions, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignResultTypeFromTL(), ExecInitNode(), ExecInitResultTupleSlot(), PlanState::ExecProcNode, execTuplesMatchPrepare(), ExecUnique(), makeNode, Unique::numCols, outerPlan, outerPlanState, PlanState::plan, UniqueState::ps, PlanState::ps_ProjInfo, PlanState::state, UniqueState::tempContext, and Unique::uniqOperators.

Referenced by ExecInitNode().

117 {
118  UniqueState *uniquestate;
119 
120  /* check for unsupported flags */
121  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
122 
123  /*
124  * create state structure
125  */
126  uniquestate = makeNode(UniqueState);
127  uniquestate->ps.plan = (Plan *) node;
128  uniquestate->ps.state = estate;
129  uniquestate->ps.ExecProcNode = ExecUnique;
130 
131  /*
132  * Miscellaneous initialization
133  *
134  * Unique nodes have no ExprContext initialization because they never call
135  * ExecQual or ExecProject. But they do need a per-tuple memory context
136  * anyway for calling execTuplesMatch.
137  */
138  uniquestate->tempContext =
140  "Unique",
142 
143  /*
144  * Tuple table initialization
145  */
146  ExecInitResultTupleSlot(estate, &uniquestate->ps);
147 
148  /*
149  * then initialize outer plan
150  */
151  outerPlanState(uniquestate) = ExecInitNode(outerPlan(node), estate, eflags);
152 
153  /*
154  * unique nodes do no projections, so initialize projection info for this
155  * node appropriately
156  */
157  ExecAssignResultTypeFromTL(&uniquestate->ps);
158  uniquestate->ps.ps_ProjInfo = NULL;
159 
160  /*
161  * Precompute fmgr lookup data for inner loop
162  */
163  uniquestate->eqfunctions =
165  node->uniqOperators);
166 
167  return uniquestate;
168 }
int numCols
Definition: plannodes.h:821
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:882
EState * state
Definition: execnodes.h:849
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
Oid * uniqOperators
Definition: plannodes.h:823
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:893
PlanState ps
Definition: execnodes.h:1913
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define outerPlan(node)
Definition: plannodes.h:174
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
FmgrInfo * eqfunctions
Definition: execnodes.h:1914
Plan * plan
Definition: execnodes.h:847
MemoryContext tempContext
Definition: execnodes.h:1915
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:681
#define EXEC_FLAG_MARK
Definition: executor.h:61
static TupleTableSlot * ExecUnique(PlanState *pstate)
Definition: nodeUnique.c:47
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
Definition: execGrouping.c:204
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
void ExecReScanUnique ( UniqueState node)

Definition at line 190 of file nodeUnique.c.

References PlanState::chgParam, ExecClearTuple(), ExecReScan(), PlanState::lefttree, UniqueState::ps, and PlanState::ps_ResultTupleSlot.

Referenced by ExecReScan().

191 {
192  /* must clear result tuple so first input tuple is returned */
194 
195  /*
196  * if chgParam of subnode is not null then plan will be re-scanned by
197  * first ExecProcNode.
198  */
199  if (node->ps.lefttree->chgParam == NULL)
200  ExecReScan(node->ps.lefttree);
201 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
struct PlanState * lefttree
Definition: execnodes.h:866
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
PlanState ps
Definition: execnodes.h:1913
Bitmapset * chgParam
Definition: execnodes.h:875
static TupleTableSlot* ExecUnique ( PlanState pstate)
static

Definition at line 47 of file nodeUnique.c.

References castNode, CHECK_FOR_INTERRUPTS, UniqueState::eqfunctions, ExecClearTuple(), ExecCopySlot(), ExecProcNode(), execTuplesMatch(), Unique::numCols, outerPlan, outerPlanState, PlanState::plan, UniqueState::ps, PlanState::ps_ResultTupleSlot, UniqueState::tempContext, TupIsNull, and Unique::uniqColIdx.

Referenced by ExecInitUnique().

48 {
49  UniqueState *node = castNode(UniqueState, pstate);
50  Unique *plannode = (Unique *) node->ps.plan;
51  TupleTableSlot *resultTupleSlot;
52  TupleTableSlot *slot;
54 
56 
57  /*
58  * get information from the node
59  */
60  outerPlan = outerPlanState(node);
61  resultTupleSlot = node->ps.ps_ResultTupleSlot;
62 
63  /*
64  * now loop, returning only non-duplicate tuples. We assume that the
65  * tuples arrive in sorted order so we can detect duplicates easily. The
66  * first tuple of each group is returned.
67  */
68  for (;;)
69  {
70  /*
71  * fetch a tuple from the outer subplan
72  */
73  slot = ExecProcNode(outerPlan);
74  if (TupIsNull(slot))
75  {
76  /* end of subplan, so we're done */
77  ExecClearTuple(resultTupleSlot);
78  return NULL;
79  }
80 
81  /*
82  * Always return the first tuple from the subplan.
83  */
84  if (TupIsNull(resultTupleSlot))
85  break;
86 
87  /*
88  * Else test if the new tuple and the previously returned tuple match.
89  * If so then we loop back and fetch another new tuple from the
90  * subplan.
91  */
92  if (!execTuplesMatch(slot, resultTupleSlot,
93  plannode->numCols, plannode->uniqColIdx,
94  node->eqfunctions,
95  node->tempContext))
96  break;
97  }
98 
99  /*
100  * We have a new tuple different from the previous saved tuple (if any).
101  * Save it and return it. We must copy it because the source subplan
102  * won't guarantee that this source tuple is still accessible after
103  * fetching the next source tuple.
104  */
105  return ExecCopySlot(resultTupleSlot, slot);
106 }
int numCols
Definition: plannodes.h:821
#define castNode(_type_, nodeptr)
Definition: nodes.h:579
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
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define outerPlanState(node)
Definition: execnodes.h:893
PlanState ps
Definition: execnodes.h:1913
#define TupIsNull(slot)
Definition: tuptable.h:138
#define outerPlan(node)
Definition: plannodes.h:174
AttrNumber * uniqColIdx
Definition: plannodes.h:822
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:246
FmgrInfo * eqfunctions
Definition: execnodes.h:1914
Plan * plan
Definition: execnodes.h:847
MemoryContext tempContext
Definition: execnodes.h:1915
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
Definition: execTuples.c:795
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98