PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeNestloop.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeNestloop.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

NestLoopStateExecInitNestLoop (NestLoop *node, EState *estate, int eflags)
 
TupleTableSlotExecNestLoop (NestLoopState *node)
 
void ExecEndNestLoop (NestLoopState *node)
 
void ExecReScanNestLoop (NestLoopState *node)
 

Function Documentation

void ExecEndNestLoop ( NestLoopState node)

Definition at line 358 of file nodeNestloop.c.

References ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), innerPlanState, NestLoopState::js, NL1_printf, outerPlanState, JoinState::ps, and PlanState::ps_ResultTupleSlot.

Referenced by ExecEndNode().

359 {
360  NL1_printf("ExecEndNestLoop: %s\n",
361  "ending node processing");
362 
363  /*
364  * Free the exprcontext
365  */
366  ExecFreeExprContext(&node->js.ps);
367 
368  /*
369  * clean out the tuple table
370  */
372 
373  /*
374  * close down subplans
375  */
378 
379  NL1_printf("ExecEndNestLoop: %s\n",
380  "node processing ended");
381 }
PlanState ps
Definition: execnodes.h:1760
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:644
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:686
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1079
#define outerPlanState(node)
Definition: execnodes.h:1092
#define NL1_printf(s, a)
Definition: execdebug.h:84
JoinState js
Definition: execnodes.h:1775
#define innerPlanState(node)
Definition: execnodes.h:1091
NestLoopState* ExecInitNestLoop ( NestLoop node,
EState estate,
int  eflags 
)

Definition at line 258 of file nodeNestloop.c.

