File: protocol.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (200 lines) | stat: -rw-r--r-- 12,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter43.Frontend/Backend Protocol</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="internals.html" title="PartVII.Internals">
<link rel="prev" href="view-pg-views.html" title="42.46.pg_views">
<link rel="next" href="protocol-flow.html" title="43.2.Message Flow">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="protocol">
<div class="titlepage"><div><div><h2 class="title">
<a name="protocol"></a>Chapter43.Frontend/Backend Protocol</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="protocol.html#protocol-overview">43.1. Overview</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="protocol.html#protocol-message-concepts">43.1.1. Messaging Overview</a></span></dt>
<dt><span class="sect2"><a href="protocol.html#protocol-query-concepts">43.1.2. Extended Query Overview</a></span></dt>
<dt><span class="sect2"><a href="protocol.html#protocol-format-codes">43.1.3. Formats and Format Codes</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="protocol-flow.html">43.2. Message Flow</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="protocol-flow.html#id834234">43.2.1. Start-Up</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id834480">43.2.2. Simple Query</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id834738">43.2.3. Extended Query</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id835121">43.2.4. Function Call</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#protocol-copy">43.2.5. COPY Operations</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#protocol-async">43.2.6. Asynchronous Operations</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id835564">43.2.7. Cancelling Requests in Progress</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id835628">43.2.8. Termination</a></span></dt>
<dt><span class="sect2"><a href="protocol-flow.html#id835688">43.2.9. <acronym class="acronym">SSL</acronym> Session Encryption</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="protocol-message-types.html">43.3. Message Data Types</a></span></dt>
<dt><span class="sect1"><a href="protocol-message-formats.html">43.4. Message Formats</a></span></dt>
<dt><span class="sect1"><a href="protocol-error-fields.html">43.5. Error and Notice Message Fields</a></span></dt>
<dt><span class="sect1"><a href="protocol-changes.html">43.6. Summary of Changes since Protocol 2.0</a></span></dt>
</dl>
</div>
<p>  <span class="productname">PostgreSQL</span> uses a message-based protocol
  for communication between frontends and backends (clients and servers).
  The protocol is supported over <acronym class="acronym">TCP/IP</acronym> and also over
  Unix-domain sockets.  Port number 5432 has been registered with IANA as
  the customary TCP port number for servers supporting this protocol, but
  in practice any non-privileged port number may be used.
 </p>
<p>  This document describes version 3.0 of the protocol, implemented in
  <span class="productname">PostgreSQL</span> 7.4 and later.  For descriptions
  of the earlier protocol versions, see previous releases of the
  <span class="productname">PostgreSQL</span> documentation.  A single server
  can support multiple protocol versions.  The initial
  startup-request message tells the server which protocol version the
  client is attempting to use, and then the server follows that protocol
  if it is able.
 </p>
<p>  Higher level features built on this protocol (for example, how
  <span class="application">libpq</span> passes certain environment
  variables when the connection is established) are covered elsewhere.
 </p>
<p>   In order to serve multiple clients efficiently, the server launches
   a new &#8220;<span class="quote">backend</span>&#8221; process for each client.
   In the current implementation, a new child
   process is created immediately after an incoming connection is detected.
   This is transparent to the protocol, however.  For purposes of the
   protocol, the terms &#8220;<span class="quote">backend</span>&#8221; and &#8220;<span class="quote">server</span>&#8221; are
   interchangeable; likewise &#8220;<span class="quote">frontend</span>&#8221; and &#8220;<span class="quote">client</span>&#8221;
   are interchangeable.
  </p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="protocol-overview"></a>43.1.Overview</h2></div></div></div>
<p>   The protocol has separate phases for startup and normal operation.
   In the startup phase, the frontend opens a connection to the server
   and authenticates itself to the satisfaction of the server.  (This might
   involve a single message, or multiple messages depending on the
   authentication method being used.)  If all goes well, the server then sends
   status information to the frontend, and finally enters normal operation.
   Except for the initial startup-request message, this part of the
   protocol is driven by the server.
  </p>
<p>   During normal operation, the frontend sends queries and
   other commands to the backend, and the backend sends back query results
   and other responses.  There are a few cases (such as <code class="command">NOTIFY</code>)
   wherein the
   backend will send unsolicited messages, but for the most part this portion
   of a session is driven by frontend requests.
  </p>
<p>   Termination of the session is normally by frontend choice, but can be
   forced by the backend in certain cases.  In any case, when the backend
   closes the connection, it will roll back any open (incomplete) transaction
   before exiting.
  </p>
