]> granicus.if.org Git - postgresql/blob - doc/src/sgml/protocol.sgml
Reset master xmin when hot_standby_feedback disabled.
[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 either end sends a CopyDone message. After the client
1028     sends a CopyDone message, the connection goes from copy-both mode to
1029     copy-out mode, and the client may not send any more CopyData messages.
1030     Similarly, when the server sends a CopyDone message, the connection
1031     goes into copy-in mode, and the server may not send any more CopyData
1032     messages. After both sides have sent a CopyDone message, the copy mode
1033     is terminated, and the backend reverts to the command-processing mode.
1034     See <xref linkend="protocol-replication"> for more information on the
1035     subprotocol transmitted over copy-both mode.
1036    </para>
1037
1038    <para>
1039     The CopyInResponse, CopyOutResponse and CopyBothResponse messages
1040     include fields that inform the frontend of the number of columns
1041     per row and the format codes being used for each column.  (As of
1042     the present implementation, all columns in a given <command>COPY</>
1043     operation will use the same format, but the message design does not
1044     assume this.)
1045    </para>
1046
1047   </sect2>
1048
1049   <sect2 id="protocol-async">
1050    <title>Asynchronous Operations</title>
1051
1052    <para>
1053     There are several cases in which the backend will send messages that
1054     are not specifically prompted by the frontend's command stream.
1055     Frontends must be prepared to deal with these messages at any time,
1056     even when not engaged in a query.
1057     At minimum, one should check for these cases before beginning to
1058     read a query response.
1059    </para>
1060
1061    <para>
1062     It is possible for NoticeResponse messages to be generated due to
1063     outside activity; for example, if the database administrator commands
1064     a <quote>fast</> database shutdown, the backend will send a NoticeResponse
1065     indicating this fact before closing the connection.  Accordingly,
1066     frontends should always be prepared to accept and display NoticeResponse
1067     messages, even when the connection is nominally idle.
1068    </para>
1069
1070    <para>
1071     ParameterStatus messages will be generated whenever the active
1072     value changes for any of the parameters the backend believes the
1073     frontend should know about.  Most commonly this occurs in response
1074     to a <command>SET</> SQL command executed by the frontend, and
1075     this case is effectively synchronous &mdash; but it is also possible
1076     for parameter status changes to occur because the administrator
1077     changed a configuration file and then sent the
1078     <systemitem>SIGHUP</systemitem> signal to the server.  Also,
1079     if a <command>SET</command> command is rolled back, an appropriate
1080     ParameterStatus message will be generated to report the current
1081     effective value.
1082    </para>
1083
1084    <para>
1085     At present there is a hard-wired set of parameters for which
1086     ParameterStatus will be generated: they are
1087     <varname>server_version</>,
1088     <varname>server_encoding</>,
1089     <varname>client_encoding</>,
1090     <varname>application_name</>,
1091     <varname>is_superuser</>,
1092     <varname>session_authorization</>,
1093     <varname>DateStyle</>,
1094     <varname>IntervalStyle</>,
1095     <varname>TimeZone</>,
1096     <varname>integer_datetimes</>, and
1097     <varname>standard_conforming_strings</>.
1098     (<varname>server_encoding</>, <varname>TimeZone</>, and
1099     <varname>integer_datetimes</> were not reported by releases before 8.0;
1100     <varname>standard_conforming_strings</> was not reported by releases
1101     before 8.1;
1102     <varname>IntervalStyle</> was not reported by releases before 8.4;
1103     <varname>application_name</> was not reported by releases before 9.0.)
1104     Note that
1105     <varname>server_version</>,
1106     <varname>server_encoding</> and
1107     <varname>integer_datetimes</>
1108     are pseudo-parameters that cannot change after startup.
1109     This set might change in the future, or even become configurable.
1110     Accordingly, a frontend should simply ignore ParameterStatus for
1111     parameters that it does not understand or care about.
1112    </para>
1113
1114    <para>
1115     If a frontend issues a <command>LISTEN</command> command, then the
1116     backend will send a NotificationResponse message (not to be
1117     confused with NoticeResponse!)  whenever a
1118     <command>NOTIFY</command> command is executed for the same
1119     channel name.
1120    </para>
1121
1122    <note>
1123     <para>
1124      At present, NotificationResponse can only be sent outside a
1125      transaction, and thus it will not occur in the middle of a
1126      command-response series, though it might occur just before ReadyForQuery.
1127      It is unwise to design frontend logic that assumes that, however.
1128      Good practice is to be able to accept NotificationResponse at any
1129      point in the protocol.
1130     </para>
1131    </note>
1132   </sect2>
1133
1134   <sect2>
1135    <title>Canceling Requests in Progress</title>
1136
1137    <para>
1138     During the processing of a query, the frontend might request
1139     cancellation of the query.  The cancel request is not sent
1140     directly on the open connection to the backend for reasons of
1141     implementation efficiency: we don't want to have the backend
1142     constantly checking for new input from the frontend during query
1143     processing.  Cancel requests should be relatively infrequent, so
1144     we make them slightly cumbersome in order to avoid a penalty in
1145     the normal case.
1146    </para>
1147
1148    <para>
1149     To issue a cancel request, the frontend opens a new connection to
1150     the server and sends a CancelRequest message, rather than the
1151     StartupMessage message that would ordinarily be sent across a new
1152     connection.  The server will process this request and then close
1153     the connection.  For security reasons, no direct reply is made to
1154     the cancel request message.
1155    </para>
1156
1157    <para>
1158     A CancelRequest message will be ignored unless it contains the
1159     same key data (PID and secret key) passed to the frontend during
1160     connection start-up.  If the request matches the PID and secret
1161     key for a currently executing backend, the processing of the
1162     current query is aborted.  (In the existing implementation, this is
1163     done by sending a special signal to the backend process that is
1164     processing the query.)
1165    </para>
1166
1167    <para>
1168     The cancellation signal might or might not have any effect &mdash; for
1169     example, if it arrives after the backend has finished processing
1170     the query, then it will have no effect.  If the cancellation is
1171     effective, it results in the current command being terminated
1172     early with an error message.
1173    </para>
1174
1175    <para>
1176     The upshot of all this is that for reasons of both security and
1177     efficiency, the frontend has no direct way to tell whether a
1178     cancel request has succeeded.  It must continue to wait for the
1179     backend to respond to the query.  Issuing a cancel simply improves
1180     the odds that the current query will finish soon, and improves the
1181     odds that it will fail with an error message instead of
1182     succeeding.
1183    </para>
1184
1185    <para>
1186     Since the cancel request is sent across a new connection to the
1187     server and not across the regular frontend/backend communication
1188     link, it is possible for the cancel request to be issued by any
1189     process, not just the frontend whose query is to be canceled.
1190     This might provide additional flexibility when building
1191     multiple-process applications.  It also introduces a security
1192     risk, in that unauthorized persons might try to cancel queries.
1193     The security risk is addressed by requiring a dynamically
1194     generated secret key to be supplied in cancel requests.
1195    </para>
1196   </sect2>
1197
1198   <sect2>
1199    <title>Termination</title>
1200
1201    <para>
1202     The normal, graceful termination procedure is that the frontend
1203     sends a Terminate message and immediately closes the connection.
1204     On receipt of this message, the backend closes the connection and
1205     terminates.
1206    </para>
1207
1208    <para>
1209     In rare cases (such as an administrator-commanded database shutdown)
1210     the backend might disconnect without any frontend request to do so.
1211     In such cases the backend will attempt to send an error or notice message
1212     giving the reason for the disconnection before it closes the connection.
1213    </para>
1214
1215    <para>
1216     Other termination scenarios arise from various failure cases, such as core
1217     dump at one end or the other, loss of the communications link, loss of
1218     message-boundary synchronization, etc.  If either frontend or backend sees
1219     an unexpected closure of the connection, it should clean
1220     up and terminate.  The frontend has the option of launching a new backend
1221     by recontacting the server if it doesn't want to terminate itself.
1222     Closing the connection is also advisable if an unrecognizable message type
1223     is received, since this probably indicates loss of message-boundary sync.
1224    </para>
1225
1226    <para>
1227     For either normal or abnormal termination, any open transaction is
1228     rolled back, not committed.  One should note however that if a
1229     frontend disconnects while a non-<command>SELECT</command> query
1230     is being processed, the backend will probably finish the query
1231     before noticing the disconnection.  If the query is outside any
1232     transaction block (<command>BEGIN</> ... <command>COMMIT</>
1233     sequence) then its results might be committed before the
1234     disconnection is recognized.
1235    </para>
1236   </sect2>
1237
1238   <sect2>
1239    <title><acronym>SSL</acronym> Session Encryption</title>
1240
1241    <para>
1242     If <productname>PostgreSQL</> was built with
1243     <acronym>SSL</acronym> support, frontend/backend communications
1244     can be encrypted using <acronym>SSL</acronym>.  This provides
1245     communication security in environments where attackers might be
1246     able to capture the session traffic. For more information on
1247     encrypting <productname>PostgreSQL</productname> sessions with
1248     <acronym>SSL</acronym>, see <xref linkend="ssl-tcp">.
1249    </para>
1250
1251    <para>
1252     To initiate an <acronym>SSL</acronym>-encrypted connection, the
1253     frontend initially sends an SSLRequest message rather than a
1254     StartupMessage.  The server then responds with a single byte
1255     containing <literal>S</> or <literal>N</>, indicating that it is
1256     willing or unwilling to perform <acronym>SSL</acronym>,
1257     respectively.  The frontend might close the connection at this point
1258     if it is dissatisfied with the response.  To continue after
1259     <literal>S</>, perform an <acronym>SSL</acronym> startup handshake
1260     (not described here, part of the <acronym>SSL</acronym>
1261     specification) with the server.  If this is successful, continue
1262     with sending the usual StartupMessage.  In this case the
1263     StartupMessage and all subsequent data will be
1264     <acronym>SSL</acronym>-encrypted.  To continue after
1265     <literal>N</>, send the usual StartupMessage and proceed without
1266     encryption.
1267    </para>
1268
1269    <para>
1270     The frontend should also be prepared to handle an ErrorMessage
1271     response to SSLRequest from the server.  This would only occur if
1272     the server predates the addition of <acronym>SSL</acronym> support
1273     to <productname>PostgreSQL</>.  (Such servers are now very ancient,
1274     and likely do not exist in the wild anymore.)
1275     In this case the connection must
1276     be closed, but the frontend might choose to open a fresh connection
1277     and proceed without requesting <acronym>SSL</acronym>.
1278    </para>
1279
1280    <para>
1281     An initial SSLRequest can also be used in a connection that is being
1282     opened to send a CancelRequest message.
1283    </para>
1284
1285    <para>
1286     While the protocol itself does not provide a way for the server to
1287     force <acronym>SSL</acronym> encryption, the administrator can
1288     configure the server to reject unencrypted sessions as a byproduct
1289     of authentication checking.
1290    </para>
1291   </sect2>
1292  </sect1>
1293
1294 <sect1 id="protocol-replication">
1295 <title>Streaming Replication Protocol</title>
1296
1297 <para>
1298 To initiate streaming replication, the frontend sends the
1299 <literal>replication</> parameter in the startup message. This tells the
1300 backend to go into walsender mode, wherein a small set of replication commands
1301 can be issued instead of SQL statements. Only the simple query protocol can be
1302 used in walsender mode.
1303
1304 The commands accepted in walsender mode are:
1305
1306 <variablelist>
1307   <varlistentry>
1308     <term>IDENTIFY_SYSTEM</term>
1309     <listitem>
1310      <para>
1311       Requests the server to identify itself. Server replies with a result
1312       set of a single row, containing three fields:
1313      </para>
1314
1315      <para>
1316       <variablelist>
1317       <varlistentry>
1318       <term>
1319        systemid
1320       </term>
1321       <listitem>
1322       <para>
1323        The unique system identifier identifying the cluster. This
1324        can be used to check that the base backup used to initialize the
1325        standby came from the same cluster.
1326       </para>
1327       </listitem>
1328       </varlistentry>
1329
1330       <varlistentry>
1331       <term>
1332        timeline
1333       </term>
1334       <listitem>
1335       <para>
1336        Current TimelineID. Also useful to check that the standby is
1337        consistent with the master.
1338       </para>
1339       </listitem>
1340       </varlistentry>
1341
1342       <varlistentry>
1343       <term>
1344        xlogpos
1345       </term>
1346       <listitem>
1347       <para>
1348        Current xlog write location. Useful to get a known location in the
1349        transaction log where streaming can start.
1350       </para>
1351       </listitem>
1352       </varlistentry>
1353
1354       </variablelist>
1355      </para>
1356     </listitem>
1357   </varlistentry>
1358
1359   <varlistentry>
1360     <term>TIMELINE_HISTORY <replaceable class="parameter">tli</replaceable></term>
1361     <listitem>
1362      <para>
1363       Requests the server to send over the timeline history file for timeline
1364       <replaceable class="parameter">tli</replaceable>.  Server replies with a
1365       result set of a single row, containing two fields:
1366      </para>
1367
1368      <para>
1369       <variablelist>
1370       <varlistentry>
1371       <term>
1372        filename
1373       </term>
1374       <listitem>
1375       <para>
1376        Filename of the timeline history file, e.g 00000002.history.
1377       </para>
1378       </listitem>
1379       </varlistentry>
1380
1381       <varlistentry>
1382       <term>
1383        content
1384       </term>
1385       <listitem>
1386       <para>
1387        Contents of the timeline history file.
1388       </para>
1389       </listitem>
1390       </varlistentry>
1391
1392       </variablelist>
1393      </para>
1394     </listitem>
1395   </varlistentry>
1396
1397   <varlistentry>
1398     <term>START_REPLICATION <replaceable class="parameter">XXX/XXX</> TIMELINE <replaceable class="parameter">tli</></term>
1399     <listitem>
1400      <para>
1401       Instructs server to start streaming WAL, starting at
1402       WAL position <replaceable class="parameter">XXX/XXX</> on timeline
1403       <replaceable class="parameter">tli</>.
1404       The server can reply with an error, e.g. if the requested section of WAL
1405       has already been recycled. On success, server responds with a
1406       CopyBothResponse message, and then starts to stream WAL to the frontend.
1407      </para>
1408
1409      <para>
1410       If the client requests a timeline that's not the latest, but is part of
1411       the history of the server, the server will stream all the WAL on that
1412       timeline starting from the requested startpoint, up to the point where
1413       the server switched to another timeline. If the client requests
1414       streaming at exactly the end of an old timeline, the server responds
1415       immediately with CommandComplete without entering COPY mode.
1416      </para>
1417
1418      <para>
1419       After streaming all the WAL on a timeline that is not the latest one,
1420       the server will end streaming by exiting the COPY mode. When the client
1421       acknowledges this by also exiting COPY mode, the server sends a
1422       single-row, single-column result set indicating the next timeline in
1423       this server's history. That is followed by a CommandComplete message,
1424       and the server is ready to accept a new command.
1425      </para>
1426
1427      <para>
1428       WAL data is sent as a series of CopyData messages.  (This allows
1429       other information to be intermixed; in particular the server can send
1430       an ErrorResponse message if it encounters a failure after beginning
1431       to stream.)  The payload of each CopyData message from server to the
1432       client contains a message of one of the following formats:
1433      </para>
1434
1435      <para>
1436       <variablelist>
1437       <varlistentry>
1438       <term>
1439           XLogData (B)
1440       </term>
1441       <listitem>
1442       <para>
1443       <variablelist>
1444       <varlistentry>
1445       <term>
1446           Byte1('w')
1447       </term>
1448       <listitem>
1449       <para>
1450           Identifies the message as WAL data.
1451       </para>
1452       </listitem>
1453       </varlistentry>
1454       <varlistentry>
1455       <term>
1456           Int64
1457       </term>
1458       <listitem>
1459       <para>
1460           The starting point of the WAL data in this message.
1461       </para>
1462       </listitem>
1463       </varlistentry>
1464       <varlistentry>
1465       <term>
1466           Int64
1467       </term>
1468       <listitem>
1469       <para>
1470           The current end of WAL on the server.
1471       </para>
1472       </listitem>
1473       </varlistentry>
1474       <varlistentry>
1475       <term>
1476           Int64
1477       </term>
1478       <listitem>
1479       <para>
1480           The server's system clock at the time of transmission, as
1481           microseconds since midnight on 2000-01-01.
1482       </para>
1483       </listitem>
1484       </varlistentry>
1485       <varlistentry>
1486       <term>
1487           Byte<replaceable>n</replaceable>
1488       </term>
1489       <listitem>
1490       <para>
1491           A section of the WAL data stream.
1492       </para>
1493       <para>
1494           A single WAL record is never split across two XLogData messages.
1495           When a WAL record crosses a WAL page boundary, and is therefore
1496           already split using continuation records, it can be split at the page
1497           boundary. In other words, the first main WAL record and its
1498           continuation records can be sent in different XLogData messages.
1499       </para>
1500       </listitem>
1501       </varlistentry>
1502       </variablelist>
1503       </para>
1504       </listitem>
1505       </varlistentry>
1506       <varlistentry>
1507       <term>
1508           Primary keepalive message (B)
1509       </term>
1510       <listitem>
1511       <para>
1512       <variablelist>
1513       <varlistentry>
1514       <term>
1515           Byte1('k')
1516       </term>
1517       <listitem>
1518       <para>
1519           Identifies the message as a sender keepalive.
1520       </para>
1521       </listitem>
1522       </varlistentry>
1523       <varlistentry>
1524       <term>
1525           Int64
1526       </term>
1527       <listitem>
1528       <para>
1529           The current end of WAL on the server.
1530       </para>
1531       </listitem>
1532       </varlistentry>
1533       <varlistentry>
1534       <term>
1535           Int64
1536       </term>
1537       <listitem>
1538       <para>
1539           The server's system clock at the time of transmission, as
1540           microseconds since midnight on 2000-01-01.
1541       </para>
1542       </listitem>
1543       </varlistentry>
1544       <varlistentry>
1545       <term>
1546           Byte1
1547       </term>
1548       <listitem>
1549       <para>
1550           1 means that the client should reply to this message as soon as
1551           possible, to avoid a timeout disconnect. 0 otherwise.
1552       </para>
1553       </listitem>
1554       </varlistentry>
1555       </variablelist>
1556       </para>
1557       </listitem>
1558       </varlistentry>
1559       </variablelist>
1560      </para>
1561
1562      <para>
1563        The receiving process can send replies back to the sender at any time,
1564        using one of the following message formats (also in the payload of a
1565        CopyData message):
1566      </para>
1567
1568      <para>
1569       <variablelist>
1570       <varlistentry>
1571       <term>
1572           Standby status update (F)
1573       </term>
1574       <listitem>
1575       <para>
1576       <variablelist>
1577       <varlistentry>
1578       <term>
1579           Byte1('r')
1580       </term>
1581       <listitem>
1582       <para>
1583           Identifies the message as a receiver status update.
1584       </para>
1585       </listitem>
1586       </varlistentry>
1587       <varlistentry>
1588       <term>
1589           Int64
1590       </term>
1591       <listitem>
1592       <para>
1593           The location of the last WAL byte + 1 received and written to disk
1594           in the standby.
1595       </para>
1596       </listitem>
1597       </varlistentry>
1598       <varlistentry>
1599       <term>
1600           Int64
1601       </term>
1602       <listitem>
1603       <para>
1604           The location of the last WAL byte + 1 flushed to disk in
1605           the standby.
1606       </para>
1607       </listitem>
1608       </varlistentry>
1609       <varlistentry>
1610       <term>
1611           Int64
1612       </term>
1613       <listitem>
1614       <para>
1615           The location of the last WAL byte + 1 applied in the standby.
1616       </para>
1617       </listitem>
1618       </varlistentry>
1619       <varlistentry>
1620       <term>
1621           Int64
1622       </term>
1623       <listitem>
1624       <para>
1625           The client's system clock at the time of transmission, as
1626           microseconds since midnight on 2000-01-01.
1627       </para>
1628       </listitem>
1629       </varlistentry>
1630       <varlistentry>
1631       <term>
1632           Byte1
1633       </term>
1634       <listitem>
1635       <para>
1636           If 1, the client requests the server to reply to this message
1637           immediately. This can be used to ping the server, to test if
1638           the connection is still healthy.
1639       </para>
1640       </listitem>
1641       </varlistentry>
1642       </variablelist>
1643       </para>
1644       </listitem>
1645       </varlistentry>
1646       </variablelist>
1647      </para>
1648
1649      <para>
1650       <variablelist>
1651       <varlistentry>
1652       <term>
1653           Hot Standby feedback message (F)
1654       </term>
1655       <listitem>
1656       <para>
1657       <variablelist>
1658       <varlistentry>
1659       <term>
1660           Byte1('h')
1661       </term>
1662       <listitem>
1663       <para>
1664           Identifies the message as a Hot Standby feedback message.
1665       </para>
1666       </listitem>
1667       </varlistentry>
1668       <varlistentry>
1669       <term>
1670           Int64
1671       </term>
1672       <listitem>
1673       <para>
1674           The client's system clock at the time of transmission, as
1675           microseconds since midnight on 2000-01-01.
1676       </para>
1677       </listitem>
1678       </varlistentry>
1679       <varlistentry>
1680       <term>
1681           Int32
1682       </term>
1683       <listitem>
1684       <para>
1685           The standby's current xmin. This may be 0, if the standby is
1686           sending notification that Hot Standby feedback will no longer
1687           be sent on this connection. Later non-zero messages may
1688           reinitiate the feedback mechanism.
1689       </para>
1690       </listitem>
1691       </varlistentry>
1692       <varlistentry>
1693       <term>
1694           Int32
1695       </term>
1696       <listitem>
1697       <para>
1698           The standby's current epoch.
1699       </para>
1700       </listitem>
1701       </varlistentry>
1702       </variablelist>
1703       </para>
1704       </listitem>
1705       </varlistentry>
1706       </variablelist>
1707      </para>
1708     </listitem>
1709   </varlistentry>
1710
1711   <varlistentry>
1712     <term>BASE_BACKUP [<literal>LABEL</literal> <replaceable>'label'</replaceable>] [<literal>PROGRESS</literal>] [<literal>FAST</literal>] [<literal>WAL</literal>] [<literal>NOWAIT</literal>]</term>
1713     <listitem>
1714      <para>
1715       Instructs the server to start streaming a base backup.
1716       The system will automatically be put in backup mode before the backup
1717       is started, and taken out of it when the backup is complete. The
1718       following options are accepted:
1719       <variablelist>
1720        <varlistentry>
1721         <term><literal>LABEL</literal> <replaceable>'label'</replaceable></term>
1722         <listitem>
1723          <para>
1724           Sets the label of the backup. If none is specified, a backup label
1725           of <literal>base backup</literal> will be used. The quoting rules
1726           for the label are the same as a standard SQL string with
1727           <xref linkend="guc-standard-conforming-strings"> turned on.
1728          </para>
1729         </listitem>
1730        </varlistentry>
1731
1732        <varlistentry>
1733         <term><literal>PROGRESS</></term>
1734         <listitem>
1735          <para>
1736           Request information required to generate a progress report. This will
1737           send back an approximate size in the header of each tablespace, which
1738           can be used to calculate how far along the stream is done. This is
1739           calculated by enumerating all the file sizes once before the transfer
1740           is even started, and may as such have a negative impact on the
1741           performance - in particular it may take longer before the first data
1742           is streamed. Since the database files can change during the backup,
1743           the size is only approximate and may both grow and shrink between
1744           the time of approximation and the sending of the actual files.
1745          </para>
1746         </listitem>
1747        </varlistentry>
1748
1749        <varlistentry>
1750         <term><literal>FAST</></term>
1751         <listitem>
1752          <para>
1753           Request a fast checkpoint.
1754          </para>
1755         </listitem>
1756        </varlistentry>
1757
1758        <varlistentry>
1759         <term><literal>WAL</literal></term>
1760         <listitem>
1761          <para>
1762           Include the necessary WAL segments in the backup. This will include
1763           all the files between start and stop backup in the
1764           <filename>pg_xlog</filename> directory of the base directory tar
1765           file.
1766          </para>
1767         </listitem>
1768        </varlistentry>
1769
1770        <varlistentry>
1771         <term><literal>NOWAIT</literal></term>
1772         <listitem>
1773          <para>
1774           By default, the backup will wait until the last required xlog
1775           segment has been archived, or emit a warning if log archiving is
1776           not enabled. Specifying <literal>NOWAIT</literal> disables both
1777           the waiting and the warning, leaving the client responsible for
1778           ensuring the required log is available.
1779          </para>
1780          </listitem>
1781        </varlistentry>
1782       </variablelist>
1783      </para>
1784      <para>
1785       When the backup is started, the server will first send two
1786       ordinary result sets, followed by one or more CopyResponse
1787       results.
1788      </para>
1789      <para>
1790       The first ordinary result set contains the starting position of the
1791       backup, in a single row with two columns. The first column contains
1792       the start position given in XLogRecPtr format, and the second column
1793       contains the corresponding timeline ID.
1794      </para>
1795      <para>
1796       The second ordinary result set has one row for each tablespace.
1797       The fields in this row are:
1798       <variablelist>
1799        <varlistentry>
1800         <term>spcoid</term>
1801         <listitem>
1802          <para>
1803           The oid of the tablespace, or <literal>NULL</> if it's the base
1804           directory.
1805          </para>
1806         </listitem>
1807        </varlistentry>
1808        <varlistentry>
1809         <term>spclocation</term>
1810         <listitem>
1811          <para>
1812           The full path of the tablespace directory, or <literal>NULL</>
1813           if it's the base directory.
1814          </para>
1815         </listitem>
1816        </varlistentry>
1817        <varlistentry>
1818         <term>size</term>
1819         <listitem>
1820          <para>
1821           The approximate size of the tablespace, if progress report has
1822           been requested; otherwise it's <literal>NULL</>.
1823          </para>
1824         </listitem>
1825        </varlistentry>
1826       </variablelist>
1827      </para>
1828      <para>
1829       After the second regular result set, one or more CopyResponse results
1830       will be sent, one for PGDATA and one for each additional tablespace other
1831       than <literal>pg_default</> and <literal>pg_global</>. The data in
1832       the CopyResponse results will be a tar format (following the
1833       <quote>ustar interchange format</> specified in the POSIX 1003.1-2008
1834       standard) dump of the tablespace contents, except that the two trailing
1835       blocks of zeroes specified in the standard are omitted.
1836       After the tar data is complete, a final ordinary result set will be sent,
1837       containing the WAL end position of the backup, in the same format as
1838       the start position.
1839      </para>
1840
1841      <para>
1842       The tar archive for the data directory and each tablespace will contain
1843       all files in the directories, regardless of whether they are
1844       <productname>PostgreSQL</> files or other files added to the same
1845       directory. The only excluded files are:
1846       <itemizedlist spacing="compact" mark="bullet">
1847        <listitem>
1848         <para>
1849          <filename>postmaster.pid</>
1850         </para>
1851        </listitem>
1852        <listitem>
1853         <para>
1854          <filename>postmaster.opts</>
1855         </para>
1856        </listitem>
1857        <listitem>
1858         <para>
1859          <filename>pg_xlog</>, including subdirectories. If the backup is run
1860          with WAL files included, a synthesized version of <filename>pg_xlog</filename> will be
1861          included, but it will only contain the files necessary for the
1862          backup to work, not the rest of the contents.
1863         </para>
1864        </listitem>
1865       </itemizedlist>
1866       Owner, group and file mode are set if the underlying file system on
1867       the server supports it.
1868      </para>
1869      <para>
1870       Once all tablespaces have been sent, a final regular result set will
1871       be sent. This result set contains the end position of the
1872       backup, given in XLogRecPtr format as a single column in a single row.
1873      </para>
1874     </listitem>
1875   </varlistentry>
1876 </variablelist>
1877
1878 </para>
1879
1880 </sect1>
1881
1882 <sect1 id="protocol-message-types">
1883 <title>Message Data Types</title>
1884
1885 <para>
1886 This section describes the base data types used in messages.
1887
1888 <variablelist>
1889
1890 <varlistentry>
1891 <term>
1892         Int<replaceable>n</replaceable>(<replaceable>i</replaceable>)
1893 </term>
1894 <listitem>
1895 <para>
1896                 An <replaceable>n</replaceable>-bit integer in network byte
1897                 order (most significant byte first).
1898                 If <replaceable>i</replaceable> is specified it
1899                 is the exact value that will appear, otherwise the value
1900                 is variable.  Eg. Int16, Int32(42).
1901 </para>
1902 </listitem>
1903 </varlistentry>
1904
1905 <varlistentry>
1906 <term>
1907         Int<replaceable>n</replaceable>[<replaceable>k</replaceable>]
1908 </term>
1909 <listitem>
1910 <para>
1911                 An array of <replaceable>k</replaceable>
1912                 <replaceable>n</replaceable>-bit integers, each in network
1913                 byte order.  The array length <replaceable>k</replaceable>
1914                 is always determined by an earlier field in the message.
1915                 Eg. Int16[M].
1916 </para>
1917 </listitem>
1918 </varlistentry>
1919
1920 <varlistentry>
1921 <term>
1922         String(<replaceable>s</replaceable>)
1923 </term>
1924 <listitem>
1925 <para>
1926                 A null-terminated string (C-style string).  There is no
1927                 specific length limitation on strings.
1928                 If <replaceable>s</replaceable> is specified it is the exact
1929                 value that will appear, otherwise the value is variable.
1930                 Eg. String, String("user").
1931 </para>
1932
1933 <note>
1934 <para>
1935 <emphasis>There is no predefined limit</emphasis> on the length of a string
1936 that can be returned by the backend.  Good coding strategy for a frontend
1937 is to use an expandable buffer so that anything that fits in memory can be
1938 accepted.  If that's not feasible, read the full string and discard trailing
1939 characters that don't fit into your fixed-size buffer.
1940 </para>
1941 </note>
1942 </listitem>
1943 </varlistentry>
1944
1945 <varlistentry>
1946 <term>
1947         Byte<replaceable>n</replaceable>(<replaceable>c</replaceable>)
1948 </term>
1949 <listitem>
1950 <para>
1951                 Exactly <replaceable>n</replaceable> bytes.  If the field
1952                 width <replaceable>n</replaceable> is not a constant, it is
1953                 always determinable from an earlier field in the message.
1954                 If <replaceable>c</replaceable> is specified it is the exact
1955                 value.  Eg. Byte2, Byte1('\n').
1956 </para>
1957 </listitem>
1958 </varlistentry>
1959
1960 </variablelist>
1961 </para>
1962 </sect1>
1963
1964 <sect1 id="protocol-message-formats">
1965 <title>Message Formats</title>
1966
1967 <para>
1968 This section describes the detailed format of each message.  Each is marked to
1969 indicate that it can be sent by a frontend (F), a backend (B), or both
1970 (F &amp; B).
1971 Notice that although each message includes a byte count at the beginning,
1972 the message format is defined so that the message end can be found without
1973 reference to the byte count.  This aids validity checking.  (The CopyData
1974 message is an exception, because it forms part of a data stream; the contents
1975 of any individual CopyData message cannot be interpretable on their own.)
1976 </para>
1977
1978 <variablelist>
1979
1980
1981 <varlistentry>
1982 <term>
1983 AuthenticationOk (B)
1984 </term>
1985 <listitem>
1986 <para>
1987
1988 <variablelist>
1989 <varlistentry>
1990 <term>
1991         Byte1('R')
1992 </term>
1993 <listitem>
1994 <para>
1995                 Identifies the message as an authentication request.
1996 </para>
1997 </listitem>
1998 </varlistentry>
1999 <varlistentry>
2000 <term>
2001         Int32(8)
2002 </term>
2003 <listitem>
2004 <para>
2005                 Length of message contents in bytes, including self.
2006 </para>
2007 </listitem>
2008 </varlistentry>
2009 <varlistentry>
2010 <term>
2011         Int32(0)
2012 </term>
2013 <listitem>
2014 <para>
2015                 Specifies that the authentication was successful.
2016 </para>
2017 </listitem>
2018 </varlistentry>
2019 </variablelist>
2020
2021 </para>
2022 </listitem>
2023 </varlistentry>
2024
2025
2026 <varlistentry>
2027 <term>
2028 AuthenticationKerberosV5 (B)
2029 </term>
2030 <listitem>
2031 <para>
2032
2033 <variablelist>
2034 <varlistentry>
2035 <term>
2036         Byte1('R')
2037 </term>
2038 <listitem>
2039 <para>
2040                 Identifies the message as an authentication request.
2041 </para>
2042 </listitem>
2043 </varlistentry>
2044 <varlistentry>
2045 <term>
2046         Int32(8)
2047 </term>
2048 <listitem>
2049 <para>
2050                 Length of message contents in bytes, including self.
2051 </para>
2052 </listitem>
2053 </varlistentry>
2054 <varlistentry>
2055 <term>
2056         Int32(2)
2057 </term>
2058 <listitem>
2059 <para>
2060                 Specifies that Kerberos V5 authentication is required.
2061 </para>
2062 </listitem>
2063 </varlistentry>
2064 </variablelist>
2065 </para>
2066 </listitem>
2067 </varlistentry>
2068
2069
2070 <varlistentry>
2071 <term>
2072 AuthenticationCleartextPassword (B)
2073 </term>
2074 <listitem>
2075 <para>
2076
2077 <variablelist>
2078 <varlistentry>
2079 <term>
2080         Byte1('R')
2081 </term>
2082 <listitem>
2083 <para>
2084                 Identifies the message as an authentication request.
2085 </para>
2086 </listitem>
2087 </varlistentry>
2088 <varlistentry>
2089 <term>
2090         Int32(8)
2091 </term>
2092 <listitem>
2093 <para>
2094                 Length of message contents in bytes, including self.
2095 </para>
2096 </listitem>
2097 </varlistentry>
2098 <varlistentry>
2099 <term>
2100         Int32(3)
2101 </term>
2102 <listitem>
2103 <para>
2104                 Specifies that a clear-text password is required.
2105 </para>
2106 </listitem>
2107 </varlistentry>
2108 </variablelist>
2109 </para>
2110 </listitem>
2111 </varlistentry>
2112
2113
2114 <varlistentry>
2115 <term>
2116 AuthenticationMD5Password (B)
2117 </term>
2118 <listitem>
2119 <para>
2120
2121 <variablelist>
2122 <varlistentry>
2123 <term>
2124         Byte1('R')
2125 </term>
2126 <listitem>
2127 <para>
2128                 Identifies the message as an authentication request.
2129 </para>
2130 </listitem>
2131 </varlistentry>
2132 <varlistentry>
2133 <term>
2134         Int32(12)
2135 </term>
2136 <listitem>
2137 <para>
2138                 Length of message contents in bytes, including self.
2139 </para>
2140 </listitem>
2141 </varlistentry>
2142 <varlistentry>
2143 <term>
2144         Int32(5)
2145 </term>
2146 <listitem>
2147 <para>
2148                 Specifies that an MD5-encrypted password is required.
2149 </para>
2150 </listitem>
2151 </varlistentry>
2152 <varlistentry>
2153 <term>
2154         Byte4
2155 </term>
2156 <listitem>
2157 <para>
2158                 The salt to use when encrypting the password.
2159 </para>
2160 </listitem>
2161 </varlistentry>
2162 </variablelist>
2163
2164 </para>
2165 </listitem>
2166 </varlistentry>
2167
2168
2169 <varlistentry>
2170 <term>
2171 AuthenticationSCMCredential (B)
2172 </term>
2173 <listitem>
2174 <para>
2175
2176 <variablelist>
2177 <varlistentry>
2178 <term>
2179         Byte1('R')
2180 </term>
2181 <listitem>
2182 <para>
2183                 Identifies the message as an authentication request.
2184 </para>
2185 </listitem>
2186 </varlistentry>
2187 <varlistentry>
2188 <term>
2189         Int32(8)
2190 </term>
2191 <listitem>
2192 <para>
2193                 Length of message contents in bytes, including self.
2194 </para>
2195 </listitem>
2196 </varlistentry>
2197 <varlistentry>
2198 <term>
2199         Int32(6)
2200 </term>
2201 <listitem>
2202 <para>
2203                 Specifies that an SCM credentials message is required.
2204 </para>
2205 </listitem>
2206 </varlistentry>
2207 </variablelist>
2208
2209 </para>
2210 </listitem>
2211 </varlistentry>
2212
2213
2214 <varlistentry>
2215 <term>
2216 AuthenticationGSS (B)
2217 </term>
2218 <listitem>
2219 <para>
2220
2221 <variablelist>
2222 <varlistentry>
2223 <term>
2224         Byte1('R')
2225 </term>
2226 <listitem>
2227 <para>
2228                 Identifies the message as an authentication request.
2229 </para>
2230 </listitem>
2231 </varlistentry>
2232 <varlistentry>
2233 <term>
2234         Int32(8)
2235 </term>
2236 <listitem>
2237 <para>
2238                 Length of message contents in bytes, including self.
2239 </para>
2240 </listitem>
2241 </varlistentry>
2242 <varlistentry>
2243 <term>
2244         Int32(7)
2245 </term>
2246 <listitem>
2247 <para>
2248                 Specifies that GSSAPI authentication is required.
2249 </para>
2250 </listitem>
2251 </varlistentry>
2252 </variablelist>
2253
2254 </para>
2255 </listitem>
2256 </varlistentry>
2257
2258
2259 <varlistentry>
2260 <term>
2261 AuthenticationSSPI (B)
2262 </term>
2263 <listitem>
2264 <para>
2265
2266 <variablelist>
2267 <varlistentry>
2268 <term>
2269         Byte1('R')
2270 </term>
2271 <listitem>
2272 <para>
2273                 Identifies the message as an authentication request.
2274 </para>
2275 </listitem>
2276 </varlistentry>
2277 <varlistentry>
2278 <term>
2279         Int32(8)
2280 </term>
2281 <listitem>
2282 <para>
2283                 Length of message contents in bytes, including self.
2284 </para>
2285 </listitem>
2286 </varlistentry>
2287 <varlistentry>
2288 <term>
2289         Int32(9)
2290 </term>
2291 <listitem>
2292 <para>
2293                 Specifies that SSPI authentication is required.
2294 </para>
2295 </listitem>
2296 </varlistentry>
2297 </variablelist>
2298
2299 </para>
2300 </listitem>
2301 </varlistentry>
2302 <varlistentry>
2303 <term>
2304 AuthenticationGSSContinue (B)
2305 </term>
2306 <listitem>
2307 <para>
2308
2309 <variablelist>
2310 <varlistentry>
2311 <term>
2312         Byte1('R')
2313 </term>
2314 <listitem>
2315 <para>
2316                 Identifies the message as an authentication request.
2317 </para>
2318 </listitem>
2319 </varlistentry>
2320 <varlistentry>
2321 <term>
2322         Int32
2323 </term>
2324 <listitem>
2325 <para>
2326                 Length of message contents in bytes, including self.
2327 </para>
2328 </listitem>
2329 </varlistentry>
2330 <varlistentry>
2331 <term>
2332         Int32(8)
2333 </term>
2334 <listitem>
2335 <para>
2336                 Specifies that this message contains GSSAPI or SSPI data.
2337 </para>
2338 </listitem>
2339 </varlistentry>
2340 <varlistentry>
2341 <term>
2342         Byte<replaceable>n</replaceable>
2343 </term>
2344 <listitem>
2345 <para>
2346                 GSSAPI or SSPI authentication data.
2347 </para>
2348 </listitem>
2349 </varlistentry>
2350 </variablelist>
2351
2352 </para>
2353 </listitem>
2354 </varlistentry>
2355
2356
2357 <varlistentry>
2358 <term>
2359 BackendKeyData (B)
2360 </term>
2361 <listitem>
2362 <para>
2363
2364 <variablelist>
2365 <varlistentry>
2366 <term>
2367         Byte1('K')
2368 </term>
2369 <listitem>
2370 <para>
2371                 Identifies the message as cancellation key data.
2372                 The frontend must save these values if it wishes to be
2373                 able to issue CancelRequest messages later.
2374 </para>
2375 </listitem>
2376 </varlistentry>
2377 <varlistentry>
2378 <term>
2379         Int32(12)
2380 </term>
2381 <listitem>
2382 <para>
2383                 Length of message contents in bytes, including self.
2384 </para>
2385 </listitem>
2386 </varlistentry>
2387 <varlistentry>
2388 <term>
2389         Int32
2390 </term>
2391 <listitem>
2392 <para>
2393                 The process ID of this backend.
2394 </para>
2395 </listitem>
2396 </varlistentry>
2397 <varlistentry>
2398 <term>
2399         Int32
2400 </term>
2401 <listitem>
2402 <para>
2403                 The secret key of this backend.
2404 </para>
2405 </listitem>
2406 </varlistentry>
2407 </variablelist>
2408
2409 </para>
2410 </listitem>
2411 </varlistentry>
2412
2413
2414 <varlistentry>
2415 <term>
2416 Bind (F)
2417 </term>
2418 <listitem>
2419 <para>
2420
2421 <variablelist>
2422 <varlistentry>
2423 <term>
2424         Byte1('B')
2425 </term>
2426 <listitem>
2427 <para>
2428                 Identifies the message as a Bind command.
2429 </para>
2430 </listitem>
2431 </varlistentry>
2432 <varlistentry>
2433 <term>
2434         Int32
2435 </term>
2436 <listitem>
2437 <para>
2438                 Length of message contents in bytes, including self.
2439 </para>
2440 </listitem>
2441 </varlistentry>
2442 <varlistentry>
2443 <term>
2444         String
2445 </term>
2446 <listitem>
2447 <para>
2448                 The name of the destination portal
2449                 (an empty string selects the unnamed portal).
2450 </para>
2451 </listitem>
2452 </varlistentry>
2453 <varlistentry>
2454 <term>
2455         String
2456 </term>
2457 <listitem>
2458 <para>
2459                 The name of the source prepared statement
2460                 (an empty string selects the unnamed prepared statement).
2461 </para>
2462 </listitem>
2463 </varlistentry>
2464 <varlistentry>
2465 <term>
2466         Int16
2467 </term>
2468 <listitem>
2469 <para>
2470                 The number of parameter format codes that follow
2471                 (denoted <replaceable>C</> below).
2472                 This can be zero to indicate that there are no parameters
2473                 or that the parameters all use the default format (text);
2474                 or one, in which case the specified format code is applied
2475                 to all parameters; or it can equal the actual number of
2476                 parameters.
2477 </para>
2478 </listitem>
2479 </varlistentry>
2480 <varlistentry>
2481 <term>
2482         Int16[<replaceable>C</>]
2483 </term>
2484 <listitem>
2485 <para>
2486                 The parameter format codes.  Each must presently be
2487                 zero (text) or one (binary).
2488 </para>
2489 </listitem>
2490 </varlistentry>
2491 <varlistentry>
2492 <term>
2493         Int16
2494 </term>
2495 <listitem>
2496 <para>
2497                 The number of parameter values that follow (possibly zero).
2498                 This must match the number of parameters needed by the query.
2499 </para>
2500 </listitem>
2501 </varlistentry>
2502 </variablelist>
2503         Next, the following pair of fields appear for each parameter:
2504 <variablelist>
2505 <varlistentry>
2506 <term>
2507         Int32
2508 </term>
2509 <listitem>
2510 <para>
2511                 The length of the parameter value, in bytes (this count
2512                 does not include itself).  Can be zero.
2513                 As a special case, -1 indicates a NULL parameter value.
2514                 No value bytes follow in the NULL case.
2515 </para>
2516 </listitem>
2517 </varlistentry>
2518 <varlistentry>
2519 <term>
2520         Byte<replaceable>n</replaceable>
2521 </term>
2522 <listitem>
2523 <para>
2524                 The value of the parameter, in the format indicated by the
2525                 associated format code.
2526                 <replaceable>n</replaceable> is the above length.
2527 </para>
2528 </listitem>
2529 </varlistentry>
2530 </variablelist>
2531         After the last parameter, the following fields appear:
2532 <variablelist>
2533 <varlistentry>
2534 <term>
2535         Int16
2536 </term>
2537 <listitem>
2538 <para>
2539                 The number of result-column format codes that follow
2540                 (denoted <replaceable>R</> below).
2541                 This can be zero to indicate that there are no result columns
2542                 or that the result columns should all use the default format
2543                 (text);
2544                 or one, in which case the specified format code is applied
2545                 to all result columns (if any); or it can equal the actual
2546                 number of result columns of the query.
2547 </para>
2548 </listitem>
2549 </varlistentry>
2550 <varlistentry>
2551 <term>
2552         Int16[<replaceable>R</>]
2553 </term>
2554 <listitem>
2555 <para>
2556                 The result-column format codes.  Each must presently be
2557                 zero (text) or one (binary).
2558 </para>
2559 </listitem>
2560 </varlistentry>
2561 </variablelist>
2562 </para>
2563 </listitem>
2564 </varlistentry>
2565
2566
2567 <varlistentry>
2568 <term>
2569 BindComplete (B)
2570 </term>
2571 <listitem>
2572 <para>
2573
2574 <variablelist>
2575 <varlistentry>
2576 <term>
2577         Byte1('2')
2578 </term>
2579 <listitem>
2580 <para>
2581                 Identifies the message as a Bind-complete indicator.
2582 </para>
2583 </listitem>
2584 </varlistentry>
2585 <varlistentry>
2586 <term>
2587         Int32(4)
2588 </term>
2589 <listitem>
2590 <para>
2591                 Length of message contents in bytes, including self.
2592 </para>
2593 </listitem>
2594 </varlistentry>
2595 </variablelist>
2596
2597 </para>
2598 </listitem>
2599 </varlistentry>
2600
2601
2602 <varlistentry>
2603 <term>
2604 CancelRequest (F)
2605 </term>
2606 <listitem>
2607 <para>
2608
2609 <variablelist>
2610 <varlistentry>
2611 <term>
2612         Int32(16)
2613 </term>
2614 <listitem>
2615 <para>
2616                 Length of message contents in bytes, including self.
2617 </para>
2618 </listitem>
2619 </varlistentry>
2620 <varlistentry>
2621 <term>
2622         Int32(80877102)
2623 </term>
2624 <listitem>
2625 <para>
2626                 The cancel request code.  The value is chosen to contain
2627                 <literal>1234</> in the most significant 16 bits, and <literal>5678</> in the
2628                 least 16 significant bits.  (To avoid confusion, this code
2629                 must not be the same as any protocol version number.)
2630 </para>
2631 </listitem>
2632 </varlistentry>
2633 <varlistentry>
2634 <term>
2635         Int32
2636 </term>
2637 <listitem>
2638 <para>
2639                 The process ID of the target backend.
2640 </para>
2641 </listitem>
2642 </varlistentry>
2643 <varlistentry>
2644 <term>
2645         Int32
2646 </term>
2647 <listitem>
2648 <para>
2649                 The secret key for the target backend.
2650 </para>
2651 </listitem>
2652 </varlistentry>
2653 </variablelist>
2654
2655 </para>
2656 </listitem>
2657 </varlistentry>
2658
2659
2660 <varlistentry>
2661 <term>
2662 Close (F)
2663 </term>
2664 <listitem>
2665 <para>
2666
2667 <variablelist>
2668 <varlistentry>
2669 <term>
2670         Byte1('C')
2671 </term>
2672 <listitem>
2673 <para>
2674                 Identifies the message as a Close command.
2675 </para>
2676 </listitem>
2677 </varlistentry>
2678 <varlistentry>
2679 <term>
2680         Int32
2681 </term>
2682 <listitem>
2683 <para>
2684                 Length of message contents in bytes, including self.
2685 </para>
2686 </listitem>
2687 </varlistentry>
2688 <varlistentry>
2689 <term>
2690         Byte1
2691 </term>
2692 <listitem>
2693 <para>
2694                 '<literal>S</>' to close a prepared statement; or
2695                 '<literal>P</>' to close a portal.
2696 </para>
2697 </listitem>
2698 </varlistentry>
2699 <varlistentry>
2700 <term>
2701         String
2702 </term>
2703 <listitem>
2704 <para>
2705                 The name of the prepared statement or portal to close
2706                 (an empty string selects the unnamed prepared statement
2707                 or portal).
2708 </para>
2709 </listitem>
2710 </varlistentry>
2711 </variablelist>
2712 </para>
2713 </listitem>
2714 </varlistentry>
2715
2716
2717 <varlistentry>
2718 <term>
2719 CloseComplete (B)
2720 </term>
2721 <listitem>
2722 <para>
2723
2724 <variablelist>
2725 <varlistentry>
2726 <term>
2727         Byte1('3')
2728 </term>
2729 <listitem>
2730 <para>
2731                 Identifies the message as a Close-complete indicator.
2732 </para>
2733 </listitem>
2734 </varlistentry>
2735 <varlistentry>
2736 <term>
2737         Int32(4)
2738 </term>
2739 <listitem>
2740 <para>
2741                 Length of message contents in bytes, including self.
2742 </para>
2743 </listitem>
2744 </varlistentry>
2745 </variablelist>
2746
2747 </para>
2748 </listitem>
2749 </varlistentry>
2750
2751
2752 <varlistentry>
2753 <term>
2754 CommandComplete (B)
2755 </term>
2756 <listitem>
2757 <para>
2758
2759 <variablelist>
2760 <varlistentry>
2761 <term>
2762         Byte1('C')
2763 </term>
2764 <listitem>
2765 <para>
2766                 Identifies the message as a command-completed response.
2767 </para>
2768 </listitem>
2769 </varlistentry>
2770 <varlistentry>
2771 <term>
2772         Int32
2773 </term>
2774 <listitem>
2775 <para>
2776                 Length of message contents in bytes, including self.
2777 </para>
2778 </listitem>
2779 </varlistentry>
2780 <varlistentry>
2781 <term>
2782         String
2783 </term>
2784 <listitem>
2785        <para>
2786         The command tag.  This is usually a single
2787         word that identifies which SQL command was completed.
2788        </para>
2789
2790        <para>
2791         For an <command>INSERT</command> command, the tag is
2792         <literal>INSERT <replaceable>oid</replaceable>
2793         <replaceable>rows</replaceable></literal>, where
2794         <replaceable>rows</replaceable> is the number of rows
2795         inserted. <replaceable>oid</replaceable> is the object ID
2796         of the inserted row if <replaceable>rows</replaceable> is 1
2797         and the target table has OIDs;
2798         otherwise <replaceable>oid</replaceable> is 0.
2799        </para>
2800
2801        <para>
2802         For a <command>DELETE</command> command, the tag is
2803         <literal>DELETE <replaceable>rows</replaceable></literal> where
2804         <replaceable>rows</replaceable> is the number of rows deleted.
2805        </para>
2806
2807        <para>
2808         For an <command>UPDATE</command> command, the tag is
2809         <literal>UPDATE <replaceable>rows</replaceable></literal> where
2810         <replaceable>rows</replaceable> is the number of rows updated.
2811        </para>
2812
2813        <para>
2814         For a <command>SELECT</command> or <command>CREATE TABLE AS</command>
2815         command, the tag is <literal>SELECT <replaceable>rows</replaceable></literal>
2816         where <replaceable>rows</replaceable> is the number of rows retrieved.
2817        </para>
2818
2819        <para>
2820         For a <command>MOVE</command> command, the tag is
2821         <literal>MOVE <replaceable>rows</replaceable></literal> where
2822         <replaceable>rows</replaceable> is the number of rows the
2823         cursor's position has been changed by.
2824        </para>
2825
2826        <para>
2827         For a <command>FETCH</command> command, the tag is
2828         <literal>FETCH <replaceable>rows</replaceable></literal> where
2829         <replaceable>rows</replaceable> is the number of rows that
2830         have been retrieved from the cursor.
2831        </para>
2832
2833        <para>
2834         For a <command>COPY</command> command, the tag is
2835         <literal>COPY <replaceable>rows</replaceable></literal> where
2836         <replaceable>rows</replaceable> is the number of rows copied.
2837         (Note: the row count appears only in
2838         <productname>PostgreSQL</productname> 8.2 and later.)
2839        </para>
2840
2841 </listitem>
2842 </varlistentry>
2843 </variablelist>
2844
2845 </para>
2846 </listitem>
2847 </varlistentry>
2848
2849
2850 <varlistentry>
2851 <term>
2852 CopyData (F &amp; B)
2853 </term>
2854 <listitem>
2855 <para>
2856 <variablelist>
2857 <varlistentry>
2858 <term>
2859         Byte1('d')
2860 </term>
2861 <listitem>
2862 <para>
2863                 Identifies the message as <command>COPY</command> data.
2864 </para>
2865 </listitem>
2866 </varlistentry>
2867 <varlistentry>
2868 <term>
2869         Int32
2870 </term>
2871 <listitem>
2872 <para>
2873                 Length of message contents in bytes, including self.
2874 </para>
2875 </listitem>
2876 </varlistentry>
2877 <varlistentry>
2878 <term>
2879         Byte<replaceable>n</replaceable>
2880 </term>
2881 <listitem>
2882 <para>
2883                 Data that forms part of a <command>COPY</command> data stream.  Messages sent
2884                 from the backend will always correspond to single data rows,
2885                 but messages sent by frontends might divide the data stream
2886                 arbitrarily.
2887 </para>
2888 </listitem>
2889 </varlistentry>
2890 </variablelist>
2891 </para>
2892 </listitem>
2893 </varlistentry>
2894
2895
2896 <varlistentry>
2897 <term>
2898 CopyDone (F &amp; B)
2899 </term>
2900 <listitem>
2901 <para>
2902
2903 <variablelist>
2904 <varlistentry>
2905 <term>
2906         Byte1('c')
2907 </term>
2908 <listitem>
2909 <para>
2910                 Identifies the message as a <command>COPY</command>-complete indicator.
2911 </para>
2912 </listitem>
2913 </varlistentry>
2914 <varlistentry>
2915 <term>
2916         Int32(4)
2917 </term>
2918 <listitem>
2919 <para>
2920                 Length of message contents in bytes, including self.
2921 </para>
2922 </listitem>
2923 </varlistentry>
2924 </variablelist>
2925
2926 </para>
2927 </listitem>
2928 </varlistentry>
2929
2930
2931 <varlistentry>
2932 <term>
2933 CopyFail (F)
2934 </term>
2935 <listitem>
2936 <para>
2937
2938 <variablelist>
2939 <varlistentry>
2940 <term>
2941         Byte1('f')
2942 </term>
2943 <listitem>
2944 <para>
2945                 Identifies the message as a <command>COPY</command>-failure indicator.
2946 </para>
2947 </listitem>
2948 </varlistentry>
2949 <varlistentry>
2950 <term>
2951         Int32
2952 </term>
2953 <listitem>
2954 <para>
2955                 Length of message contents in bytes, including self.
2956 </para>
2957 </listitem>
2958 </varlistentry>
2959 <varlistentry>
2960 <term>
2961         String
2962 </term>
2963 <listitem>
2964 <para>
2965                 An error message to report as the cause of failure.
2966 </para>
2967 </listitem>
2968 </varlistentry>
2969 </variablelist>
2970
2971 </para>
2972 </listitem>
2973 </varlistentry>
2974
2975
2976 <varlistentry>
2977 <term>
2978 CopyInResponse (B)
2979 </term>
2980 <listitem>
2981 <para>
2982
2983 <variablelist>
2984 <varlistentry>
2985 <term>
2986         Byte1('G')
2987 </term>
2988 <listitem>
2989 <para>
2990                 Identifies the message as a Start Copy In response.
2991                 The frontend must now send copy-in data (if not
2992                 prepared to do so, send a CopyFail message).
2993 </para>
2994 </listitem>
2995 </varlistentry>
2996 <varlistentry>
2997 <term>
2998         Int32
2999 </term>
3000 <listitem>
3001 <para>
3002                 Length of message contents in bytes, including self.
3003 </para>
3004 </listitem>
3005 </varlistentry>
3006 <varlistentry>
3007 <term>
3008         Int8
3009 </term>
3010 <listitem>
3011 <para>
3012                 0 indicates the overall <command>COPY</command> format is textual (rows
3013                 separated by newlines, columns separated by separator
3014                 characters, etc).
3015                 1 indicates the overall copy format is binary (similar
3016                 to DataRow format).
3017                 See <xref linkend="sql-copy">
3018                 for more information.
3019 </para>
3020 </listitem>
3021 </varlistentry>
3022 <varlistentry>
3023 <term>
3024         Int16
3025 </term>
3026 <listitem>
3027 <para>
3028                 The number of columns in the data to be copied
3029                 (denoted <replaceable>N</> below).
3030 </para>
3031 </listitem>
3032 </varlistentry>
3033 <varlistentry>
3034 <term>
3035         Int16[<replaceable>N</>]
3036 </term>
3037 <listitem>
3038 <para>
3039                 The format codes to be used for each column.
3040                 Each must presently be zero (text) or one (binary).
3041                 All must be zero if the overall copy format is textual.
3042 </para>
3043 </listitem>
3044 </varlistentry>
3045 </variablelist>
3046
3047 </para>
3048 </listitem>
3049 </varlistentry>
3050
3051
3052 <varlistentry>
3053 <term>
3054 CopyOutResponse (B)
3055 </term>
3056 <listitem>
3057 <para>
3058
3059 <variablelist>
3060 <varlistentry>
3061 <term>
3062         Byte1('H')
3063 </term>
3064 <listitem>
3065 <para>
3066                 Identifies the message as a Start Copy Out response.
3067                 This message will be followed by copy-out data.
3068 </para>
3069 </listitem>
3070 </varlistentry>
3071 <varlistentry>
3072 <term>
3073         Int32
3074 </term>
3075 <listitem>
3076 <para>
3077                 Length of message contents in bytes, including self.
3078 </para>
3079 </listitem>
3080 </varlistentry>
3081 <varlistentry>
3082 <term>
3083         Int8
3084 </term>
3085 <listitem>
3086 <para>
3087                 0 indicates the overall <command>COPY</command> format
3088                 is textual (rows separated by newlines, columns
3089                 separated by separator characters, etc). 1 indicates
3090                 the overall copy format is binary (similar to DataRow
3091                 format). See <xref linkend="sql-copy"> for more information.
3092 </para>
3093 </listitem>
3094 </varlistentry>
3095 <varlistentry>
3096 <term>
3097         Int16
3098 </term>
3099 <listitem>
3100 <para>
3101                 The number of columns in the data to be copied
3102                 (denoted <replaceable>N</> below).
3103 </para>
3104 </listitem>
3105 </varlistentry>
3106 <varlistentry>
3107 <term>
3108         Int16[<replaceable>N</>]
3109 </term>
3110 <listitem>
3111 <para>
3112                 The format codes to be used for each column.
3113                 Each must presently be zero (text) or one (binary).
3114                 All must be zero if the overall copy format is textual.
3115 </para>
3116 </listitem>
3117 </varlistentry>
3118 </variablelist>
3119
3120 </para>
3121 </listitem>
3122 </varlistentry>
3123
3124
3125 <varlistentry>
3126 <term>
3127 CopyBothResponse (B)
3128 </term>
3129 <listitem>
3130 <para>
3131
3132 <variablelist>
3133 <varlistentry>
3134 <term>
3135         Byte1('W')
3136 </term>
3137 <listitem>
3138 <para>
3139                 Identifies the message as a Start Copy Both response.
3140                 This message is used only for Streaming Replication.
3141 </para>
3142 </listitem>
3143 </varlistentry>
3144 <varlistentry>
3145 <term>
3146         Int32
3147 </term>
3148 <listitem>
3149 <para>
3150                 Length of message contents in bytes, including self.
3151 </para>
3152 </listitem>
3153 </varlistentry>
3154 <varlistentry>
3155 <term>
3156         Int8
3157 </term>
3158 <listitem>
3159 <para>
3160                 0 indicates the overall <command>COPY</command> format
3161                 is textual (rows separated by newlines, columns
3162                 separated by separator characters, etc). 1 indicates
3163                 the overall copy format is binary (similar to DataRow
3164                 format). See <xref linkend="sql-copy"> for more information.
3165 </para>
3166 </listitem>
3167 </varlistentry>
3168 <varlistentry>
3169 <term>
3170         Int16
3171 </term>
3172 <listitem>
3173 <para>
3174                 The number of columns in the data to be copied
3175                 (denoted <replaceable>N</> below).
3176 </para>
3177 </listitem>
3178 </varlistentry>
3179 <varlistentry>
3180 <term>
3181         Int16[<replaceable>N</>]
3182 </term>
3183 <listitem>
3184 <para>
3185                 The format codes to be used for each column.
3186                 Each must presently be zero (text) or one (binary).
3187                 All must be zero if the overall copy format is textual.
3188 </para>
3189 </listitem>
3190 </varlistentry>
3191 </variablelist>
3192
3193 </para>
3194 </listitem>
3195 </varlistentry>
3196
3197
3198 <varlistentry>
3199 <term>
3200 DataRow (B)
3201 </term>
3202 <listitem>
3203 <para>
3204 <variablelist>
3205 <varlistentry>
3206 <term>
3207         Byte1('D')
3208 </term>
3209 <listitem>
3210 <para>
3211                 Identifies the message as a data row.
3212 </para>
3213 </listitem>
3214 </varlistentry>
3215 <varlistentry>
3216 <term>
3217         Int32
3218 </term>
3219 <listitem>
3220 <para>
3221                 Length of message contents in bytes, including self.
3222 </para>
3223 </listitem>
3224 </varlistentry>
3225 <varlistentry>
3226 <term>
3227         Int16
3228 </term>
3229 <listitem>
3230 <para>
3231                 The number of column values that follow (possibly zero).
3232 </para>
3233 </listitem>
3234 </varlistentry>
3235 </variablelist>
3236         Next, the following pair of fields appear for each column:
3237 <variablelist>
3238 <varlistentry>
3239 <term>
3240         Int32
3241 </term>
3242 <listitem>
3243 <para>
3244                 The length of the column value, in bytes (this count
3245                 does not include itself).  Can be zero.
3246                 As a special case, -1 indicates a NULL column value.
3247                 No value bytes follow in the NULL case.
3248 </para>
3249 </listitem>
3250 </varlistentry>
3251 <varlistentry>
3252 <term>
3253         Byte<replaceable>n</replaceable>
3254 </term>
3255 <listitem>
3256 <para>
3257                 The value of the column, in the format indicated by the
3258                 associated format code.
3259                 <replaceable>n</replaceable> is the above length.
3260 </para>
3261 </listitem>
3262 </varlistentry>
3263 </variablelist>
3264
3265 </para>
3266 </listitem>
3267 </varlistentry>
3268
3269
3270 <varlistentry>
3271 <term>
3272 Describe (F)
3273 </term>
3274 <listitem>
3275 <para>
3276
3277 <variablelist>
3278 <varlistentry>
3279 <term>
3280         Byte1('D')
3281 </term>
3282 <listitem>
3283 <para>
3284                 Identifies the message as a Describe command.
3285 </para>
3286 </listitem>
3287 </varlistentry>
3288 <varlistentry>
3289 <term>
3290         Int32
3291 </term>
3292 <listitem>
3293 <para>
3294                 Length of message contents in bytes, including self.
3295 </para>
3296 </listitem>
3297 </varlistentry>
3298 <varlistentry>
3299 <term>
3300         Byte1
3301 </term>
3302 <listitem>
3303 <para>
3304                 '<literal>S</>' to describe a prepared statement; or
3305                 '<literal>P</>' to describe a portal.
3306 </para>
3307 </listitem>
3308 </varlistentry>
3309 <varlistentry>
3310 <term>
3311         String
3312 </term>
3313 <listitem>
3314 <para>
3315                 The name of the prepared statement or portal to describe
3316                 (an empty string selects the unnamed prepared statement
3317                 or portal).
3318 </para>
3319 </listitem>
3320 </varlistentry>
3321 </variablelist>
3322 </para>
3323 </listitem>
3324 </varlistentry>
3325
3326
3327 <varlistentry>
3328 <term>
3329 EmptyQueryResponse (B)
3330 </term>
3331 <listitem>
3332 <para>
3333
3334 <variablelist>
3335 <varlistentry>
3336 <term>
3337         Byte1('I')
3338 </term>
3339 <listitem>
3340 <para>
3341                 Identifies the message as a response to an empty query string.
3342                 (This substitutes for CommandComplete.)
3343 </para>
3344 </listitem>
3345 </varlistentry>
3346 <varlistentry>
3347 <term>
3348         Int32(4)
3349 </term>
3350 <listitem>
3351 <para>
3352                 Length of message contents in bytes, including self.
3353 </para>
3354 </listitem>
3355 </varlistentry>
3356 </variablelist>
3357
3358 </para>
3359 </listitem>
3360 </varlistentry>
3361
3362
3363 <varlistentry>
3364 <term>
3365 ErrorResponse (B)
3366 </term>
3367 <listitem>
3368 <para>
3369
3370 <variablelist>
3371 <varlistentry>
3372 <term>
3373         Byte1('E')
3374 </term>
3375 <listitem>
3376 <para>
3377                 Identifies the message as an error.
3378 </para>
3379 </listitem>
3380 </varlistentry>
3381 <varlistentry>
3382 <term>
3383         Int32
3384 </term>
3385 <listitem>
3386 <para>
3387                 Length of message contents in bytes, including self.
3388 </para>
3389 </listitem>
3390 </varlistentry>
3391 </variablelist>
3392         The message body consists of one or more identified fields,
3393         followed by a zero byte as a terminator.  Fields can appear in
3394         any order.  For each field there is the following:
3395 <variablelist>
3396 <varlistentry>
3397 <term>
3398         Byte1
3399 </term>
3400 <listitem>
3401 <para>
3402                 A code identifying the field type; if zero, this is
3403                 the message terminator and no string follows.
3404                 The presently defined field types are listed in
3405                 <xref linkend="protocol-error-fields">.
3406                 Since more field types might be added in future,
3407                 frontends should silently ignore fields of unrecognized
3408                 type.
3409 </para>
3410 </listitem>
3411 </varlistentry>
3412 <varlistentry>
3413 <term>
3414         String
3415 </term>
3416 <listitem>
3417 <para>
3418                 The field value.
3419 </para>
3420 </listitem>
3421 </varlistentry>
3422 </variablelist>
3423
3424 </para>
3425 </listitem>
3426 </varlistentry>
3427
3428
3429 <varlistentry>
3430 <term>
3431 Execute (F)
3432 </term>
3433 <listitem>
3434 <para>
3435
3436 <variablelist>
3437 <varlistentry>
3438 <term>
3439         Byte1('E')
3440 </term>
3441 <listitem>
3442 <para>
3443                 Identifies the message as an Execute command.
3444 </para>
3445 </listitem>
3446 </varlistentry>
3447 <varlistentry>
3448 <term>
3449         Int32
3450 </term>
3451 <listitem>
3452 <para>
3453                 Length of message contents in bytes, including self.
3454 </para>
3455 </listitem>
3456 </varlistentry>
3457 <varlistentry>
3458 <term>
3459         String
3460 </term>
3461 <listitem>
3462 <para>
3463                 The name of the portal to execute
3464                 (an empty string selects the unnamed portal).
3465 </para>
3466 </listitem>
3467 </varlistentry>
3468 <varlistentry>
3469 <term>
3470         Int32
3471 </term>
3472 <listitem>
3473 <para>
3474                 Maximum number of rows to return, if portal contains
3475                 a query that returns rows (ignored otherwise).  Zero
3476                 denotes <quote>no limit</>.
3477 </para>
3478 </listitem>
3479 </varlistentry>
3480 </variablelist>
3481 </para>
3482 </listitem>
3483 </varlistentry>
3484
3485
3486 <varlistentry>
3487 <term>
3488 Flush (F)
3489 </term>
3490 <listitem>
3491 <para>
3492
3493 <variablelist>
3494 <varlistentry>
3495 <term>
3496         Byte1('H')
3497 </term>
3498 <listitem>
3499 <para>
3500                 Identifies the message as a Flush command.
3501 </para>
3502 </listitem>
3503 </varlistentry>
3504 <varlistentry>
3505 <term>
3506         Int32(4)
3507 </term>
3508 <listitem>
3509 <para>
3510                 Length of message contents in bytes, including self.
3511 </para>
3512 </listitem>
3513 </varlistentry>
3514 </variablelist>
3515
3516 </para>
3517 </listitem>
3518 </varlistentry>
3519
3520
3521 <varlistentry>
3522 <term>
3523 FunctionCall (F)
3524 </term>
3525 <listitem>
3526 <para>
3527
3528 <variablelist>
3529 <varlistentry>
3530 <term>
3531         Byte1('F')
3532 </term>
3533 <listitem>
3534 <para>
3535                 Identifies the message as a function call.
3536 </para>
3537 </listitem>
3538 </varlistentry>
3539 <varlistentry>
3540 <term>
3541         Int32
3542 </term>
3543 <listitem>
3544 <para>
3545                 Length of message contents in bytes, including self.
3546 </para>
3547 </listitem>
3548 </varlistentry>
3549 <varlistentry>
3550 <term>
3551         Int32
3552 </term>
3553 <listitem>
3554 <para>
3555                 Specifies the object ID of the function to call.
3556 </para>
3557 </listitem>
3558 </varlistentry>
3559 <varlistentry>
3560 <term>
3561         Int16
3562 </term>
3563 <listitem>
3564 <para>
3565                 The number of argument format codes that follow
3566                 (denoted <replaceable>C</> below).
3567                 This can be zero to indicate that there are no arguments
3568                 or that the arguments all use the default format (text);
3569                 or one, in which case the specified format code is applied
3570                 to all arguments; or it can equal the actual number of
3571                 arguments.
3572 </para>
3573 </listitem>
3574 </varlistentry>
3575 <varlistentry>
3576 <term>
3577         Int16[<replaceable>C</>]
3578 </term>
3579 <listitem>
3580 <para>
3581                 The argument format codes.  Each must presently be
3582                 zero (text) or one (binary).
3583 </para>
3584 </listitem>
3585 </varlistentry>
3586 <varlistentry>
3587 <term>
3588         Int16
3589 </term>
3590 <listitem>
3591 <para>
3592                 Specifies the number of arguments being supplied to the
3593                 function.
3594 </para>
3595 </listitem>
3596 </varlistentry>
3597 </variablelist>
3598         Next, the following pair of fields appear for each argument:
3599 <variablelist>
3600 <varlistentry>
3601 <term>
3602         Int32
3603 </term>
3604 <listitem>
3605 <para>
3606                 The length of the argument value, in bytes (this count
3607                 does not include itself).  Can be zero.
3608                 As a special case, -1 indicates a NULL argument value.
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 argument, in the format indicated by the
3620                 associated format code.
3621                 <replaceable>n</replaceable> is the above length.
3622 </para>
3623 </listitem>
3624 </varlistentry>
3625 </variablelist>
3626         After the last argument, the following field appears:
3627 <variablelist>
3628 <varlistentry>
3629 <term>
3630         Int16
3631 </term>
3632 <listitem>
3633 <para>
3634                 The format code for the function result. Must presently be
3635                 zero (text) or one (binary).
3636 </para>
3637 </listitem>
3638 </varlistentry>
3639 </variablelist>
3640
3641 </para>
3642 </listitem>
3643 </varlistentry>
3644
3645
3646 <varlistentry>
3647 <term>
3648 FunctionCallResponse (B)
3649 </term>
3650 <listitem>
3651 <para>
3652
3653 <variablelist>
3654 <varlistentry>
3655 <term>
3656         Byte1('V')
3657 </term>
3658 <listitem>
3659 <para>
3660                 Identifies the message as a function call result.
3661 </para>
3662 </listitem>
3663 </varlistentry>
3664 <varlistentry>
3665 <term>
3666         Int32
3667 </term>
3668 <listitem>
3669 <para>
3670                 Length of message contents in bytes, including self.
3671 </para>
3672 </listitem>
3673 </varlistentry>
3674 <varlistentry>
3675 <term>
3676         Int32
3677 </term>
3678 <listitem>
3679 <para>
3680                 The length of the function result value, in bytes (this count
3681                 does not include itself).  Can be zero.
3682                 As a special case, -1 indicates a NULL function result.
3683                 No value bytes follow in the NULL case.
3684 </para>
3685 </listitem>
3686 </varlistentry>
3687 <varlistentry>
3688 <term>
3689         Byte<replaceable>n</replaceable>
3690 </term>
3691 <listitem>
3692 <para>
3693                 The value of the function result, in the format indicated by
3694                 the associated format code.
3695                 <replaceable>n</replaceable> is the above length.
3696 </para>
3697 </listitem>
3698 </varlistentry>
3699 </variablelist>
3700
3701 </para>
3702 </listitem>
3703 </varlistentry>
3704
3705
3706 <varlistentry>
3707 <term>
3708 NoData (B)
3709 </term>
3710 <listitem>
3711 <para>
3712
3713 <variablelist>
3714 <varlistentry>
3715 <term>
3716         Byte1('n')
3717 </term>
3718 <listitem>
3719 <para>
3720                 Identifies the message as a no-data indicator.
3721 </para>
3722 </listitem>
3723 </varlistentry>
3724 <varlistentry>
3725 <term>
3726         Int32(4)
3727 </term>
3728 <listitem>
3729 <para>
3730                 Length of message contents in bytes, including self.
3731 </para>
3732 </listitem>
3733 </varlistentry>
3734 </variablelist>
3735
3736 </para>
3737 </listitem>
3738 </varlistentry>
3739
3740
3741 <varlistentry>
3742 <term>
3743 NoticeResponse (B)
3744 </term>
3745 <listitem>
3746 <para>
3747
3748 <variablelist>
3749 <varlistentry>
3750 <term>
3751         Byte1('N')
3752 </term>
3753 <listitem>
3754 <para>
3755                 Identifies the message as a notice.
3756 </para>
3757 </listitem>
3758 </varlistentry>
3759 <varlistentry>
3760 <term>
3761         Int32
3762 </term>
3763 <listitem>
3764 <para>
3765                 Length of message contents in bytes, including self.
3766 </para>
3767 </listitem>
3768 </varlistentry>
3769 </variablelist>
3770         The message body consists of one or more identified fields,
3771         followed by a zero byte as a terminator.  Fields can appear in
3772         any order.  For each field there is the following:
3773 <variablelist>
3774 <varlistentry>
3775 <term>
3776         Byte1
3777 </term>
3778 <listitem>
3779 <para>
3780                 A code identifying the field type; if zero, this is
3781                 the message terminator and no string follows.
3782                 The presently defined field types are listed in
3783                 <xref linkend="protocol-error-fields">.
3784                 Since more field types might be added in future,
3785                 frontends should silently ignore fields of unrecognized
3786                 type.
3787 </para>
3788 </listitem>
3789 </varlistentry>
3790 <varlistentry>
3791 <term>
3792         String
3793 </term>
3794 <listitem>
3795 <para>
3796                 The field value.
3797 </para>
3798 </listitem>
3799 </varlistentry>
3800 </variablelist>
3801
3802 </para>
3803 </listitem>
3804 </varlistentry>
3805
3806
3807 <varlistentry>
3808 <term>
3809 NotificationResponse (B)
3810 </term>
3811 <listitem>
3812 <para>
3813
3814 <variablelist>
3815 <varlistentry>
3816 <term>
3817         Byte1('A')
3818 </term>
3819 <listitem>
3820 <para>
3821                 Identifies the message as a notification response.
3822 </para>
3823 </listitem>
3824 </varlistentry>
3825 <varlistentry>
3826 <term>
3827         Int32
3828 </term>
3829 <listitem>
3830 <para>
3831                 Length of message contents in bytes, including self.
3832 </para>
3833 </listitem>
3834 </varlistentry>
3835 <varlistentry>
3836 <term>
3837         Int32
3838 </term>
3839 <listitem>
3840 <para>
3841                 The process ID of the notifying backend process.
3842 </para>
3843 </listitem>
3844 </varlistentry>
3845 <varlistentry>
3846 <term>
3847         String
3848 </term>
3849 <listitem>
3850 <para>
3851                 The name of the channel that the notify has been raised on.
3852 </para>
3853 </listitem>
3854 </varlistentry>
3855 <varlistentry>
3856 <term>
3857         String
3858 </term>
3859 <listitem>
3860 <para>
3861                 The <quote>payload</> string passed from the notifying process.
3862 </para>
3863 </listitem>
3864 </varlistentry>
3865 </variablelist>
3866
3867 </para>
3868 </listitem>
3869 </varlistentry>
3870
3871
3872 <varlistentry>
3873 <term>
3874 ParameterDescription (B)
3875 </term>
3876 <listitem>
3877 <para>
3878
3879 <variablelist>
3880 <varlistentry>
3881 <term>
3882         Byte1('t')
3883 </term>
3884 <listitem>
3885 <para>
3886                 Identifies the message as a parameter description.
3887 </para>
3888 </listitem>
3889 </varlistentry>
3890 <varlistentry>
3891 <term>
3892         Int32
3893 </term>
3894 <listitem>
3895 <para>
3896                 Length of message contents in bytes, including self.
3897 </para>
3898 </listitem>
3899 </varlistentry>
3900 <varlistentry>
3901 <term>
3902         Int16
3903 </term>
3904 <listitem>
3905 <para>
3906                 The number of parameters used by the statement
3907                 (can be zero).
3908 </para>
3909 </listitem>
3910 </varlistentry>
3911 </variablelist>
3912         Then, for each parameter, there is the following:
3913 <variablelist>
3914 <varlistentry>
3915 <term>
3916         Int32
3917 </term>
3918 <listitem>
3919 <para>
3920                 Specifies the object ID of the parameter data type.
3921 </para>
3922 </listitem>
3923 </varlistentry>
3924 </variablelist>
3925 </para>
3926 </listitem>
3927 </varlistentry>
3928
3929
3930 <varlistentry>
3931 <term>
3932 ParameterStatus (B)
3933 </term>
3934 <listitem>
3935 <para>
3936
3937 <variablelist>
3938 <varlistentry>
3939 <term>
3940         Byte1('S')
3941 </term>
3942 <listitem>
3943 <para>
3944                 Identifies the message as a run-time parameter status report.
3945 </para>
3946 </listitem>
3947 </varlistentry>
3948 <varlistentry>
3949 <term>
3950         Int32
3951 </term>
3952 <listitem>
3953 <para>
3954                 Length of message contents in bytes, including self.
3955 </para>
3956 </listitem>
3957 </varlistentry>
3958 <varlistentry>
3959 <term>
3960         String
3961 </term>
3962 <listitem>
3963 <para>
3964                 The name of the run-time parameter being reported.
3965 </para>
3966 </listitem>
3967 </varlistentry>
3968 <varlistentry>
3969 <term>
3970         String
3971 </term>
3972 <listitem>
3973 <para>
3974                 The current value of the parameter.
3975 </para>
3976 </listitem>
3977 </varlistentry>
3978 </variablelist>
3979 </para>
3980 </listitem>
3981 </varlistentry>
3982
3983
3984 <varlistentry>
3985 <term>
3986 Parse (F)
3987 </term>
3988 <listitem>
3989 <para>
3990
3991 <variablelist>
3992 <varlistentry>
3993 <term>
3994         Byte1('P')
3995 </term>
3996 <listitem>
3997 <para>
3998                 Identifies the message as a Parse command.
3999 </para>
4000 </listitem>
4001 </varlistentry>
4002 <varlistentry>
4003 <term>
4004         Int32
4005 </term>
4006 <listitem>
4007 <para>
4008                 Length of message contents in bytes, including self.
4009 </para>
4010 </listitem>
4011 </varlistentry>
4012 <varlistentry>
4013 <term>
4014         String
4015 </term>
4016 <listitem>
4017 <para>
4018                 The name of the destination prepared statement
4019                 (an empty string selects the unnamed prepared statement).
4020 </para>
4021 </listitem>
4022 </varlistentry>
4023 <varlistentry>
4024 <term>
4025         String
4026 </term>
4027 <listitem>
4028 <para>
4029                 The query string to be parsed.
4030 </para>
4031 </listitem>
4032 </varlistentry>
4033 <varlistentry>
4034 <term>
4035         Int16
4036 </term>
4037 <listitem>
4038 <para>
4039                 The number of parameter data types specified
4040                 (can be zero).  Note that this is not an indication of
4041                 the number of parameters that might appear in the
4042                 query string, only the number that the frontend wants to
4043                 prespecify types for.
4044 </para>
4045 </listitem>
4046 </varlistentry>
4047 </variablelist>
4048         Then, for each parameter, there is the following:
4049 <variablelist>
4050 <varlistentry>
4051 <term>
4052         Int32
4053 </term>
4054 <listitem>
4055 <para>
4056                 Specifies the object ID of the parameter data type.
4057                 Placing a zero here is equivalent to leaving the type
4058                 unspecified.
4059 </para>
4060 </listitem>
4061 </varlistentry>
4062 </variablelist>
4063 </para>
4064 </listitem>
4065 </varlistentry>
4066
4067
4068 <varlistentry>
4069 <term>
4070 ParseComplete (B)
4071 </term>
4072 <listitem>
4073 <para>
4074
4075 <variablelist>
4076 <varlistentry>
4077 <term>
4078         Byte1('1')
4079 </term>
4080 <listitem>
4081 <para>
4082                 Identifies the message as a Parse-complete indicator.
4083 </para>
4084 </listitem>
4085 </varlistentry>
4086 <varlistentry>
4087 <term>
4088         Int32(4)
4089 </term>
4090 <listitem>
4091 <para>
4092                 Length of message contents in bytes, including self.
4093 </para>
4094 </listitem>
4095 </varlistentry>
4096 </variablelist>
4097
4098 </para>
4099 </listitem>
4100 </varlistentry>
4101
4102
4103 <varlistentry>
4104 <term>
4105 PasswordMessage (F)
4106 </term>
4107 <listitem>
4108 <para>
4109
4110 <variablelist>
4111 <varlistentry>
4112 <term>
4113         Byte1('p')
4114 </term>
4115 <listitem>
4116 <para>
4117                 Identifies the message as a password response. Note that
4118                 this is also used for GSSAPI and SSPI response messages
4119                 (which is really a design error, since the contained data
4120                 is not a null-terminated string in that case, but can be
4121                 arbitrary binary data).
4122 </para>
4123 </listitem>
4124 </varlistentry>
4125 <varlistentry>
4126 <term>
4127         Int32
4128 </term>
4129 <listitem>
4130 <para>
4131                 Length of message contents in bytes, including self.
4132 </para>
4133 </listitem>
4134 </varlistentry>
4135 <varlistentry>
4136 <term>
4137         String
4138 </term>
4139 <listitem>
4140 <para>
4141                 The password (encrypted, if requested).
4142 </para>
4143 </listitem>
4144 </varlistentry>
4145 </variablelist>
4146 </para>
4147 </listitem>
4148 </varlistentry>
4149
4150
4151 <varlistentry>
4152 <term>
4153 PortalSuspended (B)
4154 </term>
4155 <listitem>
4156 <para>
4157
4158 <variablelist>
4159 <varlistentry>
4160 <term>
4161         Byte1('s')
4162 </term>
4163 <listitem>
4164 <para>
4165                 Identifies the message as a portal-suspended indicator.
4166                 Note this only appears if an Execute message's row-count limit
4167                 was reached.
4168 </para>
4169 </listitem>
4170 </varlistentry>
4171 <varlistentry>
4172 <term>
4173         Int32(4)
4174 </term>
4175 <listitem>
4176 <para>
4177                 Length of message contents in bytes, including self.
4178 </para>
4179 </listitem>
4180 </varlistentry>
4181 </variablelist>
4182
4183 </para>
4184 </listitem>
4185 </varlistentry>
4186
4187
4188 <varlistentry>
4189 <term>
4190 Query (F)
4191 </term>
4192 <listitem>
4193 <para>
4194
4195 <variablelist>
4196 <varlistentry>
4197 <term>
4198         Byte1('Q')
4199 </term>
4200 <listitem>
4201 <para>
4202                 Identifies the message as a simple query.
4203 </para>
4204 </listitem>
4205 </varlistentry>
4206 <varlistentry>
4207 <term>
4208         Int32
4209 </term>
4210 <listitem>
4211 <para>
4212                 Length of message contents in bytes, including self.
4213 </para>
4214 </listitem>
4215 </varlistentry>
4216 <varlistentry>
4217 <term>
4218         String
4219 </term>
4220 <listitem>
4221 <para>
4222                 The query string itself.
4223 </para>
4224 </listitem>
4225 </varlistentry>
4226 </variablelist>
4227
4228 </para>
4229 </listitem>
4230 </varlistentry>
4231
4232
4233 <varlistentry>
4234 <term>
4235 ReadyForQuery (B)
4236 </term>
4237 <listitem>
4238 <para>
4239
4240 <variablelist>
4241 <varlistentry>
4242 <term>
4243         Byte1('Z')
4244 </term>
4245 <listitem>
4246 <para>
4247                 Identifies the message type.  ReadyForQuery is sent
4248                 whenever the backend is ready for a new query cycle.
4249 </para>
4250 </listitem>
4251 </varlistentry>
4252 <varlistentry>
4253 <term>
4254         Int32(5)
4255 </term>
4256 <listitem>
4257 <para>
4258                 Length of message contents in bytes, including self.
4259 </para>
4260 </listitem>
4261 </varlistentry>
4262 <varlistentry>
4263 <term>
4264         Byte1
4265 </term>
4266 <listitem>
4267 <para>
4268                 Current backend transaction status indicator.
4269                 Possible values are '<literal>I</>' if idle (not in
4270                 a transaction block); '<literal>T</>' if in a transaction
4271                 block; or '<literal>E</>' if in a failed transaction
4272                 block (queries will be rejected until block is ended).
4273 </para>
4274 </listitem>
4275 </varlistentry>
4276 </variablelist>
4277
4278 </para>
4279 </listitem>
4280 </varlistentry>
4281
4282
4283 <varlistentry>
4284 <term>
4285 RowDescription (B)
4286 </term>
4287 <listitem>
4288 <para>
4289
4290 <variablelist>
4291 <varlistentry>
4292 <term>
4293         Byte1('T')
4294 </term>
4295 <listitem>
4296 <para>
4297                 Identifies the message as a row description.
4298 </para>
4299 </listitem>
4300 </varlistentry>
4301 <varlistentry>
4302 <term>
4303         Int32
4304 </term>
4305 <listitem>
4306 <para>
4307                 Length of message contents in bytes, including self.
4308 </para>
4309 </listitem>
4310 </varlistentry>
4311 <varlistentry>
4312 <term>
4313         Int16
4314 </term>
4315 <listitem>
4316 <para>
4317                 Specifies the number of fields in a row (can be zero).
4318 </para>
4319 </listitem>
4320 </varlistentry>
4321 </variablelist>
4322         Then, for each field, there is the following:
4323 <variablelist>
4324 <varlistentry>
4325 <term>
4326         String
4327 </term>
4328 <listitem>
4329 <para>
4330                 The field name.
4331 </para>
4332 </listitem>
4333 </varlistentry>
4334 <varlistentry>
4335 <term>
4336         Int32
4337 </term>
4338 <listitem>
4339 <para>
4340                 If the field can be identified as a column of a specific
4341                 table, the object ID of the table; otherwise zero.
4342 </para>
4343 </listitem>
4344 </varlistentry>
4345 <varlistentry>
4346 <term>
4347         Int16
4348 </term>
4349 <listitem>
4350 <para>
4351                 If the field can be identified as a column of a specific
4352                 table, the attribute number of the column; otherwise zero.
4353 </para>
4354 </listitem>
4355 </varlistentry>
4356 <varlistentry>
4357 <term>
4358         Int32
4359 </term>
4360 <listitem>
4361 <para>
4362                 The object ID of the field's data type.
4363 </para>
4364 </listitem>
4365 </varlistentry>
4366 <varlistentry>
4367 <term>
4368         Int16
4369 </term>
4370 <listitem>
4371 <para>
4372                 The data type size (see <varname>pg_type.typlen</>).
4373                 Note that negative values denote variable-width types.
4374 </para>
4375 </listitem>
4376 </varlistentry>
4377 <varlistentry>
4378 <term>
4379         Int32
4380 </term>
4381 <listitem>
4382 <para>
4383                 The type modifier (see <varname>pg_attribute.atttypmod</>).
4384                 The meaning of the modifier is type-specific.
4385 </para>
4386 </listitem>
4387 </varlistentry>
4388 <varlistentry>
4389 <term>
4390         Int16
4391 </term>
4392 <listitem>
4393 <para>
4394                 The format code being used for the field.  Currently will
4395                 be zero (text) or one (binary).  In a RowDescription
4396                 returned from the statement variant of Describe, the
4397                 format code is not yet known and will always be zero.
4398 </para>
4399 </listitem>
4400 </varlistentry>
4401 </variablelist>
4402
4403 </para>
4404 </listitem>
4405 </varlistentry>
4406
4407
4408 <varlistentry>
4409 <term>
4410 SSLRequest (F)
4411 </term>
4412 <listitem>
4413 <para>
4414
4415 <variablelist>
4416 <varlistentry>
4417 <term>
4418         Int32(8)
4419 </term>
4420 <listitem>
4421 <para>
4422                 Length of message contents in bytes, including self.
4423 </para>
4424 </listitem>
4425 </varlistentry>
4426 <varlistentry>
4427 <term>
4428         Int32(80877103)
4429 </term>
4430 <listitem>
4431 <para>
4432                 The <acronym>SSL</acronym> request code.  The value is chosen to contain
4433                 <literal>1234</> in the most significant 16 bits, and <literal>5679</> in the
4434                 least 16 significant bits.  (To avoid confusion, this code
4435                 must not be the same as any protocol version number.)
4436 </para>
4437 </listitem>
4438 </varlistentry>
4439 </variablelist>
4440
4441 </para>
4442 </listitem>
4443 </varlistentry>
4444
4445
4446 <varlistentry>
4447 <term>
4448 StartupMessage (F)
4449 </term>
4450 <listitem>
4451 <para>
4452
4453 <variablelist>
4454 <varlistentry>
4455 <term>
4456         Int32
4457 </term>
4458 <listitem>
4459 <para>
4460                 Length of message contents in bytes, including self.
4461 </para>
4462 </listitem>
4463 </varlistentry>
4464 <varlistentry>
4465 <term>
4466         Int32(196608)
4467 </term>
4468 <listitem>
4469 <para>
4470                 The protocol version number.  The most significant 16 bits are
4471                 the major version number (3 for the protocol described here).
4472                 The least significant 16 bits are the minor version number
4473                 (0 for the protocol described here).
4474 </para>
4475 </listitem>
4476 </varlistentry>
4477 </variablelist>
4478         The protocol version number is followed by one or more pairs of
4479         parameter name and value strings.  A zero byte is required as a
4480         terminator after the last name/value pair.
4481         Parameters can appear in any
4482         order.  <literal>user</> is required, others are optional.
4483         Each parameter is specified as:
4484 <variablelist>
4485 <varlistentry>
4486 <term>
4487         String
4488 </term>
4489 <listitem>
4490 <para>
4491                 The parameter name.  Currently recognized names are:
4492
4493 <variablelist>
4494 <varlistentry>
4495 <term>
4496                 <literal>user</>
4497 </term>
4498 <listitem>
4499 <para>
4500                         The database user name to connect as.  Required;
4501                         there is no default.
4502 </para>
4503 </listitem>
4504 </varlistentry>
4505 <varlistentry>
4506 <term>
4507                 <literal>database</>
4508 </term>
4509 <listitem>
4510 <para>
4511                         The database to connect to.  Defaults to the user name.
4512 </para>
4513 </listitem>
4514 </varlistentry>
4515 <varlistentry>
4516 <term>
4517                 <literal>options</>
4518 </term>
4519 <listitem>
4520 <para>
4521                         Command-line arguments for the backend.  (This is
4522                         deprecated in favor of setting individual run-time
4523                         parameters.)
4524 </para>
4525 </listitem>
4526 </varlistentry>
4527 </variablelist>
4528
4529                 In addition to the above, any run-time parameter that can be
4530                 set at backend start time might be listed.  Such settings
4531                 will be applied during backend start (after parsing the
4532                 command-line options if any).  The values will act as
4533                 session defaults.
4534 </para>
4535 </listitem>
4536 </varlistentry>
4537 <varlistentry>
4538 <term>
4539         String
4540 </term>
4541 <listitem>
4542 <para>
4543                 The parameter value.
4544 </para>
4545 </listitem>
4546 </varlistentry>
4547 </variablelist>
4548
4549 </para>
4550 </listitem>
4551 </varlistentry>
4552
4553
4554 <varlistentry>
4555 <term>
4556 Sync (F)
4557 </term>
4558 <listitem>
4559 <para>
4560
4561 <variablelist>
4562 <varlistentry>
4563 <term>
4564         Byte1('S')
4565 </term>
4566 <listitem>
4567 <para>
4568                 Identifies the message as a Sync command.
4569 </para>
4570 </listitem>
4571 </varlistentry>
4572 <varlistentry>
4573 <term>
4574         Int32(4)
4575 </term>
4576 <listitem>
4577 <para>
4578                 Length of message contents in bytes, including self.
4579 </para>
4580 </listitem>
4581 </varlistentry>
4582 </variablelist>
4583
4584 </para>
4585 </listitem>
4586 </varlistentry>
4587
4588
4589 <varlistentry>
4590 <term>
4591 Terminate (F)
4592 </term>
4593 <listitem>
4594 <para>
4595
4596 <variablelist>
4597 <varlistentry>
4598 <term>
4599         Byte1('X')
4600 </term>
4601 <listitem>
4602 <para>
4603                 Identifies the message as a termination.
4604 </para>
4605 </listitem>
4606 </varlistentry>
4607 <varlistentry>
4608 <term>
4609         Int32(4)
4610 </term>
4611 <listitem>
4612 <para>
4613                 Length of message contents in bytes, including self.
4614 </para>
4615 </listitem>
4616 </varlistentry>
4617 </variablelist>
4618
4619 </para>
4620 </listitem>
4621 </varlistentry>
4622
4623
4624 </variablelist>
4625
4626 </sect1>
4627
4628
4629 <sect1 id="protocol-error-fields">
4630 <title>Error and Notice Message Fields</title>
4631
4632 <para>
4633 This section describes the fields that can appear in ErrorResponse and
4634 NoticeResponse messages.  Each field type has a single-byte identification
4635 token.  Note that any given field type should appear at most once per
4636 message.
4637 </para>
4638
4639 <variablelist>
4640
4641 <varlistentry>
4642 <term>
4643 <literal>S</>
4644 </term>
4645 <listitem>
4646 <para>
4647         Severity: the field contents are
4648         <literal>ERROR</>, <literal>FATAL</>, or
4649         <literal>PANIC</> (in an error message), or
4650         <literal>WARNING</>, <literal>NOTICE</>, <literal>DEBUG</>,
4651         <literal>INFO</>, or <literal>LOG</> (in a notice message),
4652         or a localized translation of one of these.  Always present.
4653 </para>
4654 </listitem>
4655 </varlistentry>
4656
4657 <varlistentry>
4658 <term>
4659 <literal>C</>
4660 </term>
4661 <listitem>
4662 <para>
4663         Code: the SQLSTATE code for the error (see <xref
4664         linkend="errcodes-appendix">).  Not localizable.  Always present.
4665 </para>
4666 </listitem>
4667 </varlistentry>
4668
4669 <varlistentry>
4670 <term>
4671 <literal>M</>
4672 </term>
4673 <listitem>
4674 <para>
4675         Message: the primary human-readable error message.
4676         This should be accurate but terse (typically one line).
4677         Always present.
4678 </para>
4679 </listitem>
4680 </varlistentry>
4681
4682 <varlistentry>
4683 <term>
4684 <literal>D</>
4685 </term>
4686 <listitem>
4687 <para>
4688         Detail: an optional secondary error message carrying more
4689         detail about the problem.  Might run to multiple lines.
4690 </para>
4691 </listitem>
4692 </varlistentry>
4693
4694 <varlistentry>
4695 <term>
4696 <literal>H</>
4697 </term>
4698 <listitem>
4699 <para>
4700         Hint: an optional suggestion what to do about the problem.
4701         This is intended to differ from Detail in that it offers advice
4702         (potentially inappropriate) rather than hard facts.
4703         Might run to multiple lines.
4704 </para>
4705 </listitem>
4706 </varlistentry>
4707
4708 <varlistentry>
4709 <term>
4710 <literal>P</>
4711 </term>
4712 <listitem>
4713 <para>
4714         Position: the field value is a decimal ASCII integer, indicating
4715         an error cursor position as an index into the original query string.
4716         The first character has index 1, and positions are measured in
4717         characters not bytes.
4718 </para>
4719 </listitem>
4720 </varlistentry>
4721
4722 <varlistentry>
4723 <term>
4724 <literal>p</>
4725 </term>
4726 <listitem>
4727 <para>
4728         Internal position: this is defined the same as the <literal>P</>
4729         field, but it is used when the cursor position refers to an internally
4730         generated command rather than the one submitted by the client.
4731         The <literal>q</> field will always appear when this field appears.
4732 </para>
4733 </listitem>
4734 </varlistentry>
4735
4736 <varlistentry>
4737 <term>
4738 <literal>q</>
4739 </term>
4740 <listitem>
4741 <para>
4742         Internal query: the text of a failed internally-generated command.
4743         This could be, for example, a SQL query issued by a PL/pgSQL function.
4744 </para>
4745 </listitem>
4746 </varlistentry>
4747
4748 <varlistentry>
4749 <term>
4750 <literal>W</>
4751 </term>
4752 <listitem>
4753 <para>
4754         Where: an indication of the context in which the error occurred.
4755         Presently this includes a call stack traceback of active
4756         procedural language functions and internally-generated queries.
4757         The trace is one entry per line, most recent first.
4758 </para>
4759 </listitem>
4760 </varlistentry>
4761
4762 <varlistentry>
4763 <term>
4764 <literal>s</>
4765 </term>
4766 <listitem>
4767 <para>
4768         Schema name: if the error was associated with a specific database
4769         object, the name of the schema containing that object, if any.
4770 </para>
4771 </listitem>
4772 </varlistentry>
4773
4774 <varlistentry>
4775 <term>
4776 <literal>t</>
4777 </term>
4778 <listitem>
4779 <para>
4780         Table name: if the error was associated with a specific table, the
4781         name of the table.  (When this field is present, the schema name field
4782         provides the name of the table's schema.)
4783 </para>
4784 </listitem>
4785 </varlistentry>
4786
4787 <varlistentry>
4788 <term>
4789 <literal>c</>
4790 </term>
4791 <listitem>
4792 <para>
4793         Column name: if the error was associated with a specific table column,
4794         the name of the column.  (When this field is present, the schema and
4795         table name fields identify the table.)
4796 </para>
4797 </listitem>
4798 </varlistentry>
4799
4800 <varlistentry>
4801 <term>
4802 <literal>d</>
4803 </term>
4804 <listitem>
4805 <para>
4806         Datatype name: if the error was associated with a specific datatype,
4807         the name of the datatype.  (When this field is present, the schema
4808         name field provides the name of the datatype's schema.)
4809 </para>
4810 </listitem>
4811 </varlistentry>
4812
4813 <varlistentry>
4814 <term>
4815 <literal>n</>
4816 </term>
4817 <listitem>
4818 <para>
4819         Constraint name: if the error was associated with a specific
4820         constraint, the name of the constraint.  The table or domain that the
4821         constraint belongs to is reported using the fields listed above.  (For
4822         this purpose, indexes are treated as constraints, even if they weren't
4823         created with constraint syntax.)
4824 </para>
4825 </listitem>
4826 </varlistentry>
4827
4828 <varlistentry>
4829 <term>
4830 <literal>F</>
4831 </term>
4832 <listitem>
4833 <para>
4834         File: the file name of the source-code location where the error
4835         was reported.
4836 </para>
4837 </listitem>
4838 </varlistentry>
4839
4840 <varlistentry>
4841 <term>
4842 <literal>L</>
4843 </term>
4844 <listitem>
4845 <para>
4846         Line: the line number of the source-code location where the error
4847         was reported.
4848 </para>
4849 </listitem>
4850 </varlistentry>
4851
4852 <varlistentry>
4853 <term>
4854 <literal>R</>
4855 </term>
4856 <listitem>
4857 <para>
4858         Routine: the name of the source-code routine reporting the error.
4859 </para>
4860 </listitem>
4861 </varlistentry>
4862
4863 </variablelist>
4864
4865 <note>
4866  <para>
4867   The fields for schema name, table name, column name, datatype name, and
4868   constraint name are supplied only for a limited number of error types;
4869   see <xref linkend="errcodes-appendix">.
4870  </para>
4871 </note>
4872
4873 <para>
4874 The client is responsible for formatting displayed information to meet its
4875 needs; in particular it should break long lines as needed.  Newline characters
4876 appearing in the error message fields should be treated as paragraph breaks,
4877 not line breaks.
4878 </para>
4879
4880 </sect1>
4881
4882 <sect1 id="protocol-changes">
4883 <title>Summary of Changes since Protocol 2.0</title>
4884
4885 <para>
4886 This section provides a quick checklist of changes, for the benefit of
4887 developers trying to update existing client libraries to protocol 3.0.
4888 </para>
4889
4890 <para>
4891 The initial startup packet uses a flexible list-of-strings format
4892 instead of a fixed format.  Notice that session default values for run-time
4893 parameters can now be specified directly in the startup packet.  (Actually,
4894 you could do that before using the <literal>options</> field, but given the
4895 limited width of <literal>options</> and the lack of any way to quote
4896 whitespace in the values, it wasn't a very safe technique.)
4897 </para>
4898
4899 <para>
4900 All messages now have a length count immediately following the message type
4901 byte (except for startup packets, which have no type byte).  Also note that
4902 PasswordMessage now has a type byte.
4903 </para>
4904
4905 <para>
4906 ErrorResponse and NoticeResponse ('<literal>E</>' and '<literal>N</>')
4907 messages now contain multiple fields, from which the client code can
4908 assemble an error message of the desired level of verbosity.  Note that
4909 individual fields will typically not end with a newline, whereas the single
4910 string sent in the older protocol always did.
4911 </para>
4912
4913 <para>
4914 The ReadyForQuery ('<literal>Z</>') message includes a transaction status
4915 indicator.
4916 </para>
4917
4918 <para>
4919 The distinction between BinaryRow and DataRow message types is gone; the
4920 single DataRow message type serves for returning data in all formats.
4921 Note that the layout of DataRow has changed to make it easier to parse.
4922 Also, the representation of binary values has changed: it is no longer
4923 directly tied to the server's internal representation.
4924 </para>
4925
4926 <para>
4927 There is a new <quote>extended query</> sub-protocol, which adds the frontend
4928 message types Parse, Bind, Execute, Describe, Close, Flush, and Sync, and the
4929 backend message types ParseComplete, BindComplete, PortalSuspended,
4930 ParameterDescription, NoData, and CloseComplete.  Existing clients do not
4931 have to concern themselves with this sub-protocol, but making use of it
4932 might allow improvements in performance or functionality.
4933 </para>
4934
4935 <para>
4936 <command>COPY</command> data is now encapsulated into CopyData and CopyDone messages.  There
4937 is a well-defined way to recover from errors during <command>COPY</command>.  The special
4938 <quote><literal>\.</></quote> last line is not needed anymore, and is not sent
4939 during <command>COPY OUT</command>.
4940 (It is still recognized as a terminator during <command>COPY IN</command>, but its use is
4941 deprecated and will eventually be removed.)  Binary <command>COPY</command> is supported.
4942 The CopyInResponse and CopyOutResponse messages include fields indicating
4943 the number of columns and the format of each column.
4944 </para>
4945
4946 <para>
4947 The layout of FunctionCall and FunctionCallResponse messages has changed.
4948 FunctionCall can now support passing NULL arguments to functions.  It also
4949 can handle passing parameters and retrieving results in either text or
4950 binary format.  There is no longer any reason to consider FunctionCall a
4951 potential security hole, since it does not offer direct access to internal
4952 server data representations.
4953 </para>
4954
4955 <para>
4956 The backend sends ParameterStatus ('<literal>S</>') messages during connection
4957 startup for all parameters it considers interesting to the client library.
4958 Subsequently, a ParameterStatus message is sent whenever the active value
4959 changes for any of these parameters.
4960 </para>
4961
4962 <para>
4963 The RowDescription ('<literal>T</>') message carries new table OID and column
4964 number fields for each column of the described row.  It also shows the format
4965 code for each column.
4966 </para>
4967
4968 <para>
4969 The CursorResponse ('<literal>P</>') message is no longer generated by
4970 the backend.
4971 </para>
4972
4973 <para>
4974 The NotificationResponse ('<literal>A</>') message has an additional string
4975 field, which can carry a <quote>payload</> string passed
4976 from the <command>NOTIFY</command> event sender.
4977 </para>
4978
4979 <para>
4980 The EmptyQueryResponse ('<literal>I</>') message used to include an empty
4981 string parameter; this has been removed.
4982 </para>
4983
4984 </sect1>
4985
4986 </chapter>