PostgreSQL Source Code  git master
nodeMergejoin.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeMergejoin.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

MergeJoinStateExecInitMergeJoin (MergeJoin *node, EState *estate, int eflags)
 
void ExecEndMergeJoin (MergeJoinState *node)
 
void ExecReScanMergeJoin (MergeJoinState *node)
 

Function Documentation

◆ ExecEndMergeJoin()

void ExecEndMergeJoin ( MergeJoinState node)

Definition at line 1640 of file nodeMergejoin.c.

1641 {
1642  MJ1_printf("ExecEndMergeJoin: %s\n",
1643  "ending node processing");
1644 
1645  /*
1646  * shut down the subplans
1647  */
1648  ExecEndNode(innerPlanState(node));
1649  ExecEndNode(outerPlanState(node));
1650 
1651  MJ1_printf("ExecEndMergeJoin: %s\n",
1652  "node processing ended");
1653 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
#define MJ1_printf(s, p)
Definition: execdebug.h:121
#define outerPlanState(node)
Definition: execnodes.h:1213
#define innerPlanState(node)
Definition: execnodes.h:1212

References ExecEndNode(), innerPlanState, MJ1_printf, and outerPlanState.

Referenced by ExecEndNode().

◆ ExecInitMergeJoin()

MergeJoinState* ExecInitMergeJoin ( MergeJoin node,
EState estate,
int  eflags 
)

Definition at line 1443 of file nodeMergejoin.c.

1444 {
1445  MergeJoinState *mergestate;
1446  TupleDesc outerDesc,
1447  innerDesc;
1448  const TupleTableSlotOps *innerOps;
1449 
1450  /* check for unsupported flags */
1451  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1452 
1453  MJ1_printf("ExecInitMergeJoin: %s\n",
1454  "initializing node");
1455 
1456  /*
1457  * create state structure
1458  */
1459  mergestate = makeNode(MergeJoinState);
1460  mergestate->js.ps.plan = (Plan *) node;
1461  mergestate->js.ps.state = estate;
1462  mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1463  mergestate->js.jointype = node->join.jointype;
1464  mergestate->mj_ConstFalseJoin = false;
1465 
1466  /*
1467  * Miscellaneous initialization
1468  *
1469  * create expression context for node
1470  */
1471  ExecAssignExprContext(estate, &mergestate->js.ps);
1472 
1473  /*
1474  * we need two additional econtexts in which we can compute the join
1475  * expressions from the left and right input tuples. The node's regular
1476  * econtext won't do because it gets reset too often.
1477  */
1478  mergestate->mj_OuterEContext = CreateExprContext(estate);
1479  mergestate->mj_InnerEContext = CreateExprContext(estate);
1480 
1481  /*
1482  * initialize child nodes
1483  *
1484  * inner child must support MARK/RESTORE, unless we have detected that we
1485  * don't need that. Note that skip_mark_restore must never be set if
1486  * there are non-mergeclause joinquals, since the logic wouldn't work.
1487  */
1488  Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1489  mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1490 
1491  outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1492  outerDesc = ExecGetResultType(outerPlanState(mergestate));
1493  innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1494  mergestate->mj_SkipMarkRestore ?
1495  eflags :
1496  (eflags | EXEC_FLAG_MARK));
1497  innerDesc = ExecGetResultType(innerPlanState(mergestate));
1498 
1499  /*
1500  * For certain types of inner child nodes, it is advantageous to issue
1501  * MARK every time we advance past an inner tuple we will never return to.
1502  * For other types, MARK on a tuple we cannot return to is a waste of
1503  * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1504  * issue "unnecessary" MARK calls.
1505  *
1506  * Currently, only Material wants the extra MARKs, and it will be helpful
1507  * only if eflags doesn't specify REWIND.
1508  *
1509  * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1510  * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1511  * the first inner tuple, which they do not support.
1512  */
1513  if (IsA(innerPlan(node), Material) &&
1514  (eflags & EXEC_FLAG_REWIND) == 0 &&
1515  !mergestate->mj_SkipMarkRestore)
1516  mergestate->mj_ExtraMarks = true;
1517  else
1518  mergestate->mj_ExtraMarks = false;
1519 
1520  /*
1521  * Initialize result slot, type and projection.
1522  */
1523  ExecInitResultTupleSlotTL(&mergestate->js.ps, &TTSOpsVirtual);
1524  ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1525 
1526  /*
1527  * tuple table initialization
1528  */
1529  innerOps = ExecGetResultSlotOps(innerPlanState(mergestate), NULL);
1530  mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc,
1531  innerOps);
1532 
1533  /*
1534  * initialize child expressions
1535  */
1536  mergestate->js.ps.qual =
1537  ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1538  mergestate->js.joinqual =
1539  ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1540  /* mergeclauses are handled below */
1541 
1542  /*
1543  * detect whether we need only consider the first matching inner tuple
1544  */
1545  mergestate->js.single_match = (node->join.inner_unique ||
1546  node->join.jointype == JOIN_SEMI);
1547 
1548  /* set up null tuples for outer joins, if needed */
1549  switch (node->join.jointype)
1550  {
1551  case JOIN_INNER:
1552  case JOIN_SEMI:
1553  mergestate->mj_FillOuter = false;
1554  mergestate->mj_FillInner = false;
1555  break;
1556  case JOIN_LEFT:
1557  case JOIN_ANTI:
1558  mergestate->mj_FillOuter = true;
1559  mergestate->mj_FillInner = false;
1560  mergestate->mj_NullInnerTupleSlot =
1561  ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1562  break;
1563  case JOIN_RIGHT:
1564  case JOIN_RIGHT_ANTI:
1565  mergestate->mj_FillOuter = false;
1566  mergestate->mj_FillInner = true;
1567  mergestate->mj_NullOuterTupleSlot =
1568  ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1569 
1570  /*
1571  * Can't handle right, right-anti or full join with non-constant
1572  * extra joinclauses. This should have been caught by planner.
1573  */
1574  if (!check_constant_qual(node->join.joinqual,
1575  &mergestate->mj_ConstFalseJoin))
1576  ereport(ERROR,
1577  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1578  errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1579  break;
1580  case JOIN_FULL:
1581  mergestate->mj_FillOuter = true;
1582  mergestate->mj_FillInner = true;
1583  mergestate->mj_NullOuterTupleSlot =
1584  ExecInitNullTupleSlot(estate, outerDesc, &TTSOpsVirtual);
1585  mergestate->mj_NullInnerTupleSlot =
1586  ExecInitNullTupleSlot(estate, innerDesc, &TTSOpsVirtual);
1587 
1588  /*
1589  * Can't handle right, right-anti or full join with non-constant
1590  * extra joinclauses. This should have been caught by planner.
1591  */
1592  if (!check_constant_qual(node->join.joinqual,
1593  &mergestate->mj_ConstFalseJoin))
1594  ereport(ERROR,
1595  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1596  errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1597  break;
1598  default:
1599  elog(ERROR, "unrecognized join type: %d",
1600  (int) node->join.jointype);
1601  }
1602 
1603  /*
1604  * preprocess the merge clauses
1605  */
1606  mergestate->mj_NumClauses = list_length(node->mergeclauses);
1607  mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1608  node->mergeFamilies,
1609  node->mergeCollations,
1610  node->mergeStrategies,
1611  node->mergeNullsFirst,
1612  (PlanState *) mergestate);
1613 
1614  /*
1615  * initialize join state
1616  */
1617  mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1618  mergestate->mj_MatchedOuter = false;
1619  mergestate->mj_MatchedInner = false;
1620  mergestate->mj_OuterTupleSlot = NULL;
1621  mergestate->mj_InnerTupleSlot = NULL;
1622 
1623  /*
1624  * initialization successful
1625  */
1626  MJ1_printf("ExecInitMergeJoin: %s\n",
1627  "node initialized");
1628 
1629  return mergestate;
1630 }
#define Assert(condition)
Definition: c.h:858
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:220
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1934
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1918
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1886
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:493
const TupleTableSlotOps * ExecGetResultSlotOps(PlanState *planstate, bool *isfixed)
Definition: execUtils.c:502
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:304
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:538
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_REWIND
Definition: executor.h:67
#define EXEC_FLAG_MARK
Definition: executor.h:69
static MergeJoinClause MJExamineQuals(List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, PlanState *parent)
#define EXEC_MJ_INITIALIZE_OUTER
static TupleTableSlot * ExecMergeJoin(PlanState *pstate)
static bool check_constant_qual(List *qual, bool *is_const_false)
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define makeNode(_type_)
Definition: nodes.h:155
@ JOIN_SEMI
Definition: nodes.h:307
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_INNER
Definition: nodes.h:293
@ JOIN_RIGHT
Definition: nodes.h:296
@ JOIN_LEFT
Definition: nodes.h:294
@ JOIN_RIGHT_ANTI
Definition: nodes.h:309
@ JOIN_ANTI
Definition: nodes.h:308
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define innerPlan(node)
Definition: plannodes.h:181
#define outerPlan(node)
Definition: plannodes.h:182
JoinType jointype
Definition: execnodes.h:2087
PlanState ps
Definition: execnodes.h:2086
ExprState * joinqual
Definition: execnodes.h:2090
bool single_match
Definition: execnodes.h:2088
List * joinqual
Definition: plannodes.h:793
JoinType jointype
Definition: plannodes.h:791
bool inner_unique
Definition: plannodes.h:792
bool mj_MatchedOuter
Definition: execnodes.h:2145
bool mj_SkipMarkRestore
Definition: execnodes.h:2140
bool mj_ConstFalseJoin
Definition: execnodes.h:2142
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:2149
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:2151
ExprContext * mj_InnerEContext
Definition: execnodes.h:2153
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:2150
bool mj_ExtraMarks
Definition: execnodes.h:2141
MergeJoinClause mj_Clauses
Definition: execnodes.h:2138
bool mj_MatchedInner
Definition: execnodes.h:2146
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:2148
ExprContext * mj_OuterEContext
Definition: execnodes.h:2152
JoinState js
Definition: execnodes.h:2136
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:2147
List * mergeclauses
Definition: plannodes.h:841
Join join
Definition: plannodes.h:835
bool skip_mark_restore
Definition: plannodes.h:838
ExprState * qual
Definition: execnodes.h:1138
Plan * plan
Definition: execnodes.h:1117
EState * state
Definition: execnodes.h:1119
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1123

