OSDN Git Service

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