1
<!-- src/tools/backend/index.html -->
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml">
7
content="HTML Tidy for BSD/OS (vers 1st July 2002), see www.w3.org" />
8
<title>How PostgreSQL Processes a Query</title>
10
<body bgcolor="#FFFFFF" text="#000000" link="#FF0000"
11
vlink="#A00000" alink="#0000FF">
12
<h1>How PostgreSQL Processes a Query</h1>
14
<h2>by Bruce Momjian</h2>
17
<h3><i>Click on an item</i> to see more detail or look at the full
18
<a href="backend_dirs.html">index.</a></h3>
20
<p><img src="flow.gif" usemap="#flowmap" alt="flowchart" />
22
<map name="flowmap" id="flowmap">
23
<area coords="45, 0, 175, 30" href="backend_dirs.html#main" alt="main" />
24
<area coords="255, 35, 380, 65" href="backend_dirs.html#libpq" alt="libpq" />
25
<area coords="45, 65, 175, 95" href="backend_dirs.html#postmaster" alt="postmaster" />
26
<area coords="45, 130, 175, 160" href="backend_dirs.html#tcop" alt="tcop" />
27
<area coords="250, 130, 380, 160" href="backend_dirs.html#tcop" alt="tcop" />
28
<area coords="45, 205, 175, 240" href="backend_dirs.html#parser" alt="parser" />
29
<area coords="45, 270, 175, 300" href="backend_dirs.html#tcop" alt="tcop" />
30
<area coords="255, 270, 380, 300" href="backend_dirs.html#commands" alt="commands" />
31
<area coords="45, 335, 175, 365" href="backend_dirs.html#rewrite" alt="rewrite" />
32
<area coords="45, 400, 175, 430" href="backend_dirs.html#optimizer_path" alt="path" />
33
<area coords="45, 460, 175, 490" href="backend_dirs.html#optimizer_plan" alt="plan" />
34
<area coords="45, 525, 175, 555" href="backend_dirs.html#executor" alt="executor" />
35
<area coords="0, 640, 130, 675" href="backend_dirs.html#utils" alt="utils" />
36
<area coords="175, 640, 300, 675" href="backend_dirs.html#catalog" alt="catalog" />
37
<area coords="330, 640, 475, 675" href="backend_dirs.html#storage" alt="storage" />
38
<area coords="75, 700, 210, 735" href="backend_dirs.html#access" alt="access" />
39
<area coords="255, 705, 380, 735" href="backend_dirs.html#nodes" alt="nodes" />
45
<p>A query comes to the backend via data packets arriving through
46
TCP/IP or Unix Domain sockets. It is loaded into a string, and
47
passed to the <a href="../../backend/parser">parser,</a> where the
48
lexical scanner, <a href="../../backend/parser/scan.l">scan.l,</a>
49
breaks the query up into tokens(words). The parser uses <a
50
href="../../backend/parser/gram.y">gram.y</a> and the tokens to
51
identify the query type, and load the proper query-specific
53
href="../../include/nodes/parsenodes.h">CreateStmt</a> or <a
54
href="../../include/nodes/parsenodes.h">SelectStmt.</a></p>
56
<p>The statement is then identified as complex (<i>SELECT / INSERT /
57
UPDATE / DELETE</i>) or a simple, e.g <i> CREATE USER, ANALYZE, </i>,
58
etc. Simple utility commands are processed by statement-specific
59
functions in <a href="../../backend/commands">backend/commands.</a>
60
Complex statements require more handling.</p>
62
<p>The parser takes a complex query, and creates a <a
63
href="../../include/nodes/parsenodes.h">Query</a> structure that
64
contains all the elements used by complex queries. Query.qual holds
65
the <i>WHERE</i> clause qualification, which is filled in by <a
66
href="../../backend/parser/parse_clause.c">transformWhereClause().</a>
67
Each table referenced in the query is represented by a <a
68
href="../../include/nodes/parsenodes.h">RangeTableEntry,</a> and
69
they are linked together to form the <i>range table</i> of the
70
query, which is generated by <a
71
href="../../backend/parser/parse_clause.c">transformFromClause().</a>
72
Query.rtable holds the query's range table.</p>
74
<p>Certain queries, like <i>SELECT,</i> return columns of data.
75
Other queries, like <i>INSERT</i> and <i>UPDATE,</i> specify the
76
columns modified by the query. These column references are
78
href="../../include/nodes/primnodes.h">TargetEntry</a> entries,
79
which are linked together to make up the <i>target list</i> of the
80
query. The target list is stored in Query.targetList, which is
82
href="../../backend/parser/parse_target.c">transformTargetList().</a></p>
84
<p>Other query elements, like aggregates(<i>SUM()</i>), <i>GROUP
85
BY,</i> and <i>ORDER BY</i> are also stored in their own Query
88
<p>The next step is for the Query to be modified by any
89
<i>VIEWS</i> or <i>RULES</i> that may apply to the query. This is
90
performed by the <a href="../../backend/rewrite">rewrite</a>
93
<p>The <a href="../../backend/optimizer">optimizer</a> takes the
94
Query structure and generates an optimal <a
95
href="../../include/nodes/plannodes.h">Plan,</a> which contains the
96
operations to be performed to execute the query. The <a
97
href="../../backend/optimizer/path">path</a> module determines the
98
best table join order and join type of each table in the
99
RangeTable, using Query.qual(<i>WHERE</i> clause) to consider
100
optimal index usage.</p>
102
<p>The Plan is then passed to the <a
103
href="../../backend/executor">executor</a> for execution, and the
104
result returned to the client. The Plan is actually as set of nodes,
105
arranged in a tree structure with a top-level node, and various
106
sub-nodes as children.</p>
108
<p>There are many other modules that support this basic
109
functionality. They can be accessed by clicking on the
113
<p>Another area of interest is the shared memory area, which
114
contains data accessable to all backends. It has recently used
115
data/index blocks, locks, backend process information, and lookup
116
tables for these structures:</p>
119
<li>ShmemIndex - lookup shared memory addresses using structure
122
<li><a href="../../include/storage/buf_internals.h">Buffer
123
Descriptor</a> - control header for buffer cache block</li>
125
<li><a href="../../include/storage/buf_internals.h">Buffer
126
Block</a> - data/index buffer cache block</li>
128
<li>Shared Buffer Lookup Table - lookup of buffer cache block
129
addresses using table name and block number( <a
130
href="../../include/storage/buf_internals.h">BufferTag</a>)</li>
132
<li>Lock Manager Tables (lock hash) - the <a
133
href="../../include/storage/lock.h">LOCK</a> structure, looked up
134
using a <a href="../../include/storage/lock.h">LOCKTAG</a>.
135
A LOCK structure exists for each lockable object that is currently
136
locked by any backend. Also, there is a subsidiary <a
137
href="../../include/storage/lock.h">PROCLOCK</a> structure for each
138
backend currently interested in a given LOCK</li>
140
<li><a href="../../include/storage/proc.h">PGPROC Structures</a> -
141
information about each backend, including locks held/waiting</li>
144
<p>Each data structure is created by calling <a
145
href="../../backend/storage/ipc/shmem.c">ShmemInitStruct(),</a> and
146
the lookups are created by <a
147
href="../../backend/storage/ipc/shmem.c">ShmemInitHash().</a></p>
150
<small>Maintainer: Bruce Momjian (<a
151
href="mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</a>)<br />
153
Last updated: Fri May 6 14:22:27 EDT 2005</small>