]> granicus.if.org Git - postgresql/blob - doc/src/sgml/protocol.sgml
8778c58219bd8df84d9e2fec3c7b4fbf3c75fa69
[postgresql] / doc / src / sgml / protocol.sgml
1 <!-- doc/src/sgml/protocol.sgml -->
2
3 <chapter id="protocol">
4  <title>Frontend/Backend Protocol</title>
5
6  <indexterm zone="protocol">
7   <primary>protocol</primary>
8   <secondary>frontend-backend</secondary>
9  </indexterm>
10
11  <para>
12   <productname>PostgreSQL</productname> uses a message-based protocol
13   for communication between frontends and backends (clients and servers).
14   The protocol is supported over <acronym>TCP/IP</acronym> and also over
15   Unix-domain sockets.  Port number 5432 has been registered with IANA as
16   the customary TCP port number for servers supporting this protocol, but
17   in practice any non-privileged port number can be used.
18  </para>
19
20  <para>
21   This document describes version 3.0 of the protocol, implemented in
22   <productname>PostgreSQL</productname> 7.4 and later.  For descriptions
23   of the earlier protocol versions, see previous releases of the
24   <productname>PostgreSQL</productname> documentation.  A single server
25   can support multiple protocol versions.  The initial
26   startup-request message tells the server which protocol version the
27   client is attempting to use, and then the server follows that protocol
28   if it is able.
29  </para>
30
31   <para>
32    In order to serve multiple clients efficiently, the server launches
33    a new <quote>backend</> process for each client.
34    In the current implementation, a new child
35    process is created immediately after an incoming connection is detected.
36    This is transparent to the protocol, however.  For purposes of the
37    protocol, the terms <quote>backend</> and <quote>server</> are
38    interchangeable; likewise <quote>frontend</> and <quote>client</>
39    are interchangeable.
40   </para>
41
42  <sect1 id="protocol-overview">
43   <title>Overview</title>
44
45   <para>
46    The protocol has separate phases for startup and normal operation.
47    In the startup phase, the frontend opens a connection to the server
48    and authenticates itself to the satisfaction of the server.  (This might
49    involve a single message, or multiple messages depending on the
50    authentication method being used.)  If all goes well, the server then sends
51    status information to the frontend, and finally enters normal operation.
52    Except for the initial startup-request message, this part of the
53    protocol is driven by the server.
54   </para>
55
56   <para>
57    During normal operation, the frontend sends queries and
58    other commands to the backend, and the backend sends back query results
59    and other responses.  There are a few cases (such as <command>NOTIFY</>)
60    wherein the
61    backend will send unsolicited messages, but for the most part this portion
62    of a session is driven by frontend requests.
63   </para>
64
65   <para>
66    Termination of the session is normally by frontend choice, but can be
67    forced by the backend in certain cases.  In any case, when the backend
68    closes the connection, it will roll back any open (incomplete) transaction
69    before exiting.
70   </para>
71
72   <para>
73    Within normal operation, SQL commands can be executed through either of
74    two sub-protocols.  In the <quote>simple query</> protocol, the frontend
75    just sends a textual query string, which is parsed and immediately
76    executed by the backend.  In the <quote>extended query</> protocol,
77    processing of queries is separated into multiple steps: parsing,
78    binding of parameter values, and execution.  This offers flexibility
79    and performance benefits, at the cost of extra complexity.
80   </para>
81
82   <para>
83    Normal operation has additional sub-protocols for special operations
84    such as <command>COPY</>.
85   </para>
86
87  <sect2 id="protocol-message-concepts">
88   <title>Messaging Overview</title>
89
90   <para>
91    All communication is through a stream of messages.  The first byte of a
92    message identifies the message type, and the next four bytes specify the
93    length of the rest of the message (this length count includes itself, but
94    not the message-type byte).  The remaining contents of the message are
95    determined by the message type.  For historical reasons, the very first
96    message sent by the client (the startup message) has no initial
97    message-type byte.
98   </para>
99
100   <para>
101    To avoid losing synchronization with the message stream, both servers and
102    clients typically read an entire message into a buffer (using the byte
103    count) before attempting to process its contents.  This allows easy
104    recovery if an error is detected while processing the contents.  In
105    extreme situations (such as not having enough memory to buffer the
106    message), the receiver can use the byte count to determine how much
107    input to skip before it resumes reading messages.
108   </para>
109
110   <para>
111    Conversely, both servers and clients must take care never to send an
112    incomplete message.  This is commonly done by marshaling the entire message
113    in a buffer before beginning to send it.  If a communications failure
114    occurs partway through sending or receiving a message, the only sensible
115    response is to abandon the connection, since there is little hope of
116    recovering message-boundary synchronization.
117   </para>
118  </sect2>
119
120   <sect2 id="protocol-query-concepts">
121    <title>Extended Query Overview</title>
122
123    <para>
124     In the extended-query protocol, execution of SQL commands is divided
125     into multiple steps.  The state retained between steps is represented
126     by two types of objects: <firstterm>prepared statements</> and
127     <firstterm>portals</>.  A prepared statement represents the result of
128     parsing and semantic analysis of a textual query string.
129     A prepared statement is not in itself ready to execute, because it might
130     lack specific values for <firstterm>parameters</>.  A portal represents
131     a ready-to-execute or already-partially-executed statement, with any
132     missing parameter values filled in.  (For <command>SELECT</> statements,
133     a portal is equivalent to an open cursor, but we choose to use a different
134     term since cursors don't handle non-<command>SELECT</> statements.)
135    </para>
136
137    <para>
138     The overall execution cycle consists of a <firstterm>parse</> step,
139     which creates a prepared statement from a textual query string; a
140     <firstterm>bind</> step, which creates a portal given a prepared
141     statement and values for any needed parameters; and an
142     <firstterm>execute</> step that runs a portal's query.  In the case of
143     a query that returns rows (<command>SELECT</>, <command>SHOW</>, etc),
144     the execute step can be told to fetch only
145     a limited number of rows, so that multiple execute steps might be needed
146     to complete the operation.
147    </para>
148
149    <para>
150     The backend can keep track of multiple prepared statements and portals
151     (but note that these exist only within a session, and are never shared
152     across sessions).  Existing prepared statements and portals are
153     referenced by names assigned when they were created.  In addition,
154     an <quote>unnamed</> prepared statement and portal exist.  Although these
155     behave largely the same as named objects, operations on them are optimized
156     for the case of executing a query only once and then discarding it,
157     whereas operations on named objects are optimized on the expectation
158     of multiple uses.
159    </para>
160   </sect2>
161
162   <sect2 id="protocol-format-codes">
163    <title>Formats and Format Codes</title>
164
165    <para>
166     Data of a particular data type might be transmitted in any of several
167     different <firstterm>formats</>.  As of <productname>PostgreSQL</> 7.4
168     the only supported formats are <quote>text</> and <quote>binary</>,
169     but the protocol makes provision for future extensions.  The desired
170     format for any value is specified by a <firstterm>format code</>.
171     Clients can specify a format code for each transmitted parameter value
172     and for each column of a query result.  Text has format code zero,
173     binary has format code one, and all other format codes are reserved
174     for future definition.
175    </para>
176
177    <para>
178     The text representation of values is whatever strings are produced
179     and accepted by the input/output conversion functions for the
180     particular data type.  In the transmitted representation, there is
181     no trailing null character; the frontend must add one to received
182     values if it wants to process them as C strings.
183     (The text format does not allow embedded nulls, by the way.)
184    </para>
185
186    <para>
187     Binary representations for integers use network byte order (most
188     significant byte first).  For other data types consult the documentation
189     or source code to learn about the binary representation.  Keep in mind
190     that binary representations for complex data types might change across
191     server versions; the text format is usually the more portable choice.
192    </para>
193   </sect2>
194  </sect1>
195
196  <sect1 id="protocol-flow">
197   <title>Message Flow</title>
198
199   <para>
200    This section describes the message flow and the semantics of each
201    message type.  (Details of the exact representation of each message
202    appear in <xref linkend="protocol-message-formats">.)  There are
203    several different sub-protocols depending on the state of the
204    connection: start-up, query, function call,
205    <command>COPY</command>, and termination.  There are also special
206    provisions for asynchronous operations (including notification
207    responses and command cancellation), which can occur at any time
208    after the start-up phase.
209   </para>
210
211   <sect2>
212    <title>Start-up</title>
213
214    <para>
215     To begin a session, a frontend opens a connection to the server and sends
216     a startup message.  This message includes the names of the user and of the
217     database the user wants to connect to; it also identifies the particular
218     protocol version to be used.  (Optionally, the startup message can include
219     additional settings for run-time parameters.)
220     The server then uses this information and
221     the contents of its configuration files (such as
222     <filename>pg_hba.conf</filename>) to determine
223     whether the connection is provisionally acceptable, and what additional
224     authentication is required (if any).
225    </para>
226
227    <para>
228     The server then sends an appropriate authentication request message,
229     to which the frontend must reply with an appropriate authentication
230     response message (such as a password).
231     For all authentication methods except GSSAPI and SSPI, there is at most
232     one request and one response. In some methods, no response
233     at all is needed from the frontend, and so no authentication request
234     occurs. For GSSAPI and SSPI, multiple exchanges of packets may be needed
235     to complete the authentication.
236    </para>
237
238    <para>
239     The authentication cycle ends with the server either rejecting the
240     connection attempt (ErrorResponse), or sending AuthenticationOk.
241    </para>
242
243    <para>
244     The possible messages from the server in this phase are:
245
246     <variablelist>
247      <varlistentry>
248       <term>ErrorResponse</term>
249       <listitem>
250        <para>
251         The connection attempt has been rejected.
252         The server then immediately closes the connection.
253        </para>
254       </listitem>
255      </varlistentry>
256
257      <varlistentry>
258       <term>AuthenticationOk</term>
259       <listitem>
260        <para>
261         The authentication exchange is successfully completed.
262        </para>
263       </listitem>
264      </varlistentry>
265
266      <varlistentry>
267       <term>AuthenticationKerberosV5</term>
268       <listitem>
269        <para>
270         The frontend must now take part in a Kerberos V5
271         authentication dialog (not described here, part of the
272         Kerberos specification) with the server.  If this is
273         successful, the server responds with an AuthenticationOk,
274         otherwise it responds with an ErrorResponse.
275        </para>
276       </listitem>
277      </varlistentry>
278
279      <varlistentry>
280       <term>AuthenticationCleartextPassword</term>
281       <listitem>
282        <para>
283         The frontend must now send a PasswordMessage containing the
284         password in clear-text form.  If
285         this is the correct password, the server responds with an
286         AuthenticationOk, otherwise it responds with an ErrorResponse.
287        </para>
288       </listitem>
289      </varlistentry>
290
291      <varlistentry>
292       <term>AuthenticationMD5Password</term>
293       <listitem>
294        <para>
295         The frontend must now send a PasswordMessage containing the
296         password (with username) encrypted via MD5, then encrypted
297         again using the 4-byte random salt specified in the
298         AuthenticationMD5Password message.  If this is the correct
299         password, the server responds with an AuthenticationOk,
300         otherwise it responds with an ErrorResponse.  The actual
301         PasswordMessage can be computed in SQL as <literal>concat('md5',
302         md5(concat(md5(concat(password, username)), random-salt)))</>.
303         (Keep in mind the <function>md5()</> function returns its
304         result as a hex string.)
305        </para>
306       </listitem>
307      </varlistentry>
308
309      <varlistentry>
310       <term>AuthenticationSCMCredential</term>
311       <listitem>
312        <para>
313         This response is only possible for local Unix-domain connections
314         on platforms that support SCM credential messages.  The frontend
315         must issue an SCM credential message and then send a single data
316         byte.  (The contents of the data byte are uninteresting; it's
317         only used to ensure that the server waits long enough to receive
318         the credential message.)  If the credential is acceptable,
319         the server responds with an
320         AuthenticationOk, otherwise it responds with an ErrorResponse.
321         (This message type is only issued by pre-9.1 servers.  It may
322         eventually be removed from the protocol specification.)
323        </para>
324       </listitem>
325      </varlistentry>
326
327      <varlistentry>
328       <term>AuthenticationGSS</term>
329       <listitem>
330        <para>
331         The frontend must now initiate a GSSAPI negotiation. The frontend
332         will send a PasswordMessage with the first part of the GSSAPI
333         data stream in response to this. If further messages are needed,
334         the server will respond with AuthenticationGSSContinue.
335        </para>
336       </listitem>
337      </varlistentry>
338
339      <varlistentry>
340       <term>AuthenticationSSPI</term>
341       <listitem>
342        <para>
343         The frontend must now initiate a SSPI negotiation. The frontend
344         will send a PasswordMessage with the first part of the SSPI
345         data stream in response to this. If further messages are needed,
346         the server will respond with AuthenticationGSSContinue.
347        </para>
348       </listitem>
349
350      </varlistentry>
351      <varlistentry>
352       <term>AuthenticationGSSContinue</term>
353       <listitem>
354        <para>
355         This message contains the response data from the previous step
356         of GSSAPI or SSPI negotiation (AuthenticationGSS, AuthenticationSSPI
357         or a previous AuthenticationGSSContinue). If the GSSAPI
358         or SSPI data in this message
359         indicates more data is needed to complete the authentication,
360         the frontend must send that data as another PasswordMessage. If
361         GSSAPI or SSPI authentication is completed by this message, the server
362         will next send AuthenticationOk to indicate successful authentication
363         or ErrorResponse to indicate failure.
364        </para>
365       </listitem>
366      </varlistentry>
367
368     </variablelist>
369    </para>
370
371    <para>
372     If the frontend does not support the authentication method
373     requested by the server, then it should immediately close the
374     connection.
375    </para>
376
377    <para>
378     After having received AuthenticationOk, the frontend must wait
379     for further messages from the server.  In this phase a backend process
380     is being started, and the frontend is just an interested bystander.
381     It is still possible for the startup attempt
382     to fail (ErrorResponse), but in the normal case the backend will send
383     some ParameterStatus messages, BackendKeyData, and finally ReadyForQuery.
384    </para>
385
386    <para>
387     During this phase the backend will attempt to apply any additional
388     run-time parameter settings that were given in the startup message.
389     If successful, these values become session defaults.  An error causes
390     ErrorResponse and exit.
391    </para>
392
393    <para>
394     The possible messages from the backend in this phase are:
395
396     <variablelist>
397      <varlistentry>
398       <term>BackendKeyData</term>
399       <listitem>
400        <para>
401         This message provides secret-key data that the frontend must
402         save if it wants to be able to issue cancel requests later.
403         The frontend should not respond to this message, but should
404         continue listening for a ReadyForQuery message.
405        </para>
406       </listitem>
407      </varlistentry>
408
409      <varlistentry>
410       <term>ParameterStatus</term>
411       <listitem>
412        <para>
413         This message informs the frontend about the current (initial)
414          setting of backend parameters, such as <xref
415          linkend="guc-client-encoding"> or <xref linkend="guc-datestyle">.
416          The frontend can ignore this message, or record the settings
417          for its future use; see <xref linkend="protocol-async"> for
418          more details.  The frontend should not respond to this
419          message, but should continue listening for a ReadyForQuery
420          message.
421        </para>
422       </listitem>
423      </varlistentry>
424
425      <varlistentry>
426       <term>ReadyForQuery</term>
427       <listitem>
428        <para>
429         Start-up is completed.  The frontend can now issue commands.
430        </para>
431       </listitem>
432      </varlistentry>
433
434      <varlistentry>
435       <term>ErrorResponse</term>
436       <listitem>
437        <para>
438         Start-up failed.  The connection is closed after sending this
439         message.
440        </para>
441       </listitem>
442      </varlistentry>
443
444      <varlistentry>
445       <term>NoticeResponse</term>
446       <listitem>
447        <para>
448         A warning message has been issued.  The frontend should
449         display the message but continue listening for ReadyForQuery
450         or ErrorResponse.
451        </para>
452       </listitem>
453      </varlistentry>
454     </variablelist>
455    </para>
456
457    <para>
458     The ReadyForQuery message is the same one that the backend will
459     issue after each command cycle.  Depending on the coding needs of
460     the frontend, it is reasonable to consider ReadyForQuery as
461     starting a command cycle, or to consider ReadyForQuery as ending the
462     start-up phase and each subsequent command cycle.
463    </para>
464   </sect2>
465
466   <sect2>
467    <title>Simple Query</title>
468
469    <para>
470     A simple query cycle is initiated by the frontend sending a Query message
471     to the backend.  The message includes an SQL command (or commands)
472     expressed as a text string.
473     The backend then sends one or more response
474     messages depending on the contents of the query command string,
475     and finally a ReadyForQuery response message.  ReadyForQuery
476     informs the frontend that it can safely send a new command.
477     (It is not actually necessary for the frontend to wait for
478     ReadyForQuery before issuing another command, but the frontend must
479     then take responsibility for figuring out what happens if the earlier
480     command fails and already-issued later commands succeed.)
481    </para>
482
483    <para>
484     The possible response messages from the backend are:
485
486     <variablelist>
487      <varlistentry>
488       <term>CommandComplete</term>
489       <listitem>
490        <para>
491         An SQL command completed normally.
492        </para>
493       </listitem>
494      </varlistentry>
495
496      <varlistentry>
497       <term>CopyInResponse</term>
498       <listitem>
499        <para>
500         The backend is ready to copy data from the frontend to a
501         table; see <xref linkend="protocol-copy">.
502        </para>
503       </listitem>
504      </varlistentry>
505
506      <varlistentry>
507       <term>CopyOutResponse</term>
508       <listitem>
509        <para>
510         The backend is ready to copy data from a table to the
511         frontend; see <xref linkend="protocol-copy">.
512        </para>
513       </listitem>
514      </varlistentry>
515
516      <varlistentry>
517       <term>RowDescription</term>
518       <listitem>
519        <para>
520         Indicates that rows are about to be returned in response to
521         a <command>SELECT</command>, <command>FETCH</command>, etc query.
522         The contents of this message describe the column layout of the rows.
523         This will be followed by a DataRow message for each row being returned
524         to the frontend.
525        </para>
526       </listitem>
527      </varlistentry>
528
529      <varlistentry>
530       <term>DataRow</term>
531       <listitem>
532        <para>
533         One of the set of rows returned by
534         a <command>SELECT</command>, <command>FETCH</command>, etc query.
535        </para>
536       </listitem>
537      </varlistentry>
538
539      <varlistentry>
540       <term>EmptyQueryResponse</term>
541       <listitem>
542        <para>
543         An empty query string was recognized.
544        </para>
545       </listitem>
546      </varlistentry>
547
548      <varlistentry>
549       <term>ErrorResponse</term>
550       <listitem>
551        <para>
552         An error has occurred.
553        </para>
554       </listitem>
555      </varlistentry>
556
557      <varlistentry>
558       <term>ReadyForQuery</term>
559       <listitem>
560        <para>
561         Processing of the query string is complete.  A separate
562         message is sent to indicate this because the query string might
563         contain multiple SQL commands.  (CommandComplete marks the
564         end of processing one SQL command, not the whole string.)
565         ReadyForQuery will always be sent, whether processing
566         terminates successfully or with an error.
567        </para>
568       </listitem>
569      </varlistentry>
570
571      <varlistentry>
572       <term>NoticeResponse</term>
573       <listitem>
574        <para>
575         A warning message has been issued in relation to the query.
576         Notices are in addition to other responses, i.e., the backend
577         will continue processing the command.
578        </para>
579       </listitem>
580      </varlistentry>
581
582     </variablelist>
583    </para>
584
585    <para>
586     The response to a <command>SELECT</> query (or other queries that
587     return row sets, such as <command>EXPLAIN</> or <command>SHOW</>)
588     normally consists of RowDescription, zero or more
589     DataRow messages, and then CommandComplete.
590     <command>COPY</> to or from the frontend invokes special protocol
591     as described in <xref linkend="protocol-copy">.
592     All other query types normally produce only
593     a CommandComplete message.
594    </para>
595
596    <para>
597     Since a query string could contain several queries (separated by
598     semicolons), there might be several such response sequences before the
599     backend finishes processing the query string.  ReadyForQuery is issued
600     when the entire string has been processed and the backend is ready to
601     accept a new query string.
602    </para>
603
604    <para>
605     If a completely empty (no contents other than whitespace) query string
606     is received, the response is EmptyQueryResponse followed by ReadyForQuery.
607    </para>
608
609    <para>
610     In the event of an error, ErrorResponse is issued followed by
611     ReadyForQuery.  All further processing of the query string is aborted by
612     ErrorResponse (even if more queries remained in it).  Note that this
613     might occur partway through the sequence of messages generated by an
614     individual query.
615    </para>
616
617    <para>
618     In simple Query mode, the format of retrieved values is always text,
619     except when the given command is a <command>FETCH</> from a cursor
620     declared with the <literal>BINARY</> option.  In that case, the
621     retrieved values are in binary format.  The format codes given in
622     the RowDescription message tell which format is being used.
623    </para>
624
625    <para>
626     A frontend must be prepared to accept ErrorResponse and
627     NoticeResponse messages whenever it is expecting any other type of
628     message.  See also <xref linkend="protocol-async"> concerning messages
629     that the backend might generate due to outside events.
630    </para>
631
632    <para>
633     Recommended practice is to code frontends in a state-machine style
634     that will accept any message type at any time that it could make sense,
635     rather than wiring in assumptions about the exact sequence of messages.
636    </para>
637   </sect2>
638
639   <sect2 id="protocol-flow-ext-query">
640    <title>Extended Query</title>
641
642    <para>
643     The extended query protocol breaks down the above-described simple
644     query protocol into multiple steps.  The results of preparatory
645     steps can be re-used multiple times for improved efficiency.
646     Furthermore, additional features are available, such as the possibility
647     of supplying data values as separate parameters instead of having to
648     insert them directly into a query string.
649    </para>
650
651    <para>
652     In the extended protocol, the frontend first sends a Parse message,
653     which contains a textual query string, optionally some information
654     about data types of parameter placeholders, and the
655     name of a destination prepared-statement object (an empty string
656     selects the unnamed prepared statement).  The response is
657     either ParseComplete or ErrorResponse.  Parameter data types can be
658     specified by OID; if not given, the parser attempts to infer the
659     data types in the same way as it would do for untyped literal string
660     constants.
661    </para>
662
663    <note>
664     <para>
665      A parameter data type can be left unspecified by setting it to zero,
666      or by making the array of parameter type OIDs shorter than the
667      number of parameter symbols (<literal>$</><replaceable>n</>)
668      used in the query string.  Another special case is that a parameter's
669      type can be specified as <type>void</> (that is, the OID of the
670      <type>void</> pseudotype).  This is meant to allow parameter symbols
671      to be used for function parameters that are actually OUT parameters.
672      Ordinarily there is no context in which a <type>void</> parameter
673      could be used, but if such a parameter symbol appears in a function's
674      parameter list, it is effectively ignored.  For example, a function
675      call such as <literal>foo($1,$2,$3,$4)</> could match a function with
676      two IN and two OUT arguments, if <literal>$3</> and <literal>$4</>
677      are specified as having type <type>void</>.
678     </para>
679    </note>
680
681    <note>
682     <para>
683      The query string contained in a Parse message cannot include more
684      than one SQL statement; else a syntax error is reported.  This
685      restriction does not exist in the simple-query protocol, but it
686      does exist in the extended protocol, because allowing prepared
687      statements or portals to contain multiple commands would complicate
688      the protocol unduly.
689     </para>
690    </note>
691
692    <para>
693     If successfully created, a named prepared-statement object lasts till
694     the end of the current session, unless explicitly destroyed.  An unnamed
695     prepared statement lasts only until the next Parse statement specifying
696     the unnamed statement as destination is issued.  (Note that a simple
697     Query message also destroys the unnamed statement.)  Named prepared
698     statements must be explicitly closed before they can be redefined by
699     another Parse message, but this is not required for the unnamed statement.
700     Named prepared statements can also be created and accessed at the SQL
701     command level, using <command>PREPARE</> and <command>EXECUTE</>.
702    </para>
703
704    <para>
705     Once a prepared statement exists, it can be readied for execution using a
706     Bind message.  The Bind message gives the name of the source prepared
707     statement (empty string denotes the unnamed prepared statement), the name
708     of the destination portal (empty string denotes the unnamed portal), and
709     the values to use for any parameter placeholders present in the prepared
710     statement.  The
711     supplied parameter set must match those needed by the prepared statement.
712     (If you declared any <type>void</> parameters in the Parse message,
713     pass NULL values for them in the Bind message.)
714     Bind also specifies the format to use for any data returned
715     by the query; the format can be specified overall, or per-column.
716     The response is either BindComplete or ErrorResponse.
717    </para>
718
719    <note>
720     <para>
721      The choice between text and binary output is determined by the format
722      codes given in Bind, regardless of the SQL command involved.  The
723      <literal>BINARY</> attribute in cursor declarations is irrelevant when
724      using extended query protocol.
725     </para>
726    </note>
727
728    <para>
729     Query planning typically occurs when the Bind message is processed.
730     If the prepared statement has no parameters, or is executed repeatedly,
731     the server might save the created plan and re-use it during subsequent
732     Bind messages for the same prepared statement.  However, it will do so
733     only if it finds that a generic plan can be created that is not much
734     less efficient than a plan that depends on the specific parameter values
735     supplied.  This happens transparently so far as the protocol is concerned.
736    </para>
737
738    <para>
739     If successfully created, a named portal object lasts till the end of the
740     current transaction, unless explicitly destroyed.  An unnamed portal is
741     destroyed at the end of the transaction, or as soon as the next Bind
742     statement specifying the unnamed portal as destination is issued.  (Note
743     that a simple Query message also destroys the unnamed portal.)  Named
744     portals must be explicitly closed before they can be redefined by another
745     Bind message, but this is not required for the unnamed portal.
746     Named portals can also be created and accessed at the SQL
747     command level, using <command>DECLARE CURSOR</> and <command>FETCH</>.
748    </para>
749
750    <para>
751     Once a portal exists, it can be executed using an Execute message.
752     The Execute message specifies the portal name (empty string denotes the
753     unnamed portal) and
754     a maximum result-row count (zero meaning <quote>fetch all rows</>).
755     The result-row count is only meaningful for portals
756     containing commands that return row sets; in other cases the command is
757     always executed to completion, and the row count is ignored.
758     The possible
759     responses to Execute are the same as those described above for queries
760     issued via simple query protocol, except that Execute doesn't cause
761     ReadyForQuery or RowDescription to be issued.
762    </para>
763
764    <para>
765     If Execute terminates before completing the execution of a portal
766     (due to reaching a nonzero result-row count), it will send a
767     PortalSuspended message; the appearance of this message tells the frontend
768     that another Execute should be issued against the same portal to
769     complete the operation.  The CommandComplete message indicating
770     completion of the source SQL command is not sent until
771     the portal's execution is completed.  Therefore, an Execute phase is
772     always terminated by the appearance of exactly one of these messages:
773     CommandComplete, EmptyQueryResponse (if the portal was created from
774     an empty query string), ErrorResponse, or PortalSuspended.
775    </para>
776
777    <para>
778     At completion of each series of extended-query messages, the frontend
779     should issue a Sync message.  This parameterless message causes the
780     backend to close the current transaction if it's not inside a
781     <command>BEGIN</>/<command>COMMIT</> transaction block (<quote>close</>
782     meaning to commit if no error, or roll back if error).  Then a
783     ReadyForQuery response is issued.  The purpose of Sync is to provide
784     a resynchronization point for error recovery.  When an error is detected
785     while processing any extended-query message, the backend issues
786     ErrorResponse, then reads and discards messages until a Sync is reached,
787     then issues ReadyForQuery and returns to normal message processing.
788     (But note that no skipping occurs if an error is detected
789     <emphasis>while</> processing Sync &mdash; this ensures that there is one
790     and only one ReadyForQuery sent for each Sync.)
791    </para>
792
793    <note>
794     <para>
795      Sync does not cause a transaction block opened with <command>BEGIN</>
796      to be closed.  It is possible to detect this situation since the
797      ReadyForQuery message includes transaction status information.
798     </para>
799    </note>
800
801    <para>
802     In addition to these fundamental, required operations, there are several
803     optional operations that can be used with extended-query protocol.
804    </para>
805
806    <para>
807     The Describe message (portal variant) specifies the name of an existing
808     portal (or an empty string for the unnamed portal).  The response is a
809     RowDescription message describing the rows that will be returned by
810     executing the portal; or a NoData message if the portal does not contain a
811     query that will return rows; or ErrorResponse if there is no such portal.
812    </para>
813
814    <para>
815     The Describe message (statement variant) specifies the name of an existing
816     prepared statement (or an empty string for the unnamed prepared
817     statement).  The response is a ParameterDescription message describing the
818     parameters needed by the statement, followed by a RowDescription message
819     describing the rows that will be returned when the statement is eventually
820     executed (or a NoData message if the statement will not return rows).
821     ErrorResponse is issued if there is no such prepared statement.  Note that
822     since Bind has not yet been issued, the formats to be used for returned
823     columns are not yet known to the backend; the format code fields in the
824     RowDescription message will be zeroes in this case.
825    </para>
826
827    <tip>
828     <para>
829      In most scenarios the frontend should issue one or the other variant
830      of Describe before issuing Execute, to ensure that it knows how to
831      interpret the results it will get back.
832     </para>
833    </tip>
834
835    <para>
836     The Close message closes an existing prepared statement or portal
837     and releases resources.  It is not an error to issue Close against
838     a nonexistent statement or portal name.  The response is normally
839     CloseComplete, but could be ErrorResponse if some difficulty is
840     encountered while releasing resources.  Note that closing a prepared
841     statement implicitly closes any open portals that were constructed
842     from that statement.
843    </para>
844
845    <para>
846     The Flush message does not cause any specific output to be generated,
847     but forces the backend to deliver any data pending in its output
848     buffers.  A Flush must be sent after any extended-query command except
849     Sync, if the frontend wishes to examine the results of that command before
850     issuing more commands.  Without Flush, messages returned by the backend
851     will be combined into the minimum possible number of packets to minimize
852     network overhead.
853    </para>
854
855    <note>
856     <para>
857      The simple Query message is approximately equivalent to the series Parse,
858      Bind, portal Describe, Execute, Close, Sync, using the unnamed prepared
859      statement and portal objects and no parameters.  One difference is that
860      it will accept multiple SQL statements in the query string, automatically
861      performing the bind/describe/execute sequence for each one in succession.
862      Another difference is that it will not return ParseComplete, BindComplete,
863      CloseComplete, or NoData messages.
864     </para>
865    </note>
866   </sect2>
867
868   <sect2>
869    <title>Function Call</title>
870
871    <para>
872     The Function Call sub-protocol allows the client to request a direct
873     call of any function that exists in the database's
874     <structname>pg_proc</structname> system catalog.  The client must have
875     execute permission for the function.
876    </para>
877
878    <note>
879     <para>
880      The Function Call sub-protocol is a legacy feature that is probably best
881      avoided in new code.  Similar results can be accomplished by setting up
882      a prepared statement that does <literal>SELECT function($1, ...)</>.
883      The Function Call cycle can then be replaced with Bind/Execute.
884     </para>
885    </note>
886
887    <para>
888     A Function Call cycle is initiated by the frontend sending a
889     FunctionCall message to the backend.  The backend then sends one
890     or more response messages depending on the results of the function
891     call, and finally a ReadyForQuery response message.  ReadyForQuery
892     informs the frontend that it can safely send a new query or
893     function call.
894    </para>
895
896    <para>
897     The possible response messages from the backend are:
898
899     <variablelist>
900      <varlistentry>
901       <term>ErrorResponse</term>
902       <listitem>
903        <para>
904         An error has occurred.
905        </para>
906       </listitem>
907      </varlistentry>
908
909      <varlistentry>
910       <term>FunctionCallResponse</term>
911       <listitem>
912        <para>
913         The function call was completed and returned the result given
914         in the message.
915         (Note that the Function Call protocol can only handle a single
916         scalar result, not a row type or set of results.)
917        </para>
918       </listitem>
919      </varlistentry>
920
921      <varlistentry>
922       <term>ReadyForQuery</term>
923       <listitem>
924        <para>
925         Processing of the function call is complete.  ReadyForQuery
926         will always be sent, whether processing terminates
927         successfully or with an error.
928        </para>
929       </listitem>
930      </varlistentry>
931
932      <varlistentry>
933       <term>NoticeResponse</term>
934       <listitem>
935        <para>
936         A warning message has been issued in relation to the function
937         call.  Notices are in addition to other responses, i.e., the
938         backend will continue processing the command.
939        </para>
940       </listitem>
941      </varlistentry>
942     </variablelist>
943    </para>
944   </sect2>
945
946   <sect2 id="protocol-copy">
947    <title>COPY Operations</title>
948
949    <para>
950     The <command>COPY</> command allows high-speed bulk data transfer
951     to or from the server.  Copy-in and copy-out operations each switch
952     the connection into a distinct sub-protocol, which lasts until the
953     operation is completed.
954    </para>
955
956    <para>
957     Copy-in mode (data transfer to the server) is initiated when the
958     backend executes a <command>COPY FROM STDIN</> SQL statement.  The backend
959     sends a CopyInResponse message to the frontend.  The frontend should
960     then send zero or more CopyData messages, forming a stream of input
961     data.  (The message boundaries are not required to have anything to do
962     with row boundaries, although that is often a reasonable choice.)
963     The frontend can terminate the copy-in mode by sending either a CopyDone
964     message (allowing successful termination) or a CopyFail message (which
965     will cause the <command>COPY</> SQL statement to fail with an
966     error).  The backend then reverts to the command-processing mode it was
967     in before the <command>COPY</> started, which will be either simple or
968     extended query protocol.  It will next send either CommandComplete
969     (if successful) or ErrorResponse (if not).
970    </para>
971
972    <para>
973     In the event of a backend-detected error during copy-in mode (including
974     receipt of a CopyFail message), the backend will issue an ErrorResponse
975     message.  If the <command>COPY</> command was issued via an extended-query
976     message, the backend will now discard frontend messages until a Sync
977     message is received, then it will issue ReadyForQuery and return to normal
978     processing.  If the <command>COPY</> command was issued in a simple
979     Query message, the rest of that message is discarded and ReadyForQuery
980     is issued.  In either case, any subsequent CopyData, CopyDone, or CopyFail
981     messages issued by the frontend will simply be dropped.
982    </para>
983
984    <para>
985     The backend will ignore Flush and Sync messages received during copy-in
986     mode.  Receipt of any other non-copy message type constitutes an error
987     that will abort the copy-in state as described above.  (The exception for
988     Flush and Sync is for the convenience of client libraries that always
989     send Flush or Sync after an Execute message, without checking whether
990     the command to be executed is a <command>COPY FROM STDIN</>.)
991    </para>
992
993    <para>
994     Copy-out mode (data transfer from the server) is initiated when the
995     backend executes a <command>COPY TO STDOUT</> SQL statement.  The backend
996     sends a CopyOutResponse message to the frontend, followed by
997     zero or more CopyData messages (always one per row), followed by CopyDone.
998     The backend then reverts to the command-processing mode it was
999     in before the <command>COPY</> started, and sends CommandComplete.
1000     The frontend cannot abort the transfer (except by closing the connection
1001     or issuing a Cancel request),
1002     but it can discard unwanted CopyData and CopyDone messages.
1003    </para>
1004
1005    <para>
1006     In the event of a backend-detected error during copy-out mode,
1007     the backend will issue an ErrorResponse message and revert to normal
1008     processing.  The frontend should treat receipt of ErrorResponse as
1009     terminating the copy-out mode.
1010    </para>
1011
1012    <para>
1013     It is possible for NoticeResponse and ParameterStatus messages to be
1014     interspersed between CopyData messages; frontends must handle these cases,
1015     and should be prepared for other asynchronous message types as well (see
1016     <xref linkend="protocol-async">).  Otherwise, any message type other than
1017     CopyData or CopyDone may be treated as terminating copy-out mode.
1018    </para>
1019
1020    <para>
1021     There is another Copy-related mode called Copy-both, which allows
1022     high-speed bulk data transfer to <emphasis>and</> from the server.
1023     Copy-both mode is initiated when a backend in walsender mode
1024     executes a <command>START_REPLICATION</command> statement.  The
1025     backend sends a CopyBothResponse message to the frontend.  Both
1026     the backend and the frontend may then send CopyData messages
1027     until the connection is terminated.  See <xref
1028     linkend="protocol-replication">.
1029    </para>
1030
1031    <para>
1032     The CopyInResponse, CopyOutResponse and CopyBothResponse messages
1033     include fields that inform the frontend of the number of columns
1034     per row and the format codes being used for each column.  (As of
1035     the present implementation, all columns in a given <command>COPY</>
1036     operation will use the same format, but the message design does not
1037     assume this.)
1038    </para>
1039
1040   </sect2>
1041
1042   <sect2 id="protocol-async">
1043    <title>Asynchronous Operations</title>
1044
1045    <para>
1046     There are several cases in which the backend will send messages that
1047     are not specifically prompted by the frontend's command stream.
1048     Frontends must be prepared to deal with these messages at any time,
1049     even when not engaged in a query.
1050     At minimum, one should check for these cases before beginning to
1051     read a query response.
1052    </para>
1053
1054    <para>
1055     It is possible for NoticeResponse messages to be generated due to
1056     outside activity; for example, if the database administrator commands
1057     a <quote>fast</> database shutdown, the backend will send a NoticeResponse
1058     indicating this fact before closing the connection.  Accordingly,
1059     frontends should always be prepared to accept and display NoticeResponse
1060     messages, even when the connection is nominally idle.
1061    </para>
1062
1063    <para>
1064     ParameterStatus messages will be generated whenever the active
1065     value changes for any of the parameters the backend believes the
1066     frontend should know about.  Most commonly this occurs in response
1067     to a <command>SET</> SQL command executed by the frontend, and
1068     this case is effectively synchronous &mdash; but it is also possible
1069     for parameter status changes to occur because the administrator
1070     changed a configuration file and then sent the
1071     <systemitem>SIGHUP</systemitem> signal to the server.  Also,
1072     if a <command>SET</command> command is rolled back, an appropriate
1073     ParameterStatus message will be generated to report the current
1074     effective value.
1075    </para>
1076
1077    <para>
1078     At present there is a hard-wired set of parameters for which
1079     ParameterStatus will be generated: they are
1080     <varname>server_version</>,
1081     <varname>server_encoding</>,
1082     <varname>client_encoding</>,
1083     <varname>application_name</>,
1084     <varname>is_superuser</>,
1085     <varname>session_authorization</>,
1086     <varname>DateStyle</>,
1087     <varname>IntervalStyle</>,
1088     <varname>TimeZone</>,
1089     <varname>integer_datetimes</>, and
1090     <varname>standard_conforming_strings</>.
1091     (<varname>server_encoding</>, <varname>TimeZone</>, and
1092     <varname>integer_datetimes</> were not reported by releases before 8.0;
1093     <varname>standard_conforming_strings</> was not reported by releases
1094     before 8.1;
1095     <varname>IntervalStyle</> was not reported by releases before 8.4;
1096     <varname>application_name</> was not reported by releases before 9.0.)
1097     Note that
1098     <varname>server_version</>,
1099     <varname>server_encoding</> and
1100     <varname>integer_datetimes</>
1101     are pseudo-parameters that cannot change after startup.
1102     This set might change in the future, or even become configurable.
1103     Accordingly, a frontend should simply ignore ParameterStatus for
1104     parameters that it does not understand or care about.
1105    </para>
1106
1107    <para>
1108     If a frontend issues a <command>LISTEN</command> command, then the
1109     backend will send a NotificationResponse message (not to be
1110     confused with NoticeResponse!)  whenever a
1111     <command>NOTIFY</command> command is executed for the same
1112     channel name.
1113    </para>
1114
1115    <note>
1116     <para>
1117      At present, NotificationResponse can only be sent outside a
1118      transaction, and thus it will not occur in the middle of a
1119      command-response series, though it might occur just before ReadyForQuery.
1120      It is unwise to design frontend logic that assumes that, however.
1121      Good practice is to be able to accept NotificationResponse at any
1122      point in the protocol.
1123     </para>
1124    </note>
1125   </sect2>
1126
1127   <sect2>
1128    <title>Canceling Requests in Progress</title>
1129
1130    <para>
1131     During the processing of a query, the frontend might request
1132     cancellation of the query.  The cancel request is not sent
1133     directly on the open connection to the backend for reasons of
1134     implementation efficiency: we don't want to have the backend
1135     constantly checking for new input from the frontend during query
1136     processing.  Cancel requests should be relatively infrequent, so
1137     we make them slightly cumbersome in order to avoid a penalty in
1138     the normal case.
1139    </para>
1140
1141    <para>
1142     To issue a cancel request, the frontend opens a new connection to
1143     the server and sends a CancelRequest message, rather than the
1144     StartupMessage message that would ordinarily be sent across a new
1145     connection.  The server will process this request and then close
1146     the connection.  For security reasons, no direct reply is made to
1147     the cancel request message.
1148    </para>
1149
1150    <para>
1151     A CancelRequest message will be ignored unless it contains the
1152     same key data (PID and secret key) passed to the frontend during
1153     connection start-up.  If the request matches the PID and secret
1154     key for a currently executing backend, the processing of the
1155     current query is aborted.  (In the existing implementation, this is
1156     done by sending a special signal to the backend process that is
1157     processing the query.)
1158    </para>
1159
1160    <para>
1161     The cancellation signal might or might not have any effect &mdash; for
1162     example, if it arrives after the backend has finished processing
1163     the query, then it will have no effect.  If the cancellation is
1164     effective, it results in the current command being terminated
1165     early with an error message.
1166    </para>
1167
1168    <para>
1169     The upshot of all this is that for reasons of both security and
1170     efficiency, the frontend has no direct way to tell whether a
1171     cancel request has succeeded.  It must continue to wait for the
1172     backend to respond to the query.  Issuing a cancel simply improves
1173     the odds that the current query will finish soon, and improves the
1174     odds that it will fail with an error message instead of
1175     succeeding.
1176    </para>
1177
1178    <para>
1179     Since the cancel request is sent across a new connection to the
1180     server and not across the regular frontend/backend communication
1181     link, it is possible for the cancel request to be issued by any
1182     process, not just the frontend whose query is to be canceled.
1183     This might provide additional flexibility when building
1184     multiple-process applications.  It also introduces a security
1185     risk, in that unauthorized persons might try to cancel queries.
1186     The security risk is addressed by requiring a dynamically
1187     generated secret key to be supplied in cancel requests.
1188    </para>
1189   </sect2>
1190
1191   <sect2>
1192    <title>Termination</title>
1193
1194    <para>
1195     The normal, graceful termination procedure is that the frontend
1196     sends a Terminate message and immediately closes the connection.
1197     On receipt of this message, the backend closes the connection and
1198     terminates.
1199    </para>
1200
1201    <para>
1202     In rare cases (such as an administrator-commanded database shutdown)
1203     the backend might disconnect without any frontend request to do so.
1204     In such cases the backend will attempt to send an error or notice message
1205     giving the reason for the disconnection before it closes the connection.
1206    </para>
1207
1208    <para>
1209     Other termination scenarios arise from various failure cases, such as core
1210     dump at one end or the other, loss of the communications link, loss of
1211     message-boundary synchronization, etc.  If either frontend or backend sees
1212     an unexpected closure of the connection, it should clean
1213     up and terminate.  The frontend has the option of launching a new backend
1214     by recontacting the server if it doesn't want to terminate itself.
1215     Closing the connection is also advisable if an unrecognizable message type
1216     is received, since this probably indicates loss of message-boundary sync.
1217    </para>
1218
1219    <para>
1220     For either normal or abnormal termination, any open transaction is
1221     rolled back, not committed.  One should note however that if a
1222     frontend disconnects while a non-<command>SELECT</command> query
1223     is being processed, the backend will probably finish the query
1224     before noticing the disconnection.  If the query is outside any
1225     transaction block (<command>BEGIN</> ... <command>COMMIT</>
1226     sequence) then its results might be committed before the
1227     disconnection is recognized.
1228    </para>
1229   </sect2>
1230
1231   <sect2>
1232    <title><acronym>SSL</acronym> Session Encryption</title>
1233
1234    <para>
1235     If <productname>PostgreSQL</> was built with
1236     <acronym>SSL</acronym> support, frontend/backend communications
1237     can be encrypted using <acronym>SSL</acronym>.  This provides
1238     communication security in environments where attackers might be
1239     able to capture the session traffic. For more information on
1240     encrypting <productname>PostgreSQL</productname> sessions with
1241     <acronym>SSL</acronym>, see <xref linkend="ssl-tcp">.
1242    </para>
1243
1244    <para>
1245     To initiate an <acronym>SSL</acronym>-encrypted connection, the
1246     frontend initially sends an SSLRequest message rather than a
1247     StartupMessage.  The server then responds with a single byte
1248     containing <literal>S</> or <literal>N</>, indicating that it is
1249     willing or unwilling to perform <acronym>SSL</acronym>,
1250     respectively.  The frontend might close the connection at this point
1251     if it is dissatisfied with the response.  To continue after
1252     <literal>S</>, perform an <acronym>SSL</acronym> startup handshake
1253     (not described here, part of the <acronym>SSL</acronym>
1254     specification) with the server.  If this is successful, continue
1255     with sending the usual StartupMessage.  In this case the
1256     StartupMessage and all subsequent data will be
1257     <acronym>SSL</acronym>-encrypted.  To continue after
1258     <literal>N</>, send the usual StartupMessage and proceed without
1259     encryption.
1260    </para>
1261
1262    <para>
1263     The frontend should also be prepared to handle an ErrorMessage
1264     response to SSLRequest from the server.  This would only occur if
1265     the server predates the addition of <acronym>SSL</acronym> support
1266     to <productname>PostgreSQL</>.  (Such servers are now very ancient,
1267     and likely do not exist in the wild anymore.)
1268     In this case the connection must
1269     be closed, but the frontend might choose to open a fresh connection
1270     and proceed without requesting <acronym>SSL</acronym>.
1271    </para>
1272
1273    <para>
1274     An initial SSLRequest can also be used in a connection that is being
1275     opened to send a CancelRequest message.
1276    </para>
1277
1278    <para>
1279     While the protocol itself does not provide a way for the server to
1280     force <acronym>SSL</acronym> encryption, the administrator can
1281     configure the server to reject unencrypted sessions as a byproduct
1282     of authentication checking.
1283    </para>
1284   </sect2>
1285  </sect1>
1286
1287 <sect1 id="protocol-replication">
1288 <title>Streaming Replication Protocol</title>
1289
1290 <para>
1291 To initiate streaming replication, the frontend sends the
1292 <literal>replication</> parameter in the startup message. This tells the
1293 backend to go into walsender mode, wherein a small set of replication commands
1294 can be issued instead of SQL statements. Only the simple query protocol can be
1295 used in walsender mode.
1296
1297 The commands accepted in walsender mode are:
1298
1299 <variablelist>
1300   <varlistentry>
1301     <term>IDENTIFY_SYSTEM</term>
1302     <listitem>
1303      <para>
1304       Requests the server to identify itself. Server replies with a result
1305       set of a single row, containing three fields:
1306      </para>
1307
1308      <para>
1309       <variablelist>
1310       <varlistentry>
1311       <term>
1312        systemid
1313       </term>
1314       <listitem>
1315       <para>
1316        The unique system identifier identifying the cluster. This
1317        can be used to check that the base backup used to initialize the
1318        standby came from the same cluster.
1319       </para>
1320       </listitem>
1321       </varlistentry>
1322
1323       <varlistentry>
1324       <term>
1325        timeline
1326       </term>
1327       <listitem>
1328       <para>
1329        Current TimelineID. Also useful to check that the standby is
1330        consistent with the master.
1331       </para>
1332       </listitem>
1333       </varlistentry>
1334
1335       <varlistentry>
1336       <term>
1337        xlogpos
1338       </term>
1339       <listitem>
1340       <para>
1341        Current xlog write location. Useful to get a known location in the
1342        transaction log where streaming can start.
1343       </para>
1344       </listitem>
1345       </varlistentry>
1346
1347       </variablelist>
1348      </para>
1349     </listitem>
1350   </varlistentry>
1351
1352   <varlistentry>
1353     <term>START_REPLICATION <replaceable>XXX</>/<replaceable>XXX</></term>
1354     <listitem>
1355      <para>
1356       Instructs server to start streaming WAL, starting at
1357       WAL position <replaceable>XXX</>/<replaceable>XXX</>.
1358       The server can reply with an error, e.g. if the requested section of WAL
1359       has already been recycled. On success, server responds with a
1360       CopyBothResponse message, and then starts to stream WAL to the frontend.
1361       WAL will continue to be streamed until the connection is broken;
1362       no further commands will be accepted.
1363      </para>
1364
1365      <para>
1366       WAL data is sent as a series of CopyData messages.  (This allows
1367       other information to be intermixed; in particular the server can send
1368       an ErrorResponse message if it encounters a failure after beginning
1369       to stream.)  The payload in each CopyData message follows this format:
1370      </para>
1371
1372      <para>
1373       <variablelist>
1374       <varlistentry>
1375       <term>
1376           XLogData (B)
1377       </term>
1378       <listitem>
1379       <para>
1380       <variablelist>
1381       <varlistentry>
1382       <term>
1383           Byte1('w')
1384       </term>
1385       <listitem>
1386       <para>
1387           Identifies the message as WAL data.
1388       </para>
1389       </listitem>
1390       </varlistentry>
1391       <varlistentry>
1392       <term>
1393           Byte8
1394       </term>
1395       <listitem>
1396       <para>
1397           The starting point of the WAL data in this message, given in
1398           XLogRecPtr format.
1399       </para>
1400       </listitem>
1401       </varlistentry>
1402       <varlistentry>
1403       <term>
1404           Byte8
1405       </term>
1406       <listitem>
1407       <para>
1408           The current end of WAL on the server, given in
1409           XLogRecPtr format.
1410       </para>
1411       </listitem>
1412       </varlistentry>
1413       <varlistentry>
1414       <term>
1415           Byte8
1416       </term>
1417       <listitem>
1418       <para>
1419           The server's system clock at the time of transmission,
1420           given in TimestampTz format.
1421       </para>
1422       </listitem>
1423       </varlistentry>
1424       <varlistentry>
1425       <term>
1426           Byte<replaceable>n</replaceable>
1427       </term>
1428       <listitem>
1429       <para>
1430           A section of the WAL data stream.
1431       </para>
1432       </listitem>
1433       </varlistentry>
1434       </variablelist>
1435       </para>
1436       </listitem>
1437       </varlistentry>
1438       </variablelist>
1439      </para>
1440      <para>
1441        A single WAL record is never split across two CopyData messages.
1442        When a WAL record crosses a WAL page boundary, and is therefore
1443        already split using continuation records, it can be split at the page
1444        boundary. In other words, the first main WAL record and its
1445        continuation records can be sent in different CopyData messages.
1446      </para>
1447      <para>
1448        Note that all fields within the WAL data and the above-described header
1449        will be in the sending server's native format.  Endianness, and the
1450        format for the timestamp, are unpredictable unless the receiver has
1451        verified that the sender's system identifier matches its own
1452        <filename>pg_control</> contents.
1453      </para>
1454      <para>
1455        If the WAL sender process is terminated normally (during postmaster
1456        shutdown), it will send a CommandComplete message before exiting.
1457        This might not happen during an abnormal shutdown, of course.
1458      </para>
1459
1460      <para>
1461        The receiving process can send replies back to the sender at any time,
1462        using one of the following message formats (also in the payload of a
1463        CopyData message):
1464      </para>
1465
1466      <para>
1467       <variablelist>
1468       <varlistentry>
1469       <term>
1470           Primary keepalive message (B)
1471       </term>
1472       <listitem>
1473       <para>
1474       <variablelist>
1475       <varlistentry>
1476       <term>
1477           Byte1('k')
1478       </term>
1479       <listitem>
1480       <para>
1481           Identifies the message as a sender keepalive.
1482       </para>
1483       </listitem>
1484       </varlistentry>
1485       <varlistentry>
1486       <term>
1487           Byte8
1488       </term>
1489       <listitem>
1490       <para>
1491           The current end of WAL on the server, given in
1492           XLogRecPtr format.
1493       </para>
1494       </listitem>
1495       </varlistentry>
1496       <varlistentry>
1497       <term>
1498           Byte8
1499       </term>
1500       <listitem>
1501       <para>
1502           The server's system clock at the time of transmission,
1503           given in TimestampTz format.
1504       </para>
1505       </listitem>
1506       </varlistentry>
1507       </variablelist>
1508       </para>
1509       </listitem>
1510       </varlistentry>
1511       </variablelist>
1512      </para>
1513
1514      <para>
1515       <variablelist>
1516       <varlistentry>
1517       <term>
1518           Standby status update (F)
1519       </term>
1520       <listitem>
1521       <para>
1522       <variablelist>
1523       <varlistentry>
1524       <term>
1525           Byte1('r')
1526       </term>
1527       <listitem>
1528       <para>
1529           Identifies the message as a receiver status update.
1530       </para>
1531       </listitem>
1532       </varlistentry>
1533       <varlistentry>
1534       <term>
1535           Byte8
1536       </term>
1537       <listitem>
1538       <para>
1539           The location of the last WAL byte + 1 received and written to disk
1540           in the standby, in XLogRecPtr format.
1541       </para>
1542       </listitem>
1543       </varlistentry>
1544       <varlistentry>
1545       <term>
1546           Byte8
1547       </term>
1548       <listitem>
1549       <para>
1550           The location of the last WAL byte + 1 flushed to disk in
1551           the standby, in XLogRecPtr format.
1552       </para>
1553       </listitem>
1554       </varlistentry>
1555       <varlistentry>
1556       <term>
1557           Byte8
1558       </term>
1559       <listitem>
1560       <para>
1561           The location of the last WAL byte + 1 applied in the standby, in
1562           XLogRecPtr format.
1563       </para>
1564       </listitem>
1565       </varlistentry>
1566       <varlistentry>
1567       <term>
1568           Byte8
1569       </term>
1570       <listitem>
1571       <para>
1572           The server's system clock at the time of transmission,
1573           given in TimestampTz format.
1574       </para>
1575       </listitem>
1576       </varlistentry>
1577       </variablelist>
1578       </para>
1579       </listitem>
1580       </varlistentry>
1581       </variablelist>
1582      </para>
1583
1584      <para>
1585       <variablelist>
1586       <varlistentry>
1587       <term>
1588           Hot Standby feedback message (F)
1589       </term>
1590       <listitem>
1591       <para>
1592       <variablelist>
1593       <varlistentry>
1594       <term>
1595           Byte1('h')
1596       </term>
1597       <listitem>
1598       <para>
1599           Identifies the message as a Hot Standby feedback message.
1600       </para>
1601       </listitem>
1602       </varlistentry>
1603       <varlistentry>
1604       <term>
1605           Byte8
1606       </term>
1607       <listitem>
1608       <para>
1609           The server's system clock at the time of transmission,
1610           given in TimestampTz format.
1611       </para>
1612       </listitem>
1613       </varlistentry>
1614       <varlistentry>
1615       <term>
1616           Byte4
1617       </term>
1618       <listitem>
1619       <para>
1620           The standby's current xmin.
1621       </para>
1622       </listitem>
1623       </varlistentry>
1624       <varlistentry>
1625       <term>
1626           Byte4
1627       </term>
1628       <listitem>
1629       <para>
1630           The standby's current epoch.
1631       </para>
1632       </listitem>
1633       </varlistentry>
1634       </variablelist>
1635       </para>
1636       </listitem>
1637       </varlistentry>
1638       </variablelist>
1639      </para>
1640     </listitem>
1641   </varlistentry>
1642
1643   <varlistentry>
1644     <term>BASE_BACKUP [<literal>LABEL</literal> <replaceable>'label'</replaceable>] [<literal>PROGRESS</literal>] [<literal>FAST</literal>] [<literal>WAL</literal>] [<literal>NOWAIT</literal>]</term>
1645     <listitem>
1646      <para>
1647       Instructs the server to start streaming a base backup.
1648       The system will automatically be put in backup mode before the backup
1649       is started, and taken out of it when the backup is complete. The
1650       following options are accepted:
1651       <variablelist>
1652        <varlistentry>
1653         <term><literal>LABEL</literal> <replaceable>'label'</replaceable></term>
1654         <listitem>
1655          <para>
1656           Sets the label of the backup. If none is specified, a backup label
1657           of <literal>base backup</literal> will be used. The quoting rules
1658           for the label are the same as a standard SQL string with
1659           <xref linkend="guc-standard-conforming-strings"> turned on.
1660          </para>
1661         </listitem>
1662        </varlistentry>
1663
1664        <varlistentry>
1665         <term><literal>PROGRESS</></term>
1666         <listitem>
1667          <para>
1668           Request information required to generate a progress report. This will
1669           send back an approximate size in the header of each tablespace, which
1670           can be used to calculate how far along the stream is done. This is
1671           calculated by enumerating all the file sizes once before the transfer
1672           is even started, and may as such have a negative impact on the
1673           performance - in particular it may take longer before the first data
1674           is streamed. Since the database files can change during the backup,
1675           the size is only approximate and may both grow and shrink between
1676           the time of approximation and the sending of the actual files.
1677          </para>
1678         </listitem>
1679        </varlistentry>
1680
1681        <varlistentry>
1682         <term><literal>FAST</></term>
1683         <listitem>
1684          <para>
1685           Request a fast checkpoint.
1686          </para>
1687         </listitem>
1688        </varlistentry>
1689
1690        <varlistentry>
1691         <term><literal>WAL</literal></term>
1692         <listitem>
1693          <para>
1694           Include the necessary WAL segments in the backup. This will include
1695           all the files between start and stop backup in the
1696           <filename>pg_xlog</filename> directory of the base directory tar
1697           file.
1698          </para>
1699         </listitem>
1700        </varlistentry>
1701
1702        <varlistentry>
1703         <term><literal>NOWAIT</literal></term>
1704         <listitem>
1705          <para>
1706           By default, the backup will wait until the last required xlog
1707           segment has been archived, or emit a warning if log archiving is
1708           not enabled. Specifying <literal>NOWAIT</literal> disables both
1709           the waiting and the warning, leaving the client responsible for
1710           ensuring the required log is available.
1711          </para>
1712          </listitem>
1713        </varlistentry>
1714       </variablelist>
1715      </para>
1716      <para>
1717       When the backup is started, the server will first send two
1718       ordinary result sets, followed by one or more CopyResponse
1719       results.
1720      </para>
1721      <para>
1722       The first ordinary result set contains the starting position of the
1723       backup, given in XLogRecPtr format as a single column in a single row.
1724      </para>
1725      <para>
1726       The second ordinary result set has one row for each tablespace.
1727       The fields in this row are:
1728       <variablelist>
1729        <varlistentry>
1730         <term>spcoid</term>
1731         <listitem>
1732          <para>
1733           The oid of the tablespace, or <literal>NULL</> if it's the base
1734           directory.
1735          </para>
1736         </listitem>
1737        </varlistentry>
1738        <varlistentry>
1739         <term>spclocation</term>
1740         <listitem>
1741          <para>
1742           The full path of the tablespace directory, or <literal>NULL</>
1743           if it's the base directory.
1744          </para>
1745         </listitem>
1746        </varlistentry>
1747        <varlistentry>
1748         <term>size</term>
1749         <listitem>
1750          <para>
1751           The approximate size of the tablespace, if progress report has
1752           been requested; otherwise it's <literal>NULL</>.
1753          </para>
1754         </listitem>
1755        </varlistentry>
1756       </variablelist>
1757      </para>
1758      <para>
1759       After the second regular result set, one or more CopyResponse results
1760       will be sent, one for PGDATA and one for each additional tablespace other
1761       than <literal>pg_default</> and <literal>pg_global</>. The data in
1762       the CopyResponse results will be a tar format (using ustar00
1763       extensions) dump of the tablespace contents. After the tar data is
1764       complete, a final ordinary result set will be sent.
1765      </para>
1766
1767      <para>
1768       The tar archive for the data directory and each tablespace will contain
1769       all files in the directories, regardless of whether they are
1770       <productname>PostgreSQL</> files or other files added to the same
1771       directory. The only excluded files are:
1772       <itemizedlist spacing="compact" mark="bullet">
1773        <listitem>
1774         <para>
1775          <filename>postmaster.pid</>
1776         </para>
1777        </listitem>
1778        <listitem>
1779         <para>
1780          <filename>postmaster.opts</>
1781         </para>
1782        </listitem>
1783        <listitem>
1784         <para>
1785          <filename>pg_xlog</>, including subdirectories. If the backup is run
1786          with WAL files included, a synthesized version of <filename>pg_xlog</filename> will be
1787          included, but it will only contain the files necessary for the
1788          backup to work, not the rest of the contents.
1789         </para>
1790        </listitem>
1791       </itemizedlist>
1792       Owner, group and file mode are set if the underlying file system on
1793       the server supports it.
1794      </para>
1795      <para>
1796       Once all tablespaces have been sent, a final regular result set will
1797       be sent. This result set contains the end position of the
1798       backup, given in XLogRecPtr format as a single column in a single row.
1799      </para>
1800     </listitem>
1801   </varlistentry>
1802 </variablelist>
1803
1804 </para>
1805
1806 </sect1>
1807
1808 <sect1 id="protocol-message-types">
1809 <title>Message Data Types</title>
1810
1811 <para>
1812 This section describes the base data types used in messages.
1813
1814 <variablelist>
1815
1816 <varlistentry>
1817 <term>
1818         Int<replaceable>n</replaceable>(<replaceable>i</replaceable>)
1819 </term>
1820 <listitem>
1821 <para>
1822                 An <replaceable>n</replaceable>-bit signed integer in network byte
1823                 order (most significant byte first).
1824                 If <replaceable>i</replaceable> is specified it
1825                 is the exact value that will appear, otherwise the value
1826                 is variable.  Eg. Int16, Int32(42).
1827 </para>
1828 </listitem>
1829 </varlistentry>
1830
1831 <varlistentry>
1832 <term>
1833         Int<replaceable>n</replaceable>[<replaceable>k</replaceable>]
1834 </term>
1835 <listitem>
1836 <para>
1837                 An array of <replaceable>k</replaceable>
1838                 <replaceable>n</replaceable>-bit integers, each in network
1839                 byte order.  The array length <replaceable>k</replaceable>
1840                 is always determined by an earlier field in the message.
1841                 Eg. Int16[M].
1842 </para>
1843 </listitem>
1844 </varlistentry>
1845
1846 <varlistentry>
1847 <term>
1848         String(<replaceable>s</replaceable>)
1849 </term>
1850 <listitem>
1851 <para>
1852                 A null-terminated string (C-style string).  There is no
1853                 specific length limitation on strings.
1854                 If <replaceable>s</replaceable> is specified it is the exact
1855                 value that will appear, otherwise the value is variable.
1856                 Eg. String, String("user").
1857 </para>
1858
1859 <note>
1860 <para>
1861 <emphasis>There is no predefined limit</emphasis> on the length of a string
1862 that can be returned by the backend.  Good coding strategy for a frontend
1863 is to use an expandable buffer so that anything that fits in memory can be
1864 accepted.  If that's not feasible, read the full string and discard trailing
1865 characters that don't fit into your fixed-size buffer.
1866 </para>
1867 </note>
1868 </listitem>
1869 </varlistentry>
1870
1871 <varlistentry>
1872 <term>
1873         Byte<replaceable>n</replaceable>(<replaceable>c</replaceable>)
1874 </term>
1875 <listitem>
1876 <para>
1877                 Exactly <replaceable>n</replaceable> bytes.  If the field
1878                 width <replaceable>n</replaceable> is not a constant, it is
1879                 always determinable from an earlier field in the message.
1880                 If <replaceable>c</replaceable> is specified it is the exact
1881                 value.  Eg. Byte2, Byte1('\n').
1882 </para>
1883 </listitem>
1884 </varlistentry>
1885
1886 </variablelist>
1887 </para>
1888 </sect1>
1889
1890 <sect1 id="protocol-message-formats">
1891 <title>Message Formats</title>
1892
1893 <para>
1894 This section describes the detailed format of each message.  Each is marked to
1895 indicate that it can be sent by a frontend (F), a backend (B), or both
1896 (F &amp; B).
1897 Notice that although each message includes a byte count at the beginning,
1898 the message format is defined so that the message end can be found without
1899 reference to the byte count.  This aids validity checking.  (The CopyData
1900 message is an exception, because it forms part of a data stream; the contents
1901 of any individual CopyData message cannot be interpretable on their own.)
1902 </para>
1903
1904 <variablelist>
1905
1906
1907 <varlistentry>
1908 <term>
1909 AuthenticationOk (B)
1910 </term>
1911 <listitem>
1912 <para>
1913
1914 <variablelist>
1915 <varlistentry>
1916 <term>
1917         Byte1('R')
1918 </term>
1919 <listitem>
1920 <para>
1921                 Identifies the message as an authentication request.
1922 </para>
1923 </listitem>
1924 </varlistentry>
1925 <varlistentry>
1926 <term>
1927         Int32(8)
1928 </term>
1929 <listitem>
1930 <para>
1931                 Length of message contents in bytes, including self.
1932 </para>
1933 </listitem>
1934 </varlistentry>
1935 <varlistentry>
1936 <term>
1937         Int32(0)
1938 </term>
1939 <listitem>
1940 <para>
1941                 Specifies that the authentication was successful.
1942 </para>
1943 </listitem>
1944 </varlistentry>
1945 </variablelist>
1946
1947 </para>
1948 </listitem>
1949 </varlistentry>
1950
1951
1952 <varlistentry>
1953 <term>
1954 AuthenticationKerberosV5 (B)
1955 </term>
1956 <listitem>
1957 <para>
1958
1959 <variablelist>
1960 <varlistentry>
1961 <term>
1962         Byte1('R')
1963 </term>
1964 <listitem>
1965 <para>
1966                 Identifies the message as an authentication request.
1967 </para>
1968 </listitem>
1969 </varlistentry>
1970 <varlistentry>
1971 <term>
1972         Int32(8)
1973 </term>
1974 <listitem>
1975 <para>
1976                 Length of message contents in bytes, including self.
1977 </para>
1978 </listitem>
1979 </varlistentry>
1980 <varlistentry>
1981 <term>
1982         Int32(2)
1983 </term>
1984 <listitem>
1985 <para>
1986                 Specifies that Kerberos V5 authentication is required.
1987 </para>
1988 </listitem>
1989 </varlistentry>
1990 </variablelist>
1991 </para>
1992 </listitem>
1993 </varlistentry>
1994
1995
1996 <varlistentry>
1997 <term>
1998 AuthenticationCleartextPassword (B)
1999 </term>
2000 <listitem>
2001 <para>
2002
2003 <variablelist>
2004 <varlistentry>
2005 <term>
2006         Byte1('R')
2007 </term>
2008 <listitem>
2009 <para>
2010                 Identifies the message as an authentication request.
2011 </para>
2012 </listitem>
2013 </varlistentry>
2014 <varlistentry>
2015 <term>
2016         Int32(8)
2017 </term>
2018 <listitem>
2019 <para>
2020                 Length of message contents in bytes, including self.
2021 </para>
2022 </listitem>
2023 </varlistentry>
2024 <varlistentry>
2025 <term>
2026         Int32(3)
2027 </term>
2028 <listitem>
2029 <para>
2030                 Specifies that a clear-text password is required.
2031 </para>
2032 </listitem>
2033 </varlistentry>
2034 </variablelist>
2035 </para>
2036 </listitem>
2037 </varlistentry>
2038
2039
2040 <varlistentry>
2041 <term>
2042 AuthenticationMD5Password (B)
2043 </term>
2044 <listitem>
2045 <para>
2046
2047 <variablelist>
2048 <varlistentry>
2049 <term>
2050         Byte1('R')
2051 </term>
2052 <listitem>
2053 <para>
2054                 Identifies the message as an authentication request.
2055 </para>
2056 </listitem>
2057 </varlistentry>
2058 <varlistentry>
2059 <term>
2060         Int32(12)
2061 </term>
2062 <listitem>
2063 <para>
2064                 Length of message contents in bytes, including self.
2065 </para>
2066 </listitem>
2067 </varlistentry>
2068 <varlistentry>
2069 <term>
2070         Int32(5)
2071 </term>
2072 <listitem>
2073 <para>
2074                 Specifies that an MD5-encrypted password is required.
2075 </para>
2076 </listitem>
2077 </varlistentry>
2078 <varlistentry>
2079 <term>
2080         Byte4
2081 </term>
2082 <listitem>
2083 <para>
2084                 The salt to use when encrypting the password.
2085 </para>
2086 </listitem>
2087 </varlistentry>
2088 </variablelist>
2089
2090 </para>
2091 </listitem>
2092 </varlistentry>
2093
2094
2095 <varlistentry>
2096 <term>
2097 AuthenticationSCMCredential (B)
2098 </term>
2099 <listitem>
2100 <para>
2101
2102 <variablelist>
2103 <varlistentry>
2104 <term>
2105         Byte1('R')
2106 </term>
2107 <listitem>
2108 <para>
2109                 Identifies the message as an authentication request.
2110 </para>
2111 </listitem>
2112 </varlistentry>
2113 <varlistentry>
2114 <term>
2115         Int32(8)
2116 </term>
2117 <listitem>
2118 <para>
2119                 Length of message contents in bytes, including self.
2120 </para>
2121 </listitem>
2122 </varlistentry>
2123 <varlistentry>
2124 <term>
2125         Int32(6)
2126 </term>
2127 <listitem>
2128 <para>
2129                 Specifies that an SCM credentials message is required.
2130 </para>
2131 </listitem>
2132 </varlistentry>
2133 </variablelist>
2134
2135 </para>
2136 </listitem>
2137 </varlistentry>
2138
2139
2140 <varlistentry>
2141 <term>
2142 AuthenticationGSS (B)
2143 </term>
2144 <listitem>
2145 <para>
2146
2147 <variablelist>
2148 <varlistentry>
2149 <term>
2150         Byte1('R')
2151 </term>
2152 <listitem>
2153 <para>
2154                 Identifies the message as an authentication request.
2155 </para>
2156 </listitem>
2157 </varlistentry>
2158 <varlistentry>
2159 <term>
2160         Int32(8)
2161 </term>
2162 <listitem>
2163 <para>
2164                 Length of message contents in bytes, including self.
2165 </para>
2166 </listitem>
2167 </varlistentry>
2168 <varlistentry>
2169 <term>
2170         Int32(7)
2171 </term>
2172 <listitem>
2173 <para>
2174                 Specifies that GSSAPI authentication is required.
2175 </para>
2176 </listitem>
2177 </varlistentry>
2178 </variablelist>
2179
2180 </para>
2181 </listitem>
2182 </varlistentry>
2183
2184
2185 <varlistentry>
2186 <term>
2187 AuthenticationSSPI (B)
2188 </term>
2189 <listitem>
2190 <para>
2191
2192 <variablelist>
2193 <varlistentry>
2194 <term>
2195         Byte1('R')
2196 </term>
2197 <listitem>
2198 <para>
2199                 Identifies the message as an authentication request.
2200 </para>
2201 </listitem>
2202 </varlistentry>
2203 <varlistentry>
2204 <term>
2205         Int32(8)
2206 </term>
2207 <listitem>
2208 <para>
2209                 Length of message contents in bytes, including self.
2210 </para>
2211 </listitem>
2212 </varlistentry>
2213 <varlistentry>
2214 <term>
2215         Int32(9)
2216 </term>
2217 <listitem>
2218 <para>
2219                 Specifies that SSPI authentication is required.
2220 </para>
2221 </listitem>
2222 </varlistentry>
2223 </variablelist>
2224
2225 </para>
2226 </listitem>
2227 </varlistentry>
2228 <varlistentry>
2229 <term>
2230 AuthenticationGSSContinue (B)
2231 </term>
2232 <listitem>
2233 <para>
2234
2235 <variablelist>
2236 <varlistentry>
2237 <term>
2238         Byte1('R')
2239 </term>
2240 <listitem>
2241 <para>
2242                 Identifies the message as an authentication request.
2243 </para>
2244 </listitem>
2245 </varlistentry>
2246 <varlistentry>
2247 <term>
2248         Int32
2249 </term>
2250 <listitem>
2251 <para>
2252                 Length of message contents in bytes, including self.
2253 </para>
2254 </listitem>
2255 </varlistentry>
2256 <varlistentry>
2257 <term>
2258         Int32(8)
2259 </term>
2260 <listitem>
2261 <para>
2262                 Specifies that this message contains GSSAPI or SSPI data.
2263 </para>
2264 </listitem>
2265 </varlistentry>
2266 <varlistentry>
2267 <term>
2268         Byte<replaceable>n</replaceable>
2269 </term>
2270 <listitem>
2271 <para>
2272                 GSSAPI or SSPI authentication data.
2273 </para>
2274 </listitem>
2275 </varlistentry>
2276 </variablelist>
2277
2278 </para>
2279 </listitem>
2280 </varlistentry>
2281
2282
2283 <varlistentry>
2284 <term>
2285 BackendKeyData (B)
2286 </term>
2287 <listitem>
2288 <para>
2289
2290 <variablelist>
2291 <varlistentry>
2292 <term>
2293         Byte1('K')
2294 </term>
2295 <listitem>
2296 <para>
2297                 Identifies the message as cancellation key data.
2298                 The frontend must save these values if it wishes to be
2299                 able to issue CancelRequest messages later.
2300 </para>
2301 </listitem>
2302 </varlistentry>
2303 <varlistentry>
2304 <term>
2305         Int32(12)
2306 </term>
2307 <listitem>
2308 <para>
2309                 Length of message contents in bytes, including self.
2310 </para>
2311 </listitem>
2312 </varlistentry>
2313 <varlistentry>
2314 <term>
2315         Int32
2316 </term>
2317 <listitem>
2318 <para>
2319                 The process ID of this backend.
2320 </para>
2321 </listitem>
2322 </varlistentry>
2323 <varlistentry>
2324 <term>
2325         Int32
2326 </term>
2327 <listitem>
2328 <para>
2329                 The secret key of this backend.
2330 </para>
2331 </listitem>
2332 </varlistentry>
2333 </variablelist>
2334
2335 </para>
2336 </listitem>
2337 </varlistentry>
2338
2339
2340 <varlistentry>
2341 <term>
2342 Bind (F)
2343 </term>
2344 <listitem>
2345 <para>
2346
2347 <variablelist>
2348 <varlistentry>
2349 <term>
2350         Byte1('B')
2351 </term>
2352 <listitem>
2353 <para>
2354                 Identifies the message as a Bind command.
2355 </para>
2356 </listitem>
2357 </varlistentry>
2358 <varlistentry>
2359 <term>
2360         Int32
2361 </term>
2362 <listitem>
2363 <para>
2364                 Length of message contents in bytes, including self.
2365 </para>
2366 </listitem>
2367 </varlistentry>
2368 <varlistentry>
2369 <term>
2370         String
2371 </term>
2372 <listitem>
2373 <para>
2374                 The name of the destination portal
2375                 (an empty string selects the unnamed portal).
2376 </para>
2377 </listitem>
2378 </varlistentry>
2379 <varlistentry>
2380 <term>
2381         String
2382 </term>
2383 <listitem>
2384 <para>
2385                 The name of the source prepared statement
2386                 (an empty string selects the unnamed prepared statement).
2387 </para>
2388 </listitem>
2389 </varlistentry>
2390 <varlistentry>
2391 <term>
2392         Int16
2393 </term>
2394 <listitem>
2395 <para>
2396                 The number of parameter format codes that follow
2397                 (denoted <replaceable>C</> below).
2398                 This can be zero to indicate that there are no parameters
2399                 or that the parameters all use the default format (text);
2400                 or one, in which case the specified format code is applied
2401                 to all parameters; or it can equal the actual number of
2402                 parameters.
2403 </para>
2404 </listitem>
2405 </varlistentry>
2406 <varlistentry>
2407 <term>
2408         Int16[<replaceable>C</>]
2409 </term>
2410 <listitem>
2411 <para>
2412                 The parameter format codes.  Each must presently be
2413                 zero (text) or one (binary).
2414 </para>
2415 </listitem>
2416 </varlistentry>
2417 <varlistentry>
2418 <term>
2419         Int16
2420 </term>
2421 <listitem>
2422 <para>
2423                 The number of parameter values that follow (possibly zero).
2424                 This must match the number of parameters needed by the query.
2425 </para>
2426 </listitem>
2427 </varlistentry>
2428 </variablelist>
2429         Next, the following pair of fields appear for each parameter:
2430 <variablelist>
2431 <varlistentry>
2432 <term>
2433         Int32
2434 </term>
2435 <listitem>
2436 <para>
2437                 The length of the parameter value, in bytes (this count
2438                 does not include itself).  Can be zero.
2439                 As a special case, -1 indicates a NULL parameter value.
2440                 No value bytes follow in the NULL case.
2441 </para>
2442 </listitem>
2443 </varlistentry>
2444 <varlistentry>
2445 <term>
2446         Byte<replaceable>n</replaceable>
2447 </term>
2448 <listitem>
2449 <para>
2450                 The value of the parameter, in the format indicated by the
2451                 associated format code.
2452                 <replaceable>n</replaceable> is the above length.
2453 </para>
2454 </listitem>
2455 </varlistentry>
2456 </variablelist>
2457         After the last parameter, the following fields appear:
2458 <variablelist>
2459 <varlistentry>
2460 <term>
2461         Int16
2462 </term>
2463 <listitem>
2464 <para>
2465                 The number of result-column format codes that follow
2466                 (denoted <replaceable>R</> below).
2467                 This can be zero to indicate that there are no result columns
2468                 or that the result columns should all use the default format
2469                 (text);
2470                 or one, in which case the specified format code is applied
2471                 to all result columns (if any); or it can equal the actual
2472                 number of result columns of the query.
2473 </para>
2474 </listitem>
2475 </varlistentry>
2476 <varlistentry>
2477 <term>
2478         Int16[<replaceable>R</>]
2479 </term>
2480 <listitem>
2481 <para>
2482                 The result-column format codes.  Each must presently be
2483                 zero (text) or one (binary).
2484 </para>
2485 </listitem>
2486 </varlistentry>
2487 </variablelist>
2488 </para>
2489 </listitem>
2490 </varlistentry>
2491
2492
2493 <varlistentry>
2494 <term>
2495 BindComplete (B)
2496 </term>
2497 <listitem>
2498 <para>
2499
2500 <variablelist>
2501 <varlistentry>
2502 <term>
2503         Byte1('2')
2504 </term>
2505 <listitem>
2506 <para>
2507                 Identifies the message as a Bind-complete indicator.
2508 </para>
2509 </listitem>
2510 </varlistentry>
2511 <varlistentry>
2512 <term>
2513         Int32(4)
2514 </term>
2515 <listitem>
2516 <para>
2517                 Length of message contents in bytes, including self.
2518 </para>
2519 </listitem>
2520 </varlistentry>
2521 </variablelist>
2522
2523 </para>
2524 </listitem>
2525 </varlistentry>
2526
2527
2528 <varlistentry>
2529 <term>
2530 CancelRequest (F)
2531 </term>
2532 <listitem>
2533 <para>
2534
2535 <variablelist>
2536 <varlistentry>
2537 <term>
2538         Int32(16)
2539 </term>
2540 <listitem>
2541 <para>
2542                 Length of message contents in bytes, including self.
2543 </para>
2544 </listitem>
2545 </varlistentry>
2546 <varlistentry>
2547 <term>
2548         Int32(80877102)
2549 </term>
2550 <listitem>
2551 <para>
2552                 The cancel request code.  The value is chosen to contain
2553                 <literal>1234</> in the most significant 16 bits, and <literal>5678</> in the
2554                 least 16 significant bits.  (To avoid confusion, this code
2555                 must not be the same as any protocol version number.)
2556 </para>
2557 </listitem>
2558 </varlistentry>
2559 <varlistentry>
2560 <term>
2561         Int32
2562 </term>
2563 <listitem>
2564 <para>
2565                 The process ID of the target backend.
2566 </para>
2567 </listitem>
2568 </varlistentry>
2569 <varlistentry>
2570 <term>
2571         Int32
2572 </term>
2573 <listitem>
2574 <para>
2575                 The secret key for the target backend.
2576 </para>
2577 </listitem>
2578 </varlistentry>
2579 </variablelist>
2580
2581 </para>
2582 </listitem>
2583 </varlistentry>
2584
2585
2586 <varlistentry>
2587 <term>
2588 Close (F)
2589 </term>
2590 <listitem>
2591 <para>
2592
2593 <variablelist>
2594 <varlistentry>
2595 <term>
2596         Byte1('C')
2597 </term>
2598 <listitem>
2599 <para>
2600                 Identifies the message as a Close command.
2601 </para>
2602 </listitem>
2603 </varlistentry>
2604 <varlistentry>
2605 <term>
2606         Int32
2607 </term>
2608 <listitem>
2609 <para>
2610                 Length of message contents in bytes, including self.
2611 </para>
2612 </listitem>
2613 </varlistentry>
2614 <varlistentry>
2615 <term>
2616         Byte1
2617 </term>
2618 <listitem>
2619 <para>
2620                 '<literal>S</>' to close a prepared statement; or
2621                 '<literal>P</>' to close a portal.
2622 </para>
2623 </listitem>
2624 </varlistentry>
2625 <varlistentry>
2626 <term>
2627         String
2628 </term>
2629 <listitem>
2630 <para>
2631                 The name of the prepared statement or portal to close
2632                 (an empty string selects the unnamed prepared statement
2633                 or portal).
2634 </para>
2635 </listitem>
2636 </varlistentry>
2637 </variablelist>
2638 </para>
2639 </listitem>
2640 </varlistentry>
2641
2642
2643 <varlistentry>
2644 <term>
2645 CloseComplete (B)
2646 </term>
2647 <listitem>
2648 <para>
2649
2650 <variablelist>
2651 <varlistentry>
2652 <term>
2653         Byte1('3')
2654 </term>
2655 <listitem>
2656 <para>
2657                 Identifies the message as a Close-complete indicator.
2658 </para>
2659 </listitem>
2660 </varlistentry>
2661 <varlistentry>
2662 <term>
2663         Int32(4)
2664 </term>
2665 <listitem>
2666 <para>
2667                 Length of message contents in bytes, including self.
2668 </para>
2669 </listitem>
2670 </varlistentry>
2671 </variablelist>
2672
2673 </para>
2674 </listitem>
2675 </varlistentry>
2676
2677
2678 <varlistentry>
2679 <term>
2680 CommandComplete (B)
2681 </term>
2682 <listitem>
2683 <para>
2684
2685 <variablelist>
2686 <varlistentry>
2687 <term>
2688         Byte1('C')
2689 </term>
2690 <listitem>
2691 <para>
2692                 Identifies the message as a command-completed response.
2693 </para>
2694 </listitem>
2695 </varlistentry>
2696 <varlistentry>
2697 <term>
2698         Int32
2699 </term>
2700 <listitem>
2701 <para>
2702                 Length of message contents in bytes, including self.
2703 </para>
2704 </listitem>
2705 </varlistentry>
2706 <varlistentry>
2707 <term>
2708         String
2709 </term>
2710 <listitem>
2711        <para>
2712         The command tag.  This is usually a single
2713         word that identifies which SQL command was completed.
2714        </para>
2715
2716        <para>
2717         For an <command>INSERT</command> command, the tag is
2718         <literal>INSERT <replaceable>oid</replaceable>
2719         <replaceable>rows</replaceable></literal>, where
2720         <replaceable>rows</replaceable> is the number of rows
2721         inserted. <replaceable>oid</replaceable> is the object ID
2722         of the inserted row if <replaceable>rows</replaceable> is 1
2723         and the target table has OIDs;
2724         otherwise <replaceable>oid</replaceable> is 0.
2725        </para>
2726
2727        <para>
2728         For a <command>DELETE</command> command, the tag is
2729         <literal>DELETE <replaceable>rows</replaceable></literal> where
2730         <replaceable>rows</replaceable> is the number of rows deleted.
2731        </para>
2732
2733        <para>
2734         For an <command>UPDATE</command> command, the tag is
2735         <literal>UPDATE <replaceable>rows</replaceable></literal> where
2736         <replaceable>rows</replaceable> is the number of rows updated.
2737        </para>
2738
2739        <para>
2740         For a <command>SELECT</command> or <command>CREATE TABLE AS</command>
2741         command, the tag is <literal>SELECT <replaceable>rows</replaceable></literal>
2742         where <replaceable>rows</replaceable> is the number of rows retrieved.
2743        </para>
2744
2745        <para>
2746         For a <command>MOVE</command> command, the tag is
2747         <literal>MOVE <replaceable>rows</replaceable></literal> where
2748         <replaceable>rows</replaceable> is the number of rows the
2749         cursor's position has been changed by.
2750        </para>
2751
2752        <para>
2753         For a <command>FETCH</command> command, the tag is
2754         <literal>FETCH <replaceable>rows</replaceable></literal> where
2755         <replaceable>rows</replaceable> is the number of rows that
2756         have been retrieved from the cursor.
2757        </para>
2758
2759        <para>
2760         For a <command>COPY</command> command, the tag is
2761         <literal>COPY <replaceable>rows</replaceable></literal> where
2762         <replaceable>rows</replaceable> is the number of rows copied.
2763         (Note: the row count appears only in
2764         <productname>PostgreSQL</productname> 8.2 and later.)
2765        </para>
2766
2767 </listitem>
2768 </varlistentry>
2769 </variablelist>
2770
2771 </para>
2772 </listitem>
2773 </varlistentry>
2774
2775
2776 <varlistentry>
2777 <term>
2778 CopyData (F &amp; B)
2779 </term>
2780 <listitem>
2781 <para>
2782 <variablelist>
2783 <varlistentry>
2784 <term>
2785         Byte1('d')
2786 </term>
2787 <listitem>
2788 <para>
2789                 Identifies the message as <command>COPY</command> data.
2790 </para>
2791 </listitem>
2792 </varlistentry>
2793 <varlistentry>
2794 <term>
2795         Int32
2796 </term>
2797 <listitem>
2798 <para>
2799                 Length of message contents in bytes, including self.
2800 </para>
2801 </listitem>
2802 </varlistentry>
2803 <varlistentry>
2804 <term>
2805         Byte<replaceable>n</replaceable>
2806 </term>
2807 <listitem>
2808 <para>
2809                 Data that forms part of a <command>COPY</command> data stream.  Messages sent
2810                 from the backend will always correspond to single data rows,
2811                 but messages sent by frontends might divide the data stream
2812                 arbitrarily.
2813 </para>
2814 </listitem>
2815 </varlistentry>
2816 </variablelist>
2817 </para>
2818 </listitem>
2819 </varlistentry>
2820
2821
2822 <varlistentry>
2823 <term>
2824 CopyDone (F &amp; B)
2825 </term>
2826 <listitem>
2827 <para>
2828
2829 <variablelist>
2830 <varlistentry>
2831 <term>
2832         Byte1('c')
2833 </term>
2834 <listitem>
2835 <para>
2836                 Identifies the message as a <command>COPY</command>-complete indicator.
2837 </para>
2838 </listitem>
2839 </varlistentry>
2840 <varlistentry>
2841 <term>
2842         Int32(4)
2843 </term>
2844 <listitem>
2845 <para>
2846                 Length of message contents in bytes, including self.
2847 </para>
2848 </listitem>
2849 </varlistentry>
2850 </variablelist>
2851
2852 </para>
2853 </listitem>
2854 </varlistentry>
2855
2856
2857 <varlistentry>
2858 <term>
2859 CopyFail (F)
2860 </term>
2861 <listitem>
2862 <para>
2863
2864 <variablelist>
2865 <varlistentry>
2866 <term>
2867         Byte1('f')
2868 </term>
2869 <listitem>
2870 <para>
2871                 Identifies the message as a <command>COPY</command>-failure indicator.
2872 </para>
2873 </listitem>
2874 </varlistentry>
2875 <varlistentry>
2876 <term>
2877         Int32
2878 </term>
2879 <listitem>
2880 <para>
2881                 Length of message contents in bytes, including self.
2882 </para>
2883 </listitem>
2884 </varlistentry>
2885 <varlistentry>
2886 <term>
2887         String
2888 </term>
2889 <listitem>
2890 <para>
2891                 An error message to report as the cause of failure.
2892 </para>
2893 </listitem>
2894 </varlistentry>
2895 </variablelist>
2896
2897 </para>
2898 </listitem>
2899 </varlistentry>
2900
2901
2902 <varlistentry>
2903 <term>
2904 CopyInResponse (B)
2905 </term>
2906 <listitem>
2907 <para>
2908
2909 <variablelist>
2910 <varlistentry>
2911 <term>
2912         Byte1('G')
2913 </term>
2914 <listitem>
2915 <para>
2916                 Identifies the message as a Start Copy In response.
2917                 The frontend must now send copy-in data (if not
2918                 prepared to do so, send a CopyFail message).
2919 </para>
2920 </listitem>
2921 </varlistentry>
2922 <varlistentry>
2923 <term>
2924         Int32
2925 </term>
2926 <listitem>
2927 <para>
2928                 Length of message contents in bytes, including self.
2929 </para>
2930 </listitem>
2931 </varlistentry>
2932 <varlistentry>
2933 <term>
2934         Int8
2935 </term>
2936 <listitem>
2937 <para>
2938                 0 indicates the overall <command>COPY</command> format is textual (rows
2939                 separated by newlines, columns separated by separator
2940                 characters, etc).
2941                 1 indicates the overall copy format is binary (similar
2942                 to DataRow format).
2943                 See <xref linkend="sql-copy">
2944                 for more information.
2945 </para>
2946 </listitem>
2947 </varlistentry>
2948 <varlistentry>
2949 <term>
2950         Int16
2951 </term>
2952 <listitem>
2953 <para>
2954                 The number of columns in the data to be copied
2955                 (denoted <replaceable>N</> below).
2956 </para>
2957 </listitem>
2958 </varlistentry>
2959 <varlistentry>
2960 <term>
2961         Int16[<replaceable>N</>]
2962 </term>
2963 <listitem>
2964 <para>
2965                 The format codes to be used for each column.
2966                 Each must presently be zero (text) or one (binary).
2967                 All must be zero if the overall copy format is textual.
2968 </para>
2969 </listitem>
2970 </varlistentry>
2971 </variablelist>
2972
2973 </para>
2974 </listitem>
2975 </varlistentry>
2976
2977
2978 <varlistentry>
2979 <term>
2980 CopyOutResponse (B)
2981 </term>
2982 <listitem>
2983 <para>
2984
2985 <variablelist>
2986 <varlistentry>
2987 <term>
2988         Byte1('H')
2989 </term>
2990 <listitem>
2991 <para>
2992                 Identifies the message as a Start Copy Out response.
2993                 This message will be followed by copy-out data.
2994 </para>
2995 </listitem>
2996 </varlistentry>
2997 <varlistentry>
2998 <term>
2999         Int32
3000 </term>
3001 <listitem>
3002 <para>
3003                 Length of message contents in bytes, including self.
3004 </para>
3005 </listitem>
3006 </varlistentry>
3007 <varlistentry>
3008 <term>
3009         Int8
3010 </term>
3011 <listitem>
3012 <para>
3013                 0 indicates the overall <command>COPY</command> format
3014                 is textual (rows separated by newlines, columns
3015                 separated by separator characters, etc). 1 indicates
3016                 the overall copy format is binary (similar to DataRow
3017                 format). See <xref linkend="sql-copy"> for more information.
3018 </para>
3019 </listitem>
3020 </varlistentry>
3021 <varlistentry>
3022 <term>
3023         Int16
3024 </term>
3025 <listitem>
3026 <para>
3027                 The number of columns in the data to be copied
3028                 (denoted <replaceable>N</> below).
3029 </para>
3030 </listitem>
3031 </varlistentry>
3032 <varlistentry>
3033 <term>
3034         Int16[<replaceable>N</>]
3035 </term>
3036 <listitem>
3037 <para>
3038                 The format codes to be used for each column.
3039                 Each must presently be zero (text) or one (binary).
3040                 All must be zero if the overall copy format is textual.
3041 </para>
3042 </listitem>
3043 </varlistentry>
3044 </variablelist>
3045
3046 </para>
3047 </listitem>
3048 </varlistentry>
3049
3050
3051 <varlistentry>
3052 <term>
3053 CopyBothResponse (B)
3054 </term>
3055 <listitem>
3056 <para>
3057
3058 <variablelist>
3059 <varlistentry>
3060 <term>
3061         Byte1('W')
3062 </term>
3063 <listitem>
3064 <para>
3065                 Identifies the message as a Start Copy Both response.
3066                 This message is used only for Streaming Replication.
3067 </para>
3068 </listitem>
3069 </varlistentry>
3070 <varlistentry>
3071 <term>
3072         Int32
3073 </term>
3074 <listitem>
3075 <para>
3076                 Length of message contents in bytes, including self.
3077 </para>
3078 </listitem>
3079 </varlistentry>
3080 <varlistentry>
3081 <term>
3082         Int8
3083 </term>
3084 <listitem>
3085 <para>
3086                 0 indicates the overall <command>COPY</command> format
3087                 is textual (rows separated by newlines, columns
3088                 separated by separator characters, etc). 1 indicates
3089                 the overall copy format is binary (similar to DataRow
3090                 format). See <xref linkend="sql-copy"> for more information.
3091 </para>
3092 </listitem>
3093 </varlistentry>
3094 <varlistentry>
3095 <term>
3096         Int16
3097 </term>
3098 <listitem>
3099 <para>
3100                 The number of columns in the data to be copied
3101                 (denoted <replaceable>N</> below).
3102 </para>
3103 </listitem>
3104 </varlistentry>
3105 <varlistentry>
3106 <term>
3107         Int16[<replaceable>N</>]
3108 </term>
3109 <listitem>
3110 <para>
3111                 The format codes to be used for each column.
3112                 Each must presently be zero (text) or one (binary).
3113                 All must be zero if the overall copy format is textual.
3114 </para>
3115 </listitem>
3116 </varlistentry>
3117 </variablelist>
3118
3119 </para>
3120 </listitem>
3121 </varlistentry>
3122
3123
3124 <varlistentry>
3125 <term>
3126 DataRow (B)
3127 </term>
3128 <listitem>
3129 <para>
3130 <variablelist>
3131 <varlistentry>
3132 <term>
3133         Byte1('D')
3134 </term>
3135 <listitem>
3136 <para>
3137                 Identifies the message as a data row.
3138 </para>
3139 </listitem>
3140 </varlistentry>
3141 <varlistentry>
3142 <term>
3143         Int32
3144 </term>
3145 <listitem>
3146 <para>
3147                 Length of message contents in bytes, including self.
3148 </para>
3149 </listitem>
3150 </varlistentry>
3151 <varlistentry>
3152 <term>
3153         Int16
3154 </term>
3155 <listitem>
3156 <para>
3157                 The number of column values that follow (possibly zero).
3158 </para>
3159 </listitem>
3160 </varlistentry>
3161 </variablelist>
3162         Next, the following pair of fields appear for each column:
3163 <variablelist>
3164 <varlistentry>
3165 <term>
3166         Int32
3167 </term>
3168 <listitem>
3169 <para>
3170                 The length of the column value, in bytes (this count
3171                 does not include itself).  Can be zero.
3172                 As a special case, -1 indicates a NULL column value.
3173                 No value bytes follow in the NULL case.
3174 </para>
3175 </listitem>
3176 </varlistentry>
3177 <varlistentry>
3178 <term>
3179         Byte<replaceable>n</replaceable>
3180 </term>
3181 <listitem>
3182 <para>
3183                 The value of the column, in the format indicated by the
3184                 associated format code.
3185                 <replaceable>n</replaceable> is the above length.
3186 </para>
3187 </listitem>
3188 </varlistentry>
3189 </variablelist>
3190
3191 </para>
3192 </listitem>
3193 </varlistentry>
3194
3195
3196 <varlistentry>
3197 <term>
3198 Describe (F)
3199 </term>
3200 <listitem>
3201 <para>
3202
3203 <variablelist>
3204 <varlistentry>
3205 <term>
3206         Byte1('D')
3207 </term>
3208 <listitem>
3209 <para>
3210                 Identifies the message as a Describe command.
3211 </para>
3212 </listitem>
3213 </varlistentry>
3214 <varlistentry>
3215 <term>
3216         Int32
3217 </term>
3218 <listitem>
3219 <para>
3220                 Length of message contents in bytes, including self.
3221 </para>
3222 </listitem>
3223 </varlistentry>
3224 <varlistentry>
3225 <term>
3226         Byte1
3227 </term>
3228 <listitem>
3229 <para>
3230                 '<literal>S</>' to describe a prepared statement; or
3231                 '<literal>P</>' to describe a portal.
3232 </para>
3233 </listitem>
3234 </varlistentry>
3235 <varlistentry>
3236 <term>
3237         String
3238 </term>
3239 <listitem>
3240 <para>
3241                 The name of the prepared statement or portal to describe
3242                 (an empty string selects the unnamed prepared statement
3243                 or portal).
3244 </para>
3245 </listitem>
3246 </varlistentry>
3247 </variablelist>
3248 </para>
3249 </listitem>
3250 </varlistentry>
3251
3252
3253 <varlistentry>
3254 <term>
3255 EmptyQueryResponse (B)
3256 </term>
3257 <listitem>
3258 <para>
3259
3260 <variablelist>
3261 <varlistentry>
3262 <term>
3263         Byte1('I')
3264 </term>
3265 <listitem>
3266 <para>
3267                 Identifies the message as a response to an empty query string.
3268                 (This substitutes for CommandComplete.)
3269 </para>
3270 </listitem>
3271 </varlistentry>
3272 <varlistentry>
3273 <term>
3274         Int32(4)
3275 </term>
3276 <listitem>
3277 <para>
3278                 Length of message contents in bytes, including self.
3279 </para>
3280 </listitem>
3281 </varlistentry>
3282 </variablelist>
3283
3284 </para>
3285 </listitem>
3286 </varlistentry>
3287
3288
3289 <varlistentry>
3290 <term>
3291 ErrorResponse (B)
3292 </term>
3293 <listitem>
3294 <para>
3295
3296 <variablelist>
3297 <varlistentry>
3298 <term>
3299         Byte1('E')
3300 </term>
3301 <listitem>
3302 <para>
3303                 Identifies the message as an error.
3304 </para>
3305 </listitem>
3306 </varlistentry>
3307 <varlistentry>
3308 <term>
3309         Int32
3310 </term>
3311 <listitem>
3312 <para>
3313                 Length of message contents in bytes, including self.
3314 </para>
3315 </listitem>
3316 </varlistentry>
3317 </variablelist>
3318         The message body consists of one or more identified fields,
3319         followed by a zero byte as a terminator.  Fields can appear in
3320         any order.  For each field there is the following:
3321 <variablelist>
3322 <varlistentry>
3323 <term>
3324         Byte1
3325 </term>
3326 <listitem>
3327 <para>
3328                 A code identifying the field type; if zero, this is
3329                 the message terminator and no string follows.
3330                 The presently defined field types are listed in
3331                 <xref linkend="protocol-error-fields">.
3332                 Since more field types might be added in future,
3333                 frontends should silently ignore fields of unrecognized
3334                 type.
3335 </para>
3336 </listitem>
3337 </varlistentry>
3338 <varlistentry>
3339 <term>
3340         String
3341 </term>
3342 <listitem>
3343 <para>
3344                 The field value.
3345 </para>
3346 </listitem>
3347 </varlistentry>
3348 </variablelist>
3349
3350 </para>
3351 </listitem>
3352 </varlistentry>
3353
3354
3355 <varlistentry>
3356 <term>
3357 Execute (F)
3358 </term>
3359 <listitem>
3360 <para>
3361
3362 <variablelist>
3363 <varlistentry>
3364 <term>
3365         Byte1('E')
3366 </term>
3367 <listitem>
3368 <para>
3369                 Identifies the message as an Execute command.
3370 </para>
3371 </listitem>
3372 </varlistentry>
3373 <varlistentry>
3374 <term>
3375         Int32
3376 </term>
3377 <listitem>
3378 <para>
3379                 Length of message contents in bytes, including self.
3380 </para>
3381 </listitem>
3382 </varlistentry>
3383 <varlistentry>
3384 <term>
3385         String
3386 </term>
3387 <listitem>
3388 <para>
3389                 The name of the portal to execute
3390                 (an empty string selects the unnamed portal).
3391 </para>
3392 </listitem>
3393 </varlistentry>
3394 <varlistentry>
3395 <term>
3396         Int32
3397 </term>
3398 <listitem>
3399 <para>
3400                 Maximum number of rows to return, if portal contains
3401                 a query that returns rows (ignored otherwise).  Zero
3402                 denotes <quote>no limit</>.
3403 </para>
3404 </listitem>
3405 </varlistentry>
3406 </variablelist>
3407 </para>
3408 </listitem>
3409 </varlistentry>
3410
3411
3412 <varlistentry>
3413 <term>
3414 Flush (F)
3415 </term>
3416 <listitem>
3417 <para>
3418
3419 <variablelist>
3420 <varlistentry>
3421 <term>
3422         Byte1('H')
3423 </term>
3424 <listitem>
3425 <para>
3426                 Identifies the message as a Flush command.
3427 </para>
3428 </listitem>
3429 </varlistentry>
3430 <varlistentry>
3431 <term>
3432         Int32(4)
3433 </term>
3434 <listitem>
3435 <para>
3436                 Length of message contents in bytes, including self.
3437 </para>
3438 </listitem>
3439 </varlistentry>
3440 </variablelist>
3441
3442 </para>
3443 </listitem>
3444 </varlistentry>
3445
3446
3447 <varlistentry>
3448 <term>
3449 FunctionCall (F)
3450 </term>
3451 <listitem>
3452 <para>
3453
3454 <variablelist>
3455 <varlistentry>
3456 <term>
3457         Byte1('F')
3458 </term>
3459 <listitem>
3460 <para>
3461                 Identifies the message as a function call.
3462 </para>
3463 </listitem>
3464 </varlistentry>
3465 <varlistentry>
3466 <term>
3467         Int32
3468 </term>
3469 <listitem>
3470 <para>
3471                 Length of message contents in bytes, including self.
3472 </para>
3473 </listitem>
3474 </varlistentry>
3475 <varlistentry>
3476 <term>
3477         Int32
3478 </term>
3479 <listitem>
3480 <para>
3481                 Specifies the object ID of the function to call.
3482 </para>
3483 </listitem>
3484 </varlistentry>
3485 <varlistentry>
3486 <term>
3487         Int16
3488 </term>
3489 <listitem>
3490 <para>
3491                 The number of argument format codes that follow
3492                 (denoted <replaceable>C</> below).
3493                 This can be zero to indicate that there are no arguments
3494                 or that the arguments all use the default format (text);
3495                 or one, in which case the specified format code is applied
3496                 to all arguments; or it can equal the actual number of
3497                 arguments.
3498 </para>
3499 </listitem>
3500 </varlistentry>
3501 <varlistentry>
3502 <term>
3503         Int16[<replaceable>C</>]
3504 </term>
3505 <listitem>
3506 <para>
3507                 The argument format codes.  Each must presently be
3508                 zero (text) or one (binary).
3509 </para>
3510 </listitem>
3511 </varlistentry>
3512 <varlistentry>
3513 <term>
3514         Int16
3515 </term>
3516 <listitem>
3517 <para>
3518                 Specifies the number of arguments being supplied to the
3519                 function.
3520 </para>
3521 </listitem>
3522 </varlistentry>
3523 </variablelist>
3524         Next, the following pair of fields appear for each argument:
3525 <variablelist>
3526 <varlistentry>
3527 <term>
3528         Int32
3529 </term>
3530 <listitem>
3531 <para>
3532                 The length of the argument value, in bytes (this count
3533                 does not include itself).  Can be zero.
3534                 As a special case, -1 indicates a NULL argument value.
3535                 No value bytes follow in the NULL case.
3536 </para>
3537 </listitem>
3538 </varlistentry>
3539 <varlistentry>
3540 <term>
3541         Byte<replaceable>n</replaceable>
3542 </term>
3543 <listitem>
3544 <para>
3545                 The value of the argument, in the format indicated by the
3546                 associated format code.
3547                 <replaceable>n</replaceable> is the above length.
3548 </para>
3549 </listitem>
3550 </varlistentry>
3551 </variablelist>
3552         After the last argument, the following field appears:
3553 <variablelist>
3554 <varlistentry>
3555 <term>
3556         Int16
3557 </term>
3558 <listitem>
3559 <para>
3560                 The format code for the function result. Must presently be
3561                 zero (text) or one (binary).
3562 </para>
3563 </listitem>
3564 </varlistentry>
3565 </variablelist>
3566
3567 </para>
3568 </listitem>
3569 </varlistentry>
3570
3571
3572 <varlistentry>
3573 <term>
3574 FunctionCallResponse (B)
3575 </term>
3576 <listitem>
3577 <para>
3578
3579 <variablelist>
3580 <varlistentry>
3581 <term>
3582         Byte1('V')
3583 </term>
3584 <listitem>
3585 <para>
3586                 Identifies the message as a function call result.
3587 </para>
3588 </listitem>
3589 </varlistentry>
3590 <varlistentry>
3591 <term>
3592         Int32
3593 </term>
3594 <listitem>
3595 <para>
3596                 Length of message contents in bytes, including self.
3597 </para>
3598 </listitem>
3599 </varlistentry>
3600 <varlistentry>
3601 <term>
3602         Int32
3603 </term>
3604 <listitem>
3605 <para>
3606                 The length of the function result value, in bytes (this count
3607                 does not include itself).  Can be zero.
3608                 As a special case, -1 indicates a NULL function result.
3609                 No value bytes follow in the NULL case.
3610 </para>
3611 </listitem>
3612 </varlistentry>
3613 <varlistentry>
3614 <term>
3615         Byte<replaceable>n</replaceable>
3616 </term>
3617 <listitem>
3618 <para>
3619                 The value of the function result, in the format indicated by
3620                 the associated format code.
3621                 <replaceable>n</replaceable> is the above length.
3622 </para>
3623 </listitem>
3624 </varlistentry>
3625 </variablelist>
3626
3627 </para>
3628 </listitem>
3629 </varlistentry>
3630
3631
3632 <varlistentry>
3633 <term>
3634 NoData (B)
3635 </term>
3636 <listitem>
3637 <para>
3638
3639 <variablelist>
3640 <varlistentry>
3641 <term>
3642         Byte1('n')
3643 </term>
3644 <listitem>
3645 <para>
3646                 Identifies the message as a no-data indicator.
3647 </para>
3648 </listitem>
3649 </varlistentry>
3650 <varlistentry>
3651 <term>
3652         Int32(4)
3653 </term>
3654 <listitem>
3655 <para>
3656                 Length of message contents in bytes, including self.
3657 </para>
3658 </listitem>
3659 </varlistentry>
3660 </variablelist>
3661
3662 </para>
3663 </listitem>
3664 </varlistentry>
3665
3666
3667 <varlistentry>
3668 <term>
3669 NoticeResponse (B)
3670 </term>
3671 <listitem>
3672 <para>
3673
3674 <variablelist>
3675 <varlistentry>
3676 <term>
3677         Byte1('N')
3678 </term>
3679 <listitem>
3680 <para>
3681                 Identifies the message as a notice.
3682 </para>
3683 </listitem>
3684 </varlistentry>
3685 <varlistentry>
3686 <term>
3687         Int32
3688 </term>
3689 <listitem>
3690 <para>
3691                 Length of message contents in bytes, including self.
3692 </para>
3693 </listitem>
3694 </varlistentry>
3695 </variablelist>
3696         The message body consists of one or more identified fields,
3697         followed by a zero byte as a terminator.  Fields can appear in
3698         any order.  For each field there is the following:
3699 <variablelist>
3700 <varlistentry>
3701 <term>
3702         Byte1
3703 </term>
3704 <listitem>
3705 <para>
3706                 A code identifying the field type; if zero, this is
3707                 the message terminator and no string follows.
3708                 The presently defined field types are listed in
3709                 <xref linkend="protocol-error-fields">.
3710                 Since more field types might be added in future,
3711                 frontends should silently ignore fields of unrecognized
3712                 type.
3713 </para>
3714 </listitem>
3715 </varlistentry>
3716 <varlistentry>
3717 <term>
3718         String
3719 </term>
3720 <listitem>
3721 <para>
3722                 The field value.
3723 </para>
3724 </listitem>
3725 </varlistentry>
3726 </variablelist>
3727
3728 </para>
3729 </listitem>
3730 </varlistentry>
3731
3732
3733 <varlistentry>
3734 <term>
3735 NotificationResponse (B)
3736 </term>
3737 <listitem>
3738 <para>
3739
3740 <variablelist>
3741 <varlistentry>
3742 <term>
3743         Byte1('A')
3744 </term>
3745 <listitem>
3746 <para>
3747                 Identifies the message as a notification response.
3748 </para>
3749 </listitem>
3750 </varlistentry>
3751 <varlistentry>
3752 <term>
3753         Int32
3754 </term>
3755 <listitem>
3756 <para>
3757                 Length of message contents in bytes, including self.
3758 </para>
3759 </listitem>
3760 </varlistentry>
3761 <varlistentry>
3762 <term>
3763         Int32
3764 </term>
3765 <listitem>
3766 <para>
3767                 The process ID of the notifying backend process.
3768 </para>
3769 </listitem>
3770 </varlistentry>
3771 <varlistentry>
3772 <term>
3773         String
3774 </term>
3775 <listitem>
3776 <para>
3777                 The name of the channel that the notify has been raised on.
3778 </para>
3779 </listitem>
3780 </varlistentry>
3781 <varlistentry>
3782 <term>
3783         String
3784 </term>
3785 <listitem>
3786 <para>
3787                 The <quote>payload</> string passed from the notifying process.
3788 </para>
3789 </listitem>
3790 </varlistentry>
3791 </variablelist>
3792
3793 </para>
3794 </listitem>
3795 </varlistentry>
3796
3797
3798 <varlistentry>
3799 <term>
3800 ParameterDescription (B)
3801 </term>
3802 <listitem>
3803 <para>
3804
3805 <variablelist>
3806 <varlistentry>
3807 <term>
3808         Byte1('t')
3809 </term>
3810 <listitem>
3811 <para>
3812                 Identifies the message as a parameter description.
3813 </para>
3814 </listitem>
3815 </varlistentry>
3816 <varlistentry>
3817 <term>
3818         Int32
3819 </term>
3820 <listitem>
3821 <para>
3822                 Length of message contents in bytes, including self.
3823 </para>
3824 </listitem>
3825 </varlistentry>
3826 <varlistentry>
3827 <term>
3828         Int16
3829 </term>
3830 <listitem>
3831 <para>
3832                 The number of parameters used by the statement
3833                 (can be zero).
3834 </para>
3835 </listitem>
3836 </varlistentry>
3837 </variablelist>
3838         Then, for each parameter, there is the following:
3839 <variablelist>
3840 <varlistentry>
3841 <term>
3842         Int32
3843 </term>
3844 <listitem>
3845 <para>
3846                 Specifies the object ID of the parameter data type.
3847 </para>
3848 </listitem>
3849 </varlistentry>
3850 </variablelist>
3851 </para>
3852 </listitem>
3853 </varlistentry>
3854
3855
3856 <varlistentry>
3857 <term>
3858 ParameterStatus (B)
3859 </term>
3860 <listitem>
3861 <para>
3862
3863 <variablelist>
3864 <varlistentry>
3865 <term>
3866         Byte1('S')
3867 </term>
3868 <listitem>
3869 <para>
3870                 Identifies the message as a run-time parameter status report.
3871 </para>
3872 </listitem>
3873 </varlistentry>
3874 <varlistentry>
3875 <term>
3876         Int32
3877 </term>
3878 <listitem>
3879 <para>
3880                 Length of message contents in bytes, including self.
3881 </para>
3882 </listitem>
3883 </varlistentry>
3884 <varlistentry>
3885 <term>
3886         String
3887 </term>
3888 <listitem>
3889 <para>
3890                 The name of the run-time parameter being reported.
3891 </para>
3892 </listitem>
3893 </varlistentry>
3894 <varlistentry>
3895 <term>
3896         String
3897 </term>
3898 <listitem>
3899 <para>
3900                 The current value of the parameter.
3901 </para>
3902 </listitem>
3903 </varlistentry>
3904 </variablelist>
3905 </para>
3906 </listitem>
3907 </varlistentry>
3908
3909
3910 <varlistentry>
3911 <term>
3912 Parse (F)
3913 </term>
3914 <listitem>
3915 <para>
3916
3917 <variablelist>
3918 <varlistentry>
3919 <term>
3920         Byte1('P')
3921 </term>
3922 <listitem>
3923 <para>
3924                 Identifies the message as a Parse command.
3925 </para>
3926 </listitem>
3927 </varlistentry>
3928 <varlistentry>
3929 <term>
3930         Int32
3931 </term>
3932 <listitem>
3933 <para>
3934                 Length of message contents in bytes, including self.
3935 </para>
3936 </listitem>
3937 </varlistentry>
3938 <varlistentry>
3939 <term>
3940         String
3941 </term>
3942 <listitem>
3943 <para>
3944                 The name of the destination prepared statement
3945                 (an empty string selects the unnamed prepared statement).
3946 </para>
3947 </listitem>
3948 </varlistentry>
3949 <varlistentry>
3950 <term>
3951         String
3952 </term>
3953 <listitem>
3954 <para>
3955                 The query string to be parsed.
3956 </para>
3957 </listitem>
3958 </varlistentry>
3959 <varlistentry>
3960 <term>
3961         Int16
3962 </term>
3963 <listitem>
3964 <para>
3965                 The number of parameter data types specified
3966                 (can be zero).  Note that this is not an indication of
3967                 the number of parameters that might appear in the
3968                 query string, only the number that the frontend wants to
3969                 prespecify types for.
3970 </para>
3971 </listitem>
3972 </varlistentry>
3973 </variablelist>
3974         Then, for each parameter, there is the following:
3975 <variablelist>
3976 <varlistentry>
3977 <term>
3978         Int32
3979 </term>
3980 <listitem>
3981 <para>
3982                 Specifies the object ID of the parameter data type.
3983                 Placing a zero here is equivalent to leaving the type
3984                 unspecified.
3985 </para>
3986 </listitem>
3987 </varlistentry>
3988 </variablelist>
3989 </para>
3990 </listitem>
3991 </varlistentry>
3992
3993
3994 <varlistentry>
3995 <term>
3996 ParseComplete (B)
3997 </term>
3998 <listitem>
3999 <para>
4000
4001 <variablelist>
4002 <varlistentry>
4003 <term>
4004         Byte1('1')
4005 </term>
4006 <listitem>
4007 <para>
4008                 Identifies the message as a Parse-complete indicator.
4009 </para>
4010 </listitem>
4011 </varlistentry>
4012 <varlistentry>
4013 <term>
4014         Int32(4)
4015 </term>
4016 <listitem>
4017 <para>
4018                 Length of message contents in bytes, including self.
4019 </para>
4020 </listitem>
4021 </varlistentry>
4022 </variablelist>
4023
4024 </para>
4025 </listitem>
4026 </varlistentry>
4027
4028
4029 <varlistentry>
4030 <term>
4031 PasswordMessage (F)
4032 </term>
4033 <listitem>
4034 <para>
4035
4036 <variablelist>
4037 <varlistentry>
4038 <term>
4039         Byte1('p')
4040 </term>
4041 <listitem>
4042 <para>
4043                 Identifies the message as a password response. Note that
4044                 this is also used for GSSAPI and SSPI response messages
4045                 (which is really a design error, since the contained data
4046                 is not a null-terminated string in that case, but can be
4047                 arbitrary binary data).
4048 </para>
4049 </listitem>
4050 </varlistentry>
4051 <varlistentry>
4052 <term>
4053         Int32
4054 </term>
4055 <listitem>
4056 <para>
4057                 Length of message contents in bytes, including self.
4058 </para>
4059 </listitem>
4060 </varlistentry>
4061 <varlistentry>
4062 <term>
4063         String
4064 </term>
4065 <listitem>
4066 <para>
4067                 The password (encrypted, if requested).
4068 </para>
4069 </listitem>
4070 </varlistentry>
4071 </variablelist>
4072 </para>
4073 </listitem>
4074 </varlistentry>
4075
4076
4077 <varlistentry>
4078 <term>
4079 PortalSuspended (B)
4080 </term>
4081 <listitem>
4082 <para>
4083
4084 <variablelist>
4085 <varlistentry>
4086 <term>
4087         Byte1('s')
4088 </term>
4089 <listitem>
4090 <para>
4091                 Identifies the message as a portal-suspended indicator.
4092                 Note this only appears if an Execute message's row-count limit
4093                 was reached.
4094 </para>
4095 </listitem>
4096 </varlistentry>
4097 <varlistentry>
4098 <term>
4099         Int32(4)
4100 </term>
4101 <listitem>
4102 <para>
4103                 Length of message contents in bytes, including self.
4104 </para>
4105 </listitem>
4106 </varlistentry>
4107 </variablelist>
4108
4109 </para>
4110 </listitem>
4111 </varlistentry>
4112
4113
4114 <varlistentry>
4115 <term>
4116 Query (F)
4117 </term>
4118 <listitem>
4119 <para>
4120
4121 <variablelist>
4122 <varlistentry>
4123 <term>
4124         Byte1('Q')
4125 </term>
4126 <listitem>
4127 <para>
4128                 Identifies the message as a simple query.
4129 </para>
4130 </listitem>
4131 </varlistentry>
4132 <varlistentry>
4133 <term>
4134         Int32
4135 </term>
4136 <listitem>
4137 <para>
4138                 Length of message contents in bytes, including self.
4139 </para>
4140 </listitem>
4141 </varlistentry>
4142 <varlistentry>
4143 <term>
4144         String
4145 </term>
4146 <listitem>
4147 <para>
4148                 The query string itself.
4149 </para>
4150 </listitem>
4151 </varlistentry>
4152 </variablelist>
4153
4154 </para>
4155 </listitem>
4156 </varlistentry>
4157
4158
4159 <varlistentry>
4160 <term>
4161 ReadyForQuery (B)
4162 </term>
4163 <listitem>
4164 <para>
4165
4166 <variablelist>
4167 <varlistentry>
4168 <term>
4169         Byte1('Z')
4170 </term>
4171 <listitem>
4172 <para>
4173                 Identifies the message type.  ReadyForQuery is sent
4174                 whenever the backend is ready for a new query cycle.
4175 </para>
4176 </listitem>
4177 </varlistentry>
4178 <varlistentry>
4179 <term>
4180         Int32(5)
4181 </term>
4182 <listitem>
4183 <para>
4184                 Length of message contents in bytes, including self.
4185 </para>
4186 </listitem>
4187 </varlistentry>
4188 <varlistentry>
4189 <term>
4190         Byte1
4191 </term>
4192 <listitem>
4193 <para>
4194                 Current backend transaction status indicator.
4195                 Possible values are '<literal>I</>' if idle (not in
4196                 a transaction block); '<literal>T</>' if in a transaction
4197                 block; or '<literal>E</>' if in a failed transaction
4198                 block (queries will be rejected until block is ended).
4199 </para>
4200 </listitem>
4201 </varlistentry>
4202 </variablelist>
4203
4204 </para>
4205 </listitem>
4206 </varlistentry>
4207
4208
4209 <varlistentry>
4210 <term>
4211 RowDescription (B)
4212 </term>
4213 <listitem>
4214 <para>
4215
4216 <variablelist>
4217 <varlistentry>
4218 <term>
4219         Byte1('T')
4220 </term>
4221 <listitem>
4222 <para>
4223                 Identifies the message as a row description.
4224 </para>
4225 </listitem>
4226 </varlistentry>
4227 <varlistentry>
4228 <term>
4229         Int32
4230 </term>
4231 <listitem>
4232 <para>
4233                 Length of message contents in bytes, including self.
4234 </para>
4235 </listitem>
4236 </varlistentry>
4237 <varlistentry>
4238 <term>
4239         Int16
4240 </term>
4241 <listitem>
4242 <para>
4243                 Specifies the number of fields in a row (can be zero).
4244 </para>
4245 </listitem>
4246 </varlistentry>
4247 </variablelist>
4248         Then, for each field, there is the following:
4249 <variablelist>
4250 <varlistentry>
4251 <term>
4252         String
4253 </term>
4254 <listitem>
4255 <para>
4256                 The field name.
4257 </para>
4258 </listitem>
4259 </varlistentry>
4260 <varlistentry>
4261 <term>
4262         Int32
4263 </term>
4264 <listitem>
4265 <para>
4266                 If the field can be identified as a column of a specific
4267                 table, the object ID of the table; otherwise zero.
4268 </para>
4269 </listitem>
4270 </varlistentry>
4271 <varlistentry>
4272 <term>
4273         Int16
4274 </term>
4275 <listitem>
4276 <para>
4277                 If the field can be identified as a column of a specific
4278                 table, the attribute number of the column; otherwise zero.
4279 </para>
4280 </listitem>
4281 </varlistentry>
4282 <varlistentry>
4283 <term>
4284         Int32
4285 </term>
4286 <listitem>
4287 <para>
4288                 The object ID of the field's data type.
4289 </para>
4290 </listitem>
4291 </varlistentry>
4292 <varlistentry>
4293 <term>
4294         Int16
4295 </term>
4296 <listitem>
4297 <para>
4298                 The data type size (see <varname>pg_type.typlen</>).
4299                 Note that negative values denote variable-width types.
4300 </para>
4301 </listitem>
4302 </varlistentry>
4303 <varlistentry>
4304 <term>
4305         Int32
4306 </term>
4307 <listitem>
4308 <para>
4309                 The type modifier (see <varname>pg_attribute.atttypmod</>).
4310                 The meaning of the modifier is type-specific.
4311 </para>
4312 </listitem>
4313 </varlistentry>
4314 <varlistentry>
4315 <term>
4316         Int16
4317 </term>
4318 <listitem>
4319 <para>
4320                 The format code being used for the field.  Currently will
4321                 be zero (text) or one (binary).  In a RowDescription
4322                 returned from the statement variant of Describe, the
4323                 format code is not yet known and will always be zero.
4324 </para>
4325 </listitem>
4326 </varlistentry>
4327 </variablelist>
4328
4329 </para>
4330 </listitem>
4331 </varlistentry>
4332
4333
4334 <varlistentry>
4335 <term>
4336 SSLRequest (F)
4337 </term>
4338 <listitem>
4339 <para>
4340
4341 <variablelist>
4342 <varlistentry>
4343 <term>
4344         Int32(8)
4345 </term>
4346 <listitem>
4347 <para>
4348                 Length of message contents in bytes, including self.
4349 </para>
4350 </listitem>
4351 </varlistentry>
4352 <varlistentry>
4353 <term>
4354         Int32(80877103)
4355 </term>
4356 <listitem>
4357 <para>
4358                 The <acronym>SSL</acronym> request code.  The value is chosen to contain
4359                 <literal>1234</> in the most significant 16 bits, and <literal>5679</> in the
4360                 least 16 significant bits.  (To avoid confusion, this code
4361                 must not be the same as any protocol version number.)
4362 </para>
4363 </listitem>
4364 </varlistentry>
4365 </variablelist>
4366
4367 </para>
4368 </listitem>
4369 </varlistentry>
4370
4371
4372 <varlistentry>
4373 <term>
4374 StartupMessage (F)
4375 </term>
4376 <listitem>
4377 <para>
4378
4379 <variablelist>
4380 <varlistentry>
4381 <term>
4382         Int32
4383 </term>
4384 <listitem>
4385 <para>
4386                 Length of message contents in bytes, including self.
4387 </para>
4388 </listitem>
4389 </varlistentry>
4390 <varlistentry>
4391 <term>
4392         Int32(196608)
4393 </term>
4394 <listitem>
4395 <para>
4396                 The protocol version number.  The most significant 16 bits are
4397                 the major version number (3 for the protocol described here).
4398                 The least significant 16 bits are the minor version number
4399                 (0 for the protocol described here).
4400 </para>
4401 </listitem>
4402 </varlistentry>
4403 </variablelist>
4404         The protocol version number is followed by one or more pairs of
4405         parameter name and value strings.  A zero byte is required as a
4406         terminator after the last name/value pair.
4407         Parameters can appear in any
4408         order.  <literal>user</> is required, others are optional.
4409         Each parameter is specified as:
4410 <variablelist>
4411 <varlistentry>
4412 <term>
4413         String
4414 </term>
4415 <listitem>
4416 <para>
4417                 The parameter name.  Currently recognized names are:
4418
4419 <variablelist>
4420 <varlistentry>
4421 <term>
4422                 <literal>user</>
4423 </term>
4424 <listitem>
4425 <para>
4426                         The database user name to connect as.  Required;
4427                         there is no default.
4428 </para>
4429 </listitem>
4430 </varlistentry>
4431 <varlistentry>
4432 <term>
4433                 <literal>database</>
4434 </term>
4435 <listitem>
4436 <para>
4437                         The database to connect to.  Defaults to the user name.
4438 </para>
4439 </listitem>
4440 </varlistentry>
4441 <varlistentry>
4442 <term>
4443                 <literal>options</>
4444 </term>
4445 <listitem>
4446 <para>
4447                         Command-line arguments for the backend.  (This is
4448                         deprecated in favor of setting individual run-time
4449                         parameters.)
4450 </para>
4451 </listitem>
4452 </varlistentry>
4453 </variablelist>
4454
4455                 In addition to the above, any run-time parameter that can be
4456                 set at backend start time might be listed.  Such settings
4457                 will be applied during backend start (after parsing the
4458                 command-line options if any).  The values will act as
4459                 session defaults.
4460 </para>
4461 </listitem>
4462 </varlistentry>
4463 <varlistentry>
4464 <term>
4465         String
4466 </term>
4467 <listitem>
4468 <para>
4469                 The parameter value.
4470 </para>
4471 </listitem>
4472 </varlistentry>
4473 </variablelist>
4474
4475 </para>
4476 </listitem>
4477 </varlistentry>
4478
4479
4480 <varlistentry>
4481 <term>
4482 Sync (F)
4483 </term>
4484 <listitem>
4485 <para>
4486
4487 <variablelist>
4488 <varlistentry>
4489 <term>
4490         Byte1('S')
4491 </term>
4492 <listitem>
4493 <para>
4494                 Identifies the message as a Sync command.
4495 </para>
4496 </listitem>
4497 </varlistentry>
4498 <varlistentry>
4499 <term>
4500         Int32(4)
4501 </term>
4502 <listitem>
4503 <para>
4504                 Length of message contents in bytes, including self.
4505 </para>
4506 </listitem>
4507 </varlistentry>
4508 </variablelist>
4509
4510 </para>
4511 </listitem>
4512 </varlistentry>
4513
4514
4515 <varlistentry>
4516 <term>
4517 Terminate (F)
4518 </term>
4519 <listitem>
4520 <para>
4521
4522 <variablelist>
4523 <varlistentry>
4524 <term>
4525         Byte1('X')
4526 </term>
4527 <listitem>
4528 <para>
4529                 Identifies the message as a termination.
4530 </para>
4531 </listitem>
4532 </varlistentry>
4533 <varlistentry>
4534 <term>
4535         Int32(4)
4536 </term>
4537 <listitem>
4538 <para>
4539                 Length of message contents in bytes, including self.
4540 </para>
4541 </listitem>
4542 </varlistentry>
4543 </variablelist>
4544
4545 </para>
4546 </listitem>
4547 </varlistentry>
4548
4549
4550 </variablelist>
4551
4552 </sect1>
4553
4554
4555 <sect1 id="protocol-error-fields">
4556 <title>Error and Notice Message Fields</title>
4557
4558 <para>
4559 This section describes the fields that can appear in ErrorResponse and
4560 NoticeResponse messages.  Each field type has a single-byte identification
4561 token.  Note that any given field type should appear at most once per
4562 message.
4563 </para>
4564
4565 <variablelist>
4566
4567 <varlistentry>
4568 <term>
4569 <literal>S</>
4570 </term>
4571 <listitem>
4572 <para>
4573         Severity: the field contents are
4574         <literal>ERROR</>, <literal>FATAL</>, or
4575         <literal>PANIC</> (in an error message), or
4576         <literal>WARNING</>, <literal>NOTICE</>, <literal>DEBUG</>,
4577         <literal>INFO</>, or <literal>LOG</> (in a notice message),
4578         or a localized translation of one of these.  Always present.
4579 </para>
4580 </listitem>
4581 </varlistentry>
4582
4583 <varlistentry>
4584 <term>
4585 <literal>C</>
4586 </term>
4587 <listitem>
4588 <para>
4589         Code: the SQLSTATE code for the error (see <xref
4590         linkend="errcodes-appendix">).  Not localizable.  Always present.
4591 </para>
4592 </listitem>
4593 </varlistentry>
4594
4595 <varlistentry>
4596 <term>
4597 <literal>M</>
4598 </term>
4599 <listitem>
4600 <para>
4601         Message: the primary human-readable error message.
4602         This should be accurate but terse (typically one line).
4603         Always present.
4604 </para>
4605 </listitem>
4606 </varlistentry>
4607
4608 <varlistentry>
4609 <term>
4610 <literal>D</>
4611 </term>
4612 <listitem>
4613 <para>
4614         Detail: an optional secondary error message carrying more
4615         detail about the problem.  Might run to multiple lines.
4616 </para>
4617 </listitem>
4618 </varlistentry>
4619
4620 <varlistentry>
4621 <term>
4622 <literal>H</>
4623 </term>
4624 <listitem>
4625 <para>
4626         Hint: an optional suggestion what to do about the problem.
4627         This is intended to differ from Detail in that it offers advice
4628         (potentially inappropriate) rather than hard facts.
4629         Might run to multiple lines.
4630 </para>
4631 </listitem>
4632 </varlistentry>
4633
4634 <varlistentry>
4635 <term>
4636 <literal>P</>
4637 </term>
4638 <listitem>
4639 <para>
4640         Position: the field value is a decimal ASCII integer, indicating
4641         an error cursor position as an index into the original query string.
4642         The first character has index 1, and positions are measured in
4643         characters not bytes.
4644 </para>
4645 </listitem>
4646 </varlistentry>
4647
4648 <varlistentry>
4649 <term>
4650 <literal>p</>
4651 </term>
4652 <listitem>
4653 <para>
4654         Internal position: this is defined the same as the <literal>P</>
4655         field, but it is used when the cursor position refers to an internally
4656         generated command rather than the one submitted by the client.
4657         The <literal>q</> field will always appear when this field appears.
4658 </para>
4659 </listitem>
4660 </varlistentry>
4661
4662 <varlistentry>
4663 <term>
4664 <literal>q</>
4665 </term>
4666 <listitem>
4667 <para>
4668         Internal query: the text of a failed internally-generated command.
4669         This could be, for example, a SQL query issued by a PL/pgSQL function.
4670 </para>
4671 </listitem>
4672 </varlistentry>
4673
4674 <varlistentry>
4675 <term>
4676 <literal>W</>
4677 </term>
4678 <listitem>
4679 <para>
4680         Where: an indication of the context in which the error occurred.
4681         Presently this includes a call stack traceback of active
4682         procedural language functions and internally-generated queries.
4683         The trace is one entry per line, most recent first.
4684 </para>
4685 </listitem>
4686 </varlistentry>
4687
4688 <varlistentry>
4689 <term>
4690 <literal>F</>
4691 </term>
4692 <listitem>
4693 <para>
4694         File: the file name of the source-code location where the error
4695         was reported.
4696 </para>
4697 </listitem>
4698 </varlistentry>
4699
4700 <varlistentry>
4701 <term>
4702 <literal>L</>
4703 </term>
4704 <listitem>
4705 <para>
4706         Line: the line number of the source-code location where the error
4707         was reported.
4708 </para>
4709 </listitem>
4710 </varlistentry>
4711
4712 <varlistentry>
4713 <term>
4714 <literal>R</>
4715 </term>
4716 <listitem>
4717 <para>
4718         Routine: the name of the source-code routine reporting the error.
4719 </para>
4720 </listitem>
4721 </varlistentry>
4722
4723 </variablelist>
4724
4725 <para>
4726 The client is responsible for formatting displayed information to meet its
4727 needs; in particular it should break long lines as needed.  Newline characters
4728 appearing in the error message fields should be treated as paragraph breaks,
4729 not line breaks.
4730 </para>
4731
4732 </sect1>
4733
4734 <sect1 id="protocol-changes">
4735 <title>Summary of Changes since Protocol 2.0</title>
4736
4737 <para>
4738 This section provides a quick checklist of changes, for the benefit of
4739 developers trying to update existing client libraries to protocol 3.0.
4740 </para>
4741
4742 <para>
4743 The initial startup packet uses a flexible list-of-strings format
4744 instead of a fixed format.  Notice that session default values for run-time
4745 parameters can now be specified directly in the startup packet.  (Actually,
4746 you could do that before using the <literal>options</> field, but given the
4747 limited width of <literal>options</> and the lack of any way to quote
4748 whitespace in the values, it wasn't a very safe technique.)
4749 </para>
4750
4751 <para>
4752 All messages now have a length count immediately following the message type
4753 byte (except for startup packets, which have no type byte).  Also note that
4754 PasswordMessage now has a type byte.
4755 </para>
4756
4757 <para>
4758 ErrorResponse and NoticeResponse ('<literal>E</>' and '<literal>N</>')
4759 messages now contain multiple fields, from which the client code can
4760 assemble an error message of the desired level of verbosity.  Note that
4761 individual fields will typically not end with a newline, whereas the single
4762 string sent in the older protocol always did.
4763 </para>
4764
4765 <para>
4766 The ReadyForQuery ('<literal>Z</>') message includes a transaction status
4767 indicator.
4768 </para>
4769
4770 <para>
4771 The distinction between BinaryRow and DataRow message types is gone; the
4772 single DataRow message type serves for returning data in all formats.
4773 Note that the layout of DataRow has changed to make it easier to parse.
4774 Also, the representation of binary values has changed: it is no longer
4775 directly tied to the server's internal representation.
4776 </para>
4777
4778 <para>
4779 There is a new <quote>extended query</> sub-protocol, which adds the frontend
4780 message types Parse, Bind, Execute, Describe, Close, Flush, and Sync, and the
4781 backend message types ParseComplete, BindComplete, PortalSuspended,
4782 ParameterDescription, NoData, and CloseComplete.  Existing clients do not
4783 have to concern themselves with this sub-protocol, but making use of it
4784 might allow improvements in performance or functionality.
4785 </para>
4786
4787 <para>
4788 <command>COPY</command> data is now encapsulated into CopyData and CopyDone messages.  There
4789 is a well-defined way to recover from errors during <command>COPY</command>.  The special
4790 <quote><literal>\.</></quote> last line is not needed anymore, and is not sent
4791 during <command>COPY OUT</command>.
4792 (It is still recognized as a terminator during <command>COPY IN</command>, but its use is
4793 deprecated and will eventually be removed.)  Binary <command>COPY</command> is supported.
4794 The CopyInResponse and CopyOutResponse messages include fields indicating
4795 the number of columns and the format of each column.
4796 </para>
4797
4798 <para>
4799 The layout of FunctionCall and FunctionCallResponse messages has changed.
4800 FunctionCall can now support passing NULL arguments to functions.  It also
4801 can handle passing parameters and retrieving results in either text or
4802 binary format.  There is no longer any reason to consider FunctionCall a
4803 potential security hole, since it does not offer direct access to internal
4804 server data representations.
4805 </para>
4806
4807 <para>
4808 The backend sends ParameterStatus ('<literal>S</>') messages during connection
4809 startup for all parameters it considers interesting to the client library.
4810 Subsequently, a ParameterStatus message is sent whenever the active value
4811 changes for any of these parameters.
4812 </para>
4813
4814 <para>
4815 The RowDescription ('<literal>T</>') message carries new table OID and column
4816 number fields for each column of the described row.  It also shows the format
4817 code for each column.
4818 </para>
4819
4820 <para>
4821 The CursorResponse ('<literal>P</>') message is no longer generated by
4822 the backend.
4823 </para>
4824
4825 <para>
4826 The NotificationResponse ('<literal>A</>') message has an additional string
4827 field, which can carry a <quote>payload</> string passed
4828 from the <command>NOTIFY</command> event sender.
4829 </para>
4830
4831 <para>
4832 The EmptyQueryResponse ('<literal>I</>') message used to include an empty
4833 string parameter; this has been removed.
4834 </para>
4835
4836 </sect1>
4837
4838 </chapter>