{%extends "base/page.html"%}
{%block title%}Backend Flowchart{%endblock%}
{%block contents%}

<h1>Backend Flowchart</h1>

<center>
<h3><i>Click on an item</i> to see more detail or look at the full
<a href="http://wiki.postgresql.org/wiki/Backend_flowchart">index.</a></h3>

<p><img src="../../../media/img/developer/backend/flow.gif" usemap="#flowmap" alt="flowchart" />

<!--
image size 529 820
clickable area size 145x45; to compute closing corner, use:
echo "0, 0," | awk -F, '{printf "%d, %d\n", $1 + 145, $2 + 45}'
 -->

<map name="flowmap" id="flowmap">
<area coords="50, 0, 195, 45" href="http://wiki.postgresql.org/wiki/Backend_flowchart#main" alt="main" />
<area coords="50, 65,  195, 110" href="http://wiki.postgresql.org/wiki/Backend_flowchart#postmaster" alt="postmaster" />
<area coords="50, 135, 195, 180" href="http://wiki.postgresql.org/wiki/Backend_flowchart#tcop" alt="tcop" />
<area coords="50, 225, 195, 270" href="http://wiki.postgresql.org/wiki/Backend_flowchart#parser" alt="parser" />
<area coords="50, 295, 195, 340" href="http://wiki.postgresql.org/wiki/Backend_flowchart#tcop" alt="tcop" />
<area coords="50, 365, 195, 410" href="http://wiki.postgresql.org/wiki/Backend_flowchart#rewrite" alt="rewrite" />
<area coords="50, 435, 195, 480" href="http://wiki.postgresql.org/wiki/Backend_flowchart#optimizer_path" alt="path" />
<area coords="50, 505, 195, 550" href="http://wiki.postgresql.org/wiki/Backend_flowchart#optimizer_plan" alt="plan" />
<area coords="50, 575, 195, 620" href="http://wiki.postgresql.org/wiki/Backend_flowchart#executor" alt="executor" />

<area coords="280, 35, 425, 80" href="http://wiki.postgresql.org/wiki/Backend_flowchart#libpq" alt="libpq" />
<area coords="280, 135, 425, 180" href="http://wiki.postgresql.org/wiki/Backend_flowchart#tcop" alt="tcop" />
<area coords="280, 285, 425, 330" href="http://wiki.postgresql.org/wiki/Backend_flowchart#commands" alt="commands" />

<area coords="0, 710, 145, 755" href="http://wiki.postgresql.org/wiki/Backend_flowchart#utils" alt="utils" />
<area coords="190, 710, 335, 755" href="http://wiki.postgresql.org/wiki/Backend_flowchart#catalog" alt="catalog" />
<area coords="365, 710, 510, 755" href="http://wiki.postgresql.org/wiki/Backend_flowchart#storage" alt="storage" />

<area coords="85, 780, 230, 825" href="http://wiki.postgresql.org/wiki/Backend_flowchart#access" alt="access" />
<area coords="280, 780, 425, 825" href="http://wiki.postgresql.org/wiki/Backend_flowchart#nodes" alt="nodes" />
</map>
</center>

<br />

<p>A query comes to the backend via data packets arriving through TCP/IP
or Unix Domain sockets. It is loaded into a string, and passed to the <a
href="http://wiki.postgresql.org/wiki/Backend_flowchart#parser">parser,</a>
where the lexical scanner, <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/parser/scan.l">scan.l,</a>
breaks the query up into tokens(words). The parser uses <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/parser/gram.y">gram.y</a>
and the tokens to identify the query type, and load the proper
query-specific structure, like <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/parsenodes.h">CreateStmt</a>
or <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/parsenodes.h">SelectStmt.</a></p>

<p>The statement is then identified as complex (<i>SELECT / INSERT /
UPDATE / DELETE</i>) or a simple, e.g <i> CREATE USER, ANALYZE, </i>,
etc.  Simple utility commands are processed by statement-specific
functions in <a href="http://wiki.postgresql.org/wiki/Backend_flowchart#commands.</a>
Complex statements require more handling.</p>

<p>The parser takes a complex query, and creates a <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/parsenodes.h">Query</a>
structure that contains all the elements used by complex queries.
Query.qual holds the <i>WHERE</i> clause qualification, which is filled
in by <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/parser/parse_clause.c">transformWhereClause().</a>
Each table referenced in the query is represented by a <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/parsenodes.h">RangeTableEntry,</a>
and they are linked together to form the <i>range table</i> of the
query, which is generated by <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/parser/parse_clause.c">transformFromClause().</a>
Query.rtable holds the query's range table.</p>

<p>Certain queries, like <i>SELECT,</i> return columns of data. Other
queries, like <i>INSERT</i> and <i>UPDATE,</i> specify the columns
modified by the query. These column references are converted to <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/primnodes.h">TargetEntry</a>
entries, which are linked together to make up the <i>target list</i> of
the query. The target list is stored in Query.targetList, which is
generated by <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/parser/parse_target.c">transformTargetList().</a></p>

<p>Other query elements, like aggregates(<i>SUM()</i>), <i>GROUP
BY,</i> and <i>ORDER BY</i> are also stored in their own Query
fields.</p>

<p>The next step is for the Query to be modified by any
<i>VIEWS</i> or <i>RULES</i> that may apply to the query. This is
performed by the <a href="http://wiki.postgresql.org/wiki/Backend_flowchart#rewrite">rewrite</a>
system.</p>

<p>The <a
href="http://wiki.postgresql.org/wiki/Backend_flowchart#optimizer_path">optimizer</a>
uses the Query structure to determine the best table join order and join
type of each table in the RangeTable, using Query.qual(<i>WHERE</i>
clause) to consider optimal index usage.</p>  The <a
href="http://wiki.postgresql.org/wiki/Backend_flowchart#optimizer_path">path</a>
module  then generates an optimal <a
href="http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/nodes/plannodes.h">Plan,</a>
which contains the operations to be performed to execute the query.

<p>The Plan is then passed to the <a
href="http://wiki.postgresql.org/wiki/Backend_flowchart#executor">executor</a> for execution, and the
result returned to the client. The Plan is actually as set of nodes,
arranged in a tree structure with a top-level node, and various
sub-nodes as children.</p>

<p>There are many other modules that support this basic
functionality. They can be accessed by clicking on the
flowchart.</p>

{%endblock%}

