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