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