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

Go to the source code of this file.

Functions

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

Function Documentation

void ExecEndNestLoop ( NestLoopState node)

Definition at line 353 of file nodeNestloop.c.

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

Referenced by ExecEndNode().

354 {
355  NL1_printf("ExecEndNestLoop: %s\n",
356  "ending node processing");
357 
358  /*
359  * Free the exprcontext
360  */
361  ExecFreeExprContext(&node->js.ps);
362 
363  /*
364  * clean out the tuple table
365  */
367 
368  /*
369  * close down subplans
370  */
373 
374  NL1_printf("ExecEndNestLoop: %s\n",
375  "node processing ended");
376 }
PlanState ps
Definition: execnodes.h:1516
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:644
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:511
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:829
#define outerPlanState(node)
Definition: execnodes.h:842
#define NL1_printf(s, a)
Definition: execdebug.h:77
JoinState js
Definition: execnodes.h:1531
#define innerPlanState(node)
Definition: execnodes.h:841
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(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitQual(), 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, and PlanState::state.

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.qual =
286  ExecInitQual(node->join.plan.qual, (PlanState *) nlstate);
287  nlstate->js.jointype = node->join.jointype;
288  nlstate->js.joinqual =
289  ExecInitQual(node->join.joinqual, (PlanState *) nlstate);
290 
291  /*
292  * initialize child nodes
293  *
294  * If we have no parameters to pass into the inner rel from the outer,
295  * tell the inner child that cheap rescans would be good. If we do have
296  * such parameters, then there is no point in REWIND support at all in the
297  * inner child, because it will always be rescanned with fresh parameter
298  * values.
299  */
300  outerPlanState(nlstate) = ExecInitNode(outerPlan(node), estate, eflags);
301  if (node->nestParams == NIL)
302  eflags |= EXEC_FLAG_REWIND;
303  else
304  eflags &= ~EXEC_FLAG_REWIND;
305  innerPlanState(nlstate) = ExecInitNode(innerPlan(node), estate, eflags);
306 
307  /*
308  * tuple table initialization
309  */
310  ExecInitResultTupleSlot(estate, &nlstate->js.ps);
311 
312  switch (node->join.jointype)
313  {
314  case JOIN_INNER:
315  case JOIN_SEMI:
316  break;
317  case JOIN_LEFT:
318  case JOIN_ANTI:
319  nlstate->nl_NullInnerTupleSlot =
320  ExecInitNullTupleSlot(estate,
322  break;
323  default:
324  elog(ERROR, "unrecognized join type: %d",
325  (int) node->join.jointype);
326  }
327 
328  /*
329  * initialize tuple type and projection info
330  */
331  ExecAssignResultTypeFromTL(&nlstate->js.ps);
332  ExecAssignProjectionInfo(&nlstate->js.ps, NULL);
333 
334  /*
335  * finally, wipe the current outer tuple clean.
336  */
337  nlstate->nl_NeedNewOuter = true;
338  nlstate->nl_MatchedOuter = false;
339 
340  NL1_printf("ExecInitNestLoop: %s\n",
341  "node initialized");
342 
343  return nlstate;
344 }
JoinType jointype
Definition: execnodes.h:1517
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:133
bool nl_MatchedOuter
Definition: execnodes.h:1533
ExprState * joinqual
Definition: execnodes.h:1518
List * nestParams
Definition: plannodes.h:658
PlanState ps
Definition: execnodes.h:1516
bool nl_NeedNewOuter
Definition: execnodes.h:1532
EState * state
Definition: execnodes.h:802
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:160
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:435
JoinType jointype
Definition: plannodes.h:640
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:842
#define innerPlan(node)
Definition: plannodes.h:161
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:482
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define outerPlan(node)
Definition: plannodes.h:162
TupleTableSlot * nl_NullInnerTupleSlot
Definition: execnodes.h:1534
Plan * plan
Definition: execnodes.h:800
#define makeNode(_type_)
Definition: nodes.h:554
#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:413
#define NL1_printf(s, a)
Definition: execdebug.h:77
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:464
ExprState * qual
Definition: execnodes.h:814
JoinState js
Definition: execnodes.h:1531
#define elog
Definition: elog.h:219
#define innerPlanState(node)
Definition: execnodes.h:841
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
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, 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  ExprState *joinqual;
68  ExprState *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 == NULL || ExecQual(otherqual, econtext))
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))
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 == NULL || ExecQual(otherqual, econtext))
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:1517
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:392
#define IsA(nodeptr, _type_)
Definition: nodes.h:557
bool nl_MatchedOuter
Definition: execnodes.h:1533
ExprState * joinqual
Definition: execnodes.h:1518
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:831
List * nestParams
Definition: plannodes.h:658
PlanState ps
Definition: execnodes.h:1516
bool nl_NeedNewOuter
Definition: execnodes.h:1532
ExprContext * ps_ExprContext
Definition: execnodes.h:830
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
AttrNumber varattno
Definition: primnodes.h:168
static bool ExecQual(ExprState *state, ExprContext *econtext)
Definition: executor.h:346
Definition: primnodes.h:163
Var * paramval
Definition: plannodes.h:665
#define ENL1_printf(message)
Definition: execdebug.h:78
bool isnull
Definition: params.h:101
#define outerPlanState(node)
Definition: execnodes.h:842
#define innerPlan(node)
Definition: plannodes.h:161
TupleTableSlot * ecxt_innertuple
Definition: execnodes.h:197
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:205
#define TupIsNull(slot)
Definition: tuptable.h:138
#define InstrCountFiltered1(node, delta)
Definition: execnodes.h:845
Bitmapset * chgParam
Definition: execnodes.h:824
#define outerPlan(node)
Definition: plannodes.h:162
Index varno
Definition: primnodes.h:166
TupleTableSlot * nl_NullInnerTupleSlot
Definition: execnodes.h:1534
Plan * plan
Definition: execnodes.h:800
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:198
#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:850
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
ExprState * qual
Definition: execnodes.h:814
JoinState js
Definition: execnodes.h:1531
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:841
#define OUTER_VAR
Definition: primnodes.h:154
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:309
#define ResetExprContext(econtext)
Definition: executor.h:450
void ExecReScanNestLoop ( NestLoopState node)

Definition at line 383 of file nodeNestloop.c.

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

Referenced by ExecReScan().

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