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