Query Execution

COMP9315 21T1 ♢ Query Execution ♢ [0/14]
❖ Query Execution

Query execution:   applies evaluation plan result tuples

[Diagram:Pics/qproc/qproc3.png]

COMP9315 21T1 ♢ Query Execution ♢ [1/14]
❖ Query Execution (cont)

Example of query translation:

select s.name, s.id, e.course, e.mark
from   Student s, Enrolment e
where  e.student = s.id and e.semester = '05s2';

maps to

πname,id,course,mark(Stu ⨝e.student=s.idsemester=05s2Enr))

maps to

Temp1  = BtreeSelect[semester=05s2](Enr)
Temp2  = HashJoin[e.student=s.id](Stu,Temp1)
Result = Project[name,id,course,mark](Temp2)

COMP9315 21T1 ♢ Query Execution ♢ [2/14]
❖ Query Execution (cont)

A query execution plan:

Results may be passed from one operator to the next:
COMP9315 21T1 ♢ Query Execution ♢ [3/14]
❖ Materialization

Steps in materialization between two operators

Advantage: Disadvantage:
COMP9315 21T1 ♢ Query Execution ♢ [4/14]
❖ Pipelining

How pipelining is organised between two operators:

Advantage: Disadvantage:
COMP9315 21T1 ♢ Query Execution ♢ [5/14]
❖ Iterators (reminder)

Iterators provide a "stream" of results:

Other possible operations: reset to specific point, restart, ...
COMP9315 21T1 ♢ Query Execution ♢ [6/14]
❖ Pipelining Example

Consider the query:

select s.id, e.course, e.mark
from   Student s, Enrolment e
where  e.student = s.id and
       e.semester = '05s2' and s.name = 'John';

Evaluated via communication between RA tree nodes:

[Diagram:Pics/qproc/qtree.png]



Note: likely that projection is combined with join in PostgreSQL

COMP9315 21T1 ♢ Query Execution ♢ [7/14]
❖ Disk Accesses

Pipelining cannot avoid all disk accesses.

Some operations use multiple passes (e.g. merge-sort, hash-join).

Thus ...
COMP9315 21T1 ♢ Query Execution ♢ [8/14]
❖ PostgreSQL Query Execution

Defs: src/include/executor and src/include/nodes

Code: src/backend/executor

PostgreSQL uses pipelining (as much as possible) ...

COMP9315 21T1 ♢ Query Execution ♢ [9/14]
❖ PostgreSQL Executor

Modules in src/backend/executor fall into two groups:

execXXX (e.g. execMain, execProcnode, execScan)

nodeXXX   (e.g. nodeSeqscan, nodeNestloop, nodeGroup)
COMP9315 21T1 ♢ Query Execution ♢ [10/14]
❖ Example PostgreSQL Execution

Consider the query:

-- get manager's age and # employees in Shoe department
select e.age, d.nemps
from   Departments d, Employees e
where  e.name = d.manager and d.name ='Shoe'

and its execution plan tree

[Diagram:Pics/qproc/qtree1.png]

COMP9315 21T1 ♢ Query Execution ♢ [11/14]
❖ Example PostgreSQL Execution (cont)

Initially InitPlan() invokes ExecInitNode() on plan tree root.

ExecInitNode() sees a NestedLoop node ...
   so dispatches to ExecInitNestLoop() to set up iterator
   then invokes ExecInitNode() on left and right sub-plans
       in left subPlan, ExecInitNode() sees an IndexScan node
        so dispatches to ExecInitIndexScan() to set up iterator
       in right sub-plan, ExecInitNode() sees a SeqScan node
        so dispatches to ExecInitSeqScan() to set up iterator

Result: a plan state  tree with same structure as plan tree.

COMP9315 21T1 ♢ Query Execution ♢ [12/14]
❖ Example PostgreSQL Execution (cont)

Plan state tree (collection of iterators):

[Diagram:Pics/qproc/qtree2.png]

COMP9315 21T1 ♢ Query Execution ♢ [13/14]
❖ Example PostgreSQL Execution (cont)

Then ExecutePlan() repeatedly invokes ExecProcNode().

ExecProcNode() sees a NestedLoop node ...
   so dispatches to ExecNestedLoop() to get next tuple
   which invokes ExecProcNode() on its sub-plans
       in left sub-plan, ExecProcNode() sees an IndexScan node
            so dispatches to ExecIndexScan() to get next tuple
            if no more tuples, return END
            for this tuple, invoke ExecProcNode() on right sub-plan
                ExecProcNode() sees a SeqScan node
                    so dispatches to ExecSeqScan() to get next tuple
                    check for match and return joined tuples if found
                    continue scan until end
                reset right sub-plan iterator

Result: stream of result tuples returned via ExecutePlan()

COMP9315 21T1 ♢ Query Execution ♢ [14/14]


Produced: 6 Apr 2021