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