Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/arch-dev.sgml`
fa95fdd7113cbd8dae83c939e0bf8c3513b4c8d18115867100000001000008e9
 required set
    of rows.  This is essentially a demand-pull pipeline mechanism.
    Each time a plan node is called, it must deliver one more row, or
    report that it is done delivering rows.
   </para>

   <para>
    To provide a concrete example, assume that the top
    node is a <literal>MergeJoin</literal> node.
    Before any merge can be done two rows have to be fetched (one from
    each subplan). So the executor recursively calls itself to
    process the subplans (it starts with the subplan attached to
    <literal>lefttree</literal>). The new top node (the top node of the left
    subplan) is, let's say, a
    <literal>Sort</literal> node and again recursion is needed to obtain
    an input row.  The child node of the <literal>Sort</literal> might
    be a <literal>SeqScan</literal> node, representing actual reading of a table.
    Execution of this node causes the executor to fetch a row from the
    table and return it up to the calling node.  The <literal>Sort</literal>
    node will repeatedly call its child to obtain all the rows to be sorted.
    When the input is exhausted (as indicated by the child node returning
    a NULL instead of a row), the <literal>Sort</literal> code performs
    the sort, and finally is able to return its first output row, namely
    the first one in sorted order.  It keeps the remaining rows stored so
    that it can deliver them in sorted order in response to later demands.
   </para>

   <para>
    The <literal>MergeJoin</literal> node similarly demands the first row
    from its right subplan.  Then it compares the two rows to see if they
    can be joined; if so, it returns a join row to its caller.  On the next
    call, or immediately if it cannot join the current pair of inputs,
    it advances to the next row of one table
    or the other (depending on how the comparison came out), and again
    checks for a match.  Eventually, one subplan or the other is exhausted,
    and the <literal>MergeJoin</literal> node returns NULL to indicate that
    no more join rows can be formed.
   </para>

   <para>
    Complex queries can involve many levels of plan nodes, but the general
    approach is the same: each node computes and returns its next output
    row each time it is called.  Each node

Title: Executor Operation and MergeJoin Example
Summary
The executor operates as a demand-pull pipeline, with each plan node delivering a row or reporting completion when called. As an example, consider a MergeJoin node, which requires fetching two rows (one from each subplan) before merging. The executor recursively processes subplans, such as a Sort node, which in turn calls its child node (e.g., a SeqScan node). The SeqScan node reads rows from a table and returns them up the chain. The Sort node sorts all rows and returns the first sorted row, storing the rest for later demands. The MergeJoin node demands rows from both subplans, compares them, and returns a join row if they match. If not, it advances to the next row of one table and checks for a match. When a subplan is exhausted, the MergeJoin node returns NULL. Complex queries follow the same general approach: each node computes and returns its next output row when called.