Re: Some questions about PostgreSQL source code

From: Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>
To: Олег Царев <zabivator(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Some questions about PostgreSQL source code
Date: 2009-05-06 15:43:18
Message-ID: 4A01B016.3060109@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Олег Царев wrote:
> Parser translate from text of query to AST.
>
> 1) Than AST go to planner for plan normalization and optimization.
> Planner work on AST structures, or build self internal tree of logical plan?

The planner works with different structures in different phases of
planning. Some transformations are made directly to the Query-tree,
which is the format that the parser outputs. In intermediate phases,
various other structures are build, e.g Path-trees. The final result of
the planner is a Plan-tree.

> 2) Who set types of any columns? Parser or planner?

That's done in the so-called "parse analysis" phase. The entry point for
that is the parse_analyze() function.

> After planner, called physical plan - executor.
>
> 1) Where in source build executor's node from logical plan (result of
> planner)?

InitPlan().

> 2) How to executor's node bulding, linked, and use one another? For example
> how to linked Table Scan and Sort on query select a,b,c,d from table order
> by a,b? Let's assume query work without indexes, for simple describing.

The structure used by the executor is a tree of PlanState nodes (which
reflects the planner's Plan-tree). See PlanState struct in execnodes.h.
Each executor node (= PlanState) has a pointers to the nodes below it,
usually in the lefttree and righttree fields, although some node types
like AppendState use different method (AppendState.appendplans array)

> 3) What the function called on Prepare/Execute? How this calls translated to
> executor's nodes?
>
> I try look for this information in source code, and found execAim.c, with
> big swtich.
> In that switch mixed brachnes of nodes, node states, some expressions and
> aggregation.
> What is mind that switch in execAim.c? How to Prepare/Execute/Fetch work
> with executor's nodes?

That's used for internal parameters in the executor, not for
prepare/execute. They're used for things like correlated subqueries,
where the subquery is run repeatedly with different values in the
enclosing query.

For prepare/execute, the executor is initialized, run, and shut down for
each execution. The Plan tree that came from the planner is reused, but
the corresponding executor tree (PlanState-tree) is recreated at each
execution.

> 4) How to manipulate data on the nodes? I understand from comments, what
> every node use own childs for get "tuple", where "tuple" - list of "cells".
> I didn't found "cells" in source code =(
> Can you descrivbe me, how to one node get data from source node, return data
> for parent, and what is "data" and where i can found in source code this
> entity?

This question I didn't quite understand. The basic mechanism is that the
top node of the executor tree is executed, and that asks for a tuple
from the node(s) below it as needed (by calling ExecProcNode()), which
in turn ask for tuples from their child nodes and so forth. IOW it's a
"pull" system, where the top node pulls the tuples through the tree.

The intermediate tuples are stored in so-called tuple table slots.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2009-05-06 15:47:57 Re: bytea vs. pg_dump
Previous Message Tom Lane 2009-05-06 15:34:49 Re: Some questions about PostgreSQL source code