<p>   Within normal operation, SQL commands can be executed through either of
   two sub-protocols.  In the &#8220;<span class="quote">simple query</span>&#8221; protocol, the frontend
   just sends a textual query string, which is parsed and immediately
   executed by the backend.  In the &#8220;<span class="quote">extended query</span>&#8221; protocol,
   processing of queries is separated into multiple steps: parsing,
   binding of parameter values, and execution.  This offers flexibility
   and performance benefits, at the cost of extra complexity.
  </p>
<p>   Normal operation has additional sub-protocols for special operations
   such as <code class="command">COPY</code>.
  </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="protocol-message-concepts"></a>43.1.1.Messaging Overview</h3></div></div></div>
<p>   All communication is through a stream of messages.  The first byte of a
   message identifies the message type, and the next four bytes specify the
   length of the rest of the message (this length count includes itself, but
   not the message-type byte).  The remaining contents of the message are
   determined by the message type.  For historical reasons, the very first
   message sent by the client (the startup message) has no initial
   message-type byte.
  </p>
<p>   To avoid losing synchronization with the message stream, both servers and
   clients typically read an entire message into a buffer (using the byte
   count) before attempting to process its contents.  This allows easy
   recovery if an error is detected while processing the contents.  In
   extreme situations (such as not having enough memory to buffer the
   message), the receiver may use the byte count to determine how much
   input to skip before it resumes reading messages.
  </p>
<p>   Conversely, both servers and clients must take care never to send an
   incomplete message.  This is commonly done by marshaling the entire message
   in a buffer before beginning to send it.  If a communications failure
   occurs partway through sending or receiving a message, the only sensible
   response is to abandon the connection, since there is little hope of
   recovering message-boundary synchronization.
  </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="protocol-query-concepts"></a>43.1.2.Extended Query Overview</h3></div></div></div>
<p>    In the extended-query protocol, execution of SQL commands is divided
    into multiple steps.  The state retained between steps is represented
    by two types of objects: <em class="firstterm">prepared statements</em> and
    <em class="firstterm">portals</em>.  A prepared statement represents the result of
    parsing, semantic analysis, and planning of a textual query string.  A
    prepared statement is not necessarily ready to execute, because it may
    lack specific values for <em class="firstterm">parameters</em>.  A portal represents
    a ready-to-execute or already-partially-executed statement, with any
    missing parameter values filled in.  (For <code class="command">SELECT</code> statements,
    a portal is equivalent to an open cursor, but we choose to use a different
    term since cursors don't handle non-<code class="command">SELECT</code> statements.)
   </p>
<p>    The overall execution cycle consists of a <em class="firstterm">parse</em> step,
    which creates a prepared statement from a textual query string; a
    <em class="firstterm">bind</em> step, which creates a portal given a prepared
    statement and values for any needed parameters; and an
    <em class="firstterm">execute</em> step that runs a portal's query.  In the case of
    a query that returns rows (<code class="command">SELECT</code>, <code class="command">SHOW</code>, etc),
    the execute step can be told to fetch only
    a limited number of rows, so that multiple execute steps may be needed
    to complete the operation.
   </p>
<p>    The backend can keep track of multiple prepared statements and portals
    (but note that these exist only within a session, and are never shared
    across sessions).  Existing prepared statements and portals are
    referenced by names assigned when they were created.  In addition,
    an &#8220;<span class="quote">unnamed</span>&#8221; prepared statement and portal exist.  Although these
    behave largely the same as named objects, operations on them are optimized
    for the case of executing a query only once and then discarding it,
    whereas operations on named objects are optimized on the expectation
    of multiple uses.
   </p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="protocol-format-codes"></a>43.1.3.Formats and Format Codes</h3></div></div></div>
<p>    Data of a particular data type might be transmitted in any of several
    different <em class="firstterm">formats</em>.  As of <span class="productname">PostgreSQL</span> 7.4
    the only supported formats are &#8220;<span class="quote">text</span>&#8221; and &#8220;<span class="quote">binary</span>&#8221;,
    but the protocol makes provision for future extensions.  The desired
    format for any value is specified by a <em class="firstterm">format code</em>.
    Clients may specify a format code for each transmitted parameter value
    and for each column of a query result.  Text has format code zero,
    binary has format code one, and all other format codes are reserved
    for future definition.
   </p>
<p>    The text representation of values is whatever strings are produced
    and accepted by the input/output conversion functions for the
    particular data type.  In the transmitted representation, there is
    no trailing null character; the frontend must add one to received
    values if it wants to process them as C strings.
    (The text format does not allow embedded nulls, by the way.)
   </p>
<p>    Binary representations for integers use network byte order (most
    significant byte first).  For other data types consult the documentation
    or source code to learn about the binary representation.  Keep in mind
    that binary representations for complex data types may change across
    server versions; the text format is usually the more portable choice.
   </p>
</div>
</div>
</div></body>
</html>