References Assert, elog, ERROR, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecAssignResultTypeFromTL(), ExecGetResultType(), ExecInitExpr(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitResultTupleSlot(), innerPlan, innerPlanState, NestLoop::join, JOIN_ANTI, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, Join::joinqual, JoinState::joinqual, Join::jointype, JoinState::jointype, NestLoopState::js, makeNode, NestLoop::nestParams, NIL, NL1_printf, NestLoopState::nl_MatchedOuter, NestLoopState::nl_NeedNewOuter, NestLoopState::nl_NullInnerTupleSlot, NULL, outerPlan, outerPlanState, Join::plan, PlanState::plan, JoinState::ps, Plan::qual, PlanState::qual, PlanState::state, Plan::targetlist, and PlanState::targetlist.

Referenced by ExecInitNode().

259 {
260  NestLoopState *nlstate;
261 
262  /* check for unsupported flags */
263  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
264 
265  NL1_printf("ExecInitNestLoop: %s\n",
266  "initializing node");
267 
268  /*
269  * create state structure
270  */
271  nlstate = makeNode(NestLoopState);
272  nlstate->js.ps.plan = (Plan *) node;
273  nlstate->js.ps.state = estate;
274 
275  /*
276  * Miscellaneous initialization
277  *
278  * create expression context for node
279  */
280  ExecAssignExprContext(estate, &nlstate->js.ps);
281 
282  /*
283  * initialize child expressions
284  */
285  nlstate->js.ps.targetlist = (List *)
287  (PlanState *) nlstate);
288  nlstate->js.ps.qual = (List *)
289  ExecInitExpr((Expr *) node->join.plan.qual,
290  (PlanState *) nlstate);
291  nlstate->js.jointype = node->join.jointype;
292  nlstate->js.joinqual = (List *)
293  ExecInitExpr((Expr *) node->join.joinqual,
294  (PlanState *) nlstate);
295 
296  /*
297  * initialize child nodes
298  *
299  * If we have no parameters to pass into the inner rel from the outer,
300  * tell the inner child that cheap rescans would be good. If we do have
301  * such parameters, then there is no point in REWIND support at all in the
302  * inner child, because it will always be rescanned with fresh parameter
303  * values.
304  */
305  outerPlanState(nlstate) = ExecInitNode(outerPlan(node), estate, eflags);
306  if (node->nestParams == NIL)
307  eflags |= EXEC_FLAG_REWIND;
308  else
309  eflags &= ~EXEC_FLAG_REWIND;
310  innerPlanState(nlstate) = ExecInitNode(innerPlan(node), estate, eflags);
311 
312  /*
313  * tuple table initialization
314  */
315  ExecInitResultTupleSlot(estate, &nlstate->js.ps);
316 
317  switch (node->join.jointype)
318  {
319  case JOIN_INNER:
320  case JOIN_SEMI:
321  break;
322  case JOIN_LEFT:
323  case JOIN_ANTI:
324  nlstate->nl_NullInnerTupleSlot =
325  ExecInitNullTupleSlot(estate,
327  break;
328  default:
329  elog(ERROR, "unrecognized join type: %d",
330  (int) node->join.jointype);
331  }
332 
333  /*
334  * initialize tuple type and projection info
335  */
336  ExecAssignResultTypeFromTL(&nlstate->js.ps);
337  ExecAssignProjectionInfo(&nlstate->js.ps, NULL);
338 
339  /*
340  * finally, wipe the current outer tuple clean.
341  */
342  nlstate->nl_NeedNewOuter = true;
343  nlstate->nl_MatchedOuter = false;
344 
345  NL1_printf("ExecInitNestLoop: %s\n",
346  "node initialized");
347 
348  return nlstate;
349 }
JoinType jointype
Definition: execnodes.h:1761
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:133
bool nl_MatchedOuter
Definition: execnodes.h:1777
List * nestParams
Definition: plannodes.h:658
PlanState ps
Definition: execnodes.h:1760
bool nl_NeedNewOuter
Definition: execnodes.h:1776
List * qual
Definition: execnodes.h:1064
List * targetlist
Definition: execnodes.h:1063
EState * state
Definition: execnodes.h:1051
List * joinqual
Definition: execnodes.h:1762
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:431
JoinType jointype
Definition: plannodes.h:640
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execQual.c:4267
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
Definition: execTuples.c:866
Join join
Definition: plannodes.h:657
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1092
#define innerPlan(node)
Definition: plannodes.h:161
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:658
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define outerPlan(node)
Definition: plannodes.h:162
TupleTableSlot * nl_NullInnerTupleSlot
Definition: execnodes.h:1778
Plan * plan
Definition: execnodes.h:1049
#define makeNode(_type_)
Definition: nodes.h:568
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define EXEC_FLAG_MARK
Definition: executor.h:61
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:409
#define NL1_printf(s, a)
Definition: execdebug.h:84
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:460
List * targetlist
Definition: plannodes.h:132
JoinState js
Definition: execnodes.h:1775
#define elog
Definition: elog.h:219
#define innerPlanState(node)
Definition: execnodes.h:1091
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
List * joinqual
Definition: plannodes.h:641
Plan plan
Definition: plannodes.h:639
TupleTableSlot* ExecNestLoop ( NestLoopState node)

Definition at line 60 of file nodeNestloop.c.

References Assert, bms_add_member(), PlanState::chgParam, ExprContext::ecxt_innertuple, ExprContext::ecxt_outertuple, ExprContext::ecxt_param_exec_vals, ENL1_printf, ExecProcNode(), ExecProject(), ExecQual(), ExecReScan(), innerPlan, innerPlanState, InstrCountFiltered1, InstrCountFiltered2, IsA, ParamExecData::isnull, JOIN_ANTI, JOIN_LEFT, JOIN_SEMI, JoinState::joinqual, JoinState::jointype, NestLoopState::js, lfirst, NestLoop::nestParams, NIL, NestLoopState::nl_MatchedOuter, NestLoopState::nl_NeedNewOuter, NestLoopState::nl_NullInnerTupleSlot, NULL, OUTER_VAR, outerPlan, outerPlanState, NestLoopParam::paramno, NestLoopParam::paramval, PlanState::plan, JoinState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, PlanState::qual, ResetExprContext, slot_getattr(), TupIsNull, ParamExecData::value, Var::varattno, and Var::varno.

Referenced by ExecProcNode().

61 {
62  NestLoop *nl;
65  TupleTableSlot *outerTupleSlot;
66  TupleTableSlot *innerTupleSlot;
67  List *joinqual;
68  List *otherqual;
69  ExprContext *econtext;
70  ListCell *lc;
71 
72  /*
73  * get information from the node
74  */
75  ENL1_printf("getting info from node");
76 
77  nl = (NestLoop *) node->js.ps.plan;
78  joinqual = node->js.joinqual;
79  otherqual = node->js.ps.qual;
80  outerPlan = outerPlanState(node);
81  innerPlan = innerPlanState(node);
82  econtext = node->js.ps.ps_ExprContext;
83 
84  /*
85  * Reset per-tuple memory context to free any expression evaluation
86  * storage allocated in the previous tuple cycle.
87  */
88  ResetExprContext(econtext);
89 
90  /*
91  * Ok, everything is setup for the join so now loop until we return a
92  * qualifying join tuple.
93  */
94  ENL1_printf("entering main loop");
95 
96  for (;;)
97  {
98  /*
99  * If we don't have an outer tuple, get the next one and reset the
100  * inner scan.
101  */
102  if (node->nl_NeedNewOuter)
103  {
104  ENL1_printf("getting new outer tuple");
105  outerTupleSlot = ExecProcNode(outerPlan);
106 
107  /*
108  * if there are no more outer tuples, then the join is complete..
109  */
110  if (TupIsNull(outerTupleSlot))
111  {
112  ENL1_printf("no outer tuple, ending join");
113  return NULL;
114  }
115 
116  ENL1_printf("saving new outer tuple information");
117  econtext->ecxt_outertuple = outerTupleSlot;
118  node->nl_NeedNewOuter = false;
119  node->nl_MatchedOuter = false;
120 
121  /*
122  * fetch the values of any outer Vars that must be passed to the
123  * inner scan, and store them in the appropriate PARAM_EXEC slots.
124  */
125  foreach(lc, nl->nestParams)
126  {
127  NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
128  int paramno = nlp->paramno;
129  ParamExecData *prm;
130 
131  prm = &(econtext->ecxt_param_exec_vals[paramno]);
132  /* Param value should be an OUTER_VAR var */
133  Assert(IsA(nlp->paramval, Var));
134  Assert(nlp->paramval->varno == OUTER_VAR);
135  Assert(nlp->paramval->varattno > 0);
136  prm->value = slot_getattr(outerTupleSlot,
137  nlp->paramval->varattno,
138  &(prm->isnull));
139  /* Flag parameter value as changed */
140  innerPlan->chgParam = bms_add_member(innerPlan->chgParam,
141  paramno);
142  }
143 
144  /*
145  * now rescan the inner plan
146  */
147  ENL1_printf("rescanning inner plan");
148  ExecReScan(innerPlan);
149  }
150 
151  /*
152  * we have an outerTuple, try to get the next inner tuple.
153  */
154  ENL1_printf("getting new inner tuple");
155 
156  innerTupleSlot = ExecProcNode(innerPlan);
157  econtext->ecxt_innertuple = innerTupleSlot;
158 
159  if (TupIsNull(innerTupleSlot))
160  {
161  ENL1_printf("no inner tuple, need new outer tuple");
162 
163  node->nl_NeedNewOuter = true;
164 
165  if (!node->nl_MatchedOuter &&
166  (node->js.jointype == JOIN_LEFT ||
167  node->js.jointype == JOIN_ANTI))
168  {
169  /*
170  * We are doing an outer join and there were no join matches
171  * for this outer tuple. Generate a fake join tuple with
172  * nulls for the inner tuple, and return it if it passes the
173  * non-join quals.
174  */
175  econtext->ecxt_innertuple = node->nl_NullInnerTupleSlot;
176 
177  ENL1_printf("testing qualification for outer-join tuple");
178 
179  if (otherqual == NIL || ExecQual(otherqual, econtext, false))
180  {
181  /*
182  * qualification was satisfied so we project and return
183  * the slot containing the result tuple using
184  * ExecProject().
185  */
186  ENL1_printf("qualification succeeded, projecting tuple");
187 
188  return ExecProject(node->js.ps.ps_ProjInfo);
189  }
190  else
191  InstrCountFiltered2(node, 1);
192  }
193 
194  /*
195  * Otherwise just return to top of loop for a new outer tuple.
196  */
197  continue;
198  }
199 
200  /*
201  * at this point we have a new pair of inner and outer tuples so we
202  * test the inner and outer tuples to see if they satisfy the node's
203  * qualification.
204  *
205  * Only the joinquals determine MatchedOuter status, but all quals
206  * must pass to actually return the tuple.
207  */
208  ENL1_printf("testing qualification");
209 
210  if (ExecQual(joinqual, econtext, false))
211  {
212  node->nl_MatchedOuter = true;
213 
214  /* In an antijoin, we never return a matched tuple */
215  if (node->js.jointype == JOIN_ANTI)
216  {
217  node->nl_NeedNewOuter = true;
218  continue; /* return to top of loop */
219  }
220 
221  /*
222  * In a semijoin, we'll consider returning the first match, but
223  * after that we're done with this outer tuple.
224  */
225  if (node->js.jointype == JOIN_SEMI)
226  node->nl_NeedNewOuter = true;
227 
228  if (otherqual == NIL || ExecQual(otherqual, econtext, false))
229  {
230  /*
231  * qualification was satisfied so we project and return the
232  * slot containing the result tuple using ExecProject().
233  */
234  ENL1_printf("qualification succeeded, projecting tuple");
235 
236  return ExecProject(node->js.ps.ps_ProjInfo);
237  }
238  else
239  InstrCountFiltered2(node, 1);
240  }
241  else
242  InstrCountFiltered1(node, 1);
243 
244  /*
245  * Tuple fails qual, so free per-tuple memory and try again.
246  */
247  ResetExprContext(econtext);
248 
249  ENL1_printf("qualification failed, looping");
250  }
251 }
JoinType jointype
Definition: execnodes.h:1761
#define NIL
Definition: pg_list.h:69
TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: execQual.c:5215
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:392
#define IsA(nodeptr, _type_)
Definition: nodes.h:571
bool nl_MatchedOuter
Definition: execnodes.h:1777
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1081
List * nestParams
Definition: plannodes.h:658
PlanState ps
Definition: execnodes.h:1760
bool nl_NeedNewOuter
Definition: execnodes.h:1776
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
List * qual
Definition: execnodes.h:1064
AttrNumber varattno
Definition: primnodes.h:168
List * joinqual
Definition: execnodes.h:1762
Definition: primnodes.h:163
Var * paramval
Definition: plannodes.h:665
#define ENL1_printf(message)
Definition: execdebug.h:85
bool isnull
Definition: params.h:101
#define outerPlanState(node)
Definition: execnodes.h:1092
#define innerPlan(node)
Definition: plannodes.h:161
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:131
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:139
bool ExecQual(List *qual, ExprContext *econtext, bool resultForNull)
Definition: execQual.c:5056
#define TupIsNull(slot)
Definition: tuptable.h:138
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:1095
Bitmapset * chgParam
Definition: execnodes.h:1074
#define outerPlan(node)
Definition: plannodes.h:162
Index varno
Definition: primnodes.h:166
TupleTableSlot * nl_NullInnerTupleSlot
Definition: execnodes.h:1778
Plan * plan
Definition: execnodes.h:1049
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:132
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
#define InstrCountFiltered2(node, delta)
Definition: execnodes.h:1100
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:668
JoinState js
Definition: execnodes.h:1775
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1143
Datum value
Definition: params.h:100
#define innerPlanState(node)
Definition: execnodes.h:1091
Definition: pg_list.h:45
#define OUTER_VAR
Definition: primnodes.h:154
#define ResetExprContext(econtext)
Definition: executor.h:333
void ExecReScanNestLoop ( NestLoopState node)

Definition at line 388 of file nodeNestloop.c.

References PlanState::chgParam, ExecReScan(), NestLoopState::nl_MatchedOuter, NestLoopState::nl_NeedNewOuter, NULL, outerPlan, and outerPlanState.

Referenced by ExecReScan().

389 {
391 
392  /*
393  * If outerPlan->chgParam is not null then plan will be automatically
394  * re-scanned by first ExecProcNode.
395  */
396  if (outerPlan->chgParam == NULL)
397  ExecReScan(outerPlan);
398 
399  /*
400  * innerPlan is re-scanned for each new outer tuple and MUST NOT be
401  * re-scanned from here or you'll get troubles from inner index scans when
402  * outer Vars are used as run-time keys...
403  */
404 
405  node->nl_NeedNewOuter = true;
406  node->nl_MatchedOuter = false;
407 }
bool nl_MatchedOuter
Definition: execnodes.h:1777
bool nl_NeedNewOuter
Definition: execnodes.h:1776
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
#define outerPlanState(node)
Definition: execnodes.h:1092
Bitmapset * chgParam
Definition: execnodes.h:1074
#define outerPlan(node)
Definition: plannodes.h:162
#define NULL
Definition: c.h:229