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 1629 of file nodeMergejoin.c.

References ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), innerPlanState, MergeJoinState::js, MJ1_printf, MergeJoinState::mj_MarkedTupleSlot, outerPlanState, JoinState::ps, and PlanState::ps_ResultTupleSlot.

Referenced by ExecEndNode().

1630 {
1631  MJ1_printf("ExecEndMergeJoin: %s\n",
1632  "ending node processing");
1633 
1634  /*
1635  * Free the exprcontext
1636  */
1637  ExecFreeExprContext(&node->js.ps);
1638 
1639  /*
1640  * clean out the tuple table
1641  */
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 }
#define MJ1_printf(s, p)
Definition: execdebug.h:119
PlanState ps
Definition: execnodes.h:1687
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:538
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1750
void ExecFreeExprContext(PlanState *planstate)
Definition: execUtils.c:566
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:946
#define outerPlanState(node)
Definition: execnodes.h:966
JoinState js
Definition: execnodes.h:1737
#define innerPlanState(node)
Definition: execnodes.h:965

◆ ExecInitMergeJoin()

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

Definition at line 1436 of file nodeMergejoin.c.

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(), 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_SEMI, Join::joinqual, Join::jointype, JoinState::jointype, MergeJoinState::js, list_length(), makeNode, MergeJoin::mergeclauses, MergeJoin::mergeCollations, MergeJoin::mergeFamilies, MergeJoin::mergeNullsFirst, MergeJoin::mergeStrategies, MJ1_printf, MergeJoinState::mj_ConstFalseJoin, MergeJoinState::mj_ExtraMarks, MergeJoinState::mj_InnerEContext, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_OuterEContext, MergeJoinState::mj_SkipMarkRestore, MJExamineQuals(), NIL, outerPlan, outerPlanState, Join::plan, PlanState::plan, JoinState::ps, Plan::qual, PlanState::qual, MergeJoin::skip_mark_restore, and PlanState::state.

Referenced by ExecInitNode().

