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