OSDN Git Service

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