1437 {
1438  MergeJoinState *mergestate;
1439  TupleDesc outerDesc,
1440  innerDesc;
1441 
1442  /* check for unsupported flags */
1443  Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
1444 
1445  MJ1_printf("ExecInitMergeJoin: %s\n",
1446  "initializing node");
1447 
1448  /*
1449  * create state structure
1450  */
1451  mergestate = makeNode(MergeJoinState);
1452  mergestate->js.ps.plan = (Plan *) node;
1453  mergestate->js.ps.state = estate;
1454  mergestate->js.ps.ExecProcNode = ExecMergeJoin;
1455  mergestate->js.jointype = node->join.jointype;
1456  mergestate->mj_ConstFalseJoin = false;
1457 
1458  /*
1459  * Miscellaneous initialization
1460  *
1461  * create expression context for node
1462  */
1463  ExecAssignExprContext(estate, &mergestate->js.ps);
1464 
1465  /*
1466  * we need two additional econtexts in which we can compute the join
1467  * expressions from the left and right input tuples. The node's regular
1468  * econtext won't do because it gets reset too often.
1469  */
1470  mergestate->mj_OuterEContext = CreateExprContext(estate);
1471  mergestate->mj_InnerEContext = CreateExprContext(estate);
1472 
1473  /*
1474  * initialize child nodes
1475  *
1476  * inner child must support MARK/RESTORE, unless we have detected that we
1477  * don't need that. Note that skip_mark_restore must never be set if
1478  * there are non-mergeclause joinquals, since the logic wouldn't work.
1479  */
1480  Assert(node->join.joinqual == NIL || !node->skip_mark_restore);
1481  mergestate->mj_SkipMarkRestore = node->skip_mark_restore;
1482 
1483  outerPlanState(mergestate) = ExecInitNode(outerPlan(node), estate, eflags);
1484  outerDesc = ExecGetResultType(outerPlanState(mergestate));
1485  innerPlanState(mergestate) = ExecInitNode(innerPlan(node), estate,
1486  mergestate->mj_SkipMarkRestore ?
1487  eflags :
1488  (eflags | EXEC_FLAG_MARK));
1489  innerDesc = ExecGetResultType(innerPlanState(mergestate));
1490 
1491  /*
1492  * For certain types of inner child nodes, it is advantageous to issue
1493  * MARK every time we advance past an inner tuple we will never return to.
1494  * For other types, MARK on a tuple we cannot return to is a waste of
1495  * cycles. Detect which case applies and set mj_ExtraMarks if we want to
1496  * issue "unnecessary" MARK calls.
1497  *
1498  * Currently, only Material wants the extra MARKs, and it will be helpful
1499  * only if eflags doesn't specify REWIND.
1500  *
1501  * Note that for IndexScan and IndexOnlyScan, it is *necessary* that we
1502  * not set mj_ExtraMarks; otherwise we might attempt to set a mark before
1503  * the first inner tuple, which they do not support.
1504  */
1505  if (IsA(innerPlan(node), Material) &&
1506  (eflags & EXEC_FLAG_REWIND) == 0 &&
1507  !mergestate->mj_SkipMarkRestore)
1508  mergestate->mj_ExtraMarks = true;
1509  else
1510  mergestate->mj_ExtraMarks = false;
1511 
1512  /*
1513  * Initialize result slot, type and projection.
1514  */
1515  ExecInitResultTupleSlotTL(estate, &mergestate->js.ps);
1516  ExecAssignProjectionInfo(&mergestate->js.ps, NULL);
1517 
1518  /*
1519  * tuple table initialization
1520  */
1521  mergestate->mj_MarkedTupleSlot = ExecInitExtraTupleSlot(estate, innerDesc);
1522 
1523  /*
1524  * initialize child expressions
1525  */
1526  mergestate->js.ps.qual =
1527  ExecInitQual(node->join.plan.qual, (PlanState *) mergestate);
1528  mergestate->js.joinqual =
1529  ExecInitQual(node->join.joinqual, (PlanState *) mergestate);
1530  /* mergeclauses are handled below */
1531 
1532  /*
1533  * detect whether we need only consider the first matching inner tuple
1534  */
1535  mergestate->js.single_match = (node->join.inner_unique ||
1536  node->join.jointype == JOIN_SEMI);
1537 
1538  /* set up null tuples for outer joins, if needed */
1539  switch (node->join.jointype)
1540  {
1541  case JOIN_INNER:
1542  case JOIN_SEMI:
1543  mergestate->mj_FillOuter = false;
1544  mergestate->mj_FillInner = false;
1545  break;
1546  case JOIN_LEFT:
1547  case JOIN_ANTI:
1548  mergestate->mj_FillOuter = true;
1549  mergestate->mj_FillInner = false;
1550  mergestate->mj_NullInnerTupleSlot =
1551  ExecInitNullTupleSlot(estate, innerDesc);
1552  break;
1553  case JOIN_RIGHT:
1554  mergestate->mj_FillOuter = false;
1555  mergestate->mj_FillInner = true;
1556  mergestate->mj_NullOuterTupleSlot =
1557  ExecInitNullTupleSlot(estate, outerDesc);
1558 
1559  /*
1560  * Can't handle right or full join with non-constant extra
1561  * joinclauses. This should have been caught by planner.
1562  */
1563  if (!check_constant_qual(node->join.joinqual,
1564  &mergestate->mj_ConstFalseJoin))
1565  ereport(ERROR,
1566  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1567  errmsg("RIGHT JOIN is only supported with merge-joinable join conditions")));
1568  break;
1569  case JOIN_FULL:
1570  mergestate->mj_FillOuter = true;
1571  mergestate->mj_FillInner = true;
1572  mergestate->mj_NullOuterTupleSlot =
1573  ExecInitNullTupleSlot(estate, outerDesc);
1574  mergestate->mj_NullInnerTupleSlot =
1575  ExecInitNullTupleSlot(estate, innerDesc);
1576 
1577  /*
1578  * Can't handle right or full join with non-constant extra
1579  * joinclauses. This should have been caught by planner.
1580  */
1581  if (!check_constant_qual(node->join.joinqual,
1582  &mergestate->mj_ConstFalseJoin))
1583  ereport(ERROR,
1584  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1585  errmsg("FULL JOIN is only supported with merge-joinable join conditions")));
1586  break;
1587  default:
1588  elog(ERROR, "unrecognized join type: %d",
1589  (int) node->join.jointype);
1590  }
1591 
1592  /*
1593  * preprocess the merge clauses
1594  */
1595  mergestate->mj_NumClauses = list_length(node->mergeclauses);
1596  mergestate->mj_Clauses = MJExamineQuals(node->mergeclauses,
1597  node->mergeFamilies,
1598  node->mergeCollations,
1599  node->mergeStrategies,
1600  node->mergeNullsFirst,
1601  (PlanState *) mergestate);
1602 
1603  /*
1604  * initialize join state
1605  */
1606  mergestate->mj_JoinState = EXEC_MJ_INITIALIZE_OUTER;
1607  mergestate->mj_MatchedOuter = false;
1608  mergestate->mj_MatchedInner = false;
1609  mergestate->mj_OuterTupleSlot = NULL;
1610  mergestate->mj_InnerTupleSlot = NULL;
1611 
1612  /*
1613  * initialization successful
1614  */
1615  MJ1_printf("ExecInitMergeJoin: %s\n",
1616  "node initialized");
1617 
1618  return mergestate;
1619 }
JoinType jointype
Definition: execnodes.h:1688
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:148
#define IsA(nodeptr, _type_)
Definition: nodes.h:567
#define MJ1_printf(s, p)
Definition: execdebug.h:119
ExprState * joinqual
Definition: execnodes.h:1691
ExprContext * mj_InnerEContext
Definition: execnodes.h:1754
bool mj_ConstFalseJoin
Definition: execnodes.h:1743
PlanState ps
Definition: execnodes.h:1687
bool single_match
Definition: execnodes.h:1689
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1750
TupleTableSlot * mj_NullInnerTupleSlot
Definition: execnodes.h:1752
int errcode(int sqlerrcode)
Definition: elog.c:575
EState * state
Definition: execnodes.h:914
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
ExprState * ExecInitQual(List *qual, PlanState *parent)
Definition: execExpr.c:205
List * mergeclauses
Definition: plannodes.h:725
JoinType jointype
Definition: plannodes.h:681
bool mj_MatchedOuter
Definition: execnodes.h:1746
TupleTableSlot * ExecInitNullTupleSlot(EState *estate, TupleDesc tupType)
Definition: execTuples.c:945
static MergeJoinClause MJExamineQuals(List *mergeclauses, Oid *mergefamilies, Oid *mergecollations, int *mergestrategies, bool *mergenullsfirst, PlanState *parent)
bool skip_mark_restore
Definition: plannodes.h:724
#define ERROR
Definition: elog.h:43
bool mj_MatchedInner
Definition: execnodes.h:1747
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:966
#define innerPlan(node)
Definition: plannodes.h:176
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
Definition: execUtils.c:456
MergeJoinClause mj_Clauses
Definition: execnodes.h:1739
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define ereport(elevel, rest)
Definition: elog.h:122
#define EXEC_MJ_INITIALIZE_OUTER
#define outerPlan(node)
Definition: plannodes.h:177
static bool check_constant_qual(List *qual, bool *is_const_false)
bool mj_SkipMarkRestore
Definition: execnodes.h:1741
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:918
Oid * mergeFamilies
Definition: plannodes.h:727
Oid * mergeCollations
Definition: plannodes.h:728
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:890
Plan * plan
Definition: execnodes.h:912
JoinState js
Definition: execnodes.h:1737
bool * mergeNullsFirst
Definition: plannodes.h:730
#define makeNode(_type_)
Definition: nodes.h:564
int * mergeStrategies
Definition: plannodes.h:729
#define Assert(condition)
Definition: c.h:699
#define EXEC_FLAG_MARK
Definition: executor.h:61
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1749
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:428
ExprContext * mj_OuterEContext
Definition: execnodes.h:1753
static int list_length(const List *l)
Definition: pg_list.h:89
TupleTableSlot * mj_NullOuterTupleSlot
Definition: execnodes.h:1751
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:438
ExprState * qual
Definition: execnodes.h:930
static TupleTableSlot * ExecMergeJoin(PlanState *pstate)
int errmsg(const char *fmt,...)
Definition: elog.c:797
Join join
Definition: plannodes.h:723
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:228
#define elog
Definition: elog.h:219
bool inner_unique
Definition: plannodes.h:682
bool mj_ExtraMarks
Definition: execnodes.h:1742
#define innerPlanState(node)
Definition: execnodes.h:965
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
List * joinqual
Definition: plannodes.h:683
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1748
Plan plan
Definition: plannodes.h:680

