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