1 <!-- doc/src/sgml/protocol.sgml -->
3 <chapter id="protocol">
4 <title>Frontend/Backend Protocol</title>
6 <indexterm zone="protocol">
7 <primary>protocol</primary>
8 <secondary>frontend-backend</secondary>
12 <productname>PostgreSQL</productname> uses a message-based protocol
13 for communication between frontends and backends (clients and servers).
14 The protocol is supported over <acronym>TCP/IP</acronym> and also over
15 Unix-domain sockets. Port number 5432 has been registered with IANA as
16 the customary TCP port number for servers supporting this protocol, but
17 in practice any non-privileged port number can be used.
21 This document describes version 3.0 of the protocol, implemented in
22 <productname>PostgreSQL</productname> 7.4 and later. For descriptions
23 of the earlier protocol versions, see previous releases of the
24 <productname>PostgreSQL</productname> documentation. A single server
25 can support multiple protocol versions. The initial
26 startup-request message tells the server which protocol version the
27 client is attempting to use, and then the server follows that protocol
32 In order to serve multiple clients efficiently, the server launches
33 a new <quote>backend</> process for each client.
34 In the current implementation, a new child
35 process is created immediately after an incoming connection is detected.
36 This is transparent to the protocol, however. For purposes of the
37 protocol, the terms <quote>backend</> and <quote>server</> are
38 interchangeable; likewise <quote>frontend</> and <quote>client</>
42 <sect1 id="protocol-overview">
43 <title>Overview</title>
46 The protocol has separate phases for startup and normal operation.
47 In the startup phase, the frontend opens a connection to the server
48 and authenticates itself to the satisfaction of the server. (This might
49 involve a single message, or multiple messages depending on the
50 authentication method being used.) If all goes well, the server then sends
51 status information to the frontend, and finally enters normal operation.
52 Except for the initial startup-request message, this part of the
53 protocol is driven by the server.
57 During normal operation, the frontend sends queries and
58 other commands to the backend, and the backend sends back query results
59 and other responses. There are a few cases (such as <command>NOTIFY</>)
61 backend will send unsolicited messages, but for the most part this portion
62 of a session is driven by frontend requests.
66 Termination of the session is normally by frontend choice, but can be
67 forced by the backend in certain cases. In any case, when the backend
68 closes the connection, it will roll back any open (incomplete) transaction
73 Within normal operation, SQL commands can be executed through either of
74 two sub-protocols. In the <quote>simple query</> protocol, the frontend
75 just sends a textual query string, which is parsed and immediately
76 executed by the backend. In the <quote>extended query</> protocol,
77 processing of queries is separated into multiple steps: parsing,
78 binding of parameter values, and execution. This offers flexibility
79 and performance benefits, at the cost of extra complexity.
83 Normal operation has additional sub-protocols for special operations
84 such as <command>COPY</>.
87 <sect2 id="protocol-message-concepts">
88 <title>Messaging Overview</title>
91 All communication is through a stream of messages. The first byte of a
92 message identifies the message type, and the next four bytes specify the
93 length of the rest of the message (this length count includes itself, but
94 not the message-type byte). The remaining contents of the message are
95 determined by the message type. For historical reasons, the very first
96 message sent by the client (the startup message) has no initial
101 To avoid losing synchronization with the message stream, both servers and
102 clients typically read an entire message into a buffer (using the byte
103 count) before attempting to process its contents. This allows easy
104 recovery if an error is detected while processing the contents. In
105 extreme situations (such as not having enough memory to buffer the
106 message), the receiver can use the byte count to determine how much
107 input to skip before it resumes reading messages.
111 Conversely, both servers and clients must take care never to send an
112 incomplete message. This is commonly done by marshaling the entire message
113 in a buffer before beginning to send it. If a communications failure
114 occurs partway through sending or receiving a message, the only sensible
115 response is to abandon the connection, since there is little hope of
116 recovering message-boundary synchronization.
120 <sect2 id="protocol-query-concepts">
121 <title>Extended Query Overview</title>
124 In the extended-query protocol, execution of SQL commands is divided
125 into multiple steps. The state retained between steps is represented
126 by two types of objects: <firstterm>prepared statements</> and
127 <firstterm>portals</>. A prepared statement represents the result of
128 parsing and semantic analysis of a textual query string.
129 A prepared statement is not in itself ready to execute, because it might
130 lack specific values for <firstterm>parameters</>. A portal represents
131 a ready-to-execute or already-partially-executed statement, with any
132 missing parameter values filled in. (For <command>SELECT</> statements,
133 a portal is equivalent to an open cursor, but we choose to use a different
134 term since cursors don't handle non-<command>SELECT</> statements.)
138 The overall execution cycle consists of a <firstterm>parse</> step,
139 which creates a prepared statement from a textual query string; a
140 <firstterm>bind</> step, which creates a portal given a prepared
141 statement and values for any needed parameters; and an
142 <firstterm>execute</> step that runs a portal's query. In the case of
143 a query that returns rows (<command>SELECT</>, <command>SHOW</>, etc),
144 the execute step can be told to fetch only
145 a limited number of rows, so that multiple execute steps might be needed
146 to complete the operation.
150 The backend can keep track of multiple prepared statements and portals
151 (but note that these exist only within a session, and are never shared
152 across sessions). Existing prepared statements and portals are
153 referenced by names assigned when they were created. In addition,
154 an <quote>unnamed</> prepared statement and portal exist. Although these
155 behave largely the same as named objects, operations on them are optimized
156 for the case of executing a query only once and then discarding it,
157 whereas operations on named objects are optimized on the expectation
162 <sect2 id="protocol-format-codes">
163 <title>Formats and Format Codes</title>
166 Data of a particular data type might be transmitted in any of several
167 different <firstterm>formats</>. As of <productname>PostgreSQL</> 7.4
168 the only supported formats are <quote>text</> and <quote>binary</>,
169 but the protocol makes provision for future extensions. The desired
170 format for any value is specified by a <firstterm>format code</>.
171 Clients can specify a format code for each transmitted parameter value
172 and for each column of a query result. Text has format code zero,
173 binary has format code one, and all other format codes are reserved
174 for future definition.
178 The text representation of values is whatever strings are produced
179 and accepted by the input/output conversion functions for the
180 particular data type. In the transmitted representation, there is
181 no trailing null character; the frontend must add one to received
182 values if it wants to process them as C strings.
183 (The text format does not allow embedded nulls, by the way.)
187 Binary representations for integers use network byte order (most
188 significant byte first). For other data types consult the documentation
189 or source code to learn about the binary representation. Keep in mind
190 that binary representations for complex data types might change across
191 server versions; the text format is usually the more portable choice.
196 <sect1 id="protocol-flow">
197 <title>Message Flow</title>
200 This section describes the message flow and the semantics of each
201 message type. (Details of the exact representation of each message
202 appear in <xref linkend="protocol-message-formats">.) There are
203 several different sub-protocols depending on the state of the
204 connection: start-up, query, function call,
205 <command>COPY</command>, and termination. There are also special
206 provisions for asynchronous operations (including notification
207 responses and command cancellation), which can occur at any time
208 after the start-up phase.
212 <title>Start-up</title>
215 To begin a session, a frontend opens a connection to the server and sends
216 a startup message. This message includes the names of the user and of the
217 database the user wants to connect to; it also identifies the particular
218 protocol version to be used. (Optionally, the startup message can include
219 additional settings for run-time parameters.)
220 The server then uses this information and
221 the contents of its configuration files (such as
222 <filename>pg_hba.conf</filename>) to determine
223 whether the connection is provisionally acceptable, and what additional
224 authentication is required (if any).
228 The server then sends an appropriate authentication request message,
229 to which the frontend must reply with an appropriate authentication
230 response message (such as a password).
231 For all authentication methods except GSSAPI and SSPI, there is at most
232 one request and one response. In some methods, no response
233 at all is needed from the frontend, and so no authentication request
234 occurs. For GSSAPI and SSPI, multiple exchanges of packets may be needed
235 to complete the authentication.
239 The authentication cycle ends with the server either rejecting the
240 connection attempt (ErrorResponse), or sending AuthenticationOk.
244 The possible messages from the server in this phase are:
248 <term>ErrorResponse</term>
251 The connection attempt has been rejected.
252 The server then immediately closes the connection.
258 <term>AuthenticationOk</term>
261 The authentication exchange is successfully completed.
267 <term>AuthenticationKerberosV5</term>
270 The frontend must now take part in a Kerberos V5
271 authentication dialog (not described here, part of the
272 Kerberos specification) with the server. If this is
273 successful, the server responds with an AuthenticationOk,
274 otherwise it responds with an ErrorResponse. This is no
275 longer supported. This is not supported any more.
281 <term>AuthenticationCleartextPassword</term>
284 The frontend must now send a PasswordMessage containing the
285 password in clear-text form. If
286 this is the correct password, the server responds with an
287 AuthenticationOk, otherwise it responds with an ErrorResponse.
293 <term>AuthenticationMD5Password</term>
296 The frontend must now send a PasswordMessage containing the
297 password (with username) encrypted via MD5, then encrypted
298 again using the 4-byte random salt specified in the
299 AuthenticationMD5Password message. If this is the correct
300 password, the server responds with an AuthenticationOk,
301 otherwise it responds with an ErrorResponse. The actual
302 PasswordMessage can be computed in SQL as <literal>concat('md5',
303 md5(concat(md5(concat(password, username)), random-salt)))</>.
304 (Keep in mind the <function>md5()</> function returns its
305 result as a hex string.)
311 <term>AuthenticationSCMCredential</term>
314 This response is only possible for local Unix-domain connections
315 on platforms that support SCM credential messages. The frontend
316 must issue an SCM credential message and then send a single data
317 byte. (The contents of the data byte are uninteresting; it's
318 only used to ensure that the server waits long enough to receive
319 the credential message.) If the credential is acceptable,
320 the server responds with an
321 AuthenticationOk, otherwise it responds with an ErrorResponse.
322 (This message type is only issued by pre-9.1 servers. It may
323 eventually be removed from the protocol specification.)
329 <term>AuthenticationGSS</term>
332 The frontend must now initiate a GSSAPI negotiation. The frontend
333 will send a PasswordMessage with the first part of the GSSAPI
334 data stream in response to this. If further messages are needed,
335 the server will respond with AuthenticationGSSContinue.
341 <term>AuthenticationSSPI</term>
344 The frontend must now initiate a SSPI negotiation. The frontend
345 will send a PasswordMessage with the first part of the SSPI
346 data stream in response to this. If further messages are needed,
347 the server will respond with AuthenticationGSSContinue.
353 <term>AuthenticationGSSContinue</term>
356 This message contains the response data from the previous step
357 of GSSAPI or SSPI negotiation (AuthenticationGSS, AuthenticationSSPI
358 or a previous AuthenticationGSSContinue). If the GSSAPI
359 or SSPI data in this message
360 indicates more data is needed to complete the authentication,
361 the frontend must send that data as another PasswordMessage. If
362 GSSAPI or SSPI authentication is completed by this message, the server
363 will next send AuthenticationOk to indicate successful authentication
364 or ErrorResponse to indicate failure.
373 If the frontend does not support the authentication method
374 requested by the server, then it should immediately close the
379 After having received AuthenticationOk, the frontend must wait
380 for further messages from the server. In this phase a backend process
381 is being started, and the frontend is just an interested bystander.
382 It is still possible for the startup attempt
383 to fail (ErrorResponse), but in the normal case the backend will send
384 some ParameterStatus messages, BackendKeyData, and finally ReadyForQuery.
388 During this phase the backend will attempt to apply any additional
389 run-time parameter settings that were given in the startup message.
390 If successful, these values become session defaults. An error causes
391 ErrorResponse and exit.
395 The possible messages from the backend in this phase are:
399 <term>BackendKeyData</term>
402 This message provides secret-key data that the frontend must
403 save if it wants to be able to issue cancel requests later.
404 The frontend should not respond to this message, but should
405 continue listening for a ReadyForQuery message.
411 <term>ParameterStatus</term>
414 This message informs the frontend about the current (initial)
415 setting of backend parameters, such as <xref
416 linkend="guc-client-encoding"> or <xref linkend="guc-datestyle">.
417 The frontend can ignore this message, or record the settings
418 for its future use; see <xref linkend="protocol-async"> for
419 more details. The frontend should not respond to this
420 message, but should continue listening for a ReadyForQuery
427 <term>ReadyForQuery</term>
430 Start-up is completed. The frontend can now issue commands.
436 <term>ErrorResponse</term>
439 Start-up failed. The connection is closed after sending this
446 <term>NoticeResponse</term>
449 A warning message has been issued. The frontend should
450 display the message but continue listening for ReadyForQuery
459 The ReadyForQuery message is the same one that the backend will
460 issue after each command cycle. Depending on the coding needs of
461 the frontend, it is reasonable to consider ReadyForQuery as
462 starting a command cycle, or to consider ReadyForQuery as ending the
463 start-up phase and each subsequent command cycle.
468 <title>Simple Query</title>
471 A simple query cycle is initiated by the frontend sending a Query message
472 to the backend. The message includes an SQL command (or commands)
473 expressed as a text string.
474 The backend then sends one or more response
475 messages depending on the contents of the query command string,
476 and finally a ReadyForQuery response message. ReadyForQuery
477 informs the frontend that it can safely send a new command.
478 (It is not actually necessary for the frontend to wait for
479 ReadyForQuery before issuing another command, but the frontend must
480 then take responsibility for figuring out what happens if the earlier
481 command fails and already-issued later commands succeed.)
485 The possible response messages from the backend are:
489 <term>CommandComplete</term>
492 An SQL command completed normally.
498 <term>CopyInResponse</term>
501 The backend is ready to copy data from the frontend to a
502 table; see <xref linkend="protocol-copy">.
508 <term>CopyOutResponse</term>
511 The backend is ready to copy data from a table to the
512 frontend; see <xref linkend="protocol-copy">.
518 <term>RowDescription</term>
521 Indicates that rows are about to be returned in response to
522 a <command>SELECT</command>, <command>FETCH</command>, etc query.
523 The contents of this message describe the column layout of the rows.
524 This will be followed by a DataRow message for each row being returned
534 One of the set of rows returned by
535 a <command>SELECT</command>, <command>FETCH</command>, etc query.
541 <term>EmptyQueryResponse</term>
544 An empty query string was recognized.
550 <term>ErrorResponse</term>
553 An error has occurred.
559 <term>ReadyForQuery</term>
562 Processing of the query string is complete. A separate
563 message is sent to indicate this because the query string might
564 contain multiple SQL commands. (CommandComplete marks the
565 end of processing one SQL command, not the whole string.)
566 ReadyForQuery will always be sent, whether processing
567 terminates successfully or with an error.
573 <term>NoticeResponse</term>
576 A warning message has been issued in relation to the query.
577 Notices are in addition to other responses, i.e., the backend
578 will continue processing the command.
587 The response to a <command>SELECT</> query (or other queries that
588 return row sets, such as <command>EXPLAIN</> or <command>SHOW</>)
589 normally consists of RowDescription, zero or more
590 DataRow messages, and then CommandComplete.
591 <command>COPY</> to or from the frontend invokes special protocol
592 as described in <xref linkend="protocol-copy">.
593 All other query types normally produce only
594 a CommandComplete message.
598 Since a query string could contain several queries (separated by
599 semicolons), there might be several such response sequences before the
600 backend finishes processing the query string. ReadyForQuery is issued
601 when the entire string has been processed and the backend is ready to
602 accept a new query string.
606 If a completely empty (no contents other than whitespace) query string
607 is received, the response is EmptyQueryResponse followed by ReadyForQuery.
611 In the event of an error, ErrorResponse is issued followed by
612 ReadyForQuery. All further processing of the query string is aborted by
613 ErrorResponse (even if more queries remained in it). Note that this
614 might occur partway through the sequence of messages generated by an
619 In simple Query mode, the format of retrieved values is always text,
620 except when the given command is a <command>FETCH</> from a cursor
621 declared with the <literal>BINARY</> option. In that case, the
622 retrieved values are in binary format. The format codes given in
623 the RowDescription message tell which format is being used.
627 A frontend must be prepared to accept ErrorResponse and
628 NoticeResponse messages whenever it is expecting any other type of
629 message. See also <xref linkend="protocol-async"> concerning messages
630 that the backend might generate due to outside events.
634 Recommended practice is to code frontends in a state-machine style
635 that will accept any message type at any time that it could make sense,
636 rather than wiring in assumptions about the exact sequence of messages.
640 <sect2 id="protocol-flow-ext-query">
641 <title>Extended Query</title>
644 The extended query protocol breaks down the above-described simple
645 query protocol into multiple steps. The results of preparatory
646 steps can be re-used multiple times for improved efficiency.
647 Furthermore, additional features are available, such as the possibility
648 of supplying data values as separate parameters instead of having to
649 insert them directly into a query string.
653 In the extended protocol, the frontend first sends a Parse message,
654 which contains a textual query string, optionally some information
655 about data types of parameter placeholders, and the
656 name of a destination prepared-statement object (an empty string
657 selects the unnamed prepared statement). The response is
658 either ParseComplete or ErrorResponse. Parameter data types can be
659 specified by OID; if not given, the parser attempts to infer the
660 data types in the same way as it would do for untyped literal string
666 A parameter data type can be left unspecified by setting it to zero,
667 or by making the array of parameter type OIDs shorter than the
668 number of parameter symbols (<literal>$</><replaceable>n</>)
669 used in the query string. Another special case is that a parameter's
670 type can be specified as <type>void</> (that is, the OID of the
671 <type>void</> pseudotype). This is meant to allow parameter symbols
672 to be used for function parameters that are actually OUT parameters.
673 Ordinarily there is no context in which a <type>void</> parameter
674 could be used, but if such a parameter symbol appears in a function's
675 parameter list, it is effectively ignored. For example, a function
676 call such as <literal>foo($1,$2,$3,$4)</> could match a function with
677 two IN and two OUT arguments, if <literal>$3</> and <literal>$4</>
678 are specified as having type <type>void</>.
684 The query string contained in a Parse message cannot include more
685 than one SQL statement; else a syntax error is reported. This
686 restriction does not exist in the simple-query protocol, but it
687 does exist in the extended protocol, because allowing prepared
688 statements or portals to contain multiple commands would complicate
694 If successfully created, a named prepared-statement object lasts till
695 the end of the current session, unless explicitly destroyed. An unnamed
696 prepared statement lasts only until the next Parse statement specifying
697 the unnamed statement as destination is issued. (Note that a simple
698 Query message also destroys the unnamed statement.) Named prepared
699 statements must be explicitly closed before they can be redefined by
700 another Parse message, but this is not required for the unnamed statement.
701 Named prepared statements can also be created and accessed at the SQL
702 command level, using <command>PREPARE</> and <command>EXECUTE</>.
706 Once a prepared statement exists, it can be readied for execution using a
707 Bind message. The Bind message gives the name of the source prepared
708 statement (empty string denotes the unnamed prepared statement), the name
709 of the destination portal (empty string denotes the unnamed portal), and
710 the values to use for any parameter placeholders present in the prepared
712 supplied parameter set must match those needed by the prepared statement.
713 (If you declared any <type>void</> parameters in the Parse message,
714 pass NULL values for them in the Bind message.)
715 Bind also specifies the format to use for any data returned
716 by the query; the format can be specified overall, or per-column.
717 The response is either BindComplete or ErrorResponse.
722 The choice between text and binary output is determined by the format
723 codes given in Bind, regardless of the SQL command involved. The
724 <literal>BINARY</> attribute in cursor declarations is irrelevant when
725 using extended query protocol.
730 Query planning typically occurs when the Bind message is processed.
731 If the prepared statement has no parameters, or is executed repeatedly,
732 the server might save the created plan and re-use it during subsequent
733 Bind messages for the same prepared statement. However, it will do so
734 only if it finds that a generic plan can be created that is not much
735 less efficient than a plan that depends on the specific parameter values
736 supplied. This happens transparently so far as the protocol is concerned.
740 If successfully created, a named portal object lasts till the end of the
741 current transaction, unless explicitly destroyed. An unnamed portal is
742 destroyed at the end of the transaction, or as soon as the next Bind
743 statement specifying the unnamed portal as destination is issued. (Note
744 that a simple Query message also destroys the unnamed portal.) Named
745 portals must be explicitly closed before they can be redefined by another
746 Bind message, but this is not required for the unnamed portal.
747 Named portals can also be created and accessed at the SQL
748 command level, using <command>DECLARE CURSOR</> and <command>FETCH</>.
752 Once a portal exists, it can be executed using an Execute message.
753 The Execute message specifies the portal name (empty string denotes the
755 a maximum result-row count (zero meaning <quote>fetch all rows</>).
756 The result-row count is only meaningful for portals
757 containing commands that return row sets; in other cases the command is
758 always executed to completion, and the row count is ignored.
760 responses to Execute are the same as those described above for queries
761 issued via simple query protocol, except that Execute doesn't cause
762 ReadyForQuery or RowDescription to be issued.
766 If Execute terminates before completing the execution of a portal
767 (due to reaching a nonzero result-row count), it will send a
768 PortalSuspended message; the appearance of this message tells the frontend
769 that another Execute should be issued against the same portal to
770 complete the operation. The CommandComplete message indicating
771 completion of the source SQL command is not sent until
772 the portal's execution is completed. Therefore, an Execute phase is
773 always terminated by the appearance of exactly one of these messages:
774 CommandComplete, EmptyQueryResponse (if the portal was created from
775 an empty query string), ErrorResponse, or PortalSuspended.
779 At completion of each series of extended-query messages, the frontend
780 should issue a Sync message. This parameterless message causes the
781 backend to close the current transaction if it's not inside a
782 <command>BEGIN</>/<command>COMMIT</> transaction block (<quote>close</>
783 meaning to commit if no error, or roll back if error). Then a
784 ReadyForQuery response is issued. The purpose of Sync is to provide
785 a resynchronization point for error recovery. When an error is detected
786 while processing any extended-query message, the backend issues
787 ErrorResponse, then reads and discards messages until a Sync is reached,
788 then issues ReadyForQuery and returns to normal message processing.
789 (But note that no skipping occurs if an error is detected
790 <emphasis>while</> processing Sync — this ensures that there is one
791 and only one ReadyForQuery sent for each Sync.)
796 Sync does not cause a transaction block opened with <command>BEGIN</>
797 to be closed. It is possible to detect this situation since the
798 ReadyForQuery message includes transaction status information.
803 In addition to these fundamental, required operations, there are several
804 optional operations that can be used with extended-query protocol.
808 The Describe message (portal variant) specifies the name of an existing
809 portal (or an empty string for the unnamed portal). The response is a
810 RowDescription message describing the rows that will be returned by
811 executing the portal; or a NoData message if the portal does not contain a
812 query that will return rows; or ErrorResponse if there is no such portal.
816 The Describe message (statement variant) specifies the name of an existing
817 prepared statement (or an empty string for the unnamed prepared
818 statement). The response is a ParameterDescription message describing the
819 parameters needed by the statement, followed by a RowDescription message
820 describing the rows that will be returned when the statement is eventually
821 executed (or a NoData message if the statement will not return rows).
822 ErrorResponse is issued if there is no such prepared statement. Note that
823 since Bind has not yet been issued, the formats to be used for returned
824 columns are not yet known to the backend; the format code fields in the
825 RowDescription message will be zeroes in this case.
830 In most scenarios the frontend should issue one or the other variant
831 of Describe before issuing Execute, to ensure that it knows how to
832 interpret the results it will get back.
837 The Close message closes an existing prepared statement or portal
838 and releases resources. It is not an error to issue Close against
839 a nonexistent statement or portal name. The response is normally
840 CloseComplete, but could be ErrorResponse if some difficulty is
841 encountered while releasing resources. Note that closing a prepared
842 statement implicitly closes any open portals that were constructed
847 The Flush message does not cause any specific output to be generated,
848 but forces the backend to deliver any data pending in its output
849 buffers. A Flush must be sent after any extended-query command except
850 Sync, if the frontend wishes to examine the results of that command before
851 issuing more commands. Without Flush, messages returned by the backend
852 will be combined into the minimum possible number of packets to minimize
858 The simple Query message is approximately equivalent to the series Parse,
859 Bind, portal Describe, Execute, Close, Sync, using the unnamed prepared
860 statement and portal objects and no parameters. One difference is that
861 it will accept multiple SQL statements in the query string, automatically
862 performing the bind/describe/execute sequence for each one in succession.
863 Another difference is that it will not return ParseComplete, BindComplete,
864 CloseComplete, or NoData messages.
870 <title>Function Call</title>
873 The Function Call sub-protocol allows the client to request a direct
874 call of any function that exists in the database's
875 <structname>pg_proc</structname> system catalog. The client must have
876 execute permission for the function.
881 The Function Call sub-protocol is a legacy feature that is probably best
882 avoided in new code. Similar results can be accomplished by setting up
883 a prepared statement that does <literal>SELECT function($1, ...)</>.
884 The Function Call cycle can then be replaced with Bind/Execute.
889 A Function Call cycle is initiated by the frontend sending a
890 FunctionCall message to the backend. The backend then sends one
891 or more response messages depending on the results of the function
892 call, and finally a ReadyForQuery response message. ReadyForQuery
893 informs the frontend that it can safely send a new query or
898 The possible response messages from the backend are:
902 <term>ErrorResponse</term>
905 An error has occurred.
911 <term>FunctionCallResponse</term>
914 The function call was completed and returned the result given
916 (Note that the Function Call protocol can only handle a single
917 scalar result, not a row type or set of results.)
923 <term>ReadyForQuery</term>
926 Processing of the function call is complete. ReadyForQuery
927 will always be sent, whether processing terminates
928 successfully or with an error.
934 <term>NoticeResponse</term>
937 A warning message has been issued in relation to the function
938 call. Notices are in addition to other responses, i.e., the
939 backend will continue processing the command.
947 <sect2 id="protocol-copy">
948 <title>COPY Operations</title>
951 The <command>COPY</> command allows high-speed bulk data transfer
952 to or from the server. Copy-in and copy-out operations each switch
953 the connection into a distinct sub-protocol, which lasts until the
954 operation is completed.
958 Copy-in mode (data transfer to the server) is initiated when the
959 backend executes a <command>COPY FROM STDIN</> SQL statement. The backend
960 sends a CopyInResponse message to the frontend. The frontend should
961 then send zero or more CopyData messages, forming a stream of input
962 data. (The message boundaries are not required to have anything to do
963 with row boundaries, although that is often a reasonable choice.)
964 The frontend can terminate the copy-in mode by sending either a CopyDone
965 message (allowing successful termination) or a CopyFail message (which
966 will cause the <command>COPY</> SQL statement to fail with an
967 error). The backend then reverts to the command-processing mode it was
968 in before the <command>COPY</> started, which will be either simple or
969 extended query protocol. It will next send either CommandComplete
970 (if successful) or ErrorResponse (if not).
974 In the event of a backend-detected error during copy-in mode (including
975 receipt of a CopyFail message), the backend will issue an ErrorResponse
976 message. If the <command>COPY</> command was issued via an extended-query
977 message, the backend will now discard frontend messages until a Sync
978 message is received, then it will issue ReadyForQuery and return to normal
979 processing. If the <command>COPY</> command was issued in a simple
980 Query message, the rest of that message is discarded and ReadyForQuery
981 is issued. In either case, any subsequent CopyData, CopyDone, or CopyFail
982 messages issued by the frontend will simply be dropped.
986 The backend will ignore Flush and Sync messages received during copy-in
987 mode. Receipt of any other non-copy message type constitutes an error
988 that will abort the copy-in state as described above. (The exception for
989 Flush and Sync is for the convenience of client libraries that always
990 send Flush or Sync after an Execute message, without checking whether
991 the command to be executed is a <command>COPY FROM STDIN</>.)
995 Copy-out mode (data transfer from the server) is initiated when the
996 backend executes a <command>COPY TO STDOUT</> SQL statement. The backend
997 sends a CopyOutResponse message to the frontend, followed by
998 zero or more CopyData messages (always one per row), followed by CopyDone.
999 The backend then reverts to the command-processing mode it was
1000 in before the <command>COPY</> started, and sends CommandComplete.
1001 The frontend cannot abort the transfer (except by closing the connection
1002 or issuing a Cancel request),
1003 but it can discard unwanted CopyData and CopyDone messages.
1007 In the event of a backend-detected error during copy-out mode,
1008 the backend will issue an ErrorResponse message and revert to normal
1009 processing. The frontend should treat receipt of ErrorResponse as
1010 terminating the copy-out mode.
1014 It is possible for NoticeResponse and ParameterStatus messages to be
1015 interspersed between CopyData messages; frontends must handle these cases,
1016 and should be prepared for other asynchronous message types as well (see
1017 <xref linkend="protocol-async">). Otherwise, any message type other than
1018 CopyData or CopyDone may be treated as terminating copy-out mode.
1022 There is another Copy-related mode called copy-both, which allows
1023 high-speed bulk data transfer to <emphasis>and</> from the server.
1024 Copy-both mode is initiated when a backend in walsender mode
1025 executes a <command>START_REPLICATION</command> statement. The
1026 backend sends a CopyBothResponse message to the frontend. Both
1027 the backend and the frontend may then send CopyData messages
1028 until either end sends a CopyDone message. After the client
1029 sends a CopyDone message, the connection goes from copy-both mode to
1030 copy-out mode, and the client may not send any more CopyData messages.
1031 Similarly, when the server sends a CopyDone message, the connection
1032 goes into copy-in mode, and the server may not send any more CopyData
1033 messages. After both sides have sent a CopyDone message, the copy mode
1034 is terminated, and the backend reverts to the command-processing mode.
1035 In the event of a backend-detected error during copy-both mode,
1036 the backend will issue an ErrorResponse message, discard frontend messages
1037 until a Sync message is received, and then issue ReadyForQuery and return
1038 to normal processing. The frontend should treat receipt of ErrorResponse
1039 as terminating the copy in both directions; no CopyDone should be sent
1040 in this case. See <xref linkend="protocol-replication"> for more
1041 information on the subprotocol transmitted over copy-both mode.
1045 The CopyInResponse, CopyOutResponse and CopyBothResponse messages
1046 include fields that inform the frontend of the number of columns
1047 per row and the format codes being used for each column. (As of
1048 the present implementation, all columns in a given <command>COPY</>
1049 operation will use the same format, but the message design does not
1055 <sect2 id="protocol-async">
1056 <title>Asynchronous Operations</title>
1059 There are several cases in which the backend will send messages that
1060 are not specifically prompted by the frontend's command stream.
1061 Frontends must be prepared to deal with these messages at any time,
1062 even when not engaged in a query.
1063 At minimum, one should check for these cases before beginning to
1064 read a query response.
1068 It is possible for NoticeResponse messages to be generated due to
1069 outside activity; for example, if the database administrator commands
1070 a <quote>fast</> database shutdown, the backend will send a NoticeResponse
1071 indicating this fact before closing the connection. Accordingly,
1072 frontends should always be prepared to accept and display NoticeResponse
1073 messages, even when the connection is nominally idle.
1077 ParameterStatus messages will be generated whenever the active
1078 value changes for any of the parameters the backend believes the
1079 frontend should know about. Most commonly this occurs in response
1080 to a <command>SET</> SQL command executed by the frontend, and
1081 this case is effectively synchronous — but it is also possible
1082 for parameter status changes to occur because the administrator
1083 changed a configuration file and then sent the
1084 <systemitem>SIGHUP</systemitem> signal to the server. Also,
1085 if a <command>SET</command> command is rolled back, an appropriate
1086 ParameterStatus message will be generated to report the current
1091 At present there is a hard-wired set of parameters for which
1092 ParameterStatus will be generated: they are
1093 <varname>server_version</>,
1094 <varname>server_encoding</>,
1095 <varname>client_encoding</>,
1096 <varname>application_name</>,
1097 <varname>is_superuser</>,
1098 <varname>session_authorization</>,
1099 <varname>DateStyle</>,
1100 <varname>IntervalStyle</>,
1101 <varname>TimeZone</>,
1102 <varname>integer_datetimes</>, and
1103 <varname>standard_conforming_strings</>.
1104 (<varname>server_encoding</>, <varname>TimeZone</>, and
1105 <varname>integer_datetimes</> were not reported by releases before 8.0;
1106 <varname>standard_conforming_strings</> was not reported by releases
1108 <varname>IntervalStyle</> was not reported by releases before 8.4;
1109 <varname>application_name</> was not reported by releases before 9.0.)
1111 <varname>server_version</>,
1112 <varname>server_encoding</> and
1113 <varname>integer_datetimes</>
1114 are pseudo-parameters that cannot change after startup.
1115 This set might change in the future, or even become configurable.
1116 Accordingly, a frontend should simply ignore ParameterStatus for
1117 parameters that it does not understand or care about.
1121 If a frontend issues a <command>LISTEN</command> command, then the
1122 backend will send a NotificationResponse message (not to be
1123 confused with NoticeResponse!) whenever a
1124 <command>NOTIFY</command> command is executed for the same
1130 At present, NotificationResponse can only be sent outside a
1131 transaction, and thus it will not occur in the middle of a
1132 command-response series, though it might occur just before ReadyForQuery.
1133 It is unwise to design frontend logic that assumes that, however.
1134 Good practice is to be able to accept NotificationResponse at any
1135 point in the protocol.
1141 <title>Canceling Requests in Progress</title>
1144 During the processing of a query, the frontend might request
1145 cancellation of the query. The cancel request is not sent
1146 directly on the open connection to the backend for reasons of
1147 implementation efficiency: we don't want to have the backend
1148 constantly checking for new input from the frontend during query
1149 processing. Cancel requests should be relatively infrequent, so
1150 we make them slightly cumbersome in order to avoid a penalty in
1155 To issue a cancel request, the frontend opens a new connection to
1156 the server and sends a CancelRequest message, rather than the
1157 StartupMessage message that would ordinarily be sent across a new
1158 connection. The server will process this request and then close
1159 the connection. For security reasons, no direct reply is made to
1160 the cancel request message.
1164 A CancelRequest message will be ignored unless it contains the
1165 same key data (PID and secret key) passed to the frontend during
1166 connection start-up. If the request matches the PID and secret
1167 key for a currently executing backend, the processing of the
1168 current query is aborted. (In the existing implementation, this is
1169 done by sending a special signal to the backend process that is
1170 processing the query.)
1174 The cancellation signal might or might not have any effect — for
1175 example, if it arrives after the backend has finished processing
1176 the query, then it will have no effect. If the cancellation is
1177 effective, it results in the current command being terminated
1178 early with an error message.
1182 The upshot of all this is that for reasons of both security and
1183 efficiency, the frontend has no direct way to tell whether a
1184 cancel request has succeeded. It must continue to wait for the
1185 backend to respond to the query. Issuing a cancel simply improves
1186 the odds that the current query will finish soon, and improves the
1187 odds that it will fail with an error message instead of
1192 Since the cancel request is sent across a new connection to the
1193 server and not across the regular frontend/backend communication
1194 link, it is possible for the cancel request to be issued by any
1195 process, not just the frontend whose query is to be canceled.
1196 This might provide additional flexibility when building
1197 multiple-process applications. It also introduces a security
1198 risk, in that unauthorized persons might try to cancel queries.
1199 The security risk is addressed by requiring a dynamically
1200 generated secret key to be supplied in cancel requests.
1205 <title>Termination</title>
1208 The normal, graceful termination procedure is that the frontend
1209 sends a Terminate message and immediately closes the connection.
1210 On receipt of this message, the backend closes the connection and
1215 In rare cases (such as an administrator-commanded database shutdown)
1216 the backend might disconnect without any frontend request to do so.
1217 In such cases the backend will attempt to send an error or notice message
1218 giving the reason for the disconnection before it closes the connection.
1222 Other termination scenarios arise from various failure cases, such as core
1223 dump at one end or the other, loss of the communications link, loss of
1224 message-boundary synchronization, etc. If either frontend or backend sees
1225 an unexpected closure of the connection, it should clean
1226 up and terminate. The frontend has the option of launching a new backend
1227 by recontacting the server if it doesn't want to terminate itself.
1228 Closing the connection is also advisable if an unrecognizable message type
1229 is received, since this probably indicates loss of message-boundary sync.
1233 For either normal or abnormal termination, any open transaction is
1234 rolled back, not committed. One should note however that if a
1235 frontend disconnects while a non-<command>SELECT</command> query
1236 is being processed, the backend will probably finish the query
1237 before noticing the disconnection. If the query is outside any
1238 transaction block (<command>BEGIN</> ... <command>COMMIT</>
1239 sequence) then its results might be committed before the
1240 disconnection is recognized.
1245 <title><acronym>SSL</acronym> Session Encryption</title>
1248 If <productname>PostgreSQL</> was built with
1249 <acronym>SSL</acronym> support, frontend/backend communications
1250 can be encrypted using <acronym>SSL</acronym>. This provides
1251 communication security in environments where attackers might be
1252 able to capture the session traffic. For more information on
1253 encrypting <productname>PostgreSQL</productname> sessions with
1254 <acronym>SSL</acronym>, see <xref linkend="ssl-tcp">.
1258 To initiate an <acronym>SSL</acronym>-encrypted connection, the
1259 frontend initially sends an SSLRequest message rather than a
1260 StartupMessage. The server then responds with a single byte
1261 containing <literal>S</> or <literal>N</>, indicating that it is
1262 willing or unwilling to perform <acronym>SSL</acronym>,
1263 respectively. The frontend might close the connection at this point
1264 if it is dissatisfied with the response. To continue after
1265 <literal>S</>, perform an <acronym>SSL</acronym> startup handshake
1266 (not described here, part of the <acronym>SSL</acronym>
1267 specification) with the server. If this is successful, continue
1268 with sending the usual StartupMessage. In this case the
1269 StartupMessage and all subsequent data will be
1270 <acronym>SSL</acronym>-encrypted. To continue after
1271 <literal>N</>, send the usual StartupMessage and proceed without
1276 The frontend should also be prepared to handle an ErrorMessage
1277 response to SSLRequest from the server. This would only occur if
1278 the server predates the addition of <acronym>SSL</acronym> support
1279 to <productname>PostgreSQL</>. (Such servers are now very ancient,
1280 and likely do not exist in the wild anymore.)
1281 In this case the connection must
1282 be closed, but the frontend might choose to open a fresh connection
1283 and proceed without requesting <acronym>SSL</acronym>.
1287 An initial SSLRequest can also be used in a connection that is being
1288 opened to send a CancelRequest message.
1292 While the protocol itself does not provide a way for the server to
1293 force <acronym>SSL</acronym> encryption, the administrator can
1294 configure the server to reject unencrypted sessions as a byproduct
1295 of authentication checking.
1300 <sect1 id="protocol-replication">
1301 <title>Streaming Replication Protocol</title>
1304 To initiate streaming replication, the frontend sends the
1305 <literal>replication</> parameter in the startup message. A boolean value
1306 of <literal>true</> tells the backend to go into walsender mode, wherein a
1307 small set of replication commands can be issued instead of SQL statements. Only
1308 the simple query protocol can be used in walsender mode.
1309 Passing <literal>database</> as the value instructs walsender to connect to
1310 the database specified in the <literal>dbname</> parameter, which will allow
1311 the connection to be used for logical replication from that database.
1314 For the purpose of testing replication commands, you can make a replication
1315 connection via <application>psql</application> or any other <literal>libpq</literal>-using
1316 tool with a connection string including the <literal>replication</literal> option,
1319 psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
1321 However it is often more useful to use
1322 <application>pg_receivexlog</application> (for physical replication) or
1323 <application>pg_recvlogical</application> (for logical replication).
1327 The commands accepted in walsender mode are:
1330 <term>IDENTIFY_SYSTEM</term>
1333 Requests the server to identify itself. Server replies with a result
1334 set of a single row, containing four fields:
1345 The unique system identifier identifying the cluster. This
1346 can be used to check that the base backup used to initialize the
1347 standby came from the same cluster.
1358 Current TimelineID. Also useful to check that the standby is
1359 consistent with the master.
1370 Current xlog write location. Useful to get a known location in the
1371 transaction log where streaming can start.
1382 Database connected to or NULL.
1393 <term>TIMELINE_HISTORY <replaceable class="parameter">tli</replaceable></term>
1396 Requests the server to send over the timeline history file for timeline
1397 <replaceable class="parameter">tli</replaceable>. Server replies with a
1398 result set of a single row, containing two fields:
1409 Filename of the timeline history file, e.g 00000002.history.
1420 Contents of the timeline history file.
1431 <term>CREATE_REPLICATION_SLOT <replaceable class="parameter">slotname</> { <literal>PHYSICAL</> | <literal>LOGICAL</> <replaceable class="parameter">output_plugin</> } <indexterm><primary>CREATE_REPLICATION_SLOT</primary></indexterm></term>
1434 Create a physical or logical replication
1435 slot. See <xref linkend="streaming-replication-slots"> for more about
1440 <term><replaceable class="parameter">slotname</></term>
1443 The name of the slot to create. Must be a valid replication slot
1444 name (see <xref linkend="streaming-replication-slots-manipulation">).
1450 <term><replaceable class="parameter">output_plugin</></term>
1453 The name of the output plugin used for logical decoding
1454 (see <xref linkend="logicaldecoding-output-plugin">).
1463 <term>START_REPLICATION [<literal>SLOT</literal> <replaceable class="parameter">slotname</>] [<literal>PHYSICAL</literal>] <replaceable class="parameter">XXX/XXX</> [<literal>TIMELINE</literal> <replaceable class="parameter">tli</>]</term>
1466 Instructs server to start streaming WAL, starting at
1467 WAL position <replaceable class="parameter">XXX/XXX</>.
1468 If <literal>TIMELINE</literal> option is specified,
1469 streaming starts on timeline <replaceable class="parameter">tli</>;
1470 otherwise, the server's current timeline is selected. The server can
1471 reply with an error, e.g. if the requested section of WAL has already
1472 been recycled. On success, server responds with a CopyBothResponse
1473 message, and then starts to stream WAL to the frontend.
1477 If the client requests a timeline that's not the latest, but is part of
1478 the history of the server, the server will stream all the WAL on that
1479 timeline starting from the requested startpoint, up to the point where
1480 the server switched to another timeline. If the client requests
1481 streaming at exactly the end of an old timeline, the server responds
1482 immediately with CommandComplete without entering COPY mode.
1486 After streaming all the WAL on a timeline that is not the latest one,
1487 the server will end streaming by exiting the COPY mode. When the client
1488 acknowledges this by also exiting COPY mode, the server sends a result
1489 set with one row and two columns, indicating the next timeline in this
1490 server's history. The first column is the next timeline's ID, and the
1491 second column is the XLOG position where the switch happened. Usually,
1492 the switch position is the end of the WAL that was streamed, but there
1493 are corner cases where the server can send some WAL from the old
1494 timeline that it has not itself replayed before promoting. Finally, the
1495 server sends CommandComplete message, and is ready to accept a new
1500 WAL data is sent as a series of CopyData messages. (This allows
1501 other information to be intermixed; in particular the server can send
1502 an ErrorResponse message if it encounters a failure after beginning
1503 to stream.) The payload of each CopyData message from server to the
1504 client contains a message of one of the following formats:
1508 If a slot's name is provided
1509 via <replaceable class="parameter">slotname</>, it will be updated
1510 as replication progresses so that the server knows which WAL segments -
1511 and if <varname>hot_standby_feedback</> is on which transactions -
1512 are still needed by the standby.
1530 Identifies the message as WAL data.
1540 The starting point of the WAL data in this message.
1550 The current end of WAL on the server.
1560 The server's system clock at the time of transmission, as
1561 microseconds since midnight on 2000-01-01.
1567 Byte<replaceable>n</replaceable>
1571 A section of the WAL data stream.
1574 A single WAL record is never split across two XLogData messages.
1575 When a WAL record crosses a WAL page boundary, and is therefore
1576 already split using continuation records, it can be split at the page
1577 boundary. In other words, the first main WAL record and its
1578 continuation records can be sent in different XLogData messages.
1588 Primary keepalive message (B)
1599 Identifies the message as a sender keepalive.
1609 The current end of WAL on the server.
1619 The server's system clock at the time of transmission, as
1620 microseconds since midnight on 2000-01-01.
1630 1 means that the client should reply to this message as soon as
1631 possible, to avoid a timeout disconnect. 0 otherwise.
1643 The receiving process can send replies back to the sender at any time,
1644 using one of the following message formats (also in the payload of a
1652 Standby status update (F)
1663 Identifies the message as a receiver status update.
1673 The location of the last WAL byte + 1 received and written to disk
1684 The location of the last WAL byte + 1 flushed to disk in
1695 The location of the last WAL byte + 1 applied in the standby.
1705 The client's system clock at the time of transmission, as
1706 microseconds since midnight on 2000-01-01.
1716 If 1, the client requests the server to reply to this message
1717 immediately. This can be used to ping the server, to test if
1718 the connection is still healthy.
1733 Hot Standby feedback message (F)
1744 Identifies the message as a Hot Standby feedback message.
1754 The client's system clock at the time of transmission, as
1755 microseconds since midnight on 2000-01-01.
1765 The standby's current xmin. This may be 0, if the standby is
1766 sending notification that Hot Standby feedback will no longer
1767 be sent on this connection. Later non-zero messages may
1768 reinitiate the feedback mechanism.
1778 The standby's current epoch.
1791 <term>START_REPLICATION <literal>SLOT</literal> <replaceable class="parameter">slotname</> <literal>LOGICAL</literal> <replaceable class="parameter">XXX/XXX</> [ ( <replaceable>option_name</replaceable> [<replaceable>option_value</replaceable>] [, ... ] ) ]</term>
1794 Instructs server to start streaming WAL for logical replication, starting
1795 at WAL position <replaceable class="parameter">XXX/XXX</>. The server can
1796 reply with an error, e.g. if the requested section of WAL has already
1797 been recycled. On success, server responds with a CopyBothResponse
1798 message, and then starts to stream WAL to the frontend.
1802 The messages inside the CopyBothResponse messages are of the same format
1803 documented for <literal>START_REPLICATION ... PHYSICAL</literal>.
1807 The output plugin associated with the selected slot is used
1808 to process the output for streaming.
1813 <term><literal>SLOT</literal> <replaceable class="parameter">slotname</></term>
1816 The name of the slot to stream changes from. This parameter is required,
1817 and must correspond to an existing logical replication slot created
1818 with <literal>CREATE_REPLICATION_SLOT</literal> in
1819 <literal>LOGICAL</literal> mode.
1824 <term><replaceable class="parameter">XXX/XXX</></term>
1827 The WAL position to begin streaming at.
1832 <term><replaceable class="parameter">option_name</></term>
1835 The name of an option passed to the slot's logical decoding plugin.
1840 <term><replaceable class="parameter">option_value</></term>
1843 Optional value, in the form of a string constant, associated with the
1853 <term>DROP_REPLICATION_SLOT <replaceable class="parameter">slotname</></term>
1856 Drops a replication slot, freeing any reserved server-side resources. If
1857 the slot is currently in use by an active connection, this command fails.
1861 <term><replaceable class="parameter">slotname</></term>
1864 The name of the slot to drop.
1873 <term>BASE_BACKUP [<literal>LABEL</literal> <replaceable>'label'</replaceable>] [<literal>PROGRESS</literal>] [<literal>FAST</literal>] [<literal>WAL</literal>] [<literal>NOWAIT</literal>] [<literal>MAX_RATE</literal> <replaceable>rate</replaceable>]</term>
1876 Instructs the server to start streaming a base backup.
1877 The system will automatically be put in backup mode before the backup
1878 is started, and taken out of it when the backup is complete. The
1879 following options are accepted:
1882 <term><literal>LABEL</literal> <replaceable>'label'</replaceable></term>
1885 Sets the label of the backup. If none is specified, a backup label
1886 of <literal>base backup</literal> will be used. The quoting rules
1887 for the label are the same as a standard SQL string with
1888 <xref linkend="guc-standard-conforming-strings"> turned on.
1894 <term><literal>PROGRESS</></term>
1897 Request information required to generate a progress report. This will
1898 send back an approximate size in the header of each tablespace, which
1899 can be used to calculate how far along the stream is done. This is
1900 calculated by enumerating all the file sizes once before the transfer
1901 is even started, and may as such have a negative impact on the
1902 performance - in particular it may take longer before the first data
1903 is streamed. Since the database files can change during the backup,
1904 the size is only approximate and may both grow and shrink between
1905 the time of approximation and the sending of the actual files.
1911 <term><literal>FAST</></term>
1914 Request a fast checkpoint.
1920 <term><literal>WAL</literal></term>
1923 Include the necessary WAL segments in the backup. This will include
1924 all the files between start and stop backup in the
1925 <filename>pg_xlog</filename> directory of the base directory tar
1932 <term><literal>NOWAIT</literal></term>
1935 By default, the backup will wait until the last required xlog
1936 segment has been archived, or emit a warning if log archiving is
1937 not enabled. Specifying <literal>NOWAIT</literal> disables both
1938 the waiting and the warning, leaving the client responsible for
1939 ensuring the required log is available.
1945 <term><literal>MAX_RATE</literal> <replaceable>rate</></term>
1948 Limit (throttle) the maximum amount of data transferred from server
1949 to client per unit of time. The expected unit is kilobytes per second.
1950 If this option is specified, the value must either be equal to zero
1951 or it must fall within the range from 32 kB through 1 GB (inclusive).
1952 If zero is passed or the option is not specified, no restriction is
1953 imposed on the transfer.
1960 When the backup is started, the server will first send two
1961 ordinary result sets, followed by one or more CopyResponse
1965 The first ordinary result set contains the starting position of the
1966 backup, in a single row with two columns. The first column contains
1967 the start position given in XLogRecPtr format, and the second column
1968 contains the corresponding timeline ID.
1971 The second ordinary result set has one row for each tablespace.
1972 The fields in this row are:
1978 The oid of the tablespace, or <literal>NULL</> if it's the base
1984 <term>spclocation</term>
1987 The full path of the tablespace directory, or <literal>NULL</>
1988 if it's the base directory.
1996 The approximate size of the tablespace, if progress report has
1997 been requested; otherwise it's <literal>NULL</>.
2004 After the second regular result set, one or more CopyResponse results
2005 will be sent, one for PGDATA and one for each additional tablespace other
2006 than <literal>pg_default</> and <literal>pg_global</>. The data in
2007 the CopyResponse results will be a tar format (following the
2008 <quote>ustar interchange format</> specified in the POSIX 1003.1-2008
2009 standard) dump of the tablespace contents, except that the two trailing
2010 blocks of zeroes specified in the standard are omitted.
2011 After the tar data is complete, a final ordinary result set will be sent,
2012 containing the WAL end position of the backup, in the same format as
2017 The tar archive for the data directory and each tablespace will contain
2018 all files in the directories, regardless of whether they are
2019 <productname>PostgreSQL</> files or other files added to the same
2020 directory. The only excluded files are:
2021 <itemizedlist spacing="compact" mark="bullet">
2024 <filename>postmaster.pid</>
2029 <filename>postmaster.opts</>
2034 various temporary files created during the operation of the PostgreSQL server
2039 <filename>pg_xlog</>, including subdirectories. If the backup is run
2040 with WAL files included, a synthesized version of <filename>pg_xlog</filename> will be
2041 included, but it will only contain the files necessary for the
2042 backup to work, not the rest of the contents.
2047 <filename>pg_replslot</> is copied as an empty directory.
2052 Files other than regular files and directories, such as symbolic
2053 links and special device files, are skipped. (Symbolic links
2054 in <filename>pg_tblspc</filename> are maintained.)
2058 Owner, group and file mode are set if the underlying file system on
2059 the server supports it.
2062 Once all tablespaces have been sent, a final regular result set will
2063 be sent. This result set contains the end position of the
2064 backup, given in XLogRecPtr format as a single column in a single row.
2074 <sect1 id="protocol-message-types">
2075 <title>Message Data Types</title>
2078 This section describes the base data types used in messages.
2084 Int<replaceable>n</replaceable>(<replaceable>i</replaceable>)
2088 An <replaceable>n</replaceable>-bit integer in network byte
2089 order (most significant byte first).
2090 If <replaceable>i</replaceable> is specified it
2091 is the exact value that will appear, otherwise the value
2092 is variable. Eg. Int16, Int32(42).
2099 Int<replaceable>n</replaceable>[<replaceable>k</replaceable>]
2103 An array of <replaceable>k</replaceable>
2104 <replaceable>n</replaceable>-bit integers, each in network
2105 byte order. The array length <replaceable>k</replaceable>
2106 is always determined by an earlier field in the message.
2114 String(<replaceable>s</replaceable>)
2118 A null-terminated string (C-style string). There is no
2119 specific length limitation on strings.
2120 If <replaceable>s</replaceable> is specified it is the exact
2121 value that will appear, otherwise the value is variable.
2122 Eg. String, String("user").
2127 <emphasis>There is no predefined limit</emphasis> on the length of a string
2128 that can be returned by the backend. Good coding strategy for a frontend
2129 is to use an expandable buffer so that anything that fits in memory can be
2130 accepted. If that's not feasible, read the full string and discard trailing
2131 characters that don't fit into your fixed-size buffer.
2139 Byte<replaceable>n</replaceable>(<replaceable>c</replaceable>)
2143 Exactly <replaceable>n</replaceable> bytes. If the field
2144 width <replaceable>n</replaceable> is not a constant, it is
2145 always determinable from an earlier field in the message.
2146 If <replaceable>c</replaceable> is specified it is the exact
2147 value. Eg. Byte2, Byte1('\n').
2156 <sect1 id="protocol-message-formats">
2157 <title>Message Formats</title>
2160 This section describes the detailed format of each message. Each is marked to
2161 indicate that it can be sent by a frontend (F), a backend (B), or both
2163 Notice that although each message includes a byte count at the beginning,
2164 the message format is defined so that the message end can be found without
2165 reference to the byte count. This aids validity checking. (The CopyData
2166 message is an exception, because it forms part of a data stream; the contents
2167 of any individual CopyData message cannot be interpretable on their own.)
2175 AuthenticationOk (B)
2187 Identifies the message as an authentication request.
2197 Length of message contents in bytes, including self.
2207 Specifies that the authentication was successful.
2220 AuthenticationKerberosV5 (B)
2232 Identifies the message as an authentication request.
2242 Length of message contents in bytes, including self.
2252 Specifies that Kerberos V5 authentication is required.
2264 AuthenticationCleartextPassword (B)
2276 Identifies the message as an authentication request.
2286 Length of message contents in bytes, including self.
2296 Specifies that a clear-text password is required.
2308 AuthenticationMD5Password (B)
2320 Identifies the message as an authentication request.
2330 Length of message contents in bytes, including self.
2340 Specifies that an MD5-encrypted password is required.
2350 The salt to use when encrypting the password.
2363 AuthenticationSCMCredential (B)
2375 Identifies the message as an authentication request.
2385 Length of message contents in bytes, including self.
2395 Specifies that an SCM credentials message is required.
2408 AuthenticationGSS (B)
2420 Identifies the message as an authentication request.
2430 Length of message contents in bytes, including self.
2440 Specifies that GSSAPI authentication is required.
2453 AuthenticationSSPI (B)
2465 Identifies the message as an authentication request.
2475 Length of message contents in bytes, including self.
2485 Specifies that SSPI authentication is required.
2496 AuthenticationGSSContinue (B)
2508 Identifies the message as an authentication request.
2518 Length of message contents in bytes, including self.
2528 Specifies that this message contains GSSAPI or SSPI data.
2534 Byte<replaceable>n</replaceable>
2538 GSSAPI or SSPI authentication data.
2563 Identifies the message as cancellation key data.
2564 The frontend must save these values if it wishes to be
2565 able to issue CancelRequest messages later.
2575 Length of message contents in bytes, including self.
2585 The process ID of this backend.
2595 The secret key of this backend.
2620 Identifies the message as a Bind command.
2630 Length of message contents in bytes, including self.
2640 The name of the destination portal
2641 (an empty string selects the unnamed portal).
2651 The name of the source prepared statement
2652 (an empty string selects the unnamed prepared statement).
2662 The number of parameter format codes that follow
2663 (denoted <replaceable>C</> below).
2664 This can be zero to indicate that there are no parameters
2665 or that the parameters all use the default format (text);
2666 or one, in which case the specified format code is applied
2667 to all parameters; or it can equal the actual number of
2674 Int16[<replaceable>C</>]
2678 The parameter format codes. Each must presently be
2679 zero (text) or one (binary).
2689 The number of parameter values that follow (possibly zero).
2690 This must match the number of parameters needed by the query.
2695 Next, the following pair of fields appear for each parameter:
2703 The length of the parameter value, in bytes (this count
2704 does not include itself). Can be zero.
2705 As a special case, -1 indicates a NULL parameter value.
2706 No value bytes follow in the NULL case.
2712 Byte<replaceable>n</replaceable>
2716 The value of the parameter, in the format indicated by the
2717 associated format code.
2718 <replaceable>n</replaceable> is the above length.
2723 After the last parameter, the following fields appear:
2731 The number of result-column format codes that follow
2732 (denoted <replaceable>R</> below).
2733 This can be zero to indicate that there are no result columns
2734 or that the result columns should all use the default format
2736 or one, in which case the specified format code is applied
2737 to all result columns (if any); or it can equal the actual
2738 number of result columns of the query.
2744 Int16[<replaceable>R</>]
2748 The result-column format codes. Each must presently be
2749 zero (text) or one (binary).
2773 Identifies the message as a Bind-complete indicator.
2783 Length of message contents in bytes, including self.
2808 Length of message contents in bytes, including self.
2818 The cancel request code. The value is chosen to contain
2819 <literal>1234</> in the most significant 16 bits, and <literal>5678</> in the
2820 least 16 significant bits. (To avoid confusion, this code
2821 must not be the same as any protocol version number.)
2831 The process ID of the target backend.
2841 The secret key for the target backend.
2866 Identifies the message as a Close command.
2876 Length of message contents in bytes, including self.
2886 '<literal>S</>' to close a prepared statement; or
2887 '<literal>P</>' to close a portal.
2897 The name of the prepared statement or portal to close
2898 (an empty string selects the unnamed prepared statement
2923 Identifies the message as a Close-complete indicator.
2933 Length of message contents in bytes, including self.
2958 Identifies the message as a command-completed response.
2968 Length of message contents in bytes, including self.
2978 The command tag. This is usually a single
2979 word that identifies which SQL command was completed.
2983 For an <command>INSERT</command> command, the tag is
2984 <literal>INSERT <replaceable>oid</replaceable>
2985 <replaceable>rows</replaceable></literal>, where
2986 <replaceable>rows</replaceable> is the number of rows
2987 inserted. <replaceable>oid</replaceable> is the object ID
2988 of the inserted row if <replaceable>rows</replaceable> is 1
2989 and the target table has OIDs;
2990 otherwise <replaceable>oid</replaceable> is 0.
2994 For a <command>DELETE</command> command, the tag is
2995 <literal>DELETE <replaceable>rows</replaceable></literal> where
2996 <replaceable>rows</replaceable> is the number of rows deleted.
3000 For an <command>UPDATE</command> command, the tag is
3001 <literal>UPDATE <replaceable>rows</replaceable></literal> where
3002 <replaceable>rows</replaceable> is the number of rows updated.
3006 For a <command>SELECT</command> or <command>CREATE TABLE AS</command>
3007 command, the tag is <literal>SELECT <replaceable>rows</replaceable></literal>
3008 where <replaceable>rows</replaceable> is the number of rows retrieved.
3012 For a <command>MOVE</command> command, the tag is
3013 <literal>MOVE <replaceable>rows</replaceable></literal> where
3014 <replaceable>rows</replaceable> is the number of rows the
3015 cursor's position has been changed by.
3019 For a <command>FETCH</command> command, the tag is
3020 <literal>FETCH <replaceable>rows</replaceable></literal> where
3021 <replaceable>rows</replaceable> is the number of rows that
3022 have been retrieved from the cursor.
3026 For a <command>COPY</command> command, the tag is
3027 <literal>COPY <replaceable>rows</replaceable></literal> where
3028 <replaceable>rows</replaceable> is the number of rows copied.
3029 (Note: the row count appears only in
3030 <productname>PostgreSQL</productname> 8.2 and later.)
3044 CopyData (F & B)
3055 Identifies the message as <command>COPY</command> data.
3065 Length of message contents in bytes, including self.
3071 Byte<replaceable>n</replaceable>
3075 Data that forms part of a <command>COPY</command> data stream. Messages sent
3076 from the backend will always correspond to single data rows,
3077 but messages sent by frontends might divide the data stream
3090 CopyDone (F & B)
3102 Identifies the message as a <command>COPY</command>-complete indicator.
3112 Length of message contents in bytes, including self.
3137 Identifies the message as a <command>COPY</command>-failure indicator.
3147 Length of message contents in bytes, including self.
3157 An error message to report as the cause of failure.
3182 Identifies the message as a Start Copy In response.
3183 The frontend must now send copy-in data (if not
3184 prepared to do so, send a CopyFail message).
3194 Length of message contents in bytes, including self.
3204 0 indicates the overall <command>COPY</command> format is textual (rows
3205 separated by newlines, columns separated by separator
3207 1 indicates the overall copy format is binary (similar
3209 See <xref linkend="sql-copy">
3210 for more information.
3220 The number of columns in the data to be copied
3221 (denoted <replaceable>N</> below).
3227 Int16[<replaceable>N</>]
3231 The format codes to be used for each column.
3232 Each must presently be zero (text) or one (binary).
3233 All must be zero if the overall copy format is textual.
3258 Identifies the message as a Start Copy Out response.
3259 This message will be followed by copy-out data.
3269 Length of message contents in bytes, including self.
3279 0 indicates the overall <command>COPY</command> format
3280 is textual (rows separated by newlines, columns
3281 separated by separator characters, etc). 1 indicates
3282 the overall copy format is binary (similar to DataRow
3283 format). See <xref linkend="sql-copy"> for more information.
3293 The number of columns in the data to be copied
3294 (denoted <replaceable>N</> below).
3300 Int16[<replaceable>N</>]
3304 The format codes to be used for each column.
3305 Each must presently be zero (text) or one (binary).
3306 All must be zero if the overall copy format is textual.
3319 CopyBothResponse (B)
3331 Identifies the message as a Start Copy Both response.
3332 This message is used only for Streaming Replication.
3342 Length of message contents in bytes, including self.
3352 0 indicates the overall <command>COPY</command> format
3353 is textual (rows separated by newlines, columns
3354 separated by separator characters, etc). 1 indicates
3355 the overall copy format is binary (similar to DataRow
3356 format). See <xref linkend="sql-copy"> for more information.
3366 The number of columns in the data to be copied
3367 (denoted <replaceable>N</> below).
3373 Int16[<replaceable>N</>]
3377 The format codes to be used for each column.
3378 Each must presently be zero (text) or one (binary).
3379 All must be zero if the overall copy format is textual.
3403 Identifies the message as a data row.
3413 Length of message contents in bytes, including self.
3423 The number of column values that follow (possibly zero).
3428 Next, the following pair of fields appear for each column:
3436 The length of the column value, in bytes (this count
3437 does not include itself). Can be zero.
3438 As a special case, -1 indicates a NULL column value.
3439 No value bytes follow in the NULL case.
3445 Byte<replaceable>n</replaceable>
3449 The value of the column, in the format indicated by the
3450 associated format code.
3451 <replaceable>n</replaceable> is the above length.
3476 Identifies the message as a Describe command.
3486 Length of message contents in bytes, including self.
3496 '<literal>S</>' to describe a prepared statement; or
3497 '<literal>P</>' to describe a portal.
3507 The name of the prepared statement or portal to describe
3508 (an empty string selects the unnamed prepared statement
3521 EmptyQueryResponse (B)
3533 Identifies the message as a response to an empty query string.
3534 (This substitutes for CommandComplete.)
3544 Length of message contents in bytes, including self.
3569 Identifies the message as an error.
3579 Length of message contents in bytes, including self.
3584 The message body consists of one or more identified fields,
3585 followed by a zero byte as a terminator. Fields can appear in
3586 any order. For each field there is the following:
3594 A code identifying the field type; if zero, this is
3595 the message terminator and no string follows.
3596 The presently defined field types are listed in
3597 <xref linkend="protocol-error-fields">.
3598 Since more field types might be added in future,
3599 frontends should silently ignore fields of unrecognized
3635 Identifies the message as an Execute command.
3645 Length of message contents in bytes, including self.
3655 The name of the portal to execute
3656 (an empty string selects the unnamed portal).
3666 Maximum number of rows to return, if portal contains
3667 a query that returns rows (ignored otherwise). Zero
3668 denotes <quote>no limit</>.
3692 Identifies the message as a Flush command.
3702 Length of message contents in bytes, including self.
3727 Identifies the message as a function call.
3737 Length of message contents in bytes, including self.
3747 Specifies the object ID of the function to call.
3757 The number of argument format codes that follow
3758 (denoted <replaceable>C</> below).
3759 This can be zero to indicate that there are no arguments
3760 or that the arguments all use the default format (text);
3761 or one, in which case the specified format code is applied
3762 to all arguments; or it can equal the actual number of
3769 Int16[<replaceable>C</>]
3773 The argument format codes. Each must presently be
3774 zero (text) or one (binary).
3784 Specifies the number of arguments being supplied to the
3790 Next, the following pair of fields appear for each argument:
3798 The length of the argument value, in bytes (this count
3799 does not include itself). Can be zero.
3800 As a special case, -1 indicates a NULL argument value.
3801 No value bytes follow in the NULL case.
3807 Byte<replaceable>n</replaceable>
3811 The value of the argument, in the format indicated by the
3812 associated format code.
3813 <replaceable>n</replaceable> is the above length.
3818 After the last argument, the following field appears:
3826 The format code for the function result. Must presently be
3827 zero (text) or one (binary).
3840 FunctionCallResponse (B)
3852 Identifies the message as a function call result.
3862 Length of message contents in bytes, including self.
3872 The length of the function result value, in bytes (this count
3873 does not include itself). Can be zero.
3874 As a special case, -1 indicates a NULL function result.
3875 No value bytes follow in the NULL case.
3881 Byte<replaceable>n</replaceable>
3885 The value of the function result, in the format indicated by
3886 the associated format code.
3887 <replaceable>n</replaceable> is the above length.
3912 Identifies the message as a no-data indicator.
3922 Length of message contents in bytes, including self.
3947 Identifies the message as a notice.
3957 Length of message contents in bytes, including self.
3962 The message body consists of one or more identified fields,
3963 followed by a zero byte as a terminator. Fields can appear in
3964 any order. For each field there is the following:
3972 A code identifying the field type; if zero, this is
3973 the message terminator and no string follows.
3974 The presently defined field types are listed in
3975 <xref linkend="protocol-error-fields">.
3976 Since more field types might be added in future,
3977 frontends should silently ignore fields of unrecognized
4001 NotificationResponse (B)
4013 Identifies the message as a notification response.
4023 Length of message contents in bytes, including self.
4033 The process ID of the notifying backend process.
4043 The name of the channel that the notify has been raised on.
4053 The <quote>payload</> string passed from the notifying process.
4066 ParameterDescription (B)
4078 Identifies the message as a parameter description.
4088 Length of message contents in bytes, including self.
4098 The number of parameters used by the statement
4104 Then, for each parameter, there is the following:
4112 Specifies the object ID of the parameter data type.
4136 Identifies the message as a run-time parameter status report.
4146 Length of message contents in bytes, including self.
4156 The name of the run-time parameter being reported.
4166 The current value of the parameter.
4190 Identifies the message as a Parse command.
4200 Length of message contents in bytes, including self.
4210 The name of the destination prepared statement
4211 (an empty string selects the unnamed prepared statement).
4221 The query string to be parsed.
4231 The number of parameter data types specified
4232 (can be zero). Note that this is not an indication of
4233 the number of parameters that might appear in the
4234 query string, only the number that the frontend wants to
4235 prespecify types for.
4240 Then, for each parameter, there is the following:
4248 Specifies the object ID of the parameter data type.
4249 Placing a zero here is equivalent to leaving the type
4274 Identifies the message as a Parse-complete indicator.
4284 Length of message contents in bytes, including self.
4309 Identifies the message as a password response. Note that
4310 this is also used for GSSAPI and SSPI response messages
4311 (which is really a design error, since the contained data
4312 is not a null-terminated string in that case, but can be
4313 arbitrary binary data).
4323 Length of message contents in bytes, including self.
4333 The password (encrypted, if requested).
4357 Identifies the message as a portal-suspended indicator.
4358 Note this only appears if an Execute message's row-count limit
4369 Length of message contents in bytes, including self.
4394 Identifies the message as a simple query.
4404 Length of message contents in bytes, including self.
4414 The query string itself.
4439 Identifies the message type. ReadyForQuery is sent
4440 whenever the backend is ready for a new query cycle.
4450 Length of message contents in bytes, including self.
4460 Current backend transaction status indicator.
4461 Possible values are '<literal>I</>' if idle (not in
4462 a transaction block); '<literal>T</>' if in a transaction
4463 block; or '<literal>E</>' if in a failed transaction
4464 block (queries will be rejected until block is ended).
4489 Identifies the message as a row description.
4499 Length of message contents in bytes, including self.
4509 Specifies the number of fields in a row (can be zero).
4514 Then, for each field, there is the following:
4532 If the field can be identified as a column of a specific
4533 table, the object ID of the table; otherwise zero.
4543 If the field can be identified as a column of a specific
4544 table, the attribute number of the column; otherwise zero.
4554 The object ID of the field's data type.
4564 The data type size (see <varname>pg_type.typlen</>).
4565 Note that negative values denote variable-width types.
4575 The type modifier (see <varname>pg_attribute.atttypmod</>).
4576 The meaning of the modifier is type-specific.
4586 The format code being used for the field. Currently will
4587 be zero (text) or one (binary). In a RowDescription
4588 returned from the statement variant of Describe, the
4589 format code is not yet known and will always be zero.
4614 Length of message contents in bytes, including self.
4624 The <acronym>SSL</acronym> request code. The value is chosen to contain
4625 <literal>1234</> in the most significant 16 bits, and <literal>5679</> in the
4626 least 16 significant bits. (To avoid confusion, this code
4627 must not be the same as any protocol version number.)
4652 Length of message contents in bytes, including self.
4662 The protocol version number. The most significant 16 bits are
4663 the major version number (3 for the protocol described here).
4664 The least significant 16 bits are the minor version number
4665 (0 for the protocol described here).
4670 The protocol version number is followed by one or more pairs of
4671 parameter name and value strings. A zero byte is required as a
4672 terminator after the last name/value pair.
4673 Parameters can appear in any
4674 order. <literal>user</> is required, others are optional.
4675 Each parameter is specified as:
4683 The parameter name. Currently recognized names are:
4692 The database user name to connect as. Required;
4693 there is no default.
4699 <literal>database</>
4703 The database to connect to. Defaults to the user name.
4713 Command-line arguments for the backend. (This is
4714 deprecated in favor of setting individual run-time
4721 In addition to the above, any run-time parameter that can be
4722 set at backend start time might be listed. Such settings
4723 will be applied during backend start (after parsing the
4724 command-line options if any). The values will act as
4735 The parameter value.
4760 Identifies the message as a Sync command.
4770 Length of message contents in bytes, including self.
4795 Identifies the message as a termination.
4805 Length of message contents in bytes, including self.
4821 <sect1 id="protocol-error-fields">
4822 <title>Error and Notice Message Fields</title>
4825 This section describes the fields that can appear in ErrorResponse and
4826 NoticeResponse messages. Each field type has a single-byte identification
4827 token. Note that any given field type should appear at most once per
4839 Severity: the field contents are
4840 <literal>ERROR</>, <literal>FATAL</>, or
4841 <literal>PANIC</> (in an error message), or
4842 <literal>WARNING</>, <literal>NOTICE</>, <literal>DEBUG</>,
4843 <literal>INFO</>, or <literal>LOG</> (in a notice message),
4844 or a localized translation of one of these. Always present.
4855 Code: the SQLSTATE code for the error (see <xref
4856 linkend="errcodes-appendix">). Not localizable. Always present.
4867 Message: the primary human-readable error message.
4868 This should be accurate but terse (typically one line).
4880 Detail: an optional secondary error message carrying more
4881 detail about the problem. Might run to multiple lines.
4892 Hint: an optional suggestion what to do about the problem.
4893 This is intended to differ from Detail in that it offers advice
4894 (potentially inappropriate) rather than hard facts.
4895 Might run to multiple lines.
4906 Position: the field value is a decimal ASCII integer, indicating
4907 an error cursor position as an index into the original query string.
4908 The first character has index 1, and positions are measured in
4909 characters not bytes.
4920 Internal position: this is defined the same as the <literal>P</>
4921 field, but it is used when the cursor position refers to an internally
4922 generated command rather than the one submitted by the client.
4923 The <literal>q</> field will always appear when this field appears.
4934 Internal query: the text of a failed internally-generated command.
4935 This could be, for example, a SQL query issued by a PL/pgSQL function.
4946 Where: an indication of the context in which the error occurred.
4947 Presently this includes a call stack traceback of active
4948 procedural language functions and internally-generated queries.
4949 The trace is one entry per line, most recent first.
4960 Schema name: if the error was associated with a specific database
4961 object, the name of the schema containing that object, if any.
4972 Table name: if the error was associated with a specific table, the
4973 name of the table. (Refer to the schema name field for the name of
4974 the table's schema.)
4985 Column name: if the error was associated with a specific table column,
4986 the name of the column. (Refer to the schema and table name fields to
4987 identify the table.)
4998 Data type name: if the error was associated with a specific data type,
4999 the name of the data type. (Refer to the schema name field for the
5000 name of the data type's schema.)
5011 Constraint name: if the error was associated with a specific
5012 constraint, the name of the constraint. Refer to fields listed above
5013 for the associated table or domain. (For this purpose, indexes are
5014 treated as constraints, even if they weren't created with constraint
5026 File: the file name of the source-code location where the error
5038 Line: the line number of the source-code location where the error
5050 Routine: the name of the source-code routine reporting the error.
5059 The fields for schema name, table name, column name, data type name, and
5060 constraint name are supplied only for a limited number of error types;
5061 see <xref linkend="errcodes-appendix">. Frontends should not assume that
5062 the presence of any of these fields guarantees the presence of another
5063 field. Core error sources observe the interrelationships noted above, but
5064 user-defined functions may use these fields in other ways. In the same
5065 vein, clients should not assume that these fields denote contemporary
5066 objects in the current database.
5071 The client is responsible for formatting displayed information to meet its
5072 needs; in particular it should break long lines as needed. Newline characters
5073 appearing in the error message fields should be treated as paragraph breaks,
5079 <sect1 id="protocol-changes">
5080 <title>Summary of Changes since Protocol 2.0</title>
5083 This section provides a quick checklist of changes, for the benefit of
5084 developers trying to update existing client libraries to protocol 3.0.
5088 The initial startup packet uses a flexible list-of-strings format
5089 instead of a fixed format. Notice that session default values for run-time
5090 parameters can now be specified directly in the startup packet. (Actually,
5091 you could do that before using the <literal>options</> field, but given the
5092 limited width of <literal>options</> and the lack of any way to quote
5093 whitespace in the values, it wasn't a very safe technique.)
5097 All messages now have a length count immediately following the message type
5098 byte (except for startup packets, which have no type byte). Also note that
5099 PasswordMessage now has a type byte.
5103 ErrorResponse and NoticeResponse ('<literal>E</>' and '<literal>N</>')
5104 messages now contain multiple fields, from which the client code can
5105 assemble an error message of the desired level of verbosity. Note that
5106 individual fields will typically not end with a newline, whereas the single
5107 string sent in the older protocol always did.
5111 The ReadyForQuery ('<literal>Z</>') message includes a transaction status
5116 The distinction between BinaryRow and DataRow message types is gone; the
5117 single DataRow message type serves for returning data in all formats.
5118 Note that the layout of DataRow has changed to make it easier to parse.
5119 Also, the representation of binary values has changed: it is no longer
5120 directly tied to the server's internal representation.
5124 There is a new <quote>extended query</> sub-protocol, which adds the frontend
5125 message types Parse, Bind, Execute, Describe, Close, Flush, and Sync, and the
5126 backend message types ParseComplete, BindComplete, PortalSuspended,
5127 ParameterDescription, NoData, and CloseComplete. Existing clients do not
5128 have to concern themselves with this sub-protocol, but making use of it
5129 might allow improvements in performance or functionality.
5133 <command>COPY</command> data is now encapsulated into CopyData and CopyDone messages. There
5134 is a well-defined way to recover from errors during <command>COPY</command>. The special
5135 <quote><literal>\.</></quote> last line is not needed anymore, and is not sent
5136 during <command>COPY OUT</command>.
5137 (It is still recognized as a terminator during <command>COPY IN</command>, but its use is
5138 deprecated and will eventually be removed.) Binary <command>COPY</command> is supported.
5139 The CopyInResponse and CopyOutResponse messages include fields indicating
5140 the number of columns and the format of each column.
5144 The layout of FunctionCall and FunctionCallResponse messages has changed.
5145 FunctionCall can now support passing NULL arguments to functions. It also
5146 can handle passing parameters and retrieving results in either text or
5147 binary format. There is no longer any reason to consider FunctionCall a
5148 potential security hole, since it does not offer direct access to internal
5149 server data representations.
5153 The backend sends ParameterStatus ('<literal>S</>') messages during connection
5154 startup for all parameters it considers interesting to the client library.
5155 Subsequently, a ParameterStatus message is sent whenever the active value
5156 changes for any of these parameters.
5160 The RowDescription ('<literal>T</>') message carries new table OID and column
5161 number fields for each column of the described row. It also shows the format
5162 code for each column.
5166 The CursorResponse ('<literal>P</>') message is no longer generated by
5171 The NotificationResponse ('<literal>A</>') message has an additional string
5172 field, which can carry a <quote>payload</> string passed
5173 from the <command>NOTIFY</command> event sender.
5177 The EmptyQueryResponse ('<literal>I</>') message used to include an empty
5178 string parameter; this has been removed.