◆ ExecReScanMergeJoin()

void ExecReScanMergeJoin ( MergeJoinState node)

Definition at line 1656 of file nodeMergejoin.c.

References PlanState::chgParam, EXEC_MJ_INITIALIZE_OUTER, ExecClearTuple(), ExecReScan(), MergeJoinState::js, PlanState::lefttree, MergeJoinState::mj_InnerTupleSlot, MergeJoinState::mj_JoinState, MergeJoinState::mj_MarkedTupleSlot, MergeJoinState::mj_MatchedInner, MergeJoinState::mj_MatchedOuter, MergeJoinState::mj_OuterTupleSlot, JoinState::ps, and PlanState::righttree.

Referenced by ExecReScan().

1657 {
1659 
1661  node->mj_MatchedOuter = false;
1662  node->mj_MatchedInner = false;
1663  node->mj_OuterTupleSlot = NULL;
1664  node->mj_InnerTupleSlot = NULL;
1665 
1666  /*
1667  * if chgParam of subnodes is not null then plans will be re-scanned by
1668  * first ExecProcNode.
1669  */
1670  if (node->js.ps.lefttree->chgParam == NULL)
1671  ExecReScan(node->js.ps.lefttree);
1672  if (node->js.ps.righttree->chgParam == NULL)
1673  ExecReScan(node->js.ps.righttree);
1674 
1675 }
PlanState ps
Definition: execnodes.h:1687
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
TupleTableSlot * mj_MarkedTupleSlot
Definition: execnodes.h:1750
struct PlanState * righttree
Definition: execnodes.h:932
struct PlanState * lefttree
Definition: execnodes.h:931
bool mj_MatchedOuter
Definition: execnodes.h:1746
bool mj_MatchedInner
Definition: execnodes.h:1747
#define EXEC_MJ_INITIALIZE_OUTER
Bitmapset * chgParam
Definition: execnodes.h:941
JoinState js
Definition: execnodes.h:1737
TupleTableSlot * mj_InnerTupleSlot
Definition: execnodes.h:1749
TupleTableSlot * mj_OuterTupleSlot
Definition: execnodes.h:1748