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 startup-request
26 message tells the server which protocol version the client is attempting to
27 use. If the major version requested by the client is not supported by
28 the server, the connection will be rejected (for example, this would occur
29 if the client requested protocol version
4.0, which does not exist as of
30 this writing). If the minor version requested by the client is not
31 supported by the server (e.g., the client requests version
3.1, but the
32 server supports only
3.0), the server may either reject the connection or
33 may respond with a NegotiateProtocolVersion message containing the highest
34 minor protocol version which it supports. The client may then choose either
35 to continue with the connection using the specified protocol version or
36 to abort the connection.
40 In order to serve multiple clients efficiently, the server launches
41 a new
<quote>backend
</quote> process for each client.
42 In the current implementation, a new child
43 process is created immediately after an incoming connection is detected.
44 This is transparent to the protocol, however. For purposes of the
45 protocol, the terms
<quote>backend
</quote> and
<quote>server
</quote> are
46 interchangeable; likewise
<quote>frontend
</quote> and
<quote>client
</quote>
50 <sect1 id=
"protocol-overview">
51 <title>Overview
</title>
54 The protocol has separate phases for startup and normal operation.
55 In the startup phase, the frontend opens a connection to the server
56 and authenticates itself to the satisfaction of the server. (This might
57 involve a single message, or multiple messages depending on the
58 authentication method being used.) If all goes well, the server then sends
59 status information to the frontend, and finally enters normal operation.
60 Except for the initial startup-request message, this part of the
61 protocol is driven by the server.
65 During normal operation, the frontend sends queries and
66 other commands to the backend, and the backend sends back query results
67 and other responses. There are a few cases (such as
<command>NOTIFY
</command>)
69 backend will send unsolicited messages, but for the most part this portion
70 of a session is driven by frontend requests.
74 Termination of the session is normally by frontend choice, but can be
75 forced by the backend in certain cases. In any case, when the backend
76 closes the connection, it will roll back any open (incomplete) transaction
81 Within normal operation, SQL commands can be executed through either of
82 two sub-protocols. In the
<quote>simple query
</quote> protocol, the frontend
83 just sends a textual query string, which is parsed and immediately
84 executed by the backend. In the
<quote>extended query
</quote> protocol,
85 processing of queries is separated into multiple steps: parsing,
86 binding of parameter values, and execution. This offers flexibility
87 and performance benefits, at the cost of extra complexity.
91 Normal operation has additional sub-protocols for special operations
92 such as
<command>COPY
</command>.
95 <sect2 id=
"protocol-message-concepts">
96 <title>Messaging Overview
</title>
99 All communication is through a stream of messages. The first byte of a
100 message identifies the message type, and the next four bytes specify the
101 length of the rest of the message (this length count includes itself, but
102 not the message-type byte). The remaining contents of the message are
103 determined by the message type. For historical reasons, the very first
104 message sent by the client (the startup message) has no initial
109 To avoid losing synchronization with the message stream, both servers and
110 clients typically read an entire message into a buffer (using the byte
111 count) before attempting to process its contents. This allows easy
112 recovery if an error is detected while processing the contents. In
113 extreme situations (such as not having enough memory to buffer the
114 message), the receiver can use the byte count to determine how much
115 input to skip before it resumes reading messages.
119 Conversely, both servers and clients must take care never to send an
120 incomplete message. This is commonly done by marshaling the entire message
121 in a buffer before beginning to send it. If a communications failure
122 occurs partway through sending or receiving a message, the only sensible
123 response is to abandon the connection, since there is little hope of
124 recovering message-boundary synchronization.
128 <sect2 id=
"protocol-query-concepts">
129 <title>Extended Query Overview
</title>
132 In the extended-query protocol, execution of SQL commands is divided
133 into multiple steps. The state retained between steps is represented
134 by two types of objects:
<firstterm>prepared statements
</firstterm> and
135 <firstterm>portals
</firstterm>. A prepared statement represents the result of
136 parsing and semantic analysis of a textual query string.
137 A prepared statement is not in itself ready to execute, because it might
138 lack specific values for
<firstterm>parameters
</firstterm>. A portal represents
139 a ready-to-execute or already-partially-executed statement, with any
140 missing parameter values filled in. (For
<command>SELECT
</command> statements,
141 a portal is equivalent to an open cursor, but we choose to use a different
142 term since cursors don't handle non-
<command>SELECT
</command> statements.)
146 The overall execution cycle consists of a
<firstterm>parse
</firstterm> step,
147 which creates a prepared statement from a textual query string; a
148 <firstterm>bind
</firstterm> step, which creates a portal given a prepared
149 statement and values for any needed parameters; and an
150 <firstterm>execute
</firstterm> step that runs a portal's query. In the case of
151 a query that returns rows (
<command>SELECT
</command>,
<command>SHOW
</command>, etc.),
152 the execute step can be told to fetch only
153 a limited number of rows, so that multiple execute steps might be needed
154 to complete the operation.
158 The backend can keep track of multiple prepared statements and portals
159 (but note that these exist only within a session, and are never shared
160 across sessions). Existing prepared statements and portals are
161 referenced by names assigned when they were created. In addition,
162 an
<quote>unnamed
</quote> prepared statement and portal exist. Although these
163 behave largely the same as named objects, operations on them are optimized
164 for the case of executing a query only once and then discarding it,
165 whereas operations on named objects are optimized on the expectation
170 <sect2 id=
"protocol-format-codes">
171 <title>Formats and Format Codes
</title>
174 Data of a particular data type might be transmitted in any of several
175 different
<firstterm>formats
</firstterm>. As of
<productname>PostgreSQL
</productname> 7.4
176 the only supported formats are
<quote>text
</quote> and
<quote>binary
</quote>,
177 but the protocol makes provision for future extensions. The desired
178 format for any value is specified by a
<firstterm>format code
</firstterm>.
179 Clients can specify a format code for each transmitted parameter value
180 and for each column of a query result. Text has format code zero,
181 binary has format code one, and all other format codes are reserved
182 for future definition.
186 The text representation of values is whatever strings are produced
187 and accepted by the input/output conversion functions for the
188 particular data type. In the transmitted representation, there is
189 no trailing null character; the frontend must add one to received
190 values if it wants to process them as C strings.
191 (The text format does not allow embedded nulls, by the way.)
195 Binary representations for integers use network byte order (most
196 significant byte first). For other data types consult the documentation
197 or source code to learn about the binary representation. Keep in mind
198 that binary representations for complex data types might change across
199 server versions; the text format is usually the more portable choice.
204 <sect1 id=
"protocol-flow">
205 <title>Message Flow
</title>
208 This section describes the message flow and the semantics of each
209 message type. (Details of the exact representation of each message
210 appear in
<xref linkend=
"protocol-message-formats"/>.) There are
211 several different sub-protocols depending on the state of the
212 connection: start-up, query, function call,
213 <command>COPY
</command>, and termination. There are also special
214 provisions for asynchronous operations (including notification
215 responses and command cancellation), which can occur at any time
216 after the start-up phase.
219 <sect2 id=
"protocol-flow-start-up">
220 <title>Start-up
</title>
223 To begin a session, a frontend opens a connection to the server and sends
224 a startup message. This message includes the names of the user and of the
225 database the user wants to connect to; it also identifies the particular
226 protocol version to be used. (Optionally, the startup message can include
227 additional settings for run-time parameters.)
228 The server then uses this information and
229 the contents of its configuration files (such as
230 <filename>pg_hba.conf
</filename>) to determine
231 whether the connection is provisionally acceptable, and what additional
232 authentication is required (if any).
236 The server then sends an appropriate authentication request message,
237 to which the frontend must reply with an appropriate authentication
238 response message (such as a password).
239 For all authentication methods except GSSAPI, SSPI and SASL, there is at
240 most one request and one response. In some methods, no response
241 at all is needed from the frontend, and so no authentication request
242 occurs. For GSSAPI, SSPI and SASL, multiple exchanges of packets may be
243 needed to complete the authentication.
247 The authentication cycle ends with the server either rejecting the
248 connection attempt (ErrorResponse), or sending AuthenticationOk.
252 The possible messages from the server in this phase are:
256 <term>ErrorResponse
</term>
259 The connection attempt has been rejected.
260 The server then immediately closes the connection.
266 <term>AuthenticationOk
</term>
269 The authentication exchange is successfully completed.
275 <term>AuthenticationKerberosV5
</term>
278 The frontend must now take part in a Kerberos V5
279 authentication dialog (not described here, part of the
280 Kerberos specification) with the server. If this is
281 successful, the server responds with an AuthenticationOk,
282 otherwise it responds with an ErrorResponse. This is no
289 <term>AuthenticationCleartextPassword
</term>
292 The frontend must now send a PasswordMessage containing the
293 password in clear-text form. If
294 this is the correct password, the server responds with an
295 AuthenticationOk, otherwise it responds with an ErrorResponse.
301 <term>AuthenticationMD5Password
</term>
304 The frontend must now send a PasswordMessage containing the
305 password (with user name) encrypted via MD5, then encrypted
306 again using the
4-byte random salt specified in the
307 AuthenticationMD5Password message. If this is the correct
308 password, the server responds with an AuthenticationOk,
309 otherwise it responds with an ErrorResponse. The actual
310 PasswordMessage can be computed in SQL as
<literal>concat('md5',
311 md5(concat(md5(concat(password, username)), random-salt)))
</literal>.
312 (Keep in mind the
<function>md5()
</function> function returns its
313 result as a hex string.)
317 Support for MD5-encrypted passwords is deprecated and will be removed
318 in a future release of
<productname>PostgreSQL
</productname>. Refer
319 to
<xref linkend=
"auth-password"/> for details about migrating to
320 another password type.
327 <term>AuthenticationGSS
</term>
330 The frontend must now initiate a GSSAPI negotiation. The frontend
331 will send a GSSResponse message with the first part of the GSSAPI
332 data stream in response to this. If further messages are needed,
333 the server will respond with AuthenticationGSSContinue.
339 <term>AuthenticationSSPI
</term>
342 The frontend must now initiate an SSPI negotiation. The frontend
343 will send a GSSResponse with the first part of the SSPI
344 data stream in response to this. If further messages are needed,
345 the server will respond with AuthenticationGSSContinue.
351 <term>AuthenticationGSSContinue
</term>
354 This message contains the response data from the previous step
355 of GSSAPI or SSPI negotiation (AuthenticationGSS, AuthenticationSSPI
356 or a previous AuthenticationGSSContinue). If the GSSAPI
357 or SSPI data in this message
358 indicates more data is needed to complete the authentication,
359 the frontend must send that data as another GSSResponse message. If
360 GSSAPI or SSPI authentication is completed by this message, the server
361 will next send AuthenticationOk to indicate successful authentication
362 or ErrorResponse to indicate failure.
368 <term>AuthenticationSASL
</term>
371 The frontend must now initiate a SASL negotiation, using one of the
372 SASL mechanisms listed in the message. The frontend will send a
373 SASLInitialResponse with the name of the selected mechanism, and the
374 first part of the SASL data stream in response to this. If further
375 messages are needed, the server will respond with
376 AuthenticationSASLContinue. See
<xref linkend=
"sasl-authentication"/>
383 <term>AuthenticationSASLContinue
</term>
386 This message contains challenge data from the previous step of SASL
387 negotiation (AuthenticationSASL, or a previous
388 AuthenticationSASLContinue). The frontend must respond with a
389 SASLResponse message.
395 <term>AuthenticationSASLFinal
</term>
398 SASL authentication has completed with additional mechanism-specific
399 data for the client. The server will next send AuthenticationOk to
400 indicate successful authentication, or an ErrorResponse to indicate
401 failure. This message is sent only if the SASL mechanism specifies
402 additional data to be sent from server to client at completion.
408 <term>NegotiateProtocolVersion
</term>
411 The server does not support the minor protocol version requested
412 by the client, but does support an earlier version of the protocol;
413 this message indicates the highest supported minor version. This
414 message will also be sent if the client requested unsupported protocol
415 options (i.e., beginning with
<literal>_pq_.
</literal>) in the
416 startup packet. This message will be followed by an ErrorResponse or
417 a message indicating the success or failure of authentication.
426 If the frontend does not support the authentication method
427 requested by the server, then it should immediately close the
432 After having received AuthenticationOk, the frontend must wait
433 for further messages from the server. In this phase a backend process
434 is being started, and the frontend is just an interested bystander.
435 It is still possible for the startup attempt
436 to fail (ErrorResponse) or the server to decline support for the requested
437 minor protocol version (NegotiateProtocolVersion), but in the normal case
438 the backend will send some ParameterStatus messages, BackendKeyData, and
439 finally ReadyForQuery.
443 During this phase the backend will attempt to apply any additional
444 run-time parameter settings that were given in the startup message.
445 If successful, these values become session defaults. An error causes
446 ErrorResponse and exit.
450 The possible messages from the backend in this phase are:
454 <term>BackendKeyData
</term>
457 This message provides secret-key data that the frontend must
458 save if it wants to be able to issue cancel requests later.
459 The frontend should not respond to this message, but should
460 continue listening for a ReadyForQuery message.
466 <term>ParameterStatus
</term>
469 This message informs the frontend about the current (initial)
470 setting of backend parameters, such as
<xref
471 linkend=
"guc-client-encoding"/> or
<xref linkend=
"guc-datestyle"/>.
472 The frontend can ignore this message, or record the settings
473 for its future use; see
<xref linkend=
"protocol-async"/> for
474 more details. The frontend should not respond to this
475 message, but should continue listening for a ReadyForQuery
482 <term>ReadyForQuery
</term>
485 Start-up is completed. The frontend can now issue commands.
491 <term>ErrorResponse
</term>
494 Start-up failed. The connection is closed after sending this
501 <term>NoticeResponse
</term>
504 A warning message has been issued. The frontend should
505 display the message but continue listening for ReadyForQuery
514 The ReadyForQuery message is the same one that the backend will
515 issue after each command cycle. Depending on the coding needs of
516 the frontend, it is reasonable to consider ReadyForQuery as
517 starting a command cycle, or to consider ReadyForQuery as ending the
518 start-up phase and each subsequent command cycle.
522 <sect2 id=
"protocol-flow-simple-query">
523 <title>Simple Query
</title>
526 A simple query cycle is initiated by the frontend sending a Query message
527 to the backend. The message includes an SQL command (or commands)
528 expressed as a text string.
529 The backend then sends one or more response
530 messages depending on the contents of the query command string,
531 and finally a ReadyForQuery response message. ReadyForQuery
532 informs the frontend that it can safely send a new command.
533 (It is not actually necessary for the frontend to wait for
534 ReadyForQuery before issuing another command, but the frontend must
535 then take responsibility for figuring out what happens if the earlier
536 command fails and already-issued later commands succeed.)
540 The possible response messages from the backend are:
544 <term>CommandComplete
</term>
547 An SQL command completed normally.
553 <term>CopyInResponse
</term>
556 The backend is ready to copy data from the frontend to a
557 table; see
<xref linkend=
"protocol-copy"/>.
563 <term>CopyOutResponse
</term>
566 The backend is ready to copy data from a table to the
567 frontend; see
<xref linkend=
"protocol-copy"/>.
573 <term>RowDescription
</term>
576 Indicates that rows are about to be returned in response to
577 a
<command>SELECT
</command>,
<command>FETCH
</command>, etc. query.
578 The contents of this message describe the column layout of the rows.
579 This will be followed by a DataRow message for each row being returned
589 One of the set of rows returned by
590 a
<command>SELECT
</command>,
<command>FETCH
</command>, etc. query.
596 <term>EmptyQueryResponse
</term>
599 An empty query string was recognized.
605 <term>ErrorResponse
</term>
608 An error has occurred.
614 <term>ReadyForQuery
</term>
617 Processing of the query string is complete. A separate
618 message is sent to indicate this because the query string might
619 contain multiple SQL commands. (CommandComplete marks the
620 end of processing one SQL command, not the whole string.)
621 ReadyForQuery will always be sent, whether processing
622 terminates successfully or with an error.
628 <term>NoticeResponse
</term>
631 A warning message has been issued in relation to the query.
632 Notices are in addition to other responses, i.e., the backend
633 will continue processing the command.
642 The response to a
<command>SELECT
</command> query (or other queries that
643 return row sets, such as
<command>EXPLAIN
</command> or
<command>SHOW
</command>)
644 normally consists of RowDescription, zero or more
645 DataRow messages, and then CommandComplete.
646 <command>COPY
</command> to or from the frontend invokes special protocol
647 as described in
<xref linkend=
"protocol-copy"/>.
648 All other query types normally produce only
649 a CommandComplete message.
653 Since a query string could contain several queries (separated by
654 semicolons), there might be several such response sequences before the
655 backend finishes processing the query string. ReadyForQuery is issued
656 when the entire string has been processed and the backend is ready to
657 accept a new query string.
661 If a completely empty (no contents other than whitespace) query string
662 is received, the response is EmptyQueryResponse followed by ReadyForQuery.
666 In the event of an error, ErrorResponse is issued followed by
667 ReadyForQuery. All further processing of the query string is aborted by
668 ErrorResponse (even if more queries remained in it). Note that this
669 might occur partway through the sequence of messages generated by an
674 In simple Query mode, the format of retrieved values is always text,
675 except when the given command is a
<command>FETCH
</command> from a cursor
676 declared with the
<literal>BINARY
</literal> option. In that case, the
677 retrieved values are in binary format. The format codes given in
678 the RowDescription message tell which format is being used.
682 A frontend must be prepared to accept ErrorResponse and
683 NoticeResponse messages whenever it is expecting any other type of
684 message. See also
<xref linkend=
"protocol-async"/> concerning messages
685 that the backend might generate due to outside events.
689 Recommended practice is to code frontends in a state-machine style
690 that will accept any message type at any time that it could make sense,
691 rather than wiring in assumptions about the exact sequence of messages.
694 <sect3 id=
"protocol-flow-multi-statement">
695 <title>Multiple Statements in a Simple Query
</title>
698 When a simple Query message contains more than one SQL statement
699 (separated by semicolons), those statements are executed as a single
700 transaction, unless explicit transaction control commands are included
701 to force a different behavior. For example, if the message contains
703 INSERT INTO mytable VALUES(
1);
705 INSERT INTO mytable VALUES(
2);
707 then the divide-by-zero failure in the
<command>SELECT
</command> will force
708 rollback of the first
<command>INSERT
</command>. Furthermore, because
709 execution of the message is abandoned at the first error, the second
710 <command>INSERT
</command> is never attempted at all.
714 If instead the message contains
717 INSERT INTO mytable VALUES(
1);
719 INSERT INTO mytable VALUES(
2);
722 then the first
<command>INSERT
</command> is committed by the
723 explicit
<command>COMMIT
</command> command. The second
<command>INSERT
</command>
724 and the
<command>SELECT
</command> are still treated as a single transaction,
725 so that the divide-by-zero failure will roll back the
726 second
<command>INSERT
</command>, but not the first one.
730 This behavior is implemented by running the statements in a
731 multi-statement Query message in an
<firstterm>implicit transaction
732 block
</firstterm> unless there is some explicit transaction block for them to
733 run in. The main difference between an implicit transaction block and
734 a regular one is that an implicit block is closed automatically at the
735 end of the Query message, either by an implicit commit if there was no
736 error, or an implicit rollback if there was an error. This is similar
737 to the implicit commit or rollback that happens for a statement
738 executed by itself (when not in a transaction block).
742 If the session is already in a transaction block, as a result of
743 a
<command>BEGIN
</command> in some previous message, then the Query message
744 simply continues that transaction block, whether the message contains
745 one statement or several. However, if the Query message contains
746 a
<command>COMMIT
</command> or
<command>ROLLBACK
</command> closing the existing
747 transaction block, then any following statements are executed in an
748 implicit transaction block.
749 Conversely, if a
<command>BEGIN
</command> appears in a multi-statement Query
750 message, then it starts a regular transaction block that will only be
751 terminated by an explicit
<command>COMMIT
</command> or
<command>ROLLBACK
</command>,
752 whether that appears in this Query message or a later one.
753 If the
<command>BEGIN
</command> follows some statements that were executed as
754 an implicit transaction block, those statements are not immediately
755 committed; in effect, they are retroactively included into the new
756 regular transaction block.
760 A
<command>COMMIT
</command> or
<command>ROLLBACK
</command> appearing in an implicit
761 transaction block is executed as normal, closing the implicit block;
762 however, a warning will be issued since a
<command>COMMIT
</command>
763 or
<command>ROLLBACK
</command> without a previous
<command>BEGIN
</command> might
764 represent a mistake. If more statements follow, a new implicit
765 transaction block will be started for them.
769 Savepoints are not allowed in an implicit transaction block, since
770 they would conflict with the behavior of automatically closing the
771 block upon any error.
775 Remember that, regardless of any transaction control commands that may
776 be present, execution of the Query message stops at the first error.
777 Thus for example given
783 in a single Query message, the session will be left inside a failed
784 regular transaction block, since the
<command>ROLLBACK
</command> is not
785 reached after the divide-by-zero error. Another
<command>ROLLBACK
</command>
786 will be needed to restore the session to a usable state.
790 Another behavior of note is that initial lexical and syntactic
791 analysis is done on the entire query string before any of it is
792 executed. Thus simple errors (such as a misspelled keyword) in later
793 statements can prevent execution of any of the statements. This
794 is normally invisible to users since the statements would all roll
795 back anyway when done as an implicit transaction block. However,
796 it can be visible when attempting to do multiple transactions within a
797 multi-statement Query. For instance, if a typo turned our previous
801 INSERT INTO mytable VALUES(
1);
803 INSERT INTO mytable VALUES(
2);
804 SELCT
1/
0;
<!-- this typo is intentional -->
806 then none of the statements would get run, resulting in the visible
807 difference that the first
<command>INSERT
</command> is not committed.
808 Errors detected at semantic analysis or later, such as a misspelled
809 table or column name, do not have this effect.
814 <sect2 id=
"protocol-flow-ext-query">
815 <title>Extended Query
</title>
818 The extended query protocol breaks down the above-described simple
819 query protocol into multiple steps. The results of preparatory
820 steps can be re-used multiple times for improved efficiency.
821 Furthermore, additional features are available, such as the possibility
822 of supplying data values as separate parameters instead of having to
823 insert them directly into a query string.
827 In the extended protocol, the frontend first sends a Parse message,
828 which contains a textual query string, optionally some information
829 about data types of parameter placeholders, and the
830 name of a destination prepared-statement object (an empty string
831 selects the unnamed prepared statement). The response is
832 either ParseComplete or ErrorResponse. Parameter data types can be
833 specified by OID; if not given, the parser attempts to infer the
834 data types in the same way as it would do for untyped literal string
840 A parameter data type can be left unspecified by setting it to zero,
841 or by making the array of parameter type OIDs shorter than the
842 number of parameter symbols (
<literal>$
</literal><replaceable>n
</replaceable>)
843 used in the query string. Another special case is that a parameter's
844 type can be specified as
<type>void
</type> (that is, the OID of the
845 <type>void
</type> pseudo-type). This is meant to allow parameter symbols
846 to be used for function parameters that are actually OUT parameters.
847 Ordinarily there is no context in which a
<type>void
</type> parameter
848 could be used, but if such a parameter symbol appears in a function's
849 parameter list, it is effectively ignored. For example, a function
850 call such as
<literal>foo($
1,$
2,$
3,$
4)
</literal> could match a function with
851 two IN and two OUT arguments, if
<literal>$
3</literal> and
<literal>$
4</literal>
852 are specified as having type
<type>void
</type>.
858 The query string contained in a Parse message cannot include more
859 than one SQL statement; else a syntax error is reported. This
860 restriction does not exist in the simple-query protocol, but it
861 does exist in the extended protocol, because allowing prepared
862 statements or portals to contain multiple commands would complicate
868 If successfully created, a named prepared-statement object lasts till
869 the end of the current session, unless explicitly destroyed. An unnamed
870 prepared statement lasts only until the next Parse statement specifying
871 the unnamed statement as destination is issued. (Note that a simple
872 Query message also destroys the unnamed statement.) Named prepared
873 statements must be explicitly closed before they can be redefined by
874 another Parse message, but this is not required for the unnamed statement.
875 Named prepared statements can also be created and accessed at the SQL
876 command level, using
<command>PREPARE
</command> and
<command>EXECUTE
</command>.
880 Once a prepared statement exists, it can be readied for execution using a
881 Bind message. The Bind message gives the name of the source prepared
882 statement (empty string denotes the unnamed prepared statement), the name
883 of the destination portal (empty string denotes the unnamed portal), and
884 the values to use for any parameter placeholders present in the prepared
886 supplied parameter set must match those needed by the prepared statement.
887 (If you declared any
<type>void
</type> parameters in the Parse message,
888 pass NULL values for them in the Bind message.)
889 Bind also specifies the format to use for any data returned
890 by the query; the format can be specified overall, or per-column.
891 The response is either BindComplete or ErrorResponse.
896 The choice between text and binary output is determined by the format
897 codes given in Bind, regardless of the SQL command involved. The
898 <literal>BINARY
</literal> attribute in cursor declarations is irrelevant when
899 using extended query protocol.
904 Query planning typically occurs when the Bind message is processed.
905 If the prepared statement has no parameters, or is executed repeatedly,
906 the server might save the created plan and re-use it during subsequent
907 Bind messages for the same prepared statement. However, it will do so
908 only if it finds that a generic plan can be created that is not much
909 less efficient than a plan that depends on the specific parameter values
910 supplied. This happens transparently so far as the protocol is concerned.
914 If successfully created, a named portal object lasts till the end of the
915 current transaction, unless explicitly destroyed. An unnamed portal is
916 destroyed at the end of the transaction, or as soon as the next Bind
917 statement specifying the unnamed portal as destination is issued. (Note
918 that a simple Query message also destroys the unnamed portal.) Named
919 portals must be explicitly closed before they can be redefined by another
920 Bind message, but this is not required for the unnamed portal.
921 Named portals can also be created and accessed at the SQL
922 command level, using
<command>DECLARE CURSOR
</command> and
<command>FETCH
</command>.
926 Once a portal exists, it can be executed using an Execute message.
927 The Execute message specifies the portal name (empty string denotes the
929 a maximum result-row count (zero meaning
<quote>fetch all rows
</quote>).
930 The result-row count is only meaningful for portals
931 containing commands that return row sets; in other cases the command is
932 always executed to completion, and the row count is ignored.
934 responses to Execute are the same as those described above for queries
935 issued via simple query protocol, except that Execute doesn't cause
936 ReadyForQuery or RowDescription to be issued.
940 If Execute terminates before completing the execution of a portal
941 (due to reaching a nonzero result-row count), it will send a
942 PortalSuspended message; the appearance of this message tells the frontend
943 that another Execute should be issued against the same portal to
944 complete the operation. The CommandComplete message indicating
945 completion of the source SQL command is not sent until
946 the portal's execution is completed. Therefore, an Execute phase is
947 always terminated by the appearance of exactly one of these messages:
948 CommandComplete, EmptyQueryResponse (if the portal was created from
949 an empty query string), ErrorResponse, or PortalSuspended.
953 At completion of each series of extended-query messages, the frontend
954 should issue a Sync message. This parameterless message causes the
955 backend to close the current transaction if it's not inside a
956 <command>BEGIN
</command>/
<command>COMMIT
</command> transaction block (
<quote>close
</quote>
957 meaning to commit if no error, or roll back if error). Then a
958 ReadyForQuery response is issued. The purpose of Sync is to provide
959 a resynchronization point for error recovery. When an error is detected
960 while processing any extended-query message, the backend issues
961 ErrorResponse, then reads and discards messages until a Sync is reached,
962 then issues ReadyForQuery and returns to normal message processing.
963 (But note that no skipping occurs if an error is detected
964 <emphasis>while
</emphasis> processing Sync
— this ensures that there is one
965 and only one ReadyForQuery sent for each Sync.)
970 Sync does not cause a transaction block opened with
<command>BEGIN
</command>
971 to be closed. It is possible to detect this situation since the
972 ReadyForQuery message includes transaction status information.
977 In addition to these fundamental, required operations, there are several
978 optional operations that can be used with extended-query protocol.
982 The Describe message (portal variant) specifies the name of an existing
983 portal (or an empty string for the unnamed portal). The response is a
984 RowDescription message describing the rows that will be returned by
985 executing the portal; or a NoData message if the portal does not contain a
986 query that will return rows; or ErrorResponse if there is no such portal.
990 The Describe message (statement variant) specifies the name of an existing
991 prepared statement (or an empty string for the unnamed prepared
992 statement). The response is a ParameterDescription message describing the
993 parameters needed by the statement, followed by a RowDescription message
994 describing the rows that will be returned when the statement is eventually
995 executed (or a NoData message if the statement will not return rows).
996 ErrorResponse is issued if there is no such prepared statement. Note that
997 since Bind has not yet been issued, the formats to be used for returned
998 columns are not yet known to the backend; the format code fields in the
999 RowDescription message will be zeroes in this case.
1004 In most scenarios the frontend should issue one or the other variant
1005 of Describe before issuing Execute, to ensure that it knows how to
1006 interpret the results it will get back.
1011 The Close message closes an existing prepared statement or portal
1012 and releases resources. It is not an error to issue Close against
1013 a nonexistent statement or portal name. The response is normally
1014 CloseComplete, but could be ErrorResponse if some difficulty is
1015 encountered while releasing resources. Note that closing a prepared
1016 statement implicitly closes any open portals that were constructed
1017 from that statement.
1021 The Flush message does not cause any specific output to be generated,
1022 but forces the backend to deliver any data pending in its output
1023 buffers. A Flush must be sent after any extended-query command except
1024 Sync, if the frontend wishes to examine the results of that command before
1025 issuing more commands. Without Flush, messages returned by the backend
1026 will be combined into the minimum possible number of packets to minimize
1032 The simple Query message is approximately equivalent to the series Parse,
1033 Bind, portal Describe, Execute, Close, Sync, using the unnamed prepared
1034 statement and portal objects and no parameters. One difference is that
1035 it will accept multiple SQL statements in the query string, automatically
1036 performing the bind/describe/execute sequence for each one in succession.
1037 Another difference is that it will not return ParseComplete, BindComplete,
1038 CloseComplete, or NoData messages.
1043 <sect2 id=
"protocol-flow-pipelining">
1044 <title>Pipelining
</title>
1046 <indexterm zone=
"protocol-flow-pipelining">
1047 <primary>pipelining
</primary>
1048 <secondary>protocol specification
</secondary>
1052 Use of the extended query protocol
1053 allows
<firstterm>pipelining
</firstterm>, which means sending a series
1054 of queries without waiting for earlier ones to complete. This reduces
1055 the number of network round trips needed to complete a given series of
1056 operations. However, the user must carefully consider the required
1057 behavior if one of the steps fails, since later queries will already
1058 be in flight to the server.
1062 One way to deal with that is to make the whole query series be a
1063 single transaction, that is wrap it in
<command>BEGIN
</command> ...
1064 <command>COMMIT
</command>. However, this does not help if one wishes
1065 for some of the commands to commit independently of others.
1069 The extended query protocol provides another way to manage this
1070 concern, which is to omit sending Sync messages between steps that
1071 are dependent. Since, after an error, the backend will skip command
1072 messages until it finds Sync, this allows later commands in a pipeline
1073 to be skipped automatically when an earlier one fails, without the
1074 client having to manage that explicitly with
<command>BEGIN
</command>
1075 and
<command>COMMIT
</command>. Independently-committable segments
1076 of the pipeline can be separated by Sync messages.
1080 If the client has not issued an explicit
<command>BEGIN
</command>,
1081 then an implicit transaction block is started and each Sync ordinarily
1082 causes an implicit
<command>COMMIT
</command> if the preceding step(s)
1083 succeeded, or an implicit
<command>ROLLBACK
</command> if they failed.
1084 This implicit transaction block will only be detected by the server
1085 when the first command ends without a sync. There are a few DDL
1086 commands (such as
<command>CREATE DATABASE
</command>) that cannot be
1087 executed inside a transaction block. If one of these is executed in a
1088 pipeline, it will fail unless it is the first command after a Sync.
1089 Furthermore, upon success it will force an immediate commit to preserve
1090 database consistency. Thus a Sync immediately following one of these
1091 commands has no effect except to respond with ReadyForQuery.
1095 When using this method, completion of the pipeline must be determined
1096 by counting ReadyForQuery messages and waiting for that to reach the
1097 number of Syncs sent. Counting command completion responses is
1098 unreliable, since some of the commands may be skipped and thus not
1099 produce a completion message.
1103 <sect2 id=
"protocol-flow-function-call">
1104 <title>Function Call
</title>
1107 The Function Call sub-protocol allows the client to request a direct
1108 call of any function that exists in the database's
1109 <structname>pg_proc
</structname> system catalog. The client must have
1110 execute permission for the function.
1115 The Function Call sub-protocol is a legacy feature that is probably best
1116 avoided in new code. Similar results can be accomplished by setting up
1117 a prepared statement that does
<literal>SELECT function($
1, ...)
</literal>.
1118 The Function Call cycle can then be replaced with Bind/Execute.
1123 A Function Call cycle is initiated by the frontend sending a
1124 FunctionCall message to the backend. The backend then sends one
1125 or more response messages depending on the results of the function
1126 call, and finally a ReadyForQuery response message. ReadyForQuery
1127 informs the frontend that it can safely send a new query or
1132 The possible response messages from the backend are:
1136 <term>ErrorResponse
</term>
1139 An error has occurred.
1145 <term>FunctionCallResponse
</term>
1148 The function call was completed and returned the result given
1150 (Note that the Function Call protocol can only handle a single
1151 scalar result, not a row type or set of results.)
1157 <term>ReadyForQuery
</term>
1160 Processing of the function call is complete. ReadyForQuery
1161 will always be sent, whether processing terminates
1162 successfully or with an error.
1168 <term>NoticeResponse
</term>
1171 A warning message has been issued in relation to the function
1172 call. Notices are in addition to other responses, i.e., the
1173 backend will continue processing the command.
1181 <sect2 id=
"protocol-copy">
1182 <title>COPY Operations
</title>
1185 The
<command>COPY
</command> command allows high-speed bulk data transfer
1186 to or from the server. Copy-in and copy-out operations each switch
1187 the connection into a distinct sub-protocol, which lasts until the
1188 operation is completed.
1192 Copy-in mode (data transfer to the server) is initiated when the
1193 backend executes a
<command>COPY FROM STDIN
</command> SQL statement. The backend
1194 sends a CopyInResponse message to the frontend. The frontend should
1195 then send zero or more CopyData messages, forming a stream of input
1196 data. (The message boundaries are not required to have anything to do
1197 with row boundaries, although that is often a reasonable choice.)
1198 The frontend can terminate the copy-in mode by sending either a CopyDone
1199 message (allowing successful termination) or a CopyFail message (which
1200 will cause the
<command>COPY
</command> SQL statement to fail with an
1201 error). The backend then reverts to the command-processing mode it was
1202 in before the
<command>COPY
</command> started, which will be either simple or
1203 extended query protocol. It will next send either CommandComplete
1204 (if successful) or ErrorResponse (if not).
1208 In the event of a backend-detected error during copy-in mode (including
1209 receipt of a CopyFail message), the backend will issue an ErrorResponse
1210 message. If the
<command>COPY
</command> command was issued via an extended-query
1211 message, the backend will now discard frontend messages until a Sync
1212 message is received, then it will issue ReadyForQuery and return to normal
1213 processing. If the
<command>COPY
</command> command was issued in a simple
1214 Query message, the rest of that message is discarded and ReadyForQuery
1215 is issued. In either case, any subsequent CopyData, CopyDone, or CopyFail
1216 messages issued by the frontend will simply be dropped.
1220 The backend will ignore Flush and Sync messages received during copy-in
1221 mode. Receipt of any other non-copy message type constitutes an error
1222 that will abort the copy-in state as described above. (The exception for
1223 Flush and Sync is for the convenience of client libraries that always
1224 send Flush or Sync after an Execute message, without checking whether
1225 the command to be executed is a
<command>COPY FROM STDIN
</command>.)
1229 Copy-out mode (data transfer from the server) is initiated when the
1230 backend executes a
<command>COPY TO STDOUT
</command> SQL statement. The backend
1231 sends a CopyOutResponse message to the frontend, followed by
1232 zero or more CopyData messages (always one per row), followed by CopyDone.
1233 The backend then reverts to the command-processing mode it was
1234 in before the
<command>COPY
</command> started, and sends CommandComplete.
1235 The frontend cannot abort the transfer (except by closing the connection
1236 or issuing a Cancel request),
1237 but it can discard unwanted CopyData and CopyDone messages.
1241 In the event of a backend-detected error during copy-out mode,
1242 the backend will issue an ErrorResponse message and revert to normal
1243 processing. The frontend should treat receipt of ErrorResponse as
1244 terminating the copy-out mode.
1248 It is possible for NoticeResponse and ParameterStatus messages to be
1249 interspersed between CopyData messages; frontends must handle these cases,
1250 and should be prepared for other asynchronous message types as well (see
1251 <xref linkend=
"protocol-async"/>). Otherwise, any message type other than
1252 CopyData or CopyDone may be treated as terminating copy-out mode.
1256 There is another Copy-related mode called copy-both, which allows
1257 high-speed bulk data transfer to
<emphasis>and
</emphasis> from the server.
1258 Copy-both mode is initiated when a backend in walsender mode
1259 executes a
<command>START_REPLICATION
</command> statement. The
1260 backend sends a CopyBothResponse message to the frontend. Both
1261 the backend and the frontend may then send CopyData messages
1262 until either end sends a CopyDone message. After the client
1263 sends a CopyDone message, the connection goes from copy-both mode to
1264 copy-out mode, and the client may not send any more CopyData messages.
1265 Similarly, when the server sends a CopyDone message, the connection
1266 goes into copy-in mode, and the server may not send any more CopyData
1267 messages. After both sides have sent a CopyDone message, the copy mode
1268 is terminated, and the backend reverts to the command-processing mode.
1269 In the event of a backend-detected error during copy-both mode,
1270 the backend will issue an ErrorResponse message, discard frontend messages
1271 until a Sync message is received, and then issue ReadyForQuery and return
1272 to normal processing. The frontend should treat receipt of ErrorResponse
1273 as terminating the copy in both directions; no CopyDone should be sent
1274 in this case. See
<xref linkend=
"protocol-replication"/> for more
1275 information on the subprotocol transmitted over copy-both mode.
1279 The CopyInResponse, CopyOutResponse and CopyBothResponse messages
1280 include fields that inform the frontend of the number of columns
1281 per row and the format codes being used for each column. (As of
1282 the present implementation, all columns in a given
<command>COPY
</command>
1283 operation will use the same format, but the message design does not
1289 <sect2 id=
"protocol-async">
1290 <title>Asynchronous Operations
</title>
1293 There are several cases in which the backend will send messages that
1294 are not specifically prompted by the frontend's command stream.
1295 Frontends must be prepared to deal with these messages at any time,
1296 even when not engaged in a query.
1297 At minimum, one should check for these cases before beginning to
1298 read a query response.
1302 It is possible for NoticeResponse messages to be generated due to
1303 outside activity; for example, if the database administrator commands
1304 a
<quote>fast
</quote> database shutdown, the backend will send a NoticeResponse
1305 indicating this fact before closing the connection. Accordingly,
1306 frontends should always be prepared to accept and display NoticeResponse
1307 messages, even when the connection is nominally idle.
1311 ParameterStatus messages will be generated whenever the active
1312 value changes for any of the parameters the backend believes the
1313 frontend should know about. Most commonly this occurs in response
1314 to a
<command>SET
</command> SQL command executed by the frontend, and
1315 this case is effectively synchronous
— but it is also possible
1316 for parameter status changes to occur because the administrator
1317 changed a configuration file and then sent the
1318 <systemitem>SIGHUP
</systemitem> signal to the server. Also,
1319 if a
<command>SET
</command> command is rolled back, an appropriate
1320 ParameterStatus message will be generated to report the current
1325 At present there is a hard-wired set of parameters for which
1326 ParameterStatus will be generated. They are:
1327 <simplelist type=
"vert" columns=
"2">
1328 <member><varname>application_name
</varname></member>
1329 <member><varname>client_encoding
</varname></member>
1330 <member><varname>DateStyle
</varname></member>
1331 <member><varname>default_transaction_read_only
</varname></member>
1332 <member><varname>in_hot_standby
</varname></member>
1333 <member><varname>integer_datetimes
</varname></member>
1334 <member><varname>IntervalStyle
</varname></member>
1335 <member><varname>is_superuser
</varname></member>
1336 <member><varname>scram_iterations
</varname></member>
1337 <member><varname>search_path
</varname></member>
1338 <member><varname>server_encoding
</varname></member>
1339 <member><varname>server_version
</varname></member>
1340 <member><varname>session_authorization
</varname></member>
1341 <member><varname>standard_conforming_strings
</varname></member>
1342 <member><varname>TimeZone
</varname></member>
1344 (
<varname>default_transaction_read_only
</varname> and
1345 <varname>in_hot_standby
</varname> were not reported by releases before
1346 14;
<varname>scram_iterations
</varname> was not reported by releases
1347 before
16;
<varname>search_path
</varname> was not reported by releases
1350 <varname>server_version
</varname>,
1351 <varname>server_encoding
</varname> and
1352 <varname>integer_datetimes
</varname>
1353 are pseudo-parameters that cannot change after startup.
1354 This set might change in the future, or even become configurable.
1355 Accordingly, a frontend should simply ignore ParameterStatus for
1356 parameters that it does not understand or care about.
1360 If a frontend issues a
<command>LISTEN
</command> command, then the
1361 backend will send a NotificationResponse message (not to be
1362 confused with NoticeResponse!) whenever a
1363 <command>NOTIFY
</command> command is executed for the same
1369 At present, NotificationResponse can only be sent outside a
1370 transaction, and thus it will not occur in the middle of a
1371 command-response series, though it might occur just before ReadyForQuery.
1372 It is unwise to design frontend logic that assumes that, however.
1373 Good practice is to be able to accept NotificationResponse at any
1374 point in the protocol.
1379 <sect2 id=
"protocol-flow-canceling-requests">
1380 <title>Canceling Requests in Progress
</title>
1383 During the processing of a query, the frontend might request
1384 cancellation of the query. The cancel request is not sent
1385 directly on the open connection to the backend for reasons of
1386 implementation efficiency: we don't want to have the backend
1387 constantly checking for new input from the frontend during query
1388 processing. Cancel requests should be relatively infrequent, so
1389 we make them slightly cumbersome in order to avoid a penalty in
1394 To issue a cancel request, the frontend opens a new connection to
1395 the server and sends a CancelRequest message, rather than the
1396 StartupMessage message that would ordinarily be sent across a new
1397 connection. The server will process this request and then close
1398 the connection. For security reasons, no direct reply is made to
1399 the cancel request message.
1403 A CancelRequest message will be ignored unless it contains the
1404 same key data (PID and secret key) passed to the frontend during
1405 connection start-up. If the request matches the PID and secret
1406 key for a currently executing backend, the processing of the
1407 current query is aborted. (In the existing implementation, this is
1408 done by sending a special signal to the backend process that is
1409 processing the query.)
1413 The cancellation signal might or might not have any effect
— for
1414 example, if it arrives after the backend has finished processing
1415 the query, then it will have no effect. If the cancellation is
1416 effective, it results in the current command being terminated
1417 early with an error message.
1421 The upshot of all this is that for reasons of both security and
1422 efficiency, the frontend has no direct way to tell whether a
1423 cancel request has succeeded. It must continue to wait for the
1424 backend to respond to the query. Issuing a cancel simply improves
1425 the odds that the current query will finish soon, and improves the
1426 odds that it will fail with an error message instead of
1431 Since the cancel request is sent across a new connection to the
1432 server and not across the regular frontend/backend communication
1433 link, it is possible for the cancel request to be issued by any
1434 process, not just the frontend whose query is to be canceled.
1435 This might provide additional flexibility when building
1436 multiple-process applications. It also introduces a security
1437 risk, in that unauthorized persons might try to cancel queries.
1438 The security risk is addressed by requiring a dynamically
1439 generated secret key to be supplied in cancel requests.
1443 <sect2 id=
"protocol-flow-termination">
1444 <title>Termination
</title>
1447 The normal, graceful termination procedure is that the frontend
1448 sends a Terminate message and immediately closes the connection.
1449 On receipt of this message, the backend closes the connection and
1454 In rare cases (such as an administrator-commanded database shutdown)
1455 the backend might disconnect without any frontend request to do so.
1456 In such cases the backend will attempt to send an error or notice message
1457 giving the reason for the disconnection before it closes the connection.
1461 Other termination scenarios arise from various failure cases, such as core
1462 dump at one end or the other, loss of the communications link, loss of
1463 message-boundary synchronization, etc. If either frontend or backend sees
1464 an unexpected closure of the connection, it should clean
1465 up and terminate. The frontend has the option of launching a new backend
1466 by recontacting the server if it doesn't want to terminate itself.
1467 Closing the connection is also advisable if an unrecognizable message type
1468 is received, since this probably indicates loss of message-boundary sync.
1472 For either normal or abnormal termination, any open transaction is
1473 rolled back, not committed. One should note however that if a
1474 frontend disconnects while a non-
<command>SELECT
</command> query
1475 is being processed, the backend will probably finish the query
1476 before noticing the disconnection. If the query is outside any
1477 transaction block (
<command>BEGIN
</command> ...
<command>COMMIT
</command>
1478 sequence) then its results might be committed before the
1479 disconnection is recognized.
1483 <sect2 id=
"protocol-flow-ssl">
1484 <title><acronym>SSL
</acronym> Session Encryption
</title>
1487 If
<productname>PostgreSQL
</productname> was built with
1488 <acronym>SSL
</acronym> support, frontend/backend communications
1489 can be encrypted using
<acronym>SSL
</acronym>. This provides
1490 communication security in environments where attackers might be
1491 able to capture the session traffic. For more information on
1492 encrypting
<productname>PostgreSQL
</productname> sessions with
1493 <acronym>SSL
</acronym>, see
<xref linkend=
"ssl-tcp"/>.
1497 To initiate an
<acronym>SSL
</acronym>-encrypted connection, the
1498 frontend initially sends an SSLRequest message rather than a
1499 StartupMessage. The server then responds with a single byte
1500 containing
<literal>S
</literal> or
<literal>N
</literal>, indicating that it is
1501 willing or unwilling to perform
<acronym>SSL
</acronym>,
1502 respectively. The frontend might close the connection at this point
1503 if it is dissatisfied with the response. To continue after
1504 <literal>S
</literal>, perform an
<acronym>SSL
</acronym> startup handshake
1505 (not described here, part of the
<acronym>SSL
</acronym>
1506 specification) with the server. If this is successful, continue
1507 with sending the usual StartupMessage. In this case the
1508 StartupMessage and all subsequent data will be
1509 <acronym>SSL
</acronym>-encrypted. To continue after
1510 <literal>N
</literal>, send the usual StartupMessage and proceed without
1512 (Alternatively, it is permissible to issue a GSSENCRequest message
1513 after an
<literal>N
</literal> response to try to
1514 use
<acronym>GSSAPI
</acronym> encryption instead
1515 of
<acronym>SSL
</acronym>.)
1519 The frontend should also be prepared to handle an ErrorMessage
1520 response to SSLRequest from the server. The frontend should not display
1521 this error message to the user/application, since the server has not been
1523 (
<ulink url=
"https://www.postgresql.org/support/security/CVE-2024-10977/">CVE-
2024-
10977</ulink>).
1524 In this case the connection must
1525 be closed, but the frontend might choose to open a fresh connection
1526 and proceed without requesting
<acronym>SSL
</acronym>.
1530 When
<acronym>SSL
</acronym> encryption can be performed, the server
1531 is expected to send only the single
<literal>S
</literal> byte and then
1532 wait for the frontend to initiate an
<acronym>SSL
</acronym> handshake.
1533 If additional bytes are available to read at this point, it likely
1534 means that a man-in-the-middle is attempting to perform a
1535 buffer-stuffing attack
1536 (
<ulink url=
"https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-
2021-
23222</ulink>).
1537 Frontends should be coded either to read exactly one byte from the
1538 socket before turning the socket over to their SSL library, or to
1539 treat it as a protocol violation if they find they have read additional
1544 Likewise the server expects the client to not begin
1545 the
<acronym>SSL
</acronym> negotiation until it receives the server's
1546 single byte response to the
<acronym>SSL
</acronym> request. If the
1547 client begins the
<acronym>SSL
</acronym> negotiation immediately without
1548 waiting for the server response to be received it can reduce connection
1549 latency by one round-trip. However this comes at the cost of not being
1550 able to handle the case where the server sends a negative response to the
1551 <acronym>SSL
</acronym> request. In that case instead of continuing with either GSSAPI or an
1552 unencrypted connection or a protocol error the server will simply
1557 An initial SSLRequest can also be used in a connection that is being
1558 opened to send a CancelRequest message.
1562 A second alternate way to initiate
<acronym>SSL
</acronym> encryption is
1563 available. The server will recognize connections which immediately
1564 begin
<acronym>SSL
</acronym> negotiation without any previous SSLRequest
1565 packets. Once the
<acronym>SSL
</acronym> connection is established the
1566 server will expect a normal startup-request packet and continue
1567 negotiation over the encrypted channel. In this case any other requests
1568 for encryption will be refused. This method is not preferred for general
1569 purpose tools as it cannot negotiate the best connection encryption
1570 available or handle unencrypted connections. However it is useful for
1571 environments where both the server and client are controlled together.
1572 In that case it avoids one round trip of latency and allows the use of
1573 network tools that depend on standard
<acronym>SSL
</acronym> connections.
1574 When using
<acronym>SSL
</acronym> connections in this style the client is
1575 required to use the ALPN extension defined
1576 by
<ulink url=
"https://tools.ietf.org/html/rfc7301">RFC
7301</ulink> to
1577 protect against protocol confusion attacks.
1578 The
<productname>PostgreSQL
</productname> protocol is
"postgresql" as
1580 at
<ulink url=
"https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids">IANA
1581 TLS ALPN Protocol IDs
</ulink> registry.
1585 While the protocol itself does not provide a way for the server to
1586 force
<acronym>SSL
</acronym> encryption, the administrator can
1587 configure the server to reject unencrypted sessions as a byproduct
1588 of authentication checking.
1592 <sect2 id=
"protocol-flow-gssapi">
1593 <title><acronym>GSSAPI
</acronym> Session Encryption
</title>
1596 If
<productname>PostgreSQL
</productname> was built with
1597 <acronym>GSSAPI
</acronym> support, frontend/backend communications
1598 can be encrypted using
<acronym>GSSAPI
</acronym>. This provides
1599 communication security in environments where attackers might be
1600 able to capture the session traffic. For more information on
1601 encrypting
<productname>PostgreSQL
</productname> sessions with
1602 <acronym>GSSAPI
</acronym>, see
<xref linkend=
"gssapi-enc"/>.
1606 To initiate a
<acronym>GSSAPI
</acronym>-encrypted connection, the
1607 frontend initially sends a GSSENCRequest message rather than a
1608 StartupMessage. The server then responds with a single byte
1609 containing
<literal>G
</literal> or
<literal>N
</literal>, indicating that it
1610 is willing or unwilling to perform
<acronym>GSSAPI
</acronym> encryption,
1611 respectively. The frontend might close the connection at this point
1612 if it is dissatisfied with the response. To continue after
1613 <literal>G
</literal>, using the GSSAPI C bindings as discussed in
1614 <ulink url=
"https://datatracker.ietf.org/doc/html/rfc2744">RFC
2744</ulink>
1615 or equivalent, perform a
<acronym>GSSAPI
</acronym> initialization by
1616 calling
<function>gss_init_sec_context()
</function> in a loop and sending
1617 the result to the server, starting with an empty input and then with each
1618 result from the server, until it returns no output. When sending the
1619 results of
<function>gss_init_sec_context()
</function> to the server,
1620 prepend the length of the message as a four byte integer in network byte
1623 <literal>N
</literal>, send the usual StartupMessage and proceed without
1625 (Alternatively, it is permissible to issue an SSLRequest message
1626 after an
<literal>N
</literal> response to try to
1627 use
<acronym>SSL
</acronym> encryption instead
1628 of
<acronym>GSSAPI
</acronym>.)
1632 The frontend should also be prepared to handle an ErrorMessage
1633 response to GSSENCRequest from the server. The frontend should not display
1634 this error message to the user/application, since the server has not been
1636 (
<ulink url=
"https://www.postgresql.org/support/security/CVE-2024-10977/">CVE-
2024-
10977</ulink>).
1637 In this case the connection must be closed, but the frontend might choose
1638 to open a fresh connection and proceed without requesting
1639 <acronym>GSSAPI
</acronym> encryption.
1643 When
<acronym>GSSAPI
</acronym> encryption can be performed, the server
1644 is expected to send only the single
<literal>G
</literal> byte and then
1645 wait for the frontend to initiate a
<acronym>GSSAPI
</acronym> handshake.
1646 If additional bytes are available to read at this point, it likely
1647 means that a man-in-the-middle is attempting to perform a
1648 buffer-stuffing attack
1649 (
<ulink url=
"https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-
2021-
23222</ulink>).
1650 Frontends should be coded either to read exactly one byte from the
1651 socket before turning the socket over to their GSSAPI library, or to
1652 treat it as a protocol violation if they find they have read additional
1657 An initial GSSENCRequest can also be used in a connection that is being
1658 opened to send a CancelRequest message.
1662 Once
<acronym>GSSAPI
</acronym> encryption has been successfully
1663 established, use
<function>gss_wrap()
</function> to
1664 encrypt the usual StartupMessage and all subsequent data, prepending the
1665 length of the result from
<function>gss_wrap()
</function> as a four byte
1666 integer in network byte order to the actual encrypted payload. Note that
1667 the server will only accept encrypted packets from the client which are less
1668 than
16kB;
<function>gss_wrap_size_limit()
</function> should be used by the
1669 client to determine the size of the unencrypted message which will fit
1670 within this limit and larger messages should be broken up into multiple
1671 <function>gss_wrap()
</function> calls. Typical segments are
8kB of
1672 unencrypted data, resulting in encrypted packets of slightly larger than
8kB
1673 but well within the
16kB maximum. The server can be expected to not send
1674 encrypted packets of larger than
16kB to the client.
1678 While the protocol itself does not provide a way for the server to
1679 force
<acronym>GSSAPI
</acronym> encryption, the administrator can
1680 configure the server to reject unencrypted sessions as a byproduct
1681 of authentication checking.
1686 <sect1 id=
"sasl-authentication">
1687 <title>SASL Authentication
</title>
1690 <firstterm>SASL
</firstterm> is a framework for authentication in connection-oriented
1691 protocols. At the moment,
<productname>PostgreSQL
</productname> implements two SASL
1692 authentication mechanisms, SCRAM-SHA-
256 and SCRAM-SHA-
256-PLUS. More
1693 might be added in the future. The below steps illustrate how SASL
1694 authentication is performed in general, while the next subsection gives
1695 more details on SCRAM-SHA-
256 and SCRAM-SHA-
256-PLUS.
1699 <title>SASL Authentication Message Flow
</title>
1701 <step id=
"sasl-auth-begin">
1703 To begin a SASL authentication exchange, the server sends an
1704 AuthenticationSASL message. It includes a list of SASL authentication
1705 mechanisms that the server can accept, in the server's preferred order.
1709 <step id=
"sasl-auth-initial-response">
1711 The client selects one of the supported mechanisms from the list, and sends
1712 a SASLInitialResponse message to the server. The message includes the name
1713 of the selected mechanism, and an optional Initial Client Response, if the
1714 selected mechanism uses that.
1718 <step id=
"sasl-auth-continue">
1720 One or more server-challenge and client-response message will follow. Each
1721 server-challenge is sent in an AuthenticationSASLContinue message, followed
1722 by a response from client in a SASLResponse message. The particulars of
1723 the messages are mechanism specific.
1727 <step id=
"sasl-auth-end">
1729 Finally, when the authentication exchange is completed successfully, the
1730 server sends an AuthenticationSASLFinal message, followed
1731 immediately by an AuthenticationOk message. The AuthenticationSASLFinal
1732 contains additional server-to-client data, whose content is particular to the
1733 selected authentication mechanism. If the authentication mechanism doesn't
1734 use additional data that's sent at completion, the AuthenticationSASLFinal
1735 message is not sent.
1741 On error, the server can abort the authentication at any stage, and send an
1745 <sect2 id=
"sasl-scram-sha-256">
1746 <title>SCRAM-SHA-
256 Authentication
</title>
1749 The implemented SASL mechanisms at the moment
1750 are
<literal>SCRAM-SHA-
256</literal> and its variant with channel
1751 binding
<literal>SCRAM-SHA-
256-PLUS
</literal>. They are described in
1752 detail in
<ulink url=
"https://datatracker.ietf.org/doc/html/rfc7677">RFC
7677</ulink>
1753 and
<ulink url=
"https://datatracker.ietf.org/doc/html/rfc5802">RFC
5802</ulink>.
1757 When SCRAM-SHA-
256 is used in PostgreSQL, the server will ignore the user name
1758 that the client sends in the
<structname>client-first-message
</structname>. The user name
1759 that was already sent in the startup message is used instead.
1760 <productname>PostgreSQL
</productname> supports multiple character encodings, while SCRAM
1761 dictates UTF-
8 to be used for the user name, so it might be impossible to
1762 represent the PostgreSQL user name in UTF-
8.
1766 The SCRAM specification dictates that the password is also in UTF-
8, and is
1767 processed with the
<firstterm>SASLprep
</firstterm> algorithm.
1768 <productname>PostgreSQL
</productname>, however, does not require UTF-
8 to be used for
1769 the password. When a user's password is set, it is processed with SASLprep
1770 as if it was in UTF-
8, regardless of the actual encoding used. However, if
1771 it is not a legal UTF-
8 byte sequence, or it contains UTF-
8 byte sequences
1772 that are prohibited by the SASLprep algorithm, the raw password will be used
1773 without SASLprep processing, instead of throwing an error. This allows the
1774 password to be normalized when it is in UTF-
8, but still allows a non-UTF-
8
1775 password to be used, and doesn't require the system to know which encoding
1780 <firstterm>Channel binding
</firstterm> is supported in PostgreSQL builds with
1781 SSL support. The SASL mechanism name for SCRAM with channel binding is
1782 <literal>SCRAM-SHA-
256-PLUS
</literal>. The channel binding type used by
1783 PostgreSQL is
<literal>tls-server-end-point
</literal>.
1787 In
<acronym>SCRAM
</acronym> without channel binding, the server chooses
1788 a random number that is transmitted to the client to be mixed with the
1789 user-supplied password in the transmitted password hash. While this
1790 prevents the password hash from being successfully retransmitted in
1791 a later session, it does not prevent a fake server between the real
1792 server and client from passing through the server's random value
1793 and successfully authenticating.
1797 <acronym>SCRAM
</acronym> with channel binding prevents such
1798 man-in-the-middle attacks by mixing the signature of the server's
1799 certificate into the transmitted password hash. While a fake server can
1800 retransmit the real server's certificate, it doesn't have access to the
1801 private key matching that certificate, and therefore cannot prove it is
1802 the owner, causing SSL connection failure.
1806 <title>Example
</title>
1807 <step id=
"scram-begin">
1809 The server sends an AuthenticationSASL message. It includes a list of
1810 SASL authentication mechanisms that the server can accept.
1811 This will be
<literal>SCRAM-SHA-
256-PLUS
</literal>
1812 and
<literal>SCRAM-SHA-
256</literal> if the server is built with SSL
1813 support, or else just the latter.
1817 <step id=
"scram-client-first">
1819 The client responds by sending a SASLInitialResponse message, which
1820 indicates the chosen mechanism,
<literal>SCRAM-SHA-
256</literal> or
1821 <literal>SCRAM-SHA-
256-PLUS
</literal>. (A client is free to choose either
1822 mechanism, but for better security it should choose the channel-binding
1823 variant if it can support it.) In the Initial Client response field, the
1824 message contains the SCRAM
<structname>client-first-message
</structname>.
1825 The
<structname>client-first-message
</structname> also contains the channel
1826 binding type chosen by the client.
1830 <step id=
"scram-server-first">
1832 Server sends an AuthenticationSASLContinue message, with a SCRAM
1833 <structname>server-first-message
</structname> as the content.
1837 <step id=
"scram-client-final">
1839 Client sends a SASLResponse message, with SCRAM
1840 <structname>client-final-message
</structname> as the content.
1844 <step id=
"scram-server-final">
1846 Server sends an AuthenticationSASLFinal message, with the SCRAM
1847 <structname>server-final-message
</structname>, followed immediately by
1848 an AuthenticationOk message.
1855 <sect1 id=
"protocol-replication">
1856 <title>Streaming Replication Protocol
</title>
1859 To initiate streaming replication, the frontend sends the
1860 <literal>replication
</literal> parameter in the startup message. A Boolean
1861 value of
<literal>true
</literal> (or
<literal>on
</literal>,
1862 <literal>yes
</literal>,
<literal>1</literal>) tells the backend to go into
1863 physical replication walsender mode, wherein a small set of replication
1864 commands, shown below, can be issued instead of SQL statements.
1868 Passing
<literal>database
</literal> as the value for the
1869 <literal>replication
</literal> parameter instructs the backend to go into
1870 logical replication walsender mode, connecting to the database specified in
1871 the
<literal>dbname
</literal> parameter. In logical replication walsender
1872 mode, the replication commands shown below as well as normal SQL commands can
1877 In either physical replication or logical replication walsender mode, only the
1878 simple query protocol can be used.
1882 For the purpose of testing replication commands, you can make a replication
1883 connection via
<application>psql
</application> or any other
1884 <application>libpq
</application>-using tool with a connection string including
1885 the
<literal>replication
</literal> option,
1888 psql
"dbname=postgres replication=database" -c
"IDENTIFY_SYSTEM;"
1890 However, it is often more useful to use
1891 <xref linkend=
"app-pgreceivewal"/> (for physical replication) or
1892 <xref linkend=
"app-pgrecvlogical"/> (for logical replication).
1896 Replication commands are logged in the server log when
1897 <xref linkend=
"guc-log-replication-commands"/> is enabled.
1901 The commands accepted in replication mode are:
1904 <varlistentry id=
"protocol-replication-identify-system">
1905 <term><literal>IDENTIFY_SYSTEM
</literal>
1906 <indexterm><primary>IDENTIFY_SYSTEM
</primary></indexterm>
1910 Requests the server to identify itself. Server replies with a result
1911 set of a single row, containing four fields:
1916 <term><literal>systemid
</literal> (
<type>text
</type>)
</term>
1919 The unique system identifier identifying the cluster. This
1920 can be used to check that the base backup used to initialize the
1921 standby came from the same cluster.
1927 <term><literal>timeline
</literal> (
<type>int8
</type>)
</term>
1930 Current timeline ID. Also useful to check that the standby is
1931 consistent with the primary.
1937 <term><literal>xlogpos
</literal> (
<type>text
</type>)
</term>
1940 Current WAL flush location. Useful to get a known location in the
1941 write-ahead log where streaming can start.
1947 <term><literal>dbname
</literal> (
<type>text
</type>)
</term>
1950 Database connected to or null.
1958 <varlistentry id=
"protocol-replication-show">
1959 <term><literal>SHOW
</literal> <replaceable class=
"parameter">name
</replaceable>
1960 <indexterm><primary>SHOW
</primary></indexterm>
1964 Requests the server to send the current setting of a run-time parameter.
1965 This is similar to the SQL command
<xref linkend=
"sql-show"/>.
1970 <term><replaceable class=
"parameter">name
</replaceable></term>
1973 The name of a run-time parameter. Available parameters are documented
1974 in
<xref linkend=
"runtime-config"/>.
1982 <varlistentry id=
"protocol-replication-timeline-history">
1983 <term><literal>TIMELINE_HISTORY
</literal> <replaceable class=
"parameter">tli
</replaceable>
1984 <indexterm><primary>TIMELINE_HISTORY
</primary></indexterm>
1988 Requests the server to send over the timeline history file for timeline
1989 <replaceable class=
"parameter">tli
</replaceable>. Server replies with a
1990 result set of a single row, containing two fields. While the fields
1991 are labeled as
<type>text
</type>, they effectively return raw bytes,
1992 with no encoding conversion:
1997 <term><literal>filename
</literal> (
<type>text
</type>)
</term>
2000 File name of the timeline history file, e.g.,
<filename>00000002.history
</filename>.
2006 <term><literal>content
</literal> (
<type>text
</type>)
</term>
2009 Contents of the timeline history file.
2017 <varlistentry id=
"protocol-replication-create-replication-slot" xreflabel=
"CREATE_REPLICATION_SLOT">
2018 <term><literal>CREATE_REPLICATION_SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> [
<literal>TEMPORARY
</literal> ] {
<literal>PHYSICAL
</literal> |
<literal>LOGICAL
</literal> <replaceable class=
"parameter">output_plugin
</replaceable> } [ (
<replaceable class=
"parameter">option
</replaceable> [, ...] ) ]
2019 <indexterm><primary>CREATE_REPLICATION_SLOT
</primary></indexterm>
2023 Create a physical or logical replication
2024 slot. See
<xref linkend=
"streaming-replication-slots"/> for more about
2030 <term><replaceable class=
"parameter">slot_name
</replaceable></term>
2033 The name of the slot to create. Must be a valid replication slot
2034 name (see
<xref linkend=
"streaming-replication-slots-manipulation"/>).
2040 <term><replaceable class=
"parameter">output_plugin
</replaceable></term>
2043 The name of the output plugin used for logical decoding
2044 (see
<xref linkend=
"logicaldecoding-output-plugin"/>).
2050 <term><literal>TEMPORARY
</literal></term>
2053 Specify that this replication slot is a temporary one. Temporary
2054 slots are not saved to disk and are automatically dropped on error
2055 or when the session has finished.
2061 <para>The following options are supported:
</para>
2065 <term><literal>TWO_PHASE [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2068 If true, this logical replication slot supports decoding of two-phase
2069 commit. With this option, commands related to two-phase commit such as
2070 <literal>PREPARE TRANSACTION
</literal>,
<literal>COMMIT PREPARED
</literal>
2071 and
<literal>ROLLBACK PREPARED
</literal> are decoded and transmitted.
2072 The transaction will be decoded and transmitted at
2073 <literal>PREPARE TRANSACTION
</literal> time.
2074 The default is false.
2080 <term><literal>RESERVE_WAL [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2083 If true, this physical replication slot reserves
<acronym>WAL
</acronym>
2084 immediately. Otherwise,
<acronym>WAL
</acronym> is only reserved upon
2085 connection from a streaming replication client.
2086 The default is false.
2092 <term><literal>SNAPSHOT { 'export' | 'use' | 'nothing' }
</literal></term>
2095 Decides what to do with the snapshot created during logical slot
2096 initialization.
<literal>'export'
</literal>, which is the default,
2097 will export the snapshot for use in other sessions. This option can't
2098 be used inside a transaction.
<literal>'use'
</literal> will use the
2099 snapshot for the current transaction executing the command. This
2100 option must be used in a transaction, and
2101 <literal>CREATE_REPLICATION_SLOT
</literal> must be the first command
2102 run in that transaction. Finally,
<literal>'nothing'
</literal> will
2103 just use the snapshot for logical decoding as normal but won't do
2104 anything else with it.
2110 <term><literal>FAILOVER [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2113 If true, the slot is enabled to be synced to the standbys
2114 so that logical replication can be resumed after failover.
2115 The default is false.
2122 In response to this command, the server will send a one-row result set
2123 containing the following fields:
2127 <term><literal>slot_name
</literal> (
<type>text
</type>)
</term>
2130 The name of the newly-created replication slot.
2136 <term><literal>consistent_point
</literal> (
<type>text
</type>)
</term>
2139 The WAL location at which the slot became consistent. This is the
2140 earliest location from which streaming can start on this replication
2147 <term><literal>snapshot_name
</literal> (
<type>text
</type>)
</term>
2150 The identifier of the snapshot exported by the command. The
2151 snapshot is valid until a new command is executed on this connection
2152 or the replication connection is closed. Null if the created slot
2159 <term><literal>output_plugin
</literal> (
<type>text
</type>)
</term>
2162 The name of the output plugin used by the newly-created replication
2163 slot. Null if the created slot is physical.
2172 <varlistentry id=
"protocol-replication-create-replication-slot-legacy">
2173 <term><literal>CREATE_REPLICATION_SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> [
<literal>TEMPORARY
</literal> ] {
<literal>PHYSICAL
</literal> [
<literal>RESERVE_WAL
</literal> ] |
<literal>LOGICAL
</literal> <replaceable class=
"parameter">output_plugin
</replaceable> [
<literal>EXPORT_SNAPSHOT
</literal> |
<literal>NOEXPORT_SNAPSHOT
</literal> |
<literal>USE_SNAPSHOT
</literal> |
<literal>TWO_PHASE
</literal> ] }
2177 For compatibility with older releases, this alternative syntax for
2178 the
<literal>CREATE_REPLICATION_SLOT
</literal> command is still supported.
2183 <varlistentry id=
"protocol-replication-alter-replication-slot" xreflabel=
"ALTER_REPLICATION_SLOT">
2184 <term><literal>ALTER_REPLICATION_SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> (
<replaceable class=
"parameter">option
</replaceable> [, ...] )
2185 <indexterm><primary>ALTER_REPLICATION_SLOT
</primary></indexterm>
2189 Change the definition of a replication slot.
2190 See
<xref linkend=
"streaming-replication-slots"/> for more about
2191 replication slots. This command is currently only supported for logical
2197 <term><replaceable class=
"parameter">slot_name
</replaceable></term>
2200 The name of the slot to alter. Must be a valid replication slot
2201 name (see
<xref linkend=
"streaming-replication-slots-manipulation"/>).
2207 <para>The following options are supported:
</para>
2211 <term><literal>TWO_PHASE [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2214 If true, this logical replication slot supports decoding of two-phase
2215 commit. With this option, commands related to two-phase commit such as
2216 <literal>PREPARE TRANSACTION
</literal>,
<literal>COMMIT PREPARED
</literal>
2217 and
<literal>ROLLBACK PREPARED
</literal> are decoded and transmitted.
2218 The transaction will be decoded and transmitted at
2219 <literal>PREPARE TRANSACTION
</literal> time.
2227 <term><literal>FAILOVER [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2230 If true, the slot is enabled to be synced to the standbys
2231 so that logical replication can be resumed after failover.
2240 <varlistentry id=
"protocol-replication-read-replication-slot">
2241 <term><literal>READ_REPLICATION_SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable>
2242 <indexterm><primary>READ_REPLICATION_SLOT
</primary></indexterm>
2246 Read some information associated with a replication slot. Returns a tuple
2247 with
<literal>NULL
</literal> values if the replication slot does not
2248 exist. This command is currently only supported for physical replication
2253 In response to this command, the server will return a one-row result set,
2254 containing the following fields:
2257 <term><literal>slot_type
</literal> (
<type>text
</type>)
</term>
2260 The replication slot's type, either
<literal>physical
</literal> or
2261 <literal>NULL
</literal>.
2267 <term><literal>restart_lsn
</literal> (
<type>text
</type>)
</term>
2270 The replication slot's
<literal>restart_lsn
</literal>.
2276 <term><literal>restart_tli
</literal> (
<type>int8
</type>)
</term>
2279 The timeline ID associated with
<literal>restart_lsn
</literal>,
2280 following the current timeline history.
2289 <varlistentry id=
"protocol-replication-start-replication">
2290 <term><literal>START_REPLICATION
</literal> [
<literal>SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> ] [
<literal>PHYSICAL
</literal> ]
<replaceable class=
"parameter">XXX/XXX
</replaceable> [
<literal>TIMELINE
</literal> <replaceable class=
"parameter">tli
</replaceable> ]
2291 <indexterm><primary>START_REPLICATION
</primary></indexterm>
2295 Instructs server to start streaming WAL, starting at
2296 WAL location
<replaceable class=
"parameter">XXX/XXX
</replaceable>.
2297 If
<literal>TIMELINE
</literal> option is specified,
2298 streaming starts on timeline
<replaceable class=
"parameter">tli
</replaceable>;
2299 otherwise, the server's current timeline is selected. The server can
2300 reply with an error, for example if the requested section of WAL has already
2301 been recycled. On success, the server responds with a CopyBothResponse
2302 message, and then starts to stream WAL to the frontend.
2306 If a slot's name is provided
2307 via
<replaceable class=
"parameter">slot_name
</replaceable>, it will be updated
2308 as replication progresses so that the server knows which WAL segments,
2309 and if
<varname>hot_standby_feedback
</varname> is on which transactions,
2310 are still needed by the standby.
2314 If the client requests a timeline that's not the latest but is part of
2315 the history of the server, the server will stream all the WAL on that
2316 timeline starting from the requested start point up to the point where
2317 the server switched to another timeline. If the client requests
2318 streaming at exactly the end of an old timeline, the server skips COPY
2323 After streaming all the WAL on a timeline that is not the latest one,
2324 the server will end streaming by exiting the COPY mode. When the client
2325 acknowledges this by also exiting COPY mode, the server sends a result
2326 set with one row and two columns, indicating the next timeline in this
2327 server's history. The first column is the next timeline's ID (type
<type>int8
</type>), and the
2328 second column is the WAL location where the switch happened (type
<type>text
</type>). Usually,
2329 the switch position is the end of the WAL that was streamed, but there
2330 are corner cases where the server can send some WAL from the old
2331 timeline that it has not itself replayed before promoting. Finally, the
2332 server sends two CommandComplete messages (one that ends the CopyData
2333 and the other ends the
<literal>START_REPLICATION
</literal> itself), and
2334 is ready to accept a new command.
2338 WAL data is sent as a series of CopyData messages;
2339 see
<xref linkend=
"protocol-message-types"/> and
<xref
2340 linkend=
"protocol-message-formats"/> for details.
2341 (This allows other information to be intermixed; in particular the server can send
2342 an ErrorResponse message if it encounters a failure after beginning
2343 to stream.) The payload of each CopyData message from server to the
2344 client contains a message of one of the following formats:
2348 <varlistentry id=
"protocol-replication-xlogdata">
2349 <term>XLogData (B)
</term>
2353 <term>Byte1('w')
</term>
2356 Identifies the message as WAL data.
2365 The starting point of the WAL data in this message.
2374 The current end of WAL on the server.
2383 The server's system clock at the time of transmission, as
2384 microseconds since midnight on
2000-
01-
01.
2390 <term>Byte
<replaceable>n
</replaceable></term>
2393 A section of the WAL data stream.
2397 A single WAL record is never split across two XLogData messages.
2398 When a WAL record crosses a WAL page boundary, and is therefore
2399 already split using continuation records, it can be split at the page
2400 boundary. In other words, the first main WAL record and its
2401 continuation records can be sent in different XLogData messages.
2409 <varlistentry id=
"protocol-replication-primary-keepalive-message">
2410 <term>Primary keepalive message (B)
</term>
2414 <term>Byte1('k')
</term>
2417 Identifies the message as a sender keepalive.
2426 The current end of WAL on the server.
2435 The server's system clock at the time of transmission, as
2436 microseconds since midnight on
2000-
01-
01.
2445 1 means that the client should reply to this message as soon as
2446 possible, to avoid a timeout disconnect.
0 otherwise.
2456 The receiving process can send replies back to the sender at any time,
2457 using one of the following message formats (also in the payload of a
2462 <varlistentry id=
"protocol-replication-standby-status-update">
2463 <term>Standby status update (F)
</term>
2467 <term>Byte1('r')
</term>
2470 Identifies the message as a receiver status update.
2479 The location of the last WAL byte +
1 received and written to disk
2489 The location of the last WAL byte +
1 flushed to disk in
2499 The location of the last WAL byte +
1 applied in the standby.
2508 The client's system clock at the time of transmission, as
2509 microseconds since midnight on
2000-
01-
01.
2518 If
1, the client requests the server to reply to this message
2519 immediately. This can be used to ping the server, to test if
2520 the connection is still healthy.
2528 <varlistentry id=
"protocol-replication-hot-standby-feedback-message">
2529 <term>Hot standby feedback message (F)
</term>
2533 <term>Byte1('h')
</term>
2536 Identifies the message as a hot standby feedback message.
2545 The client's system clock at the time of transmission, as
2546 microseconds since midnight on
2000-
01-
01.
2555 The standby's current global
<literal>xmin
</literal>, excluding the
2556 <literal>catalog_xmin
</literal> from any replication slots. If both
2557 this value and the following
<literal>catalog_xmin
</literal>
2558 are
0, this is treated as a notification that hot standby feedback
2559 will no longer be sent on this connection. Later non-zero messages
2560 may reinitiate the feedback mechanism.
2569 The epoch of the global
<literal>xmin
</literal> xid on the standby.
2578 The lowest
<literal>catalog_xmin
</literal> of any replication
2579 slots on the standby. Set to
0 if no
<literal>catalog_xmin
</literal>
2580 exists on the standby or if hot standby feedback is being
2590 The epoch of the
<literal>catalog_xmin
</literal> xid on the standby.
2601 <varlistentry id=
"protocol-replication-start-replication-slot-logical">
2602 <term><literal>START_REPLICATION
</literal> <literal>SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> <literal>LOGICAL
</literal> <replaceable class=
"parameter">XXX/XXX
</replaceable> [ (
<replaceable>option_name
</replaceable> [
<replaceable>option_value
</replaceable> ] [, ...] ) ]
</term>
2605 Instructs server to start streaming WAL for logical replication,
2606 starting at either WAL location
<replaceable
2607 class=
"parameter">XXX/XXX
</replaceable> or the slot's
2608 <literal>confirmed_flush_lsn
</literal> (see
<xref
2609 linkend=
"view-pg-replication-slots"/>), whichever is greater. This
2610 behavior makes it easier for clients to avoid updating their local LSN
2611 status when there is no data to process. However, starting at a
2612 different LSN than requested might not catch certain kinds of client
2613 errors; so the client may wish to check that
2614 <literal>confirmed_flush_lsn
</literal> matches its expectations before
2615 issuing
<literal>START_REPLICATION
</literal>.
2619 The server can reply with an error, for example if the
2620 slot does not exist. On success, the server responds with a CopyBothResponse
2621 message, and then starts to stream WAL to the frontend.
2625 The messages inside the CopyBothResponse messages are of the same format
2626 documented for
<literal>START_REPLICATION ... PHYSICAL
</literal>, including
2627 two CommandComplete messages.
2631 The output plugin associated with the selected slot is used
2632 to process the output for streaming.
2637 <term><literal>SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable></term>
2640 The name of the slot to stream changes from. This parameter is required,
2641 and must correspond to an existing logical replication slot created
2642 with
<literal>CREATE_REPLICATION_SLOT
</literal> in
2643 <literal>LOGICAL
</literal> mode.
2649 <term><replaceable class=
"parameter">XXX/XXX
</replaceable></term>
2652 The WAL location to begin streaming at.
2658 <term><replaceable class=
"parameter">option_name
</replaceable></term>
2661 The name of an option passed to the slot's logical decoding output
2662 plugin. See
<xref linkend=
"protocol-logical-replication"/> for
2663 options that are accepted by the standard (
<literal>pgoutput
</literal>)
2670 <term><replaceable class=
"parameter">option_value
</replaceable></term>
2673 Optional value, in the form of a string constant, associated with the
2682 <varlistentry id=
"protocol-replication-drop-replication-slot">
2684 <literal>DROP_REPLICATION_SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable> <optional> <literal>WAIT
</literal> </optional>
2685 <indexterm><primary>DROP_REPLICATION_SLOT
</primary></indexterm>
2689 Drops a replication slot, freeing any reserved server-side resources.
2690 If the slot is a logical slot that was created in a database other than
2691 the database the walsender is connected to, this command fails.
2696 <term><replaceable class=
"parameter">slot_name
</replaceable></term>
2699 The name of the slot to drop.
2705 <term><literal>WAIT
</literal></term>
2708 This option causes the command to wait if the slot is active until
2709 it becomes inactive, instead of the default behavior of raising an
2718 <varlistentry id=
"protocol-replication-upload-manifest">
2720 <literal>UPLOAD_MANIFEST
</literal>
2721 <indexterm><primary>UPLOAD_MANIFEST
</primary></indexterm>
2725 Uploads a backup manifest in preparation for taking an incremental
2731 <varlistentry id=
"protocol-replication-base-backup" xreflabel=
"BASE_BACKUP">
2732 <term><literal>BASE_BACKUP
</literal> [ (
<replaceable class=
"parameter">option
</replaceable> [, ...] ) ]
2733 <indexterm><primary>BASE_BACKUP
</primary></indexterm>
2737 Instructs the server to start streaming a base backup.
2738 The system will automatically be put in backup mode before the backup
2739 is started, and taken out of it when the backup is complete. The
2740 following options are accepted:
2744 <term><literal>LABEL
</literal> <replaceable>'label'
</replaceable></term>
2747 Sets the label of the backup. If none is specified, a backup label
2748 of
<literal>base backup
</literal> will be used. The quoting rules
2749 for the label are the same as a standard SQL string with
2750 <xref linkend=
"guc-standard-conforming-strings"/> turned on.
2756 <term><literal>TARGET
</literal> <replaceable>'target'
</replaceable></term>
2759 Tells the server where to send the backup. If the target is
2760 <literal>client
</literal>, which is the default, the backup data is
2761 sent to the client. If it is
<literal>server
</literal>, the backup
2762 data is written to the server at the pathname specified by the
2763 <literal>TARGET_DETAIL
</literal> option. If it is
2764 <literal>blackhole
</literal>, the backup data is not sent
2765 anywhere; it is simply discarded.
2769 The
<literal>server
</literal> target requires superuser privilege or
2770 being granted the
<literal>pg_write_server_files
</literal> role.
2776 <term><literal>TARGET_DETAIL
</literal> <replaceable>'detail'
</replaceable></term>
2779 Provides additional information about the backup target.
2783 Currently, this option can only be used when the backup target is
2784 <literal>server
</literal>. It specifies the server directory
2785 to which the backup should be written.
2791 <term><literal>PROGRESS [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2794 If set to true, request information required to generate a progress
2795 report. This will send back an approximate size in the header of each
2796 tablespace, which can be used to calculate how far along the stream
2797 is done. This is calculated by enumerating all the file sizes once
2798 before the transfer is even started, and might as such have a
2799 negative impact on the performance. In particular, it might take
2800 longer before the first data
2801 is streamed. Since the database files can change during the backup,
2802 the size is only approximate and might both grow and shrink between
2803 the time of approximation and the sending of the actual files.
2804 The default is false.
2810 <term><literal>CHECKPOINT { 'fast' | 'spread' }
</literal></term>
2813 Sets the type of checkpoint to be performed at the beginning of the
2814 base backup. The default is
<literal>spread
</literal>.
2820 <term><literal>WAL [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2823 If set to true, include the necessary WAL segments in the backup.
2824 This will include all the files between start and stop backup in the
2825 <filename>pg_wal
</filename> directory of the base directory tar
2826 file. The default is false.
2832 <term><literal>WAIT [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2835 If set to true, the backup will wait until the last required WAL
2836 segment has been archived, or emit a warning if WAL archiving is
2837 not enabled. If false, the backup will neither wait nor warn,
2838 leaving the client responsible for ensuring the required log is
2839 available. The default is true.
2845 <term><literal>COMPRESSION
</literal> <replaceable>'method'
</replaceable></term>
2848 Instructs the server to compress the backup using the specified
2849 method. Currently, the supported methods are
<literal>gzip
</literal>,
2850 <literal>lz4
</literal>, and
<literal>zstd
</literal>.
2856 <term><literal>COMPRESSION_DETAIL
</literal> <replaceable>detail
</replaceable></term>
2859 Specifies details for the chosen compression method. This should only
2860 be used in conjunction with the
<literal>COMPRESSION
</literal>
2861 option. If the value is an integer, it specifies the compression
2862 level. Otherwise, it should be a comma-separated list of items,
2863 each of the form
<replaceable>keyword
</replaceable> or
2864 <replaceable>keyword=value
</replaceable>. Currently, the supported
2865 keywords are
<literal>level
</literal>,
<literal>long
</literal> and
2866 <literal>workers
</literal>.
2870 The
<literal>level
</literal> keyword sets the compression level.
2871 For
<literal>gzip
</literal> the compression level should be an
2872 integer between
<literal>1</literal> and
<literal>9</literal>
2873 (default
<literal>Z_DEFAULT_COMPRESSION
</literal> or
2874 <literal>-
1</literal>), for
<literal>lz4
</literal> an integer
2875 between
1 and
12 (default
<literal>0</literal> for fast compression
2876 mode), and for
<literal>zstd
</literal> an integer between
2877 <literal>ZSTD_minCLevel()
</literal> (usually
<literal>-
131072</literal>)
2878 and
<literal>ZSTD_maxCLevel()
</literal> (usually
<literal>22</literal>),
2879 (default
<literal>ZSTD_CLEVEL_DEFAULT
</literal> or
2880 <literal>3</literal>).
2884 The
<literal>long
</literal> keyword enables long-distance matching
2885 mode, for improved compression ratio, at the expense of higher memory
2886 use. Long-distance mode is supported only for
2887 <literal>zstd
</literal>.
2891 The
<literal>workers
</literal> keyword sets the number of threads
2892 that should be used for parallel compression. Parallel compression
2893 is supported only for
<literal>zstd
</literal>.
2899 <term><literal>MAX_RATE
</literal> <replaceable>rate
</replaceable></term>
2902 Limit (throttle) the maximum amount of data transferred from server
2903 to client per unit of time. The expected unit is kilobytes per second.
2904 If this option is specified, the value must either be equal to zero
2905 or it must fall within the range from
32 kB through
1 GB (inclusive).
2906 If zero is passed or the option is not specified, no restriction is
2907 imposed on the transfer.
2913 <term><literal>TABLESPACE_MAP [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2916 If true, include information about symbolic links present in the
2917 directory
<filename>pg_tblspc
</filename> in a file named
2918 <filename>tablespace_map
</filename>. The tablespace map file includes
2919 each symbolic link name as it exists in the directory
2920 <filename>pg_tblspc/
</filename> and the full path of that symbolic link.
2921 The default is false.
2927 <term><literal>VERIFY_CHECKSUMS [
<replaceable class=
"parameter">boolean
</replaceable> ]
</literal></term>
2930 If true, checksums are verified during a base backup if they are
2931 enabled. If false, this is skipped. The default is true.
2937 <term><literal>MANIFEST
</literal> <replaceable>manifest_option
</replaceable></term>
2940 When this option is specified with a value of
<literal>yes
</literal>
2941 or
<literal>force-encode
</literal>, a backup manifest is created
2942 and sent along with the backup. The manifest is a list of every
2943 file present in the backup with the exception of any WAL files that
2944 may be included. It also stores the size, last modification time, and
2945 optionally a checksum for each file.
2946 A value of
<literal>force-encode
</literal> forces all filenames
2947 to be hex-encoded; otherwise, this type of encoding is performed only
2948 for files whose names are non-UTF8 octet sequences.
2949 <literal>force-encode
</literal> is intended primarily for testing
2950 purposes, to be sure that clients which read the backup manifest
2951 can handle this case. For compatibility with previous releases,
2952 the default is
<literal>MANIFEST 'no'
</literal>.
2958 <term><literal>MANIFEST_CHECKSUMS
</literal> <replaceable>checksum_algorithm
</replaceable></term>
2961 Specifies the checksum algorithm that should be applied to each file included
2962 in the backup manifest. Currently, the available
2963 algorithms are
<literal>NONE
</literal>,
<literal>CRC32C
</literal>,
2964 <literal>SHA224
</literal>,
<literal>SHA256
</literal>,
2965 <literal>SHA384
</literal>, and
<literal>SHA512
</literal>.
2966 The default is
<literal>CRC32C
</literal>.
2972 <term><literal>INCREMENTAL
</literal></term>
2975 Requests an incremental backup. The
2976 <literal>UPLOAD_MANIFEST
</literal> command must be executed
2977 before running a base backup with this option.
2985 When the backup is started, the server will first send two
2986 ordinary result sets, followed by one or more CopyOutResponse
2991 The first ordinary result set contains the starting position of the
2992 backup, in a single row with two columns. The first column contains
2993 the start position given in XLogRecPtr format, and the second column
2994 contains the corresponding timeline ID.
2998 The second ordinary result set has one row for each tablespace.
2999 The fields in this row are:
3003 <term><literal>spcoid
</literal> (
<type>oid
</type>)
</term>
3006 The OID of the tablespace, or null if it's the base
3013 <term><literal>spclocation
</literal> (
<type>text
</type>)
</term>
3016 The full path of the tablespace directory, or null
3017 if it's the base directory.
3023 <term><literal>size
</literal> (
<type>int8
</type>)
</term>
3026 The approximate size of the tablespace, in kilobytes (
1024 bytes),
3027 if progress report has been requested; otherwise it's null.
3035 After the second regular result set, a CopyOutResponse will be sent.
3036 The payload of each CopyData message will contain a message in one of
3037 the following formats:
3042 <term>new archive (B)
</term>
3046 <term>Byte1('n')
</term>
3048 Identifies the message as indicating the start of a new archive.
3049 There will be one archive for the main data directory and one
3050 for each additional tablespace; each will use tar format
3051 (following the
<quote>ustar interchange format
</quote> specified
3052 in the POSIX
1003.1-
2008 standard).
3059 The file name for this archive.
3066 For the main data directory, an empty string. For other
3067 tablespaces, the full path to the directory from which this
3068 archive was created.
3076 <term>manifest (B)
</term>
3080 <term>Byte1('m')
</term>
3082 Identifies the message as indicating the start of the backup
3091 <term>archive or manifest data (B)
</term>
3095 <term>Byte1('d')
</term>
3097 Identifies the message as containing archive or manifest data.
3102 <term>Byte
<replaceable>n
</replaceable></term>
3112 <term>progress report (B)
</term>
3116 <term>Byte1('p')
</term>
3118 Identifies the message as a progress report.
3125 The number of bytes from the current tablespace for which
3126 processing has been completed.
3135 After the CopyOutResponse, or all such responses, have been sent, a
3136 final ordinary result set will be sent, containing the WAL end position
3137 of the backup, in the same format as the start position.
3141 The tar archive for the data directory and each tablespace will contain
3142 all files in the directories, regardless of whether they are
3143 <productname>PostgreSQL
</productname> files or other files added to the same
3144 directory. The only excluded files are:
3146 <itemizedlist spacing=
"compact" mark=
"bullet">
3149 <filename>postmaster.pid
</filename>
3154 <filename>postmaster.opts
</filename>
3159 <filename>pg_internal.init
</filename> (found in multiple directories)
3164 Various temporary files and directories created during the operation
3165 of the PostgreSQL server, such as any file or directory beginning
3166 with
<filename>pgsql_tmp
</filename> and temporary relations.
3171 Unlogged relations, except for the init fork which is required to
3172 recreate the (empty) unlogged relation on recovery.
3177 <filename>pg_wal
</filename>, including subdirectories. If the backup is run
3178 with WAL files included, a synthesized version of
<filename>pg_wal
</filename> will be
3179 included, but it will only contain the files necessary for the
3180 backup to work, not the rest of the contents.
3185 <filename>pg_dynshmem
</filename>,
<filename>pg_notify
</filename>,
3186 <filename>pg_replslot
</filename>,
<filename>pg_serial
</filename>,
3187 <filename>pg_snapshots
</filename>,
<filename>pg_stat_tmp
</filename>, and
3188 <filename>pg_subtrans
</filename> are copied as empty directories (even if
3189 they are symbolic links).
3194 Files other than regular files and directories, such as symbolic
3195 links (other than for the directories listed above) and special
3196 device and operating system files, are skipped. (Symbolic links
3197 in
<filename>pg_tblspc
</filename> are maintained.)
3201 Owner, group, and file mode are set if the underlying file system on
3202 the server supports it.
3210 In all the above commands,
3211 when specifying a parameter of type
<type>boolean
</type> the
3212 <replaceable class=
"parameter">value
</replaceable> part can be omitted,
3213 which is equivalent to specifying
<literal>TRUE
</literal>.
3217 <sect1 id=
"protocol-logical-replication">
3218 <title>Logical Streaming Replication Protocol
</title>
3221 This section describes the logical replication protocol, which is the message
3222 flow started by the
<literal>START_REPLICATION
</literal>
3223 <literal>SLOT
</literal> <replaceable class=
"parameter">slot_name
</replaceable>
3224 <literal>LOGICAL
</literal> replication command.
3228 The logical streaming replication protocol builds on the primitives of
3229 the physical streaming replication protocol.
3233 <productname>PostgreSQL
</productname> logical decoding supports output
3234 plugins.
<literal>pgoutput
</literal> is the standard one used for
3235 the built-in logical replication.
3238 <sect2 id=
"protocol-logical-replication-params">
3239 <title>Logical Streaming Replication Parameters
</title>
3242 Using the
<literal>START_REPLICATION
</literal> command,
3243 <literal>pgoutput
</literal> accepts the following options:
3252 Protocol version. Currently versions
<literal>1</literal>,
<literal>2</literal>,
3253 <literal>3</literal>, and
<literal>4</literal> are supported. A valid
3254 version is required.
3257 Version
<literal>2</literal> is supported only for server version
14
3258 and above, and it allows streaming of large in-progress transactions.
3261 Version
<literal>3</literal> is supported only for server version
15
3262 and above, and it allows streaming of two-phase commits.
3265 Version
<literal>4</literal> is supported only for server version
16
3266 and above, and it allows streams of large in-progress transactions to
3267 be applied in parallel.
3278 Comma-separated list of publication names for which to subscribe
3279 (receive changes). The individual publication names are treated
3280 as standard objects names and can be quoted the same as needed.
3281 At least one publication name is required.
3292 Boolean option to use binary transfer mode. Binary mode is faster
3293 than the text mode but slightly less robust.
3304 Boolean option to enable sending the messages that are written
3305 by
<function>pg_logical_emit_message
</function>.
3316 Boolean option to enable streaming of in-progress transactions.
3317 It accepts an additional value
"parallel" to enable sending extra
3318 information with some messages to be used for parallelisation.
3319 Minimum protocol version
2 is required to turn it on. Minimum protocol
3320 version
4 is required for the
"parallel" option.
3331 Boolean option to enable two-phase transactions. Minimum protocol
3332 version
3 is required to turn it on.
3343 Option to send changes by their origin. Possible values are
3344 <literal>none
</literal> to only send the changes that have no origin
3345 associated, or
<literal>any
</literal>
3346 to send the changes regardless of their origin. This can be used
3347 to avoid loops (infinite replication of the same data) among
3357 <sect2 id=
"protocol-logical-messages">
3358 <title>Logical Replication Protocol Messages
</title>
3361 The individual protocol messages are discussed in the following
3362 subsections. Individual messages are described in
3363 <xref linkend=
"protocol-logicalrep-message-formats"/>.
3367 All top-level protocol messages begin with a message type byte.
3368 While represented in code as a character, this is a signed byte with no
3369 associated encoding.
3373 Since the streaming replication protocol supplies a message length there
3374 is no need for top-level protocol messages to embed a length in their
3380 <sect2 id=
"protocol-logical-messages-flow">
3381 <title>Logical Replication Protocol Message Flow
</title>
3384 With the exception of the
<literal>START_REPLICATION
</literal> command and
3385 the replay progress messages, all information flows only from the backend
3390 The logical replication protocol sends individual transactions one by one.
3391 This means that all messages between a pair of Begin and Commit messages
3392 belong to the same transaction. Similarly, all messages between a pair of
3393 Begin Prepare and Prepare messages belong to the same transaction.
3394 It also sends changes of large in-progress transactions between a pair of
3395 Stream Start and Stream Stop messages. The last stream of such a transaction
3396 contains a Stream Commit or Stream Abort message.
3400 Every sent transaction contains zero or more DML messages (Insert,
3401 Update, Delete). In case of a cascaded setup it can also contain Origin
3402 messages. The origin message indicates that the transaction originated on
3403 different replication node. Since a replication node in the scope of logical
3404 replication protocol can be pretty much anything, the only identifier
3405 is the origin name. It's downstream's responsibility to handle this as
3406 needed (if needed). The Origin message is always sent before any DML
3407 messages in the transaction.
3411 Every DML message contains a relation OID, identifying the publisher's
3412 relation that was acted on. Before the first DML message for a given
3413 relation OID, a Relation message will be sent, describing the schema of
3414 that relation. Subsequently, a new Relation message will be sent if
3415 the relation's definition has changed since the last Relation message
3416 was sent for it. (The protocol assumes that the client is capable of
3417 remembering this metadata for as many relations as needed.)
3421 Relation messages identify column types by their OIDs. In the case
3422 of a built-in type, it is assumed that the client can look up that
3423 type OID locally, so no additional data is needed. For a non-built-in
3424 type OID, a Type message will be sent before the Relation message,
3425 to provide the type name associated with that OID. Thus, a client that
3426 needs to specifically identify the types of relation columns should
3427 cache the contents of Type messages, and first consult that cache to
3428 see if the type OID is defined there. If not, look up the type OID
3434 <sect1 id=
"protocol-message-types">
3435 <title>Message Data Types
</title>
3438 This section describes the base data types used in messages.
3443 <term>Int
<replaceable>n
</replaceable>(
<replaceable>i
</replaceable>)
</term>
3446 An
<replaceable>n
</replaceable>-bit integer in network byte
3447 order (most significant byte first).
3448 If
<replaceable>i
</replaceable> is specified it
3449 is the exact value that will appear, otherwise the value
3450 is variable. Eg. Int16, Int32(
42).
3456 <term>Int
<replaceable>n
</replaceable>[
<replaceable>k
</replaceable>]
</term>
3459 An array of
<replaceable>k
</replaceable>
3460 <replaceable>n
</replaceable>-bit integers, each in network
3461 byte order. The array length
<replaceable>k
</replaceable>
3462 is always determined by an earlier field in the message.
3469 <term>String(
<replaceable>s
</replaceable>)
</term>
3472 A null-terminated string (C-style string). There is no
3473 specific length limitation on strings.
3474 If
<replaceable>s
</replaceable> is specified it is the exact
3475 value that will appear, otherwise the value is variable.
3476 Eg. String, String(
"user").
3481 <emphasis>There is no predefined limit
</emphasis> on the length of a string
3482 that can be returned by the backend. Good coding strategy for a frontend
3483 is to use an expandable buffer so that anything that fits in memory can be
3484 accepted. If that's not feasible, read the full string and discard trailing
3485 characters that don't fit into your fixed-size buffer.
3492 <term>Byte
<replaceable>n
</replaceable>(
<replaceable>c
</replaceable>)
</term>
3495 Exactly
<replaceable>n
</replaceable> bytes. If the field
3496 width
<replaceable>n
</replaceable> is not a constant, it is
3497 always determinable from an earlier field in the message.
3498 If
<replaceable>c
</replaceable> is specified it is the exact
3499 value. Eg. Byte2, Byte1('\n').
3506 <sect1 id=
"protocol-message-formats">
3507 <title>Message Formats
</title>
3510 This section describes the detailed format of each message. Each is marked to
3511 indicate that it can be sent by a frontend (F), a backend (B), or both
3513 Notice that although each message includes a byte count at the beginning,
3514 the message format is defined so that the message end can be found without
3515 reference to the byte count. This aids validity checking. (The CopyData
3516 message is an exception, because it forms part of a data stream; the contents
3517 of any individual CopyData message cannot be interpretable on their own.)
3521 <varlistentry id=
"protocol-message-formats-AuthenticationOk">
3522 <term>AuthenticationOk (B)
</term>
3526 <term>Byte1('R')
</term>
3529 Identifies the message as an authentication request.
3535 <term>Int32(
8)
</term>
3538 Length of message contents in bytes, including self.
3544 <term>Int32(
0)
</term>
3547 Specifies that the authentication was successful.
3555 <varlistentry id=
"protocol-message-formats-AuthenticationKerberosV5">
3556 <term>AuthenticationKerberosV5 (B)
</term>
3561 <term>Byte1('R')
</term>
3564 Identifies the message as an authentication request.
3570 <term>Int32(
8)
</term>
3573 Length of message contents in bytes, including self.
3579 <term>Int32(
2)
</term>
3582 Specifies that Kerberos V5 authentication is required.
3590 <varlistentry id=
"protocol-message-formats-AuthenticationCleartextPassword">
3591 <term>AuthenticationCleartextPassword (B)
</term>
3596 <term>Byte1('R')
</term>
3599 Identifies the message as an authentication request.
3605 <term>Int32(
8)
</term>
3608 Length of message contents in bytes, including self.
3614 <term>Int32(
3)
</term>
3617 Specifies that a clear-text password is required.
3625 <varlistentry id=
"protocol-message-formats-AuthenticationMD5Password">
3626 <term>AuthenticationMD5Password (B)
</term>
3630 <term>Byte1('R')
</term>
3633 Identifies the message as an authentication request.
3639 <term>Int32(
12)
</term>
3642 Length of message contents in bytes, including self.
3648 <term>Int32(
5)
</term>
3651 Specifies that an MD5-encrypted password is required.
3660 The salt to use when encrypting the password.
3668 <varlistentry id=
"protocol-message-formats-AuthenticationGSS">
3669 <term>AuthenticationGSS (B)
</term>
3673 <term>Byte1('R')
</term>
3676 Identifies the message as an authentication request.
3681 <term>Int32(
8)
</term>
3684 Length of message contents in bytes, including self.
3690 <term>Int32(
7)
</term>
3693 Specifies that GSSAPI authentication is required.
3701 <varlistentry id=
"protocol-message-formats-AuthenticationGSSContinue">
3702 <term>AuthenticationGSSContinue (B)
</term>
3706 <term>Byte1('R')
</term>
3709 Identifies the message as an authentication request.
3718 Length of message contents in bytes, including self.
3724 <term>Int32(
8)
</term>
3727 Specifies that this message contains GSSAPI or SSPI data.
3733 <term>Byte
<replaceable>n
</replaceable></term>
3736 GSSAPI or SSPI authentication data.
3744 <varlistentry id=
"protocol-message-formats-AuthenticationSSPI">
3745 <term>AuthenticationSSPI (B)
</term>
3749 <term>Byte1('R')
</term>
3752 Identifies the message as an authentication request.
3758 <term>Int32(
8)
</term>
3761 Length of message contents in bytes, including self.
3767 <term>Int32(
9)
</term>
3770 Specifies that SSPI authentication is required.
3778 <varlistentry id=
"protocol-message-formats-AuthenticationSASL">
3779 <term>AuthenticationSASL (B)
</term>
3783 <term>Byte1('R')
</term>
3786 Identifies the message as an authentication request.
3795 Length of message contents in bytes, including self.
3801 <term>Int32(
10)
</term>
3804 Specifies that SASL authentication is required.
3811 The message body is a list of SASL authentication mechanisms, in the
3812 server's order of preference. A zero byte is required as terminator after
3813 the last authentication mechanism name. For each mechanism, there is the
3821 Name of a SASL authentication mechanism.
3830 <varlistentry id=
"protocol-message-formats-AuthenticationSASLContinue">
3831 <term>AuthenticationSASLContinue (B)
</term>
3835 <term>Byte1('R')
</term>
3838 Identifies the message as an authentication request.
3847 Length of message contents in bytes, including self.
3853 <term>Int32(
11)
</term>
3856 Specifies that this message contains a SASL challenge.
3862 <term>Byte
<replaceable>n
</replaceable></term>
3865 SASL data, specific to the SASL mechanism being used.
3873 <varlistentry id=
"protocol-message-formats-AuthenticationSASLFinal">
3874 <term>AuthenticationSASLFinal (B)
</term>
3878 <term>Byte1('R')
</term>
3881 Identifies the message as an authentication request.
3890 Length of message contents in bytes, including self.
3896 <term>Int32(
12)
</term>
3899 Specifies that SASL authentication has completed.
3905 <term>Byte
<replaceable>n
</replaceable></term>
3908 SASL outcome
"additional data", specific to the SASL mechanism
3917 <varlistentry id=
"protocol-message-formats-BackendKeyData">
3918 <term>BackendKeyData (B)
</term>
3922 <term>Byte1('K')
</term>
3925 Identifies the message as cancellation key data.
3926 The frontend must save these values if it wishes to be
3927 able to issue CancelRequest messages later.
3933 <term>Int32(
12)
</term>
3936 Length of message contents in bytes, including self.
3945 The process ID of this backend.
3954 The secret key of this backend.
3962 <varlistentry id=
"protocol-message-formats-Bind">
3963 <term>Bind (F)
</term>
3967 <term>Byte1('B')
</term>
3970 Identifies the message as a Bind command.
3979 Length of message contents in bytes, including self.
3988 The name of the destination portal
3989 (an empty string selects the unnamed portal).
3998 The name of the source prepared statement
3999 (an empty string selects the unnamed prepared statement).
4008 The number of parameter format codes that follow
4009 (denoted
<replaceable>C
</replaceable> below).
4010 This can be zero to indicate that there are no parameters
4011 or that the parameters all use the default format (text);
4012 or one, in which case the specified format code is applied
4013 to all parameters; or it can equal the actual number of
4020 <term>Int16[
<replaceable>C
</replaceable>]
</term>
4023 The parameter format codes. Each must presently be
4024 zero (text) or one (binary).
4033 The number of parameter values that follow (possibly zero).
4034 This must match the number of parameters needed by the query.
4041 Next, the following pair of fields appear for each parameter:
4049 The length of the parameter value, in bytes (this count
4050 does not include itself). Can be zero.
4051 As a special case, -
1 indicates a NULL parameter value.
4052 No value bytes follow in the NULL case.
4058 <term>Byte
<replaceable>n
</replaceable></term>
4061 The value of the parameter, in the format indicated by the
4062 associated format code.
4063 <replaceable>n
</replaceable> is the above length.
4070 After the last parameter, the following fields appear:
4078 The number of result-column format codes that follow
4079 (denoted
<replaceable>R
</replaceable> below).
4080 This can be zero to indicate that there are no result columns
4081 or that the result columns should all use the default format
4083 or one, in which case the specified format code is applied
4084 to all result columns (if any); or it can equal the actual
4085 number of result columns of the query.
4091 <term>Int16[
<replaceable>R
</replaceable>]
</term>
4094 The result-column format codes. Each must presently be
4095 zero (text) or one (binary).
4103 <varlistentry id=
"protocol-message-formats-BindComplete">
4104 <term>BindComplete (B)
</term>
4108 <term>Byte1('
2')
</term>
4111 Identifies the message as a Bind-complete indicator.
4117 <term>Int32(
4)
</term>
4120 Length of message contents in bytes, including self.
4129 <varlistentry id=
"protocol-message-formats-CancelRequest">
4130 <term>CancelRequest (F)
</term>
4134 <term>Int32(
16)
</term>
4137 Length of message contents in bytes, including self.
4143 <term>Int32(
80877102)
</term>
4146 The cancel request code. The value is chosen to contain
4147 <literal>1234</literal> in the most significant
16 bits, and
<literal>5678</literal> in the
4148 least significant
16 bits. (To avoid confusion, this code
4149 must not be the same as any protocol version number.)
4158 The process ID of the target backend.
4167 The secret key for the target backend.
4175 <varlistentry id=
"protocol-message-formats-Close">
4176 <term>Close (F)
</term>
4180 <term>Byte1('C')
</term>
4183 Identifies the message as a Close command.
4192 Length of message contents in bytes, including self.
4201 '
<literal>S
</literal>' to close a prepared statement; or
4202 '
<literal>P
</literal>' to close a portal.
4211 The name of the prepared statement or portal to close
4212 (an empty string selects the unnamed prepared statement
4221 <varlistentry id=
"protocol-message-formats-CloseComplete">
4222 <term>CloseComplete (B)
</term>
4226 <term>Byte1('
3')
</term>
4229 Identifies the message as a Close-complete indicator.
4235 <term>Int32(
4)
</term>
4238 Length of message contents in bytes, including self.
4246 <varlistentry id=
"protocol-message-formats-CommandComplete">
4247 <term>CommandComplete (B)
</term>
4251 <term>Byte1('C')
</term>
4254 Identifies the message as a command-completed response.
4263 Length of message contents in bytes, including self.
4272 The command tag. This is usually a single
4273 word that identifies which SQL command was completed.
4277 For an
<command>INSERT
</command> command, the tag is
4278 <literal>INSERT
<replaceable>oid
</replaceable>
4279 <replaceable>rows
</replaceable></literal>, where
4280 <replaceable>rows
</replaceable> is the number of rows
4281 inserted.
<replaceable>oid
</replaceable> used to be the object ID
4282 of the inserted row if
<replaceable>rows
</replaceable> was
1
4283 and the target table had OIDs, but OIDs system columns are
4284 not supported anymore; therefore
<replaceable>oid
</replaceable>
4289 For a
<command>DELETE
</command> command, the tag is
4290 <literal>DELETE
<replaceable>rows
</replaceable></literal> where
4291 <replaceable>rows
</replaceable> is the number of rows deleted.
4295 For an
<command>UPDATE
</command> command, the tag is
4296 <literal>UPDATE
<replaceable>rows
</replaceable></literal> where
4297 <replaceable>rows
</replaceable> is the number of rows updated.
4301 For a
<command>MERGE
</command> command, the tag is
4302 <literal>MERGE
<replaceable>rows
</replaceable></literal> where
4303 <replaceable>rows
</replaceable> is the number of rows inserted,
4304 updated, or deleted.
4308 For a
<command>SELECT
</command> or
<command>CREATE TABLE AS
</command>
4309 command, the tag is
<literal>SELECT
<replaceable>rows
</replaceable></literal>
4310 where
<replaceable>rows
</replaceable> is the number of rows retrieved.
4314 For a
<command>MOVE
</command> command, the tag is
4315 <literal>MOVE
<replaceable>rows
</replaceable></literal> where
4316 <replaceable>rows
</replaceable> is the number of rows the
4317 cursor's position has been changed by.
4321 For a
<command>FETCH
</command> command, the tag is
4322 <literal>FETCH
<replaceable>rows
</replaceable></literal> where
4323 <replaceable>rows
</replaceable> is the number of rows that
4324 have been retrieved from the cursor.
4328 For a
<command>COPY
</command> command, the tag is
4329 <literal>COPY
<replaceable>rows
</replaceable></literal> where
4330 <replaceable>rows
</replaceable> is the number of rows copied.
4331 (Note: the row count appears only in
4332 <productname>PostgreSQL
</productname> 8.2 and later.)
4340 <varlistentry id=
"protocol-message-formats-CopyData">
4341 <term>CopyData (F
& B)
</term>
4345 <term>Byte1('d')
</term>
4348 Identifies the message as
<command>COPY
</command> data.
4357 Length of message contents in bytes, including self.
4363 <term>Byte
<replaceable>n
</replaceable></term>
4366 Data that forms part of a
<command>COPY
</command> data stream. Messages sent
4367 from the backend will always correspond to single data rows,
4368 but messages sent by frontends might divide the data stream
4377 <varlistentry id=
"protocol-message-formats-CopyDone">
4378 <term>CopyDone (F
& B)
</term>
4382 <term>Byte1('c')
</term>
4385 Identifies the message as a
<command>COPY
</command>-complete indicator.
4391 <term>Int32(
4)
</term>
4394 Length of message contents in bytes, including self.
4402 <varlistentry id=
"protocol-message-formats-CopyFail">
4403 <term>CopyFail (F)
</term>
4407 <term>Byte1('f')
</term>
4410 Identifies the message as a
<command>COPY
</command>-failure indicator.
4418 Length of message contents in bytes, including self.
4427 An error message to report as the cause of failure.
4435 <varlistentry id=
"protocol-message-formats-CopyInResponse">
4436 <term>CopyInResponse (B)
</term>
4440 <term>Byte1('G')
</term>
4443 Identifies the message as a Start Copy In response.
4444 The frontend must now send copy-in data (if not
4445 prepared to do so, send a CopyFail message).
4454 Length of message contents in bytes, including self.
4463 0 indicates the overall
<command>COPY
</command> format is textual (rows
4464 separated by newlines, columns separated by separator
4466 1 indicates the overall copy format is binary (similar
4468 See
<xref linkend=
"sql-copy"/>
4469 for more information.
4478 The number of columns in the data to be copied
4479 (denoted
<replaceable>N
</replaceable> below).
4485 <term>Int16[
<replaceable>N
</replaceable>]
</term>
4488 The format codes to be used for each column.
4489 Each must presently be zero (text) or one (binary).
4490 All must be zero if the overall copy format is textual.
4498 <varlistentry id=
"protocol-message-formats-CopyOutResponse">
4499 <term>CopyOutResponse (B)
</term>
4503 <term>Byte1('H')
</term>
4506 Identifies the message as a Start Copy Out response.
4507 This message will be followed by copy-out data.
4516 Length of message contents in bytes, including self.
4525 0 indicates the overall
<command>COPY
</command> format
4526 is textual (rows separated by newlines, columns
4527 separated by separator characters, etc.).
1 indicates
4528 the overall copy format is binary (similar to DataRow
4529 format). See
<xref linkend=
"sql-copy"/> for more information.
4538 The number of columns in the data to be copied
4539 (denoted
<replaceable>N
</replaceable> below).
4545 <term>Int16[
<replaceable>N
</replaceable>]
</term>
4548 The format codes to be used for each column.
4549 Each must presently be zero (text) or one (binary).
4550 All must be zero if the overall copy format is textual.
4558 <varlistentry id=
"protocol-message-formats-CopyBothResponse">
4559 <term>CopyBothResponse (B)
</term>
4563 <term>Byte1('W')
</term>
4566 Identifies the message as a Start Copy Both response.
4567 This message is used only for Streaming Replication.
4576 Length of message contents in bytes, including self.
4585 0 indicates the overall
<command>COPY
</command> format
4586 is textual (rows separated by newlines, columns
4587 separated by separator characters, etc.).
1 indicates
4588 the overall copy format is binary (similar to DataRow
4589 format). See
<xref linkend=
"sql-copy"/> for more information.
4598 The number of columns in the data to be copied
4599 (denoted
<replaceable>N
</replaceable> below).
4605 <term>Int16[
<replaceable>N
</replaceable>]
</term>
4608 The format codes to be used for each column.
4609 Each must presently be zero (text) or one (binary).
4610 All must be zero if the overall copy format is textual.
4618 <varlistentry id=
"protocol-message-formats-DataRow">
4619 <term>DataRow (B)
</term>
4623 <term>Byte1('D')
</term>
4626 Identifies the message as a data row.
4635 Length of message contents in bytes, including self.
4644 The number of column values that follow (possibly zero).
4651 Next, the following pair of fields appear for each column:
4659 The length of the column value, in bytes (this count
4660 does not include itself). Can be zero.
4661 As a special case, -
1 indicates a NULL column value.
4662 No value bytes follow in the NULL case.
4668 <term>Byte
<replaceable>n
</replaceable></term>
4671 The value of the column, in the format indicated by the
4672 associated format code.
4673 <replaceable>n
</replaceable> is the above length.
4681 <varlistentry id=
"protocol-message-formats-Describe">
4682 <term>Describe (F)
</term>
4686 <term>Byte1('D')
</term>
4689 Identifies the message as a Describe command.
4698 Length of message contents in bytes, including self.
4707 '
<literal>S
</literal>' to describe a prepared statement; or
4708 '
<literal>P
</literal>' to describe a portal.
4717 The name of the prepared statement or portal to describe
4718 (an empty string selects the unnamed prepared statement
4727 <varlistentry id=
"protocol-message-formats-EmptyQueryResponse">
4728 <term>EmptyQueryResponse (B)
</term>
4732 <term>Byte1('I')
</term>
4735 Identifies the message as a response to an empty query string.
4736 (This substitutes for CommandComplete.)
4742 <term>Int32(
4)
</term>
4745 Length of message contents in bytes, including self.
4753 <varlistentry id=
"protocol-message-formats-ErrorResponse">
4754 <term>ErrorResponse (B)
</term>
4758 <term>Byte1('E')
</term>
4761 Identifies the message as an error.
4770 Length of message contents in bytes, including self.
4777 The message body consists of one or more identified fields,
4778 followed by a zero byte as a terminator. Fields can appear in
4779 any order. For each field there is the following:
4787 A code identifying the field type; if zero, this is
4788 the message terminator and no string follows.
4789 The presently defined field types are listed in
4790 <xref linkend=
"protocol-error-fields"/>.
4791 Since more field types might be added in future,
4792 frontends should silently ignore fields of unrecognized
4810 <varlistentry id=
"protocol-message-formats-Execute">
4811 <term>Execute (F)
</term>
4815 <term>Byte1('E')
</term>
4818 Identifies the message as an Execute command.
4827 Length of message contents in bytes, including self.
4836 The name of the portal to execute
4837 (an empty string selects the unnamed portal).
4846 Maximum number of rows to return, if portal contains
4847 a query that returns rows (ignored otherwise). Zero
4848 denotes
<quote>no limit
</quote>.
4856 <varlistentry id=
"protocol-message-formats-Flush">
4857 <term>Flush (F)
</term>
4861 <term>Byte1('H')
</term>
4864 Identifies the message as a Flush command.
4870 <term>Int32(
4)
</term>
4873 Length of message contents in bytes, including self.
4881 <varlistentry id=
"protocol-message-formats-FunctionCall">
4882 <term>FunctionCall (F)
</term>
4886 <term>Byte1('F')
</term>
4889 Identifies the message as a function call.
4898 Length of message contents in bytes, including self.
4907 Specifies the object ID of the function to call.
4916 The number of argument format codes that follow
4917 (denoted
<replaceable>C
</replaceable> below).
4918 This can be zero to indicate that there are no arguments
4919 or that the arguments all use the default format (text);
4920 or one, in which case the specified format code is applied
4921 to all arguments; or it can equal the actual number of
4928 <term>Int16[
<replaceable>C
</replaceable>]
</term>
4931 The argument format codes. Each must presently be
4932 zero (text) or one (binary).
4941 Specifies the number of arguments being supplied to the
4949 Next, the following pair of fields appear for each argument:
4957 The length of the argument value, in bytes (this count
4958 does not include itself). Can be zero.
4959 As a special case, -
1 indicates a NULL argument value.
4960 No value bytes follow in the NULL case.
4966 <term>Byte
<replaceable>n
</replaceable></term>
4969 The value of the argument, in the format indicated by the
4970 associated format code.
4971 <replaceable>n
</replaceable> is the above length.
4978 After the last argument, the following field appears:
4986 The format code for the function result. Must presently be
4987 zero (text) or one (binary).
4995 <varlistentry id=
"protocol-message-formats-FunctionCallResponse">
4996 <term>FunctionCallResponse (B)
</term>
5000 <term>Byte1('V')
</term>
5003 Identifies the message as a function call result.
5012 Length of message contents in bytes, including self.
5021 The length of the function result value, in bytes (this count
5022 does not include itself). Can be zero.
5023 As a special case, -
1 indicates a NULL function result.
5024 No value bytes follow in the NULL case.
5030 <term>Byte
<replaceable>n
</replaceable></term>
5033 The value of the function result, in the format indicated by
5034 the associated format code.
5035 <replaceable>n
</replaceable> is the above length.
5043 <varlistentry id=
"protocol-message-formats-GSSENCRequest">
5044 <term>GSSENCRequest (F)
</term>
5048 <term>Int32(
8)
</term>
5051 Length of message contents in bytes, including self.
5057 <term>Int32(
80877104)
</term>
5060 The
<acronym>GSSAPI
</acronym> Encryption request code. The value is chosen to contain
5061 <literal>1234</literal> in the most significant
16 bits, and
<literal>5680</literal> in the
5062 least significant
16 bits. (To avoid confusion, this code
5063 must not be the same as any protocol version number.)
5071 <varlistentry id=
"protocol-message-formats-GSSResponse">
5072 <term>GSSResponse (F)
</term>
5076 <term>Byte1('p')
</term>
5079 Identifies the message as a GSSAPI or SSPI response. Note that
5080 this is also used for SASL and password response messages.
5081 The exact message type can be deduced from the context.
5090 Length of message contents in bytes, including self.
5096 <term>Byte
<replaceable>n
</replaceable></term>
5099 GSSAPI/SSPI specific message data.
5107 <varlistentry id=
"protocol-message-formats-NegotiateProtocolVersion">
5108 <term>NegotiateProtocolVersion (B)
</term>
5112 <term>Byte1('v')
</term>
5115 Identifies the message as a protocol version negotiation
5125 Length of message contents in bytes, including self.
5134 Newest minor protocol version supported by the server
5135 for the major protocol version requested by the client.
5144 Number of protocol options not recognized by the server.
5151 Then, for protocol option not recognized by the server, there
5168 <varlistentry id=
"protocol-message-formats-NoData">
5169 <term>NoData (B)
</term>
5173 <term>Byte1('n')
</term>
5176 Identifies the message as a no-data indicator.
5182 <term>Int32(
4)
</term>
5185 Length of message contents in bytes, including self.
5193 <varlistentry id=
"protocol-message-formats-NoticeResponse">
5194 <term>NoticeResponse (B)
</term>
5198 <term>Byte1('N')
</term>
5201 Identifies the message as a notice.
5210 Length of message contents in bytes, including self.
5217 The message body consists of one or more identified fields,
5218 followed by a zero byte as a terminator. Fields can appear in
5219 any order. For each field there is the following:
5227 A code identifying the field type; if zero, this is
5228 the message terminator and no string follows.
5229 The presently defined field types are listed in
5230 <xref linkend=
"protocol-error-fields"/>.
5231 Since more field types might be added in future,
5232 frontends should silently ignore fields of unrecognized
5250 <varlistentry id=
"protocol-message-formats-NotificationResponse">
5251 <term>NotificationResponse (B)
</term>
5255 <term>Byte1('A')
</term>
5258 Identifies the message as a notification response.
5267 Length of message contents in bytes, including self.
5276 The process ID of the notifying backend process.
5285 The name of the channel that the notify has been raised on.
5294 The
<quote>payload
</quote> string passed from the notifying process.
5302 <varlistentry id=
"protocol-message-formats-ParameterDescription">
5303 <term>ParameterDescription (B)
</term>
5307 <term>Byte1('t')
</term>
5310 Identifies the message as a parameter description.
5319 Length of message contents in bytes, including self.
5328 The number of parameters used by the statement
5336 Then, for each parameter, there is the following:
5344 Specifies the object ID of the parameter data type.
5352 <varlistentry id=
"protocol-message-formats-ParameterStatus">
5353 <term>ParameterStatus (B)
</term>
5357 <term>Byte1('S')
</term>
5360 Identifies the message as a run-time parameter status report.
5369 Length of message contents in bytes, including self.
5378 The name of the run-time parameter being reported.
5387 The current value of the parameter.
5395 <varlistentry id=
"protocol-message-formats-Parse">
5396 <term>Parse (F)
</term>
5400 <term>Byte1('P')
</term>
5403 Identifies the message as a Parse command.
5412 Length of message contents in bytes, including self.
5421 The name of the destination prepared statement
5422 (an empty string selects the unnamed prepared statement).
5431 The query string to be parsed.
5440 The number of parameter data types specified
5441 (can be zero). Note that this is not an indication of
5442 the number of parameters that might appear in the
5443 query string, only the number that the frontend wants to
5444 prespecify types for.
5451 Then, for each parameter, there is the following:
5459 Specifies the object ID of the parameter data type.
5460 Placing a zero here is equivalent to leaving the type
5469 <varlistentry id=
"protocol-message-formats-ParseComplete">
5470 <term>ParseComplete (B)
</term>
5474 <term>Byte1('
1')
</term>
5477 Identifies the message as a Parse-complete indicator.
5483 <term>Int32(
4)
</term>
5486 Length of message contents in bytes, including self.
5494 <varlistentry id=
"protocol-message-formats-PasswordMessage">
5495 <term>PasswordMessage (F)
</term>
5499 <term>Byte1('p')
</term>
5502 Identifies the message as a password response. Note that
5503 this is also used for GSSAPI, SSPI and SASL response messages.
5504 The exact message type can be deduced from the context.
5513 Length of message contents in bytes, including self.
5522 The password (encrypted, if requested).
5530 <varlistentry id=
"protocol-message-formats-PortalSuspended">
5531 <term>PortalSuspended (B)
</term>
5535 <term>Byte1('s')
</term>
5538 Identifies the message as a portal-suspended indicator.
5539 Note this only appears if an Execute message's row-count limit
5546 <term>Int32(
4)
</term>
5549 Length of message contents in bytes, including self.
5557 <varlistentry id=
"protocol-message-formats-Query">
5558 <term>Query (F)
</term>
5562 <term>Byte1('Q')
</term>
5565 Identifies the message as a simple query.
5574 Length of message contents in bytes, including self.
5583 The query string itself.
5591 <varlistentry id=
"protocol-message-formats-ReadyForQuery">
5592 <term>ReadyForQuery (B)
</term>
5596 <term>Byte1('Z')
</term>
5599 Identifies the message type. ReadyForQuery is sent
5600 whenever the backend is ready for a new query cycle.
5606 <term>Int32(
5)
</term>
5609 Length of message contents in bytes, including self.
5618 Current backend transaction status indicator.
5619 Possible values are '
<literal>I
</literal>' if idle (not in
5620 a transaction block); '
<literal>T
</literal>' if in a transaction
5621 block; or '
<literal>E
</literal>' if in a failed transaction
5622 block (queries will be rejected until block is ended).
5630 <varlistentry id=
"protocol-message-formats-RowDescription">
5631 <term>RowDescription (B)
</term>
5635 <term>Byte1('T')
</term>
5638 Identifies the message as a row description.
5647 Length of message contents in bytes, including self.
5656 Specifies the number of fields in a row (can be zero).
5663 Then, for each field, there is the following:
5680 If the field can be identified as a column of a specific
5681 table, the object ID of the table; otherwise zero.
5690 If the field can be identified as a column of a specific
5691 table, the attribute number of the column; otherwise zero.
5700 The object ID of the field's data type.
5709 The data type size (see
<varname>pg_type.typlen
</varname>).
5710 Note that negative values denote variable-width types.
5719 The type modifier (see
<varname>pg_attribute.atttypmod
</varname>).
5720 The meaning of the modifier is type-specific.
5729 The format code being used for the field. Currently will
5730 be zero (text) or one (binary). In a RowDescription
5731 returned from the statement variant of Describe, the
5732 format code is not yet known and will always be zero.
5740 <varlistentry id=
"protocol-message-formats-SASLInitialResponse">
5741 <term>SASLInitialResponse (F)
</term>
5745 <term>Byte1('p')
</term>
5748 Identifies the message as an initial SASL response. Note that
5749 this is also used for GSSAPI, SSPI and password response messages.
5750 The exact message type is deduced from the context.
5759 Length of message contents in bytes, including self.
5768 Name of the SASL authentication mechanism that the client
5778 Length of SASL mechanism specific
"Initial Client Response" that
5779 follows, or -
1 if there is no Initial Response.
5785 <term>Byte
<replaceable>n
</replaceable></term>
5788 SASL mechanism specific
"Initial Response".
5796 <varlistentry id=
"protocol-message-formats-SASLResponse">
5797 <term>SASLResponse (F)
</term>
5801 <term>Byte1('p')
</term>
5804 Identifies the message as a SASL response. Note that
5805 this is also used for GSSAPI, SSPI and password response messages.
5806 The exact message type can be deduced from the context.
5815 Length of message contents in bytes, including self.
5821 <term>Byte
<replaceable>n
</replaceable></term>
5824 SASL mechanism specific message data.
5832 <varlistentry id=
"protocol-message-formats-SSLRequest">
5833 <term>SSLRequest (F)
</term>
5837 <term>Int32(
8)
</term>
5840 Length of message contents in bytes, including self.
5846 <term>Int32(
80877103)
</term>
5849 The
<acronym>SSL
</acronym> request code. The value is chosen to contain
5850 <literal>1234</literal> in the most significant
16 bits, and
<literal>5679</literal> in the
5851 least significant
16 bits. (To avoid confusion, this code
5852 must not be the same as any protocol version number.)
5860 <varlistentry id=
"protocol-message-formats-StartupMessage">
5861 <term>StartupMessage (F)
</term>
5868 Length of message contents in bytes, including self.
5874 <term>Int32(
196608)
</term>
5877 The protocol version number. The most significant
16 bits are
5878 the major version number (
3 for the protocol described here).
5879 The least significant
16 bits are the minor version number
5880 (
0 for the protocol described here).
5887 The protocol version number is followed by one or more pairs of
5888 parameter name and value strings. A zero byte is required as a
5889 terminator after the last name/value pair.
5890 Parameters can appear in any
5891 order.
<literal>user
</literal> is required, others are optional.
5892 Each parameter is specified as:
5900 The parameter name. Currently recognized names are:
5904 <term><literal>user
</literal></term>
5907 The database user name to connect as. Required;
5908 there is no default.
5914 <term><literal>database
</literal></term>
5917 The database to connect to. Defaults to the user name.
5923 <term><literal>options
</literal></term>
5926 Command-line arguments for the backend. (This is
5927 deprecated in favor of setting individual run-time
5928 parameters.) Spaces within this string are
5929 considered to separate arguments, unless escaped with
5930 a backslash (
<literal>\
</literal>); write
<literal>\\
</literal> to
5931 represent a literal backslash.
5937 <term><literal>replication
</literal></term>
5940 Used to connect in streaming replication mode, where
5941 a small set of replication commands can be issued
5942 instead of SQL statements. Value can be
5943 <literal>true
</literal>,
<literal>false
</literal>, or
5944 <literal>database
</literal>, and the default is
5945 <literal>false
</literal>. See
5946 <xref linkend=
"protocol-replication"/> for details.
5952 In addition to the above, other parameters may be listed.
5953 Parameter names beginning with
<literal>_pq_.
</literal> are
5954 reserved for use as protocol extensions, while others are
5955 treated as run-time parameters to be set at backend start
5956 time. Such settings will be applied during backend start
5957 (after parsing the command-line arguments if any) and will
5958 act as session defaults.
5967 The parameter value.
5975 <varlistentry id=
"protocol-message-formats-Sync">
5976 <term>Sync (F)
</term>
5980 <term>Byte1('S')
</term>
5983 Identifies the message as a Sync command.
5989 <term>Int32(
4)
</term>
5992 Length of message contents in bytes, including self.
6000 <varlistentry id=
"protocol-message-formats-Terminate">
6001 <term>Terminate (F)
</term>
6005 <term>Byte1('X')
</term>
6008 Identifies the message as a termination.
6014 <term>Int32(
4)
</term>
6017 Length of message contents in bytes, including self.
6027 <sect1 id=
"protocol-error-fields">
6028 <title>Error and Notice Message Fields
</title>
6031 This section describes the fields that can appear in ErrorResponse and
6032 NoticeResponse messages. Each field type has a single-byte identification
6033 token. Note that any given field type should appear at most once per
6039 <term><literal>S
</literal></term>
6042 Severity: the field contents are
6043 <literal>ERROR
</literal>,
<literal>FATAL
</literal>, or
6044 <literal>PANIC
</literal> (in an error message), or
6045 <literal>WARNING
</literal>,
<literal>NOTICE
</literal>,
<literal>DEBUG
</literal>,
6046 <literal>INFO
</literal>, or
<literal>LOG
</literal> (in a notice message),
6047 or a localized translation of one of these. Always present.
6053 <term><literal>V
</literal></term>
6056 Severity: the field contents are
6057 <literal>ERROR
</literal>,
<literal>FATAL
</literal>, or
6058 <literal>PANIC
</literal> (in an error message), or
6059 <literal>WARNING
</literal>,
<literal>NOTICE
</literal>,
<literal>DEBUG
</literal>,
6060 <literal>INFO
</literal>, or
<literal>LOG
</literal> (in a notice message).
6061 This is identical to the
<literal>S
</literal> field except
6062 that the contents are never localized. This is present only in
6063 messages generated by
<productname>PostgreSQL
</productname> versions
9.6
6070 <term><literal>C
</literal></term>
6073 Code: the SQLSTATE code for the error (see
<xref
6074 linkend=
"errcodes-appendix"/>). Not localizable. Always present.
6080 <term><literal>M
</literal></term>
6083 Message: the primary human-readable error message.
6084 This should be accurate but terse (typically one line).
6091 <term><literal>D
</literal></term>
6094 Detail: an optional secondary error message carrying more
6095 detail about the problem. Might run to multiple lines.
6101 <term><literal>H
</literal></term>
6104 Hint: an optional suggestion what to do about the problem.
6105 This is intended to differ from Detail in that it offers advice
6106 (potentially inappropriate) rather than hard facts.
6107 Might run to multiple lines.
6113 <term><literal>P
</literal></term>
6116 Position: the field value is a decimal ASCII integer, indicating
6117 an error cursor position as an index into the original query string.
6118 The first character has index
1, and positions are measured in
6119 characters not bytes.
6125 <term><literal>p
</literal></term>
6128 Internal position: this is defined the same as the
<literal>P
</literal>
6129 field, but it is used when the cursor position refers to an internally
6130 generated command rather than the one submitted by the client.
6131 The
<literal>q
</literal> field will always appear when this field appears.
6137 <term><literal>q
</literal></term>
6140 Internal query: the text of a failed internally-generated command.
6141 This could be, for example, an SQL query issued by a PL/pgSQL function.
6147 <term><literal>W
</literal></term>
6150 Where: an indication of the context in which the error occurred.
6151 Presently this includes a call stack traceback of active
6152 procedural language functions and internally-generated queries.
6153 The trace is one entry per line, most recent first.
6159 <term><literal>s
</literal></term>
6162 Schema name: if the error was associated with a specific database
6163 object, the name of the schema containing that object, if any.
6169 <term><literal>t
</literal></term>
6172 Table name: if the error was associated with a specific table, the
6173 name of the table. (Refer to the schema name field for the name of
6174 the table's schema.)
6180 <term><literal>c
</literal></term>
6183 Column name: if the error was associated with a specific table column,
6184 the name of the column. (Refer to the schema and table name fields to
6185 identify the table.)
6191 <term><literal>d
</literal></term>
6194 Data type name: if the error was associated with a specific data type,
6195 the name of the data type. (Refer to the schema name field for the
6196 name of the data type's schema.)
6202 <term><literal>n
</literal></term>
6205 Constraint name: if the error was associated with a specific
6206 constraint, the name of the constraint. Refer to fields listed above
6207 for the associated table or domain. (For this purpose, indexes are
6208 treated as constraints, even if they weren't created with constraint
6215 <term><literal>F
</literal></term>
6218 File: the file name of the source-code location where the error
6225 <term><literal>L
</literal></term>
6228 Line: the line number of the source-code location where the error
6235 <term><literal>R
</literal></term>
6238 Routine: the name of the source-code routine reporting the error.
6246 The fields for schema name, table name, column name, data type name, and
6247 constraint name are supplied only for a limited number of error types;
6248 see
<xref linkend=
"errcodes-appendix"/>. Frontends should not assume that
6249 the presence of any of these fields guarantees the presence of another
6250 field. Core error sources observe the interrelationships noted above, but
6251 user-defined functions may use these fields in other ways. In the same
6252 vein, clients should not assume that these fields denote contemporary
6253 objects in the current database.
6258 The client is responsible for formatting displayed information to meet its
6259 needs; in particular it should break long lines as needed. Newline characters
6260 appearing in the error message fields should be treated as paragraph breaks,
6265 <sect1 id=
"protocol-logicalrep-message-formats">
6266 <title>Logical Replication Message Formats
</title>
6269 This section describes the detailed format of each logical replication
6270 message. These messages are either returned by the replication slot SQL
6271 interface or are sent by a walsender. In the case of a walsender, they are
6272 encapsulated inside replication protocol WAL messages as described in
6273 <xref linkend=
"protocol-replication"/>, and generally obey the same message
6274 flow as physical replication.
6278 <varlistentry id=
"protocol-logicalrep-message-formats-Begin">
6283 <term>Byte1('B')
</term>
6286 Identifies the message as a begin message.
6292 <term>Int64 (XLogRecPtr)
</term>
6295 The final LSN of the transaction.
6301 <term>Int64 (TimestampTz)
</term>
6304 Commit timestamp of the transaction. The value is in number
6305 of microseconds since PostgreSQL epoch (
2000-
01-
01).
6311 <term>Int32 (TransactionId)
</term>
6314 Xid of the transaction.
6322 <varlistentry id=
"protocol-logicalrep-message-formats-Message">
6323 <term>Message
</term>
6327 <term>Byte1('M')
</term>
6330 Identifies the message as a logical decoding message.
6336 <term>Int32 (TransactionId)
</term>
6339 Xid of the transaction (only present for streamed transactions).
6340 This field is available since protocol version
2.
6349 Flags; Either
0 for no flags or
1 if the logical decoding
6350 message is transactional.
6356 <term>Int64 (XLogRecPtr)
</term>
6359 The LSN of the logical decoding message.
6368 The prefix of the logical decoding message.
6377 Length of the content.
6383 <term>Byte
<replaceable>n
</replaceable></term>
6386 The content of the logical decoding message.
6394 <varlistentry id=
"protocol-logicalrep-message-formats-Commit">
6399 <term>Byte1('C')
</term>
6402 Identifies the message as a commit message.
6408 <term>Int8(
0)
</term>
6411 Flags; currently unused.
6417 <term>Int64 (XLogRecPtr)
</term>
6420 The LSN of the commit.
6426 <term>Int64 (XLogRecPtr)
</term>
6429 The end LSN of the transaction.
6435 <term>Int64 (TimestampTz)
</term>
6438 Commit timestamp of the transaction. The value is in number
6439 of microseconds since PostgreSQL epoch (
2000-
01-
01).
6447 <varlistentry id=
"protocol-logicalrep-message-formats-Origin">
6452 <term>Byte1('O')
</term>
6455 Identifies the message as an origin message.
6461 <term>Int64 (XLogRecPtr)
</term>
6464 The LSN of the commit on the origin server.
6480 Note that there can be multiple Origin messages inside a single transaction.
6485 <varlistentry id=
"protocol-logicalrep-message-formats-Relation">
6486 <term>Relation
</term>
6490 <term>Byte1('R')
</term>
6493 Identifies the message as a relation message.
6499 <term>Int32 (TransactionId)
</term>
6502 Xid of the transaction (only present for streamed transactions).
6503 This field is available since protocol version
2.
6509 <term>Int32 (Oid)
</term>
6512 OID of the relation.
6521 Namespace (empty string for
<literal>pg_catalog
</literal>).
6539 Replica identity setting for the relation (same as
6540 <structfield>relreplident
</structfield> in
<structname>pg_class
</structname>).
6556 Next, the following message part appears for each column included in
6565 Flags for the column. Currently can be either
0 for no flags
6566 or
1 which marks the column as part of the key.
6581 <term>Int32 (Oid)
</term>
6584 OID of the column's data type.
6593 Type modifier of the column (
<structfield>atttypmod
</structfield>).
6601 <varlistentry id=
"protocol-logicalrep-message-formats-Type">
6606 <term>Byte1('Y')
</term>
6609 Identifies the message as a type message.
6615 <term>Int32 (TransactionId)
</term>
6618 Xid of the transaction (only present for streamed transactions).
6619 This field is available since protocol version
2.
6625 <term>Int32 (Oid)
</term>
6628 OID of the data type.
6637 Namespace (empty string for
<literal>pg_catalog
</literal>).
6646 Name of the data type.
6654 <varlistentry id=
"protocol-logicalrep-message-formats-Insert">
6659 <term>Byte1('I')
</term>
6662 Identifies the message as an insert message.
6668 <term>Int32 (TransactionId)
</term>
6671 Xid of the transaction (only present for streamed transactions).
6672 This field is available since protocol version
2.
6678 <term>Int32 (Oid)
</term>
6681 OID of the relation corresponding to the ID in the relation
6688 <term>Byte1('N')
</term>
6691 Identifies the following TupleData message as a new tuple.
6697 <term>TupleData
</term>
6700 TupleData message part representing the contents of new tuple.
6708 <varlistentry id=
"protocol-logicalrep-message-formats-Update">
6713 <term>Byte1('U')
</term>
6716 Identifies the message as an update message.
6722 <term>Int32 (TransactionId)
</term>
6725 Xid of the transaction (only present for streamed transactions).
6726 This field is available since protocol version
2.
6732 <term>Int32 (Oid)
</term>
6735 OID of the relation corresponding to the ID in the relation
6742 <term>Byte1('K')
</term>
6745 Identifies the following TupleData submessage as a key.
6746 This field is optional and is only present if
6747 the update changed data in any of the column(s) that are
6748 part of the REPLICA IDENTITY index.
6754 <term>Byte1('O')
</term>
6757 Identifies the following TupleData submessage as an old tuple.
6758 This field is optional and is only present if table in which
6759 the update happened has REPLICA IDENTITY set to FULL.
6765 <term>TupleData
</term>
6768 TupleData message part representing the contents of the old tuple
6769 or primary key. Only present if the previous 'O' or 'K' part
6776 <term>Byte1('N')
</term>
6779 Identifies the following TupleData message as a new tuple.
6785 <term>TupleData
</term>
6788 TupleData message part representing the contents of a new tuple.
6795 The Update message may contain either a 'K' message part or an 'O' message part
6796 or neither of them, but never both of them.
6801 <varlistentry id=
"protocol-logicalrep-message-formats-Delete">
6806 <term>Byte1('D')
</term>
6809 Identifies the message as a delete message.
6815 <term>Int32 (TransactionId)
</term>
6818 Xid of the transaction (only present for streamed transactions).
6819 This field is available since protocol version
2.
6825 <term>Int32 (Oid)
</term>
6828 OID of the relation corresponding to the ID in the relation
6835 <term>Byte1('K')
</term>
6838 Identifies the following TupleData submessage as a key.
6839 This field is present if the table in which the delete has
6840 happened uses an index as REPLICA IDENTITY.
6846 <term>Byte1('O')
</term>
6849 Identifies the following TupleData message as an old tuple.
6850 This field is present if the table in which the delete
6851 happened has REPLICA IDENTITY set to FULL.
6857 <term>TupleData
</term>
6860 TupleData message part representing the contents of the old tuple
6861 or primary key, depending on the previous field.
6868 The Delete message may contain either a 'K' message part or an 'O' message part,
6869 but never both of them.
6874 <varlistentry id=
"protocol-logicalrep-message-formats-Truncate">
6875 <term>Truncate
</term>
6879 <term>Byte1('T')
</term>
6882 Identifies the message as a truncate message.
6888 <term>Int32 (TransactionId)
</term>
6891 Xid of the transaction (only present for streamed transactions).
6892 This field is available since protocol version
2.
6910 Option bits for
<command>TRUNCATE
</command>:
6911 1 for
<literal>CASCADE
</literal>,
2 for
<literal>RESTART IDENTITY
</literal>
6917 <term>Int32 (Oid)
</term>
6920 OID of the relation corresponding to the ID in the relation
6921 message. This field is repeated for each relation.
6931 The following messages (Stream Start, Stream Stop, Stream Commit, and
6932 Stream Abort) are available since protocol version
2.
6936 <varlistentry id=
"protocol-logicalrep-message-formats-Stream-Start">
6937 <term>Stream Start
</term>
6941 <term>Byte1('S')
</term>
6944 Identifies the message as a stream start message.
6950 <term>Int32 (TransactionId)
</term>
6953 Xid of the transaction.
6962 A value of
1 indicates this is the first stream segment for
6963 this XID,
0 for any other stream segment.
6971 <varlistentry id=
"protocol-logicalrep-message-formats-Stream-Stop">
6972 <term>Stream Stop
</term>
6976 <term>Byte1('E')
</term>
6979 Identifies the message as a stream stop message.
6987 <varlistentry id=
"protocol-logicalrep-message-formats-Stream-Commit">
6988 <term>Stream Commit
</term>
6992 <term>Byte1('c')
</term>
6995 Identifies the message as a stream commit message.
7001 <term>Int32 (TransactionId)
</term>
7004 Xid of the transaction.
7010 <term>Int8(
0)
</term>
7013 Flags; currently unused.
7019 <term>Int64 (XLogRecPtr)
</term>
7022 The LSN of the commit.
7028 <term>Int64 (XLogRecPtr)
</term>
7031 The end LSN of the transaction.
7037 <term>Int64 (TimestampTz)
</term>
7040 Commit timestamp of the transaction. The value is in number
7041 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7049 <varlistentry id=
"protocol-logicalrep-message-formats-Stream-Abort">
7050 <term>Stream Abort
</term>
7054 <term>Byte1('A')
</term>
7057 Identifies the message as a stream abort message.
7063 <term>Int32 (TransactionId)
</term>
7066 Xid of the transaction.
7072 <term>Int32 (TransactionId)
</term>
7075 Xid of the subtransaction (will be same as xid of the transaction for top-level
7082 <term>Int64 (XLogRecPtr)
</term>
7085 The LSN of the abort. This field is available since protocol version
7092 <term>Int64 (TimestampTz)
</term>
7095 Abort timestamp of the transaction. The value is in number
7096 of microseconds since PostgreSQL epoch (
2000-
01-
01). This field is
7097 available since protocol version
4.
7108 The following messages (Begin Prepare, Prepare, Commit Prepared, Rollback Prepared, Stream Prepare)
7109 are available since protocol version
3.
7113 <varlistentry id=
"protocol-logicalrep-message-formats-Begin-Prepare">
7114 <term>Begin Prepare
</term>
7118 <term>Byte1('b')
</term>
7121 Identifies the message as the beginning of a prepared transaction message.
7127 <term>Int64 (XLogRecPtr)
</term>
7130 The LSN of the prepare.
7136 <term>Int64 (XLogRecPtr)
</term>
7139 The end LSN of the prepared transaction.
7145 <term>Int64 (TimestampTz)
</term>
7148 Prepare timestamp of the transaction. The value is in number
7149 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7155 <term>Int32 (TransactionId)
</term>
7158 Xid of the transaction.
7167 The user defined GID of the prepared transaction.
7175 <varlistentry id=
"protocol-logicalrep-message-formats-Prepare">
7176 <term>Prepare
</term>
7180 <term>Byte1('P')
</term>
7183 Identifies the message as a prepared transaction message.
7189 <term>Int8(
0)
</term>
7192 Flags; currently unused.
7198 <term>Int64 (XLogRecPtr)
</term>
7201 The LSN of the prepare.
7207 <term>Int64 (XLogRecPtr)
</term>
7210 The end LSN of the prepared transaction.
7216 <term>Int64 (TimestampTz)
</term>
7219 Prepare timestamp of the transaction. The value is in number
7220 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7226 <term>Int32 (TransactionId)
</term>
7229 Xid of the transaction.
7238 The user defined GID of the prepared transaction.
7246 <varlistentry id=
"protocol-logicalrep-message-formats-Commit-Prepared">
7247 <term>Commit Prepared
</term>
7251 <term>Byte1('K')
</term>
7254 Identifies the message as the commit of a prepared transaction message.
7260 <term>Int8(
0)
</term>
7263 Flags; currently unused.
7269 <term>Int64 (XLogRecPtr)
</term>
7272 The LSN of the commit of the prepared transaction.
7278 <term>Int64 (XLogRecPtr)
</term>
7281 The end LSN of the commit of the prepared transaction.
7287 <term>Int64 (TimestampTz)
</term>
7290 Commit timestamp of the transaction. The value is in number
7291 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7297 <term>Int32 (TransactionId)
</term>
7300 Xid of the transaction.
7309 The user defined GID of the prepared transaction.
7317 <varlistentry id=
"protocol-logicalrep-message-formats-Rollback-Prepared">
7318 <term>Rollback Prepared
</term>
7322 <term>Byte1('r')
</term>
7325 Identifies the message as the rollback of a prepared transaction message.
7331 <term>Int8(
0)
</term>
7334 Flags; currently unused.
7340 <term>Int64 (XLogRecPtr)
</term>
7343 The end LSN of the prepared transaction.
7349 <term>Int64 (XLogRecPtr)
</term>
7352 The end LSN of the rollback of the prepared transaction.
7358 <term>Int64 (TimestampTz)
</term>
7361 Prepare timestamp of the transaction. The value is in number
7362 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7368 <term>Int64 (TimestampTz)
</term>
7371 Rollback timestamp of the transaction. The value is in number
7372 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7378 <term>Int32 (TransactionId)
</term>
7381 Xid of the transaction.
7390 The user defined GID of the prepared transaction.
7398 <varlistentry id=
"protocol-logicalrep-message-formats-Stream-Prepare">
7399 <term>Stream Prepare
</term>
7403 <term>Byte1('p')
</term>
7406 Identifies the message as a stream prepared transaction message.
7412 <term>Int8(
0)
</term>
7415 Flags; currently unused.
7421 <term>Int64 (XLogRecPtr)
</term>
7424 The LSN of the prepare.
7430 <term>Int64 (XLogRecPtr)
</term>
7433 The end LSN of the prepared transaction.
7439 <term>Int64 (TimestampTz)
</term>
7442 Prepare timestamp of the transaction. The value is in number
7443 of microseconds since PostgreSQL epoch (
2000-
01-
01).
7449 <term>Int32 (TransactionId)
</term>
7452 Xid of the transaction.
7461 The user defined GID of the prepared transaction.
7471 The following message parts are shared by the above messages.
7475 <varlistentry id=
"protocol-logicalrep-message-formats-TupleData">
7476 <term>TupleData
</term>
7490 Next, one of the following submessages appears for each published column:
7494 <term>Byte1('n')
</term>
7497 Identifies the data as NULL value.
7505 <term>Byte1('u')
</term>
7508 Identifies unchanged TOASTed value (the actual value is not
7517 <term>Byte1('t')
</term>
7520 Identifies the data as text formatted value.
7528 <term>Byte1('b')
</term>
7531 Identifies the data as binary formatted value.
7540 Length of the column value.
7546 <term>Byte
<replaceable>n
</replaceable></term>
7549 The value of the column, either in binary or in text format.
7550 (As specified in the preceding format byte).
7551 <replaceable>n
</replaceable> is the above length.
7562 <sect1 id=
"protocol-changes">
7563 <title>Summary of Changes since Protocol
2.0</title>
7566 This section provides a quick checklist of changes, for the benefit of
7567 developers trying to update existing client libraries to protocol
3.0.
7571 The initial startup packet uses a flexible list-of-strings format
7572 instead of a fixed format. Notice that session default values for run-time
7573 parameters can now be specified directly in the startup packet. (Actually,
7574 you could do that before using the
<literal>options
</literal> field, but given the
7575 limited width of
<literal>options
</literal> and the lack of any way to quote
7576 whitespace in the values, it wasn't a very safe technique.)
7580 All messages now have a length count immediately following the message type
7581 byte (except for startup packets, which have no type byte). Also note that
7582 PasswordMessage now has a type byte.
7586 ErrorResponse and NoticeResponse ('
<literal>E
</literal>' and '
<literal>N
</literal>')
7587 messages now contain multiple fields, from which the client code can
7588 assemble an error message of the desired level of verbosity. Note that
7589 individual fields will typically not end with a newline, whereas the single
7590 string sent in the older protocol always did.
7594 The ReadyForQuery ('
<literal>Z
</literal>') message includes a transaction status
7599 The distinction between BinaryRow and DataRow message types is gone; the
7600 single DataRow message type serves for returning data in all formats.
7601 Note that the layout of DataRow has changed to make it easier to parse.
7602 Also, the representation of binary values has changed: it is no longer
7603 directly tied to the server's internal representation.
7607 There is a new
<quote>extended query
</quote> sub-protocol, which adds the frontend
7608 message types Parse, Bind, Execute, Describe, Close, Flush, and Sync, and the
7609 backend message types ParseComplete, BindComplete, PortalSuspended,
7610 ParameterDescription, NoData, and CloseComplete. Existing clients do not
7611 have to concern themselves with this sub-protocol, but making use of it
7612 might allow improvements in performance or functionality.
7616 <command>COPY
</command> data is now encapsulated into CopyData and CopyDone messages. There
7617 is a well-defined way to recover from errors during
<command>COPY
</command>. The special
7618 <quote><literal>\.
</literal></quote> last line is not needed anymore, and is not sent
7619 during
<command>COPY OUT
</command>.
7620 (It is still recognized as a terminator during text-mode
<command>COPY
7621 IN
</command>, but not in CSV mode. The text-mode behavior is
7622 deprecated and may eventually be removed.) Binary
<command>COPY
</command> is supported.
7623 The CopyInResponse and CopyOutResponse messages include fields indicating
7624 the number of columns and the format of each column.
7628 The layout of FunctionCall and FunctionCallResponse messages has changed.
7629 FunctionCall can now support passing NULL arguments to functions. It also
7630 can handle passing parameters and retrieving results in either text or
7631 binary format. There is no longer any reason to consider FunctionCall a
7632 potential security hole, since it does not offer direct access to internal
7633 server data representations.
7637 The backend sends ParameterStatus ('
<literal>S
</literal>') messages during connection
7638 startup for all parameters it considers interesting to the client library.
7639 Subsequently, a ParameterStatus message is sent whenever the active value
7640 changes for any of these parameters.
7644 The RowDescription ('
<literal>T
</literal>') message carries new table OID and column
7645 number fields for each column of the described row. It also shows the format
7646 code for each column.
7650 The CursorResponse ('
<literal>P
</literal>') message is no longer generated by
7655 The NotificationResponse ('
<literal>A
</literal>') message has an additional string
7656 field, which can carry a
<quote>payload
</quote> string passed
7657 from the
<command>NOTIFY
</command> event sender.
7661 The EmptyQueryResponse ('
<literal>I
</literal>') message used to include an empty
7662 string parameter; this has been removed.