References Assert, check_constant_qual(), CreateExprContext(), elog, ereport, errcode(), errmsg(), ERROR, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, EXEC_MJ_INITIALIZE_OUTER, ExecAssignExprContext(), ExecAssignProjectionInfo(), ExecGetResultSlotOps(), ExecGetResultType(), ExecInitExtraTupleSlot(), ExecInitNode(), ExecInitNullTupleSlot(), ExecInitQual(), ExecInitResultTupleSlotTL(), ExecMergeJoin(), PlanState::ExecProcNode, Join::inner_unique, innerPlan, innerPlanState, IsA, MergeJoin::join, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_SEMI, JoinState::joinqual, Join::joinqual, JoinState::jointype, Join::jointype, MergeJoinState::js, list_length(), makeNode, MergeJoin::mergeclauses, MJ1_printf, MergeJoinState::mj_Clauses, MergeJoinState::mj_ConstFalseJoin, MergeJoinState::mj_ExtraMarks, MergeJoinState::mj_FillInner, MergeJoinState::mj_FillOuter, MergeJoinState::mj_InnerEContext, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_NullInnerTupleSlot, MergeJoinState::mj_NullOuterTupleSlot, MergeJoinState::mj_NumClauses, MergeJoinState::mj_OuterEContext, MergeJoinState::mj_OuterTupleSlot, MergeJoinState::mj_SkipMarkRestore, MJExamineQuals(), NIL, outerPlan, outerPlanState, PlanState::plan, JoinState::ps, PlanState::qual, JoinState::single_match, MergeJoin::skip_mark_restore, PlanState::state, and TTSOpsVirtual.

Referenced by ExecInitNode().

◆ ExecReScanMergeJoin()

void ExecReScanMergeJoin ( MergeJoinState node)

Definition at line 1656 of file nodeMergejoin.c.

1657 {
1660 
1662 
1664  node->mj_MatchedOuter = false;
1665  node->mj_MatchedInner = false;
1666  node->mj_OuterTupleSlot = NULL;
1667  node->mj_InnerTupleSlot = NULL;
1668 
1669  /*
1670  * if chgParam of subnodes is not null then plans will be re-scanned by
1671  * first ExecProcNode.
1672  */
1673  if (outerPlan->chgParam == NULL)
1675  if (innerPlan->chgParam == NULL)
1677 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454

References EXEC_MJ_INITIALIZE_OUTER, ExecClearTuple(), ExecReScan(), innerPlan, innerPlanState, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_OuterTupleSlot, outerPlan, and outerPlanState.

Referenced by ExecReScan().