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