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