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