Add support for incremental backup.
[pgsql.git] / doc / src / sgml / protocol.sgml
blob9a66918171a56ab3d59b1b1d311d01559fe6ff01
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>
9 </indexterm>
11 <para>
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.
18 </para>
20 <para>
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.
37 </para>
39 <para>
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>
47 are interchangeable.
48 </para>
50 <sect1 id="protocol-overview">
51 <title>Overview</title>
53 <para>
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.
62 </para>
64 <para>
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>)
68 wherein the
69 backend will send unsolicited messages, but for the most part this portion
70 of a session is driven by frontend requests.
71 </para>
73 <para>
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
77 before exiting.
78 </para>
80 <para>
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.
88 </para>
90 <para>
91 Normal operation has additional sub-protocols for special operations
92 such as <command>COPY</command>.
93 </para>
95 <sect2 id="protocol-message-concepts">
96 <title>Messaging Overview</title>
98 <para>
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
105 message-type byte.
106 </para>
108 <para>
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.
116 </para>
118 <para>
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.
125 </para>
126 </sect2>
128 <sect2 id="protocol-query-concepts">
129 <title>Extended Query Overview</title>
131 <para>
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.)
143 </para>
145 <para>
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.
155 </para>
157 <para>
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
166 of multiple uses.
167 </para>
168 </sect2>
170 <sect2 id="protocol-format-codes">
171 <title>Formats and Format Codes</title>
173 <para>
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.
183 </para>
185 <para>
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.)
192 </para>
194 <para>
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.
200 </para>
201 </sect2>
202 </sect1>
204 <sect1 id="protocol-flow">
205 <title>Message Flow</title>
207 <para>
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.
217 </para>
219 <sect2 id="protocol-flow-start-up">
220 <title>Start-up</title>
222 <para>
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).
233 </para>
235 <para>
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.
244 </para>
246 <para>
247 The authentication cycle ends with the server either rejecting the
248 connection attempt (ErrorResponse), or sending AuthenticationOk.
249 </para>
251 <para>
252 The possible messages from the server in this phase are:
254 <variablelist>
255 <varlistentry>
256 <term>ErrorResponse</term>
257 <listitem>
258 <para>
259 The connection attempt has been rejected.
260 The server then immediately closes the connection.
261 </para>
262 </listitem>
263 </varlistentry>
265 <varlistentry>
266 <term>AuthenticationOk</term>
267 <listitem>
268 <para>
269 The authentication exchange is successfully completed.
270 </para>
271 </listitem>
272 </varlistentry>
274 <varlistentry>
275 <term>AuthenticationKerberosV5</term>
276 <listitem>
277 <para>
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
283 longer supported.
284 </para>
285 </listitem>
286 </varlistentry>
288 <varlistentry>
289 <term>AuthenticationCleartextPassword</term>
290 <listitem>
291 <para>
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.
296 </para>
297 </listitem>
298 </varlistentry>
300 <varlistentry>
301 <term>AuthenticationMD5Password</term>
302 <listitem>
303 <para>
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.)
314 </para>
315 </listitem>
316 </varlistentry>
318 <varlistentry>
319 <term>AuthenticationGSS</term>
320 <listitem>
321 <para>
322 The frontend must now initiate a GSSAPI negotiation. The frontend
323 will send a GSSResponse message with the first part of the GSSAPI
324 data stream in response to this. If further messages are needed,
325 the server will respond with AuthenticationGSSContinue.
326 </para>
327 </listitem>
328 </varlistentry>
330 <varlistentry>
331 <term>AuthenticationSSPI</term>
332 <listitem>
333 <para>
334 The frontend must now initiate an SSPI negotiation. The frontend
335 will send a GSSResponse with the first part of the SSPI
336 data stream in response to this. If further messages are needed,
337 the server will respond with AuthenticationGSSContinue.
338 </para>
339 </listitem>
341 </varlistentry>
342 <varlistentry>
343 <term>AuthenticationGSSContinue</term>
344 <listitem>
345 <para>
346 This message contains the response data from the previous step
347 of GSSAPI or SSPI negotiation (AuthenticationGSS, AuthenticationSSPI
348 or a previous AuthenticationGSSContinue). If the GSSAPI
349 or SSPI data in this message
350 indicates more data is needed to complete the authentication,
351 the frontend must send that data as another GSSResponse message. If
352 GSSAPI or SSPI authentication is completed by this message, the server
353 will next send AuthenticationOk to indicate successful authentication
354 or ErrorResponse to indicate failure.
355 </para>
356 </listitem>
357 </varlistentry>
359 <varlistentry>
360 <term>AuthenticationSASL</term>
361 <listitem>
362 <para>
363 The frontend must now initiate a SASL negotiation, using one of the
364 SASL mechanisms listed in the message. The frontend will send a
365 SASLInitialResponse with the name of the selected mechanism, and the
366 first part of the SASL data stream in response to this. If further
367 messages are needed, the server will respond with
368 AuthenticationSASLContinue. See <xref linkend="sasl-authentication"/>
369 for details.
370 </para>
371 </listitem>
372 </varlistentry>
374 <varlistentry>
375 <term>AuthenticationSASLContinue</term>
376 <listitem>
377 <para>
378 This message contains challenge data from the previous step of SASL
379 negotiation (AuthenticationSASL, or a previous
380 AuthenticationSASLContinue). The frontend must respond with a
381 SASLResponse message.
382 </para>
383 </listitem>
384 </varlistentry>
386 <varlistentry>
387 <term>AuthenticationSASLFinal</term>
388 <listitem>
389 <para>
390 SASL authentication has completed with additional mechanism-specific
391 data for the client. The server will next send AuthenticationOk to
392 indicate successful authentication, or an ErrorResponse to indicate
393 failure. This message is sent only if the SASL mechanism specifies
394 additional data to be sent from server to client at completion.
395 </para>
396 </listitem>
397 </varlistentry>
399 <varlistentry>
400 <term>NegotiateProtocolVersion</term>
401 <listitem>
402 <para>
403 The server does not support the minor protocol version requested
404 by the client, but does support an earlier version of the protocol;
405 this message indicates the highest supported minor version. This
406 message will also be sent if the client requested unsupported protocol
407 options (i.e., beginning with <literal>_pq_.</literal>) in the
408 startup packet. This message will be followed by an ErrorResponse or
409 a message indicating the success or failure of authentication.
410 </para>
411 </listitem>
412 </varlistentry>
414 </variablelist>
415 </para>
417 <para>
418 If the frontend does not support the authentication method
419 requested by the server, then it should immediately close the
420 connection.
421 </para>
423 <para>
424 After having received AuthenticationOk, the frontend must wait
425 for further messages from the server. In this phase a backend process
426 is being started, and the frontend is just an interested bystander.
427 It is still possible for the startup attempt
428 to fail (ErrorResponse) or the server to decline support for the requested
429 minor protocol version (NegotiateProtocolVersion), but in the normal case
430 the backend will send some ParameterStatus messages, BackendKeyData, and
431 finally ReadyForQuery.
432 </para>
434 <para>
435 During this phase the backend will attempt to apply any additional
436 run-time parameter settings that were given in the startup message.
437 If successful, these values become session defaults. An error causes
438 ErrorResponse and exit.
439 </para>
441 <para>
442 The possible messages from the backend in this phase are:
444 <variablelist>
445 <varlistentry>
446 <term>BackendKeyData</term>
447 <listitem>
448 <para>
449 This message provides secret-key data that the frontend must
450 save if it wants to be able to issue cancel requests later.
451 The frontend should not respond to this message, but should
452 continue listening for a ReadyForQuery message.
453 </para>
454 </listitem>
455 </varlistentry>
457 <varlistentry>
458 <term>ParameterStatus</term>
459 <listitem>
460 <para>
461 This message informs the frontend about the current (initial)
462 setting of backend parameters, such as <xref
463 linkend="guc-client-encoding"/> or <xref linkend="guc-datestyle"/>.
464 The frontend can ignore this message, or record the settings
465 for its future use; see <xref linkend="protocol-async"/> for
466 more details. The frontend should not respond to this
467 message, but should continue listening for a ReadyForQuery
468 message.
469 </para>
470 </listitem>
471 </varlistentry>
473 <varlistentry>
474 <term>ReadyForQuery</term>
475 <listitem>
476 <para>
477 Start-up is completed. The frontend can now issue commands.
478 </para>
479 </listitem>
480 </varlistentry>
482 <varlistentry>
483 <term>ErrorResponse</term>
484 <listitem>
485 <para>
486 Start-up failed. The connection is closed after sending this
487 message.
488 </para>
489 </listitem>
490 </varlistentry>
492 <varlistentry>
493 <term>NoticeResponse</term>
494 <listitem>
495 <para>
496 A warning message has been issued. The frontend should
497 display the message but continue listening for ReadyForQuery
498 or ErrorResponse.
499 </para>
500 </listitem>
501 </varlistentry>
502 </variablelist>
503 </para>
505 <para>
506 The ReadyForQuery message is the same one that the backend will
507 issue after each command cycle. Depending on the coding needs of
508 the frontend, it is reasonable to consider ReadyForQuery as
509 starting a command cycle, or to consider ReadyForQuery as ending the
510 start-up phase and each subsequent command cycle.
511 </para>
512 </sect2>
514 <sect2 id="protocol-flow-simple-query">
515 <title>Simple Query</title>
517 <para>
518 A simple query cycle is initiated by the frontend sending a Query message
519 to the backend. The message includes an SQL command (or commands)
520 expressed as a text string.
521 The backend then sends one or more response
522 messages depending on the contents of the query command string,
523 and finally a ReadyForQuery response message. ReadyForQuery
524 informs the frontend that it can safely send a new command.
525 (It is not actually necessary for the frontend to wait for
526 ReadyForQuery before issuing another command, but the frontend must
527 then take responsibility for figuring out what happens if the earlier
528 command fails and already-issued later commands succeed.)
529 </para>
531 <para>
532 The possible response messages from the backend are:
534 <variablelist>
535 <varlistentry>
536 <term>CommandComplete</term>
537 <listitem>
538 <para>
539 An SQL command completed normally.
540 </para>
541 </listitem>
542 </varlistentry>
544 <varlistentry>
545 <term>CopyInResponse</term>
546 <listitem>
547 <para>
548 The backend is ready to copy data from the frontend to a
549 table; see <xref linkend="protocol-copy"/>.
550 </para>
551 </listitem>
552 </varlistentry>
554 <varlistentry>
555 <term>CopyOutResponse</term>
556 <listitem>
557 <para>
558 The backend is ready to copy data from a table to the
559 frontend; see <xref linkend="protocol-copy"/>.
560 </para>
561 </listitem>
562 </varlistentry>
564 <varlistentry>
565 <term>RowDescription</term>
566 <listitem>
567 <para>
568 Indicates that rows are about to be returned in response to
569 a <command>SELECT</command>, <command>FETCH</command>, etc. query.
570 The contents of this message describe the column layout of the rows.
571 This will be followed by a DataRow message for each row being returned
572 to the frontend.
573 </para>
574 </listitem>
575 </varlistentry>
577 <varlistentry>
578 <term>DataRow</term>
579 <listitem>
580 <para>
581 One of the set of rows returned by
582 a <command>SELECT</command>, <command>FETCH</command>, etc. query.
583 </para>
584 </listitem>
585 </varlistentry>
587 <varlistentry>
588 <term>EmptyQueryResponse</term>
589 <listitem>
590 <para>
591 An empty query string was recognized.
592 </para>
593 </listitem>
594 </varlistentry>
596 <varlistentry>
597 <term>ErrorResponse</term>
598 <listitem>
599 <para>
600 An error has occurred.
601 </para>
602 </listitem>
603 </varlistentry>
605 <varlistentry>
606 <term>ReadyForQuery</term>
607 <listitem>
608 <para>
609 Processing of the query string is complete. A separate
610 message is sent to indicate this because the query string might
611 contain multiple SQL commands. (CommandComplete marks the
612 end of processing one SQL command, not the whole string.)
613 ReadyForQuery will always be sent, whether processing
614 terminates successfully or with an error.
615 </para>
616 </listitem>
617 </varlistentry>
619 <varlistentry>
620 <term>NoticeResponse</term>
621 <listitem>
622 <para>
623 A warning message has been issued in relation to the query.
624 Notices are in addition to other responses, i.e., the backend
625 will continue processing the command.
626 </para>
627 </listitem>
628 </varlistentry>
630 </variablelist>
631 </para>
633 <para>
634 The response to a <command>SELECT</command> query (or other queries that
635 return row sets, such as <command>EXPLAIN</command> or <command>SHOW</command>)
636 normally consists of RowDescription, zero or more
637 DataRow messages, and then CommandComplete.
638 <command>COPY</command> to or from the frontend invokes special protocol
639 as described in <xref linkend="protocol-copy"/>.
640 All other query types normally produce only
641 a CommandComplete message.
642 </para>
644 <para>
645 Since a query string could contain several queries (separated by
646 semicolons), there might be several such response sequences before the
647 backend finishes processing the query string. ReadyForQuery is issued
648 when the entire string has been processed and the backend is ready to
649 accept a new query string.
650 </para>
652 <para>
653 If a completely empty (no contents other than whitespace) query string
654 is received, the response is EmptyQueryResponse followed by ReadyForQuery.
655 </para>
657 <para>
658 In the event of an error, ErrorResponse is issued followed by
659 ReadyForQuery. All further processing of the query string is aborted by
660 ErrorResponse (even if more queries remained in it). Note that this
661 might occur partway through the sequence of messages generated by an
662 individual query.
663 </para>
665 <para>
666 In simple Query mode, the format of retrieved values is always text,
667 except when the given command is a <command>FETCH</command> from a cursor
668 declared with the <literal>BINARY</literal> option. In that case, the
669 retrieved values are in binary format. The format codes given in
670 the RowDescription message tell which format is being used.
671 </para>
673 <para>
674 A frontend must be prepared to accept ErrorResponse and
675 NoticeResponse messages whenever it is expecting any other type of
676 message. See also <xref linkend="protocol-async"/> concerning messages
677 that the backend might generate due to outside events.
678 </para>
680 <para>
681 Recommended practice is to code frontends in a state-machine style
682 that will accept any message type at any time that it could make sense,
683 rather than wiring in assumptions about the exact sequence of messages.
684 </para>
686 <sect3 id="protocol-flow-multi-statement">
687 <title>Multiple Statements in a Simple Query</title>
689 <para>
690 When a simple Query message contains more than one SQL statement
691 (separated by semicolons), those statements are executed as a single
692 transaction, unless explicit transaction control commands are included
693 to force a different behavior. For example, if the message contains
694 <programlisting>
695 INSERT INTO mytable VALUES(1);
696 SELECT 1/0;
697 INSERT INTO mytable VALUES(2);
698 </programlisting>
699 then the divide-by-zero failure in the <command>SELECT</command> will force
700 rollback of the first <command>INSERT</command>. Furthermore, because
701 execution of the message is abandoned at the first error, the second
702 <command>INSERT</command> is never attempted at all.
703 </para>
705 <para>
706 If instead the message contains
707 <programlisting>
708 BEGIN;
709 INSERT INTO mytable VALUES(1);
710 COMMIT;
711 INSERT INTO mytable VALUES(2);
712 SELECT 1/0;
713 </programlisting>
714 then the first <command>INSERT</command> is committed by the
715 explicit <command>COMMIT</command> command. The second <command>INSERT</command>
716 and the <command>SELECT</command> are still treated as a single transaction,
717 so that the divide-by-zero failure will roll back the
718 second <command>INSERT</command>, but not the first one.
719 </para>
721 <para>
722 This behavior is implemented by running the statements in a
723 multi-statement Query message in an <firstterm>implicit transaction
724 block</firstterm> unless there is some explicit transaction block for them to
725 run in. The main difference between an implicit transaction block and
726 a regular one is that an implicit block is closed automatically at the
727 end of the Query message, either by an implicit commit if there was no
728 error, or an implicit rollback if there was an error. This is similar
729 to the implicit commit or rollback that happens for a statement
730 executed by itself (when not in a transaction block).
731 </para>
733 <para>
734 If the session is already in a transaction block, as a result of
735 a <command>BEGIN</command> in some previous message, then the Query message
736 simply continues that transaction block, whether the message contains
737 one statement or several. However, if the Query message contains
738 a <command>COMMIT</command> or <command>ROLLBACK</command> closing the existing
739 transaction block, then any following statements are executed in an
740 implicit transaction block.
741 Conversely, if a <command>BEGIN</command> appears in a multi-statement Query
742 message, then it starts a regular transaction block that will only be
743 terminated by an explicit <command>COMMIT</command> or <command>ROLLBACK</command>,
744 whether that appears in this Query message or a later one.
745 If the <command>BEGIN</command> follows some statements that were executed as
746 an implicit transaction block, those statements are not immediately
747 committed; in effect, they are retroactively included into the new
748 regular transaction block.
749 </para>
751 <para>
752 A <command>COMMIT</command> or <command>ROLLBACK</command> appearing in an implicit
753 transaction block is executed as normal, closing the implicit block;
754 however, a warning will be issued since a <command>COMMIT</command>
755 or <command>ROLLBACK</command> without a previous <command>BEGIN</command> might
756 represent a mistake. If more statements follow, a new implicit
757 transaction block will be started for them.
758 </para>
760 <para>
761 Savepoints are not allowed in an implicit transaction block, since
762 they would conflict with the behavior of automatically closing the
763 block upon any error.
764 </para>
766 <para>
767 Remember that, regardless of any transaction control commands that may
768 be present, execution of the Query message stops at the first error.
769 Thus for example given
770 <programlisting>
771 BEGIN;
772 SELECT 1/0;
773 ROLLBACK;
774 </programlisting>
775 in a single Query message, the session will be left inside a failed
776 regular transaction block, since the <command>ROLLBACK</command> is not
777 reached after the divide-by-zero error. Another <command>ROLLBACK</command>
778 will be needed to restore the session to a usable state.
779 </para>
781 <para>
782 Another behavior of note is that initial lexical and syntactic
783 analysis is done on the entire query string before any of it is
784 executed. Thus simple errors (such as a misspelled keyword) in later
785 statements can prevent execution of any of the statements. This
786 is normally invisible to users since the statements would all roll
787 back anyway when done as an implicit transaction block. However,
788 it can be visible when attempting to do multiple transactions within a
789 multi-statement Query. For instance, if a typo turned our previous
790 example into
791 <programlisting>
792 BEGIN;
793 INSERT INTO mytable VALUES(1);
794 COMMIT;
795 INSERT INTO mytable VALUES(2);
796 SELCT 1/0;<!-- this typo is intentional -->
797 </programlisting>
798 then none of the statements would get run, resulting in the visible
799 difference that the first <command>INSERT</command> is not committed.
800 Errors detected at semantic analysis or later, such as a misspelled
801 table or column name, do not have this effect.
802 </para>
803 </sect3>
804 </sect2>
806 <sect2 id="protocol-flow-ext-query">
807 <title>Extended Query</title>
809 <para>
810 The extended query protocol breaks down the above-described simple
811 query protocol into multiple steps. The results of preparatory
812 steps can be re-used multiple times for improved efficiency.
813 Furthermore, additional features are available, such as the possibility
814 of supplying data values as separate parameters instead of having to
815 insert them directly into a query string.
816 </para>
818 <para>
819 In the extended protocol, the frontend first sends a Parse message,
820 which contains a textual query string, optionally some information
821 about data types of parameter placeholders, and the
822 name of a destination prepared-statement object (an empty string
823 selects the unnamed prepared statement). The response is
824 either ParseComplete or ErrorResponse. Parameter data types can be
825 specified by OID; if not given, the parser attempts to infer the
826 data types in the same way as it would do for untyped literal string
827 constants.
828 </para>
830 <note>
831 <para>
832 A parameter data type can be left unspecified by setting it to zero,
833 or by making the array of parameter type OIDs shorter than the
834 number of parameter symbols (<literal>$</literal><replaceable>n</replaceable>)
835 used in the query string. Another special case is that a parameter's
836 type can be specified as <type>void</type> (that is, the OID of the
837 <type>void</type> pseudo-type). This is meant to allow parameter symbols
838 to be used for function parameters that are actually OUT parameters.
839 Ordinarily there is no context in which a <type>void</type> parameter
840 could be used, but if such a parameter symbol appears in a function's
841 parameter list, it is effectively ignored. For example, a function
842 call such as <literal>foo($1,$2,$3,$4)</literal> could match a function with
843 two IN and two OUT arguments, if <literal>$3</literal> and <literal>$4</literal>
844 are specified as having type <type>void</type>.
845 </para>
846 </note>
848 <note>
849 <para>
850 The query string contained in a Parse message cannot include more
851 than one SQL statement; else a syntax error is reported. This
852 restriction does not exist in the simple-query protocol, but it
853 does exist in the extended protocol, because allowing prepared
854 statements or portals to contain multiple commands would complicate
855 the protocol unduly.
856 </para>
857 </note>
859 <para>
860 If successfully created, a named prepared-statement object lasts till
861 the end of the current session, unless explicitly destroyed. An unnamed
862 prepared statement lasts only until the next Parse statement specifying
863 the unnamed statement as destination is issued. (Note that a simple
864 Query message also destroys the unnamed statement.) Named prepared
865 statements must be explicitly closed before they can be redefined by
866 another Parse message, but this is not required for the unnamed statement.
867 Named prepared statements can also be created and accessed at the SQL
868 command level, using <command>PREPARE</command> and <command>EXECUTE</command>.
869 </para>
871 <para>
872 Once a prepared statement exists, it can be readied for execution using a
873 Bind message. The Bind message gives the name of the source prepared
874 statement (empty string denotes the unnamed prepared statement), the name
875 of the destination portal (empty string denotes the unnamed portal), and
876 the values to use for any parameter placeholders present in the prepared
877 statement. The
878 supplied parameter set must match those needed by the prepared statement.
879 (If you declared any <type>void</type> parameters in the Parse message,
880 pass NULL values for them in the Bind message.)
881 Bind also specifies the format to use for any data returned
882 by the query; the format can be specified overall, or per-column.
883 The response is either BindComplete or ErrorResponse.
884 </para>
886 <note>
887 <para>
888 The choice between text and binary output is determined by the format
889 codes given in Bind, regardless of the SQL command involved. The
890 <literal>BINARY</literal> attribute in cursor declarations is irrelevant when
891 using extended query protocol.
892 </para>
893 </note>
895 <para>
896 Query planning typically occurs when the Bind message is processed.
897 If the prepared statement has no parameters, or is executed repeatedly,
898 the server might save the created plan and re-use it during subsequent
899 Bind messages for the same prepared statement. However, it will do so
900 only if it finds that a generic plan can be created that is not much
901 less efficient than a plan that depends on the specific parameter values
902 supplied. This happens transparently so far as the protocol is concerned.
903 </para>
905 <para>
906 If successfully created, a named portal object lasts till the end of the
907 current transaction, unless explicitly destroyed. An unnamed portal is
908 destroyed at the end of the transaction, or as soon as the next Bind
909 statement specifying the unnamed portal as destination is issued. (Note
910 that a simple Query message also destroys the unnamed portal.) Named
911 portals must be explicitly closed before they can be redefined by another
912 Bind message, but this is not required for the unnamed portal.
913 Named portals can also be created and accessed at the SQL
914 command level, using <command>DECLARE CURSOR</command> and <command>FETCH</command>.
915 </para>
917 <para>
918 Once a portal exists, it can be executed using an Execute message.
919 The Execute message specifies the portal name (empty string denotes the
920 unnamed portal) and
921 a maximum result-row count (zero meaning <quote>fetch all rows</quote>).
922 The result-row count is only meaningful for portals
923 containing commands that return row sets; in other cases the command is
924 always executed to completion, and the row count is ignored.
925 The possible
926 responses to Execute are the same as those described above for queries
927 issued via simple query protocol, except that Execute doesn't cause
928 ReadyForQuery or RowDescription to be issued.
929 </para>
931 <para>
932 If Execute terminates before completing the execution of a portal
933 (due to reaching a nonzero result-row count), it will send a
934 PortalSuspended message; the appearance of this message tells the frontend
935 that another Execute should be issued against the same portal to
936 complete the operation. The CommandComplete message indicating
937 completion of the source SQL command is not sent until
938 the portal's execution is completed. Therefore, an Execute phase is
939 always terminated by the appearance of exactly one of these messages:
940 CommandComplete, EmptyQueryResponse (if the portal was created from
941 an empty query string), ErrorResponse, or PortalSuspended.
942 </para>
944 <para>
945 At completion of each series of extended-query messages, the frontend
946 should issue a Sync message. This parameterless message causes the
947 backend to close the current transaction if it's not inside a
948 <command>BEGIN</command>/<command>COMMIT</command> transaction block (<quote>close</quote>
949 meaning to commit if no error, or roll back if error). Then a
950 ReadyForQuery response is issued. The purpose of Sync is to provide
951 a resynchronization point for error recovery. When an error is detected
952 while processing any extended-query message, the backend issues
953 ErrorResponse, then reads and discards messages until a Sync is reached,
954 then issues ReadyForQuery and returns to normal message processing.
955 (But note that no skipping occurs if an error is detected
956 <emphasis>while</emphasis> processing Sync &mdash; this ensures that there is one
957 and only one ReadyForQuery sent for each Sync.)
958 </para>
960 <note>
961 <para>
962 Sync does not cause a transaction block opened with <command>BEGIN</command>
963 to be closed. It is possible to detect this situation since the
964 ReadyForQuery message includes transaction status information.
965 </para>
966 </note>
968 <para>
969 In addition to these fundamental, required operations, there are several
970 optional operations that can be used with extended-query protocol.
971 </para>
973 <para>
974 The Describe message (portal variant) specifies the name of an existing
975 portal (or an empty string for the unnamed portal). The response is a
976 RowDescription message describing the rows that will be returned by
977 executing the portal; or a NoData message if the portal does not contain a
978 query that will return rows; or ErrorResponse if there is no such portal.
979 </para>
981 <para>
982 The Describe message (statement variant) specifies the name of an existing
983 prepared statement (or an empty string for the unnamed prepared
984 statement). The response is a ParameterDescription message describing the
985 parameters needed by the statement, followed by a RowDescription message
986 describing the rows that will be returned when the statement is eventually
987 executed (or a NoData message if the statement will not return rows).
988 ErrorResponse is issued if there is no such prepared statement. Note that
989 since Bind has not yet been issued, the formats to be used for returned
990 columns are not yet known to the backend; the format code fields in the
991 RowDescription message will be zeroes in this case.
992 </para>
994 <tip>
995 <para>
996 In most scenarios the frontend should issue one or the other variant
997 of Describe before issuing Execute, to ensure that it knows how to
998 interpret the results it will get back.
999 </para>
1000 </tip>
1002 <para>
1003 The Close message closes an existing prepared statement or portal
1004 and releases resources. It is not an error to issue Close against
1005 a nonexistent statement or portal name. The response is normally
1006 CloseComplete, but could be ErrorResponse if some difficulty is
1007 encountered while releasing resources. Note that closing a prepared
1008 statement implicitly closes any open portals that were constructed
1009 from that statement.
1010 </para>
1012 <para>
1013 The Flush message does not cause any specific output to be generated,
1014 but forces the backend to deliver any data pending in its output
1015 buffers. A Flush must be sent after any extended-query command except
1016 Sync, if the frontend wishes to examine the results of that command before
1017 issuing more commands. Without Flush, messages returned by the backend
1018 will be combined into the minimum possible number of packets to minimize
1019 network overhead.
1020 </para>
1022 <note>
1023 <para>
1024 The simple Query message is approximately equivalent to the series Parse,
1025 Bind, portal Describe, Execute, Close, Sync, using the unnamed prepared
1026 statement and portal objects and no parameters. One difference is that
1027 it will accept multiple SQL statements in the query string, automatically
1028 performing the bind/describe/execute sequence for each one in succession.
1029 Another difference is that it will not return ParseComplete, BindComplete,
1030 CloseComplete, or NoData messages.
1031 </para>
1032 </note>
1033 </sect2>
1035 <sect2 id="protocol-flow-pipelining">
1036 <title>Pipelining</title>
1038 <indexterm zone="protocol-flow-pipelining">
1039 <primary>pipelining</primary>
1040 <secondary>protocol specification</secondary>
1041 </indexterm>
1043 <para>
1044 Use of the extended query protocol
1045 allows <firstterm>pipelining</firstterm>, which means sending a series
1046 of queries without waiting for earlier ones to complete. This reduces
1047 the number of network round trips needed to complete a given series of
1048 operations. However, the user must carefully consider the required
1049 behavior if one of the steps fails, since later queries will already
1050 be in flight to the server.
1051 </para>
1053 <para>
1054 One way to deal with that is to make the whole query series be a
1055 single transaction, that is wrap it in <command>BEGIN</command> ...
1056 <command>COMMIT</command>. However, this does not help if one wishes
1057 for some of the commands to commit independently of others.
1058 </para>
1060 <para>
1061 The extended query protocol provides another way to manage this
1062 concern, which is to omit sending Sync messages between steps that
1063 are dependent. Since, after an error, the backend will skip command
1064 messages until it finds Sync, this allows later commands in a pipeline
1065 to be skipped automatically when an earlier one fails, without the
1066 client having to manage that explicitly with <command>BEGIN</command>
1067 and <command>COMMIT</command>. Independently-committable segments
1068 of the pipeline can be separated by Sync messages.
1069 </para>
1071 <para>
1072 If the client has not issued an explicit <command>BEGIN</command>,
1073 then each Sync ordinarily causes an implicit <command>COMMIT</command>
1074 if the preceding step(s) succeeded, or an
1075 implicit <command>ROLLBACK</command> if they failed. However, there
1076 are a few DDL commands (such as <command>CREATE DATABASE</command>)
1077 that cannot be executed inside a transaction block. If one of
1078 these is executed in a pipeline, it will fail unless it is the first
1079 command in the pipeline. Furthermore, upon success it will force an
1080 immediate commit to preserve database consistency. Thus a Sync
1081 immediately following one of these commands has no effect except to
1082 respond with ReadyForQuery.
1083 </para>
1085 <para>
1086 When using this method, completion of the pipeline must be determined
1087 by counting ReadyForQuery messages and waiting for that to reach the
1088 number of Syncs sent. Counting command completion responses is
1089 unreliable, since some of the commands may be skipped and thus not
1090 produce a completion message.
1091 </para>
1092 </sect2>
1094 <sect2 id="protocol-flow-function-call">
1095 <title>Function Call</title>
1097 <para>
1098 The Function Call sub-protocol allows the client to request a direct
1099 call of any function that exists in the database's
1100 <structname>pg_proc</structname> system catalog. The client must have
1101 execute permission for the function.
1102 </para>
1104 <note>
1105 <para>
1106 The Function Call sub-protocol is a legacy feature that is probably best
1107 avoided in new code. Similar results can be accomplished by setting up
1108 a prepared statement that does <literal>SELECT function($1, ...)</literal>.
1109 The Function Call cycle can then be replaced with Bind/Execute.
1110 </para>
1111 </note>
1113 <para>
1114 A Function Call cycle is initiated by the frontend sending a
1115 FunctionCall message to the backend. The backend then sends one
1116 or more response messages depending on the results of the function
1117 call, and finally a ReadyForQuery response message. ReadyForQuery
1118 informs the frontend that it can safely send a new query or
1119 function call.
1120 </para>
1122 <para>
1123 The possible response messages from the backend are:
1125 <variablelist>
1126 <varlistentry>
1127 <term>ErrorResponse</term>
1128 <listitem>
1129 <para>
1130 An error has occurred.
1131 </para>
1132 </listitem>
1133 </varlistentry>
1135 <varlistentry>
1136 <term>FunctionCallResponse</term>
1137 <listitem>
1138 <para>
1139 The function call was completed and returned the result given
1140 in the message.
1141 (Note that the Function Call protocol can only handle a single
1142 scalar result, not a row type or set of results.)
1143 </para>
1144 </listitem>
1145 </varlistentry>
1147 <varlistentry>
1148 <term>ReadyForQuery</term>
1149 <listitem>
1150 <para>
1151 Processing of the function call is complete. ReadyForQuery
1152 will always be sent, whether processing terminates
1153 successfully or with an error.
1154 </para>
1155 </listitem>
1156 </varlistentry>
1158 <varlistentry>
1159 <term>NoticeResponse</term>
1160 <listitem>
1161 <para>
1162 A warning message has been issued in relation to the function
1163 call. Notices are in addition to other responses, i.e., the
1164 backend will continue processing the command.
1165 </para>
1166 </listitem>
1167 </varlistentry>
1168 </variablelist>
1169 </para>
1170 </sect2>
1172 <sect2 id="protocol-copy">
1173 <title>COPY Operations</title>
1175 <para>
1176 The <command>COPY</command> command allows high-speed bulk data transfer
1177 to or from the server. Copy-in and copy-out operations each switch
1178 the connection into a distinct sub-protocol, which lasts until the
1179 operation is completed.
1180 </para>
1182 <para>
1183 Copy-in mode (data transfer to the server) is initiated when the
1184 backend executes a <command>COPY FROM STDIN</command> SQL statement. The backend
1185 sends a CopyInResponse message to the frontend. The frontend should
1186 then send zero or more CopyData messages, forming a stream of input
1187 data. (The message boundaries are not required to have anything to do
1188 with row boundaries, although that is often a reasonable choice.)
1189 The frontend can terminate the copy-in mode by sending either a CopyDone
1190 message (allowing successful termination) or a CopyFail message (which
1191 will cause the <command>COPY</command> SQL statement to fail with an
1192 error). The backend then reverts to the command-processing mode it was
1193 in before the <command>COPY</command> started, which will be either simple or
1194 extended query protocol. It will next send either CommandComplete
1195 (if successful) or ErrorResponse (if not).
1196 </para>
1198 <para>
1199 In the event of a backend-detected error during copy-in mode (including
1200 receipt of a CopyFail message), the backend will issue an ErrorResponse
1201 message. If the <command>COPY</command> command was issued via an extended-query
1202 message, the backend will now discard frontend messages until a Sync
1203 message is received, then it will issue ReadyForQuery and return to normal
1204 processing. If the <command>COPY</command> command was issued in a simple
1205 Query message, the rest of that message is discarded and ReadyForQuery
1206 is issued. In either case, any subsequent CopyData, CopyDone, or CopyFail
1207 messages issued by the frontend will simply be dropped.
1208 </para>
1210 <para>
1211 The backend will ignore Flush and Sync messages received during copy-in
1212 mode. Receipt of any other non-copy message type constitutes an error
1213 that will abort the copy-in state as described above. (The exception for
1214 Flush and Sync is for the convenience of client libraries that always
1215 send Flush or Sync after an Execute message, without checking whether
1216 the command to be executed is a <command>COPY FROM STDIN</command>.)
1217 </para>
1219 <para>
1220 Copy-out mode (data transfer from the server) is initiated when the
1221 backend executes a <command>COPY TO STDOUT</command> SQL statement. The backend
1222 sends a CopyOutResponse message to the frontend, followed by
1223 zero or more CopyData messages (always one per row), followed by CopyDone.
1224 The backend then reverts to the command-processing mode it was
1225 in before the <command>COPY</command> started, and sends CommandComplete.
1226 The frontend cannot abort the transfer (except by closing the connection
1227 or issuing a Cancel request),
1228 but it can discard unwanted CopyData and CopyDone messages.
1229 </para>
1231 <para>
1232 In the event of a backend-detected error during copy-out mode,
1233 the backend will issue an ErrorResponse message and revert to normal
1234 processing. The frontend should treat receipt of ErrorResponse as
1235 terminating the copy-out mode.
1236 </para>
1238 <para>
1239 It is possible for NoticeResponse and ParameterStatus messages to be
1240 interspersed between CopyData messages; frontends must handle these cases,
1241 and should be prepared for other asynchronous message types as well (see
1242 <xref linkend="protocol-async"/>). Otherwise, any message type other than
1243 CopyData or CopyDone may be treated as terminating copy-out mode.
1244 </para>
1246 <para>
1247 There is another Copy-related mode called copy-both, which allows
1248 high-speed bulk data transfer to <emphasis>and</emphasis> from the server.
1249 Copy-both mode is initiated when a backend in walsender mode
1250 executes a <command>START_REPLICATION</command> statement. The
1251 backend sends a CopyBothResponse message to the frontend. Both
1252 the backend and the frontend may then send CopyData messages
1253 until either end sends a CopyDone message. After the client
1254 sends a CopyDone message, the connection goes from copy-both mode to
1255 copy-out mode, and the client may not send any more CopyData messages.
1256 Similarly, when the server sends a CopyDone message, the connection
1257 goes into copy-in mode, and the server may not send any more CopyData
1258 messages. After both sides have sent a CopyDone message, the copy mode
1259 is terminated, and the backend reverts to the command-processing mode.
1260 In the event of a backend-detected error during copy-both mode,
1261 the backend will issue an ErrorResponse message, discard frontend messages
1262 until a Sync message is received, and then issue ReadyForQuery and return
1263 to normal processing. The frontend should treat receipt of ErrorResponse
1264 as terminating the copy in both directions; no CopyDone should be sent
1265 in this case. See <xref linkend="protocol-replication"/> for more
1266 information on the subprotocol transmitted over copy-both mode.
1267 </para>
1269 <para>
1270 The CopyInResponse, CopyOutResponse and CopyBothResponse messages
1271 include fields that inform the frontend of the number of columns
1272 per row and the format codes being used for each column. (As of
1273 the present implementation, all columns in a given <command>COPY</command>
1274 operation will use the same format, but the message design does not
1275 assume this.)
1276 </para>
1278 </sect2>
1280 <sect2 id="protocol-async">
1281 <title>Asynchronous Operations</title>
1283 <para>
1284 There are several cases in which the backend will send messages that
1285 are not specifically prompted by the frontend's command stream.
1286 Frontends must be prepared to deal with these messages at any time,
1287 even when not engaged in a query.
1288 At minimum, one should check for these cases before beginning to
1289 read a query response.
1290 </para>
1292 <para>
1293 It is possible for NoticeResponse messages to be generated due to
1294 outside activity; for example, if the database administrator commands
1295 a <quote>fast</quote> database shutdown, the backend will send a NoticeResponse
1296 indicating this fact before closing the connection. Accordingly,
1297 frontends should always be prepared to accept and display NoticeResponse
1298 messages, even when the connection is nominally idle.
1299 </para>
1301 <para>
1302 ParameterStatus messages will be generated whenever the active
1303 value changes for any of the parameters the backend believes the
1304 frontend should know about. Most commonly this occurs in response
1305 to a <command>SET</command> SQL command executed by the frontend, and
1306 this case is effectively synchronous &mdash; but it is also possible
1307 for parameter status changes to occur because the administrator
1308 changed a configuration file and then sent the
1309 <systemitem>SIGHUP</systemitem> signal to the server. Also,
1310 if a <command>SET</command> command is rolled back, an appropriate
1311 ParameterStatus message will be generated to report the current
1312 effective value.
1313 </para>
1315 <para>
1316 At present there is a hard-wired set of parameters for which
1317 ParameterStatus will be generated: they are
1318 <varname>server_version</varname>,
1319 <varname>server_encoding</varname>,
1320 <varname>client_encoding</varname>,
1321 <varname>application_name</varname>,
1322 <varname>default_transaction_read_only</varname>,
1323 <varname>in_hot_standby</varname>,
1324 <varname>is_superuser</varname>,
1325 <varname>session_authorization</varname>,
1326 <varname>DateStyle</varname>,
1327 <varname>IntervalStyle</varname>,
1328 <varname>TimeZone</varname>,
1329 <varname>integer_datetimes</varname>, and
1330 <varname>standard_conforming_strings</varname>.
1331 (<varname>server_encoding</varname>, <varname>TimeZone</varname>, and
1332 <varname>integer_datetimes</varname> were not reported by releases before 8.0;
1333 <varname>standard_conforming_strings</varname> was not reported by releases
1334 before 8.1;
1335 <varname>IntervalStyle</varname> was not reported by releases before 8.4;
1336 <varname>application_name</varname> was not reported by releases before
1337 9.0;
1338 <varname>default_transaction_read_only</varname> and
1339 <varname>in_hot_standby</varname> were not reported by releases before
1340 14.)
1341 Note that
1342 <varname>server_version</varname>,
1343 <varname>server_encoding</varname> and
1344 <varname>integer_datetimes</varname>
1345 are pseudo-parameters that cannot change after startup.
1346 This set might change in the future, or even become configurable.
1347 Accordingly, a frontend should simply ignore ParameterStatus for
1348 parameters that it does not understand or care about.
1349 </para>
1351 <para>
1352 If a frontend issues a <command>LISTEN</command> command, then the
1353 backend will send a NotificationResponse message (not to be
1354 confused with NoticeResponse!) whenever a
1355 <command>NOTIFY</command> command is executed for the same
1356 channel name.
1357 </para>
1359 <note>
1360 <para>
1361 At present, NotificationResponse can only be sent outside a
1362 transaction, and thus it will not occur in the middle of a
1363 command-response series, though it might occur just before ReadyForQuery.
1364 It is unwise to design frontend logic that assumes that, however.
1365 Good practice is to be able to accept NotificationResponse at any
1366 point in the protocol.
1367 </para>
1368 </note>
1369 </sect2>
1371 <sect2 id="protocol-flow-canceling-requests">
1372 <title>Canceling Requests in Progress</title>
1374 <para>
1375 During the processing of a query, the frontend might request
1376 cancellation of the query. The cancel request is not sent
1377 directly on the open connection to the backend for reasons of
1378 implementation efficiency: we don't want to have the backend
1379 constantly checking for new input from the frontend during query
1380 processing. Cancel requests should be relatively infrequent, so
1381 we make them slightly cumbersome in order to avoid a penalty in
1382 the normal case.
1383 </para>
1385 <para>
1386 To issue a cancel request, the frontend opens a new connection to
1387 the server and sends a CancelRequest message, rather than the
1388 StartupMessage message that would ordinarily be sent across a new
1389 connection. The server will process this request and then close
1390 the connection. For security reasons, no direct reply is made to
1391 the cancel request message.
1392 </para>
1394 <para>
1395 A CancelRequest message will be ignored unless it contains the
1396 same key data (PID and secret key) passed to the frontend during
1397 connection start-up. If the request matches the PID and secret
1398 key for a currently executing backend, the processing of the
1399 current query is aborted. (In the existing implementation, this is
1400 done by sending a special signal to the backend process that is
1401 processing the query.)
1402 </para>
1404 <para>
1405 The cancellation signal might or might not have any effect &mdash; for
1406 example, if it arrives after the backend has finished processing
1407 the query, then it will have no effect. If the cancellation is
1408 effective, it results in the current command being terminated
1409 early with an error message.
1410 </para>
1412 <para>
1413 The upshot of all this is that for reasons of both security and
1414 efficiency, the frontend has no direct way to tell whether a
1415 cancel request has succeeded. It must continue to wait for the
1416 backend to respond to the query. Issuing a cancel simply improves
1417 the odds that the current query will finish soon, and improves the
1418 odds that it will fail with an error message instead of
1419 succeeding.
1420 </para>
1422 <para>
1423 Since the cancel request is sent across a new connection to the
1424 server and not across the regular frontend/backend communication
1425 link, it is possible for the cancel request to be issued by any
1426 process, not just the frontend whose query is to be canceled.
1427 This might provide additional flexibility when building
1428 multiple-process applications. It also introduces a security
1429 risk, in that unauthorized persons might try to cancel queries.
1430 The security risk is addressed by requiring a dynamically
1431 generated secret key to be supplied in cancel requests.
1432 </para>
1433 </sect2>
1435 <sect2 id="protocol-flow-termination">
1436 <title>Termination</title>
1438 <para>
1439 The normal, graceful termination procedure is that the frontend
1440 sends a Terminate message and immediately closes the connection.
1441 On receipt of this message, the backend closes the connection and
1442 terminates.
1443 </para>
1445 <para>
1446 In rare cases (such as an administrator-commanded database shutdown)
1447 the backend might disconnect without any frontend request to do so.
1448 In such cases the backend will attempt to send an error or notice message
1449 giving the reason for the disconnection before it closes the connection.
1450 </para>
1452 <para>
1453 Other termination scenarios arise from various failure cases, such as core
1454 dump at one end or the other, loss of the communications link, loss of
1455 message-boundary synchronization, etc. If either frontend or backend sees
1456 an unexpected closure of the connection, it should clean
1457 up and terminate. The frontend has the option of launching a new backend
1458 by recontacting the server if it doesn't want to terminate itself.
1459 Closing the connection is also advisable if an unrecognizable message type
1460 is received, since this probably indicates loss of message-boundary sync.
1461 </para>
1463 <para>
1464 For either normal or abnormal termination, any open transaction is
1465 rolled back, not committed. One should note however that if a
1466 frontend disconnects while a non-<command>SELECT</command> query
1467 is being processed, the backend will probably finish the query
1468 before noticing the disconnection. If the query is outside any
1469 transaction block (<command>BEGIN</command> ... <command>COMMIT</command>
1470 sequence) then its results might be committed before the
1471 disconnection is recognized.
1472 </para>
1473 </sect2>
1475 <sect2 id="protocol-flow-ssl">
1476 <title><acronym>SSL</acronym> Session Encryption</title>
1478 <para>
1479 If <productname>PostgreSQL</productname> was built with
1480 <acronym>SSL</acronym> support, frontend/backend communications
1481 can be encrypted using <acronym>SSL</acronym>. This provides
1482 communication security in environments where attackers might be
1483 able to capture the session traffic. For more information on
1484 encrypting <productname>PostgreSQL</productname> sessions with
1485 <acronym>SSL</acronym>, see <xref linkend="ssl-tcp"/>.
1486 </para>
1488 <para>
1489 To initiate an <acronym>SSL</acronym>-encrypted connection, the
1490 frontend initially sends an SSLRequest message rather than a
1491 StartupMessage. The server then responds with a single byte
1492 containing <literal>S</literal> or <literal>N</literal>, indicating that it is
1493 willing or unwilling to perform <acronym>SSL</acronym>,
1494 respectively. The frontend might close the connection at this point
1495 if it is dissatisfied with the response. To continue after
1496 <literal>S</literal>, perform an <acronym>SSL</acronym> startup handshake
1497 (not described here, part of the <acronym>SSL</acronym>
1498 specification) with the server. If this is successful, continue
1499 with sending the usual StartupMessage. In this case the
1500 StartupMessage and all subsequent data will be
1501 <acronym>SSL</acronym>-encrypted. To continue after
1502 <literal>N</literal>, send the usual StartupMessage and proceed without
1503 encryption.
1504 (Alternatively, it is permissible to issue a GSSENCRequest message
1505 after an <literal>N</literal> response to try to
1506 use <acronym>GSSAPI</acronym> encryption instead
1507 of <acronym>SSL</acronym>.)
1508 </para>
1510 <para>
1511 The frontend should also be prepared to handle an ErrorMessage
1512 response to SSLRequest from the server. This would only occur if
1513 the server predates the addition of <acronym>SSL</acronym> support
1514 to <productname>PostgreSQL</productname>. (Such servers are now very ancient,
1515 and likely do not exist in the wild anymore.)
1516 In this case the connection must
1517 be closed, but the frontend might choose to open a fresh connection
1518 and proceed without requesting <acronym>SSL</acronym>.
1519 </para>
1521 <para>
1522 When <acronym>SSL</acronym> encryption can be performed, the server
1523 is expected to send only the single <literal>S</literal> byte and then
1524 wait for the frontend to initiate an <acronym>SSL</acronym> handshake.
1525 If additional bytes are available to read at this point, it likely
1526 means that a man-in-the-middle is attempting to perform a
1527 buffer-stuffing attack
1528 (<ulink url="https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-2021-23222</ulink>).
1529 Frontends should be coded either to read exactly one byte from the
1530 socket before turning the socket over to their SSL library, or to
1531 treat it as a protocol violation if they find they have read additional
1532 bytes.
1533 </para>
1535 <para>
1536 An initial SSLRequest can also be used in a connection that is being
1537 opened to send a CancelRequest message.
1538 </para>
1540 <para>
1541 While the protocol itself does not provide a way for the server to
1542 force <acronym>SSL</acronym> encryption, the administrator can
1543 configure the server to reject unencrypted sessions as a byproduct
1544 of authentication checking.
1545 </para>
1546 </sect2>
1548 <sect2 id="protocol-flow-gssapi">
1549 <title><acronym>GSSAPI</acronym> Session Encryption</title>
1551 <para>
1552 If <productname>PostgreSQL</productname> was built with
1553 <acronym>GSSAPI</acronym> support, frontend/backend communications
1554 can be encrypted using <acronym>GSSAPI</acronym>. This provides
1555 communication security in environments where attackers might be
1556 able to capture the session traffic. For more information on
1557 encrypting <productname>PostgreSQL</productname> sessions with
1558 <acronym>GSSAPI</acronym>, see <xref linkend="gssapi-enc"/>.
1559 </para>
1561 <para>
1562 To initiate a <acronym>GSSAPI</acronym>-encrypted connection, the
1563 frontend initially sends a GSSENCRequest message rather than a
1564 StartupMessage. The server then responds with a single byte
1565 containing <literal>G</literal> or <literal>N</literal>, indicating that it
1566 is willing or unwilling to perform <acronym>GSSAPI</acronym> encryption,
1567 respectively. The frontend might close the connection at this point
1568 if it is dissatisfied with the response. To continue after
1569 <literal>G</literal>, using the GSSAPI C bindings as discussed in
1570 <ulink url="https://tools.ietf.org/html/rfc2744">RFC 2744</ulink>
1571 or equivalent, perform a <acronym>GSSAPI</acronym> initialization by
1572 calling <function>gss_init_sec_context()</function> in a loop and sending
1573 the result to the server, starting with an empty input and then with each
1574 result from the server, until it returns no output. When sending the
1575 results of <function>gss_init_sec_context()</function> to the server,
1576 prepend the length of the message as a four byte integer in network byte
1577 order.
1578 To continue after
1579 <literal>N</literal>, send the usual StartupMessage and proceed without
1580 encryption.
1581 (Alternatively, it is permissible to issue an SSLRequest message
1582 after an <literal>N</literal> response to try to
1583 use <acronym>SSL</acronym> encryption instead
1584 of <acronym>GSSAPI</acronym>.)
1585 </para>
1587 <para>
1588 The frontend should also be prepared to handle an ErrorMessage
1589 response to GSSENCRequest from the server. This would only occur if
1590 the server predates the addition of <acronym>GSSAPI</acronym> encryption
1591 support to <productname>PostgreSQL</productname>. In this case the
1592 connection must be closed, but the frontend might choose to open a fresh
1593 connection and proceed without requesting <acronym>GSSAPI</acronym>
1594 encryption.
1595 </para>
1597 <para>
1598 When <acronym>GSSAPI</acronym> encryption can be performed, the server
1599 is expected to send only the single <literal>G</literal> byte and then
1600 wait for the frontend to initiate a <acronym>GSSAPI</acronym> handshake.
1601 If additional bytes are available to read at this point, it likely
1602 means that a man-in-the-middle is attempting to perform a
1603 buffer-stuffing attack
1604 (<ulink url="https://www.postgresql.org/support/security/CVE-2021-23222/">CVE-2021-23222</ulink>).
1605 Frontends should be coded either to read exactly one byte from the
1606 socket before turning the socket over to their GSSAPI library, or to
1607 treat it as a protocol violation if they find they have read additional
1608 bytes.
1609 </para>
1611 <para>
1612 An initial GSSENCRequest can also be used in a connection that is being
1613 opened to send a CancelRequest message.
1614 </para>
1616 <para>
1617 Once <acronym>GSSAPI</acronym> encryption has been successfully
1618 established, use <function>gss_wrap()</function> to
1619 encrypt the usual StartupMessage and all subsequent data, prepending the
1620 length of the result from <function>gss_wrap()</function> as a four byte
1621 integer in network byte order to the actual encrypted payload. Note that
1622 the server will only accept encrypted packets from the client which are less
1623 than 16kB; <function>gss_wrap_size_limit()</function> should be used by the
1624 client to determine the size of the unencrypted message which will fit
1625 within this limit and larger messages should be broken up into multiple
1626 <function>gss_wrap()</function> calls. Typical segments are 8kB of
1627 unencrypted data, resulting in encrypted packets of slightly larger than 8kB
1628 but well within the 16kB maximum. The server can be expected to not send
1629 encrypted packets of larger than 16kB to the client.
1630 </para>
1632 <para>
1633 While the protocol itself does not provide a way for the server to
1634 force <acronym>GSSAPI</acronym> encryption, the administrator can
1635 configure the server to reject unencrypted sessions as a byproduct
1636 of authentication checking.
1637 </para>
1638 </sect2>
1639 </sect1>
1641 <sect1 id="sasl-authentication">
1642 <title>SASL Authentication</title>
1644 <para>
1645 <firstterm>SASL</firstterm> is a framework for authentication in connection-oriented
1646 protocols. At the moment, <productname>PostgreSQL</productname> implements two SASL
1647 authentication mechanisms, SCRAM-SHA-256 and SCRAM-SHA-256-PLUS. More
1648 might be added in the future. The below steps illustrate how SASL
1649 authentication is performed in general, while the next subsection gives
1650 more details on SCRAM-SHA-256 and SCRAM-SHA-256-PLUS.
1651 </para>
1653 <procedure>
1654 <title>SASL Authentication Message Flow</title>
1656 <step id="sasl-auth-begin">
1657 <para>
1658 To begin a SASL authentication exchange, the server sends an
1659 AuthenticationSASL message. It includes a list of SASL authentication
1660 mechanisms that the server can accept, in the server's preferred order.
1661 </para>
1662 </step>
1664 <step id="sasl-auth-initial-response">
1665 <para>
1666 The client selects one of the supported mechanisms from the list, and sends
1667 a SASLInitialResponse message to the server. The message includes the name
1668 of the selected mechanism, and an optional Initial Client Response, if the
1669 selected mechanism uses that.
1670 </para>
1671 </step>
1673 <step id="sasl-auth-continue">
1674 <para>
1675 One or more server-challenge and client-response message will follow. Each
1676 server-challenge is sent in an AuthenticationSASLContinue message, followed
1677 by a response from client in a SASLResponse message. The particulars of
1678 the messages are mechanism specific.
1679 </para>
1680 </step>
1682 <step id="sasl-auth-end">
1683 <para>
1684 Finally, when the authentication exchange is completed successfully, the
1685 server sends an AuthenticationSASLFinal message, followed
1686 immediately by an AuthenticationOk message. The AuthenticationSASLFinal
1687 contains additional server-to-client data, whose content is particular to the
1688 selected authentication mechanism. If the authentication mechanism doesn't
1689 use additional data that's sent at completion, the AuthenticationSASLFinal
1690 message is not sent.
1691 </para>
1692 </step>
1693 </procedure>
1695 <para>
1696 On error, the server can abort the authentication at any stage, and send an
1697 ErrorMessage.
1698 </para>
1700 <sect2 id="sasl-scram-sha-256">
1701 <title>SCRAM-SHA-256 Authentication</title>
1703 <para>
1704 The implemented SASL mechanisms at the moment
1705 are <literal>SCRAM-SHA-256</literal> and its variant with channel
1706 binding <literal>SCRAM-SHA-256-PLUS</literal>. They are described in
1707 detail in <ulink url="https://tools.ietf.org/html/rfc7677">RFC 7677</ulink>
1708 and <ulink url="https://tools.ietf.org/html/rfc5802">RFC 5802</ulink>.
1709 </para>
1711 <para>
1712 When SCRAM-SHA-256 is used in PostgreSQL, the server will ignore the user name
1713 that the client sends in the <structname>client-first-message</structname>. The user name
1714 that was already sent in the startup message is used instead.
1715 <productname>PostgreSQL</productname> supports multiple character encodings, while SCRAM
1716 dictates UTF-8 to be used for the user name, so it might be impossible to
1717 represent the PostgreSQL user name in UTF-8.
1718 </para>
1720 <para>
1721 The SCRAM specification dictates that the password is also in UTF-8, and is
1722 processed with the <firstterm>SASLprep</firstterm> algorithm.
1723 <productname>PostgreSQL</productname>, however, does not require UTF-8 to be used for
1724 the password. When a user's password is set, it is processed with SASLprep
1725 as if it was in UTF-8, regardless of the actual encoding used. However, if
1726 it is not a legal UTF-8 byte sequence, or it contains UTF-8 byte sequences
1727 that are prohibited by the SASLprep algorithm, the raw password will be used
1728 without SASLprep processing, instead of throwing an error. This allows the
1729 password to be normalized when it is in UTF-8, but still allows a non-UTF-8
1730 password to be used, and doesn't require the system to know which encoding
1731 the password is in.
1732 </para>
1734 <para>
1735 <firstterm>Channel binding</firstterm> is supported in PostgreSQL builds with
1736 SSL support. The SASL mechanism name for SCRAM with channel binding is
1737 <literal>SCRAM-SHA-256-PLUS</literal>. The channel binding type used by
1738 PostgreSQL is <literal>tls-server-end-point</literal>.
1739 </para>
1741 <para>
1742 In <acronym>SCRAM</acronym> without channel binding, the server chooses
1743 a random number that is transmitted to the client to be mixed with the
1744 user-supplied password in the transmitted password hash. While this
1745 prevents the password hash from being successfully retransmitted in
1746 a later session, it does not prevent a fake server between the real
1747 server and client from passing through the server's random value
1748 and successfully authenticating.
1749 </para>
1751 <para>
1752 <acronym>SCRAM</acronym> with channel binding prevents such
1753 man-in-the-middle attacks by mixing the signature of the server's
1754 certificate into the transmitted password hash. While a fake server can
1755 retransmit the real server's certificate, it doesn't have access to the
1756 private key matching that certificate, and therefore cannot prove it is
1757 the owner, causing SSL connection failure.
1758 </para>
1760 <procedure>
1761 <title>Example</title>
1762 <step id="scram-begin">
1763 <para>
1764 The server sends an AuthenticationSASL message. It includes a list of
1765 SASL authentication mechanisms that the server can accept.
1766 This will be <literal>SCRAM-SHA-256-PLUS</literal>
1767 and <literal>SCRAM-SHA-256</literal> if the server is built with SSL
1768 support, or else just the latter.
1769 </para>
1770 </step>
1772 <step id="scram-client-first">
1773 <para>
1774 The client responds by sending a SASLInitialResponse message, which
1775 indicates the chosen mechanism, <literal>SCRAM-SHA-256</literal> or
1776 <literal>SCRAM-SHA-256-PLUS</literal>. (A client is free to choose either
1777 mechanism, but for better security it should choose the channel-binding
1778 variant if it can support it.) In the Initial Client response field, the
1779 message contains the SCRAM <structname>client-first-message</structname>.
1780 The <structname>client-first-message</structname> also contains the channel
1781 binding type chosen by the client.
1782 </para>
1783 </step>
1785 <step id="scram-server-first">
1786 <para>
1787 Server sends an AuthenticationSASLContinue message, with a SCRAM
1788 <structname>server-first-message</structname> as the content.
1789 </para>
1790 </step>
1792 <step id="scram-client-final">
1793 <para>
1794 Client sends a SASLResponse message, with SCRAM
1795 <structname>client-final-message</structname> as the content.
1796 </para>
1797 </step>
1799 <step id="scram-server-final">
1800 <para>
1801 Server sends an AuthenticationSASLFinal message, with the SCRAM
1802 <structname>server-final-message</structname>, followed immediately by
1803 an AuthenticationOk message.
1804 </para>
1805 </step>
1806 </procedure>
1807 </sect2>
1808 </sect1>
1810 <sect1 id="protocol-replication">
1811 <title>Streaming Replication Protocol</title>
1813 <para>
1814 To initiate streaming replication, the frontend sends the
1815 <literal>replication</literal> parameter in the startup message. A Boolean
1816 value of <literal>true</literal> (or <literal>on</literal>,
1817 <literal>yes</literal>, <literal>1</literal>) tells the backend to go into
1818 physical replication walsender mode, wherein a small set of replication
1819 commands, shown below, can be issued instead of SQL statements.
1820 </para>
1822 <para>
1823 Passing <literal>database</literal> as the value for the
1824 <literal>replication</literal> parameter instructs the backend to go into
1825 logical replication walsender mode, connecting to the database specified in
1826 the <literal>dbname</literal> parameter. In logical replication walsender
1827 mode, the replication commands shown below as well as normal SQL commands can
1828 be issued.
1829 </para>
1831 <para>
1832 In either physical replication or logical replication walsender mode, only the
1833 simple query protocol can be used.
1834 </para>
1836 <para>
1837 For the purpose of testing replication commands, you can make a replication
1838 connection via <application>psql</application> or any other
1839 <application>libpq</application>-using tool with a connection string including
1840 the <literal>replication</literal> option,
1841 e.g.:
1842 <programlisting>
1843 psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
1844 </programlisting>
1845 However, it is often more useful to use
1846 <xref linkend="app-pgreceivewal"/> (for physical replication) or
1847 <xref linkend="app-pgrecvlogical"/> (for logical replication).
1848 </para>
1850 <para>
1851 Replication commands are logged in the server log when
1852 <xref linkend="guc-log-replication-commands"/> is enabled.
1853 </para>
1855 <para>
1856 The commands accepted in replication mode are:
1858 <variablelist>
1859 <varlistentry id="protocol-replication-identify-system">
1860 <term><literal>IDENTIFY_SYSTEM</literal>
1861 <indexterm><primary>IDENTIFY_SYSTEM</primary></indexterm>
1862 </term>
1863 <listitem>
1864 <para>
1865 Requests the server to identify itself. Server replies with a result
1866 set of a single row, containing four fields:
1867 </para>
1869 <variablelist>
1870 <varlistentry>
1871 <term><literal>systemid</literal> (<type>text</type>)</term>
1872 <listitem>
1873 <para>
1874 The unique system identifier identifying the cluster. This
1875 can be used to check that the base backup used to initialize the
1876 standby came from the same cluster.
1877 </para>
1878 </listitem>
1879 </varlistentry>
1881 <varlistentry>
1882 <term><literal>timeline</literal> (<type>int8</type>)</term>
1883 <listitem>
1884 <para>
1885 Current timeline ID. Also useful to check that the standby is
1886 consistent with the primary.
1887 </para>
1888 </listitem>
1889 </varlistentry>
1891 <varlistentry>
1892 <term><literal>xlogpos</literal> (<type>text</type>)</term>
1893 <listitem>
1894 <para>
1895 Current WAL flush location. Useful to get a known location in the
1896 write-ahead log where streaming can start.
1897 </para>
1898 </listitem>
1899 </varlistentry>
1901 <varlistentry>
1902 <term><literal>dbname</literal> (<type>text</type>)</term>
1903 <listitem>
1904 <para>
1905 Database connected to or null.
1906 </para>
1907 </listitem>
1908 </varlistentry>
1909 </variablelist>
1910 </listitem>
1911 </varlistentry>
1913 <varlistentry id="protocol-replication-show">
1914 <term><literal>SHOW</literal> <replaceable class="parameter">name</replaceable>
1915 <indexterm><primary>SHOW</primary></indexterm>
1916 </term>
1917 <listitem>
1918 <para>
1919 Requests the server to send the current setting of a run-time parameter.
1920 This is similar to the SQL command <xref linkend="sql-show"/>.
1921 </para>
1923 <variablelist>
1924 <varlistentry>
1925 <term><replaceable class="parameter">name</replaceable></term>
1926 <listitem>
1927 <para>
1928 The name of a run-time parameter. Available parameters are documented
1929 in <xref linkend="runtime-config"/>.
1930 </para>
1931 </listitem>
1932 </varlistentry>
1933 </variablelist>
1934 </listitem>
1935 </varlistentry>
1937 <varlistentry id="protocol-replication-timeline-history">
1938 <term><literal>TIMELINE_HISTORY</literal> <replaceable class="parameter">tli</replaceable>
1939 <indexterm><primary>TIMELINE_HISTORY</primary></indexterm>
1940 </term>
1941 <listitem>
1942 <para>
1943 Requests the server to send over the timeline history file for timeline
1944 <replaceable class="parameter">tli</replaceable>. Server replies with a
1945 result set of a single row, containing two fields. While the fields
1946 are labeled as <type>text</type>, they effectively return raw bytes,
1947 with no encoding conversion:
1948 </para>
1950 <variablelist>
1951 <varlistentry>
1952 <term><literal>filename</literal> (<type>text</type>)</term>
1953 <listitem>
1954 <para>
1955 File name of the timeline history file, e.g., <filename>00000002.history</filename>.
1956 </para>
1957 </listitem>
1958 </varlistentry>
1960 <varlistentry>
1961 <term><literal>content</literal> (<type>text</type>)</term>
1962 <listitem>
1963 <para>
1964 Contents of the timeline history file.
1965 </para>
1966 </listitem>
1967 </varlistentry>
1968 </variablelist>
1969 </listitem>
1970 </varlistentry>
1972 <varlistentry id="protocol-replication-create-replication-slot" xreflabel="CREATE_REPLICATION_SLOT">
1973 <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> [, ...] ) ]
1974 <indexterm><primary>CREATE_REPLICATION_SLOT</primary></indexterm>
1975 </term>
1976 <listitem>
1977 <para>
1978 Create a physical or logical replication
1979 slot. See <xref linkend="streaming-replication-slots"/> for more about
1980 replication slots.
1981 </para>
1983 <variablelist>
1984 <varlistentry>
1985 <term><replaceable class="parameter">slot_name</replaceable></term>
1986 <listitem>
1987 <para>
1988 The name of the slot to create. Must be a valid replication slot
1989 name (see <xref linkend="streaming-replication-slots-manipulation"/>).
1990 </para>
1991 </listitem>
1992 </varlistentry>
1994 <varlistentry>
1995 <term><replaceable class="parameter">output_plugin</replaceable></term>
1996 <listitem>
1997 <para>
1998 The name of the output plugin used for logical decoding
1999 (see <xref linkend="logicaldecoding-output-plugin"/>).
2000 </para>
2001 </listitem>
2002 </varlistentry>
2004 <varlistentry>
2005 <term><literal>TEMPORARY</literal></term>
2006 <listitem>
2007 <para>
2008 Specify that this replication slot is a temporary one. Temporary
2009 slots are not saved to disk and are automatically dropped on error
2010 or when the session has finished.
2011 </para>
2012 </listitem>
2013 </varlistentry>
2014 </variablelist>
2016 <para>The following options are supported:</para>
2018 <variablelist>
2019 <varlistentry>
2020 <term><literal>TWO_PHASE [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2021 <listitem>
2022 <para>
2023 If true, this logical replication slot supports decoding of two-phase
2024 commit. With this option, commands related to two-phase commit such as
2025 <literal>PREPARE TRANSACTION</literal>, <literal>COMMIT PREPARED</literal>
2026 and <literal>ROLLBACK PREPARED</literal> are decoded and transmitted.
2027 The transaction will be decoded and transmitted at
2028 <literal>PREPARE TRANSACTION</literal> time.
2029 The default is false.
2030 </para>
2031 </listitem>
2032 </varlistentry>
2034 <varlistentry>
2035 <term><literal>RESERVE_WAL [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2036 <listitem>
2037 <para>
2038 If true, this physical replication slot reserves <acronym>WAL</acronym>
2039 immediately. Otherwise, <acronym>WAL</acronym> is only reserved upon
2040 connection from a streaming replication client.
2041 The default is false.
2042 </para>
2043 </listitem>
2044 </varlistentry>
2046 <varlistentry>
2047 <term><literal>SNAPSHOT { 'export' | 'use' | 'nothing' }</literal></term>
2048 <listitem>
2049 <para>
2050 Decides what to do with the snapshot created during logical slot
2051 initialization. <literal>'export'</literal>, which is the default,
2052 will export the snapshot for use in other sessions. This option can't
2053 be used inside a transaction. <literal>'use'</literal> will use the
2054 snapshot for the current transaction executing the command. This
2055 option must be used in a transaction, and
2056 <literal>CREATE_REPLICATION_SLOT</literal> must be the first command
2057 run in that transaction. Finally, <literal>'nothing'</literal> will
2058 just use the snapshot for logical decoding as normal but won't do
2059 anything else with it.
2060 </para>
2061 </listitem>
2062 </varlistentry>
2063 </variablelist>
2065 <para>
2066 In response to this command, the server will send a one-row result set
2067 containing the following fields:
2069 <variablelist>
2070 <varlistentry>
2071 <term><literal>slot_name</literal> (<type>text</type>)</term>
2072 <listitem>
2073 <para>
2074 The name of the newly-created replication slot.
2075 </para>
2076 </listitem>
2077 </varlistentry>
2079 <varlistentry>
2080 <term><literal>consistent_point</literal> (<type>text</type>)</term>
2081 <listitem>
2082 <para>
2083 The WAL location at which the slot became consistent. This is the
2084 earliest location from which streaming can start on this replication
2085 slot.
2086 </para>
2087 </listitem>
2088 </varlistentry>
2090 <varlistentry>
2091 <term><literal>snapshot_name</literal> (<type>text</type>)</term>
2092 <listitem>
2093 <para>
2094 The identifier of the snapshot exported by the command. The
2095 snapshot is valid until a new command is executed on this connection
2096 or the replication connection is closed. Null if the created slot
2097 is physical.
2098 </para>
2099 </listitem>
2100 </varlistentry>
2102 <varlistentry>
2103 <term><literal>output_plugin</literal> (<type>text</type>)</term>
2104 <listitem>
2105 <para>
2106 The name of the output plugin used by the newly-created replication
2107 slot. Null if the created slot is physical.
2108 </para>
2109 </listitem>
2110 </varlistentry>
2111 </variablelist>
2112 </para>
2113 </listitem>
2114 </varlistentry>
2116 <varlistentry id="protocol-replication-create-replication-slot-legacy">
2117 <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> ] }
2118 </term>
2119 <listitem>
2120 <para>
2121 For compatibility with older releases, this alternative syntax for
2122 the <literal>CREATE_REPLICATION_SLOT</literal> command is still supported.
2123 </para>
2124 </listitem>
2125 </varlistentry>
2127 <varlistentry id="protocol-replication-read-replication-slot">
2128 <term><literal>READ_REPLICATION_SLOT</literal> <replaceable class="parameter">slot_name</replaceable>
2129 <indexterm><primary>READ_REPLICATION_SLOT</primary></indexterm>
2130 </term>
2131 <listitem>
2132 <para>
2133 Read some information associated with a replication slot. Returns a tuple
2134 with <literal>NULL</literal> values if the replication slot does not
2135 exist. This command is currently only supported for physical replication
2136 slots.
2137 </para>
2139 <para>
2140 In response to this command, the server will return a one-row result set,
2141 containing the following fields:
2142 <variablelist>
2143 <varlistentry>
2144 <term><literal>slot_type</literal> (<type>text</type>)</term>
2145 <listitem>
2146 <para>
2147 The replication slot's type, either <literal>physical</literal> or
2148 <literal>NULL</literal>.
2149 </para>
2150 </listitem>
2151 </varlistentry>
2153 <varlistentry>
2154 <term><literal>restart_lsn</literal> (<type>text</type>)</term>
2155 <listitem>
2156 <para>
2157 The replication slot's <literal>restart_lsn</literal>.
2158 </para>
2159 </listitem>
2160 </varlistentry>
2162 <varlistentry>
2163 <term><literal>restart_tli</literal> (<type>int8</type>)</term>
2164 <listitem>
2165 <para>
2166 The timeline ID associated with <literal>restart_lsn</literal>,
2167 following the current timeline history.
2168 </para>
2169 </listitem>
2170 </varlistentry>
2171 </variablelist>
2172 </para>
2173 </listitem>
2174 </varlistentry>
2176 <varlistentry id="protocol-replication-start-replication">
2177 <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> ]
2178 <indexterm><primary>START_REPLICATION</primary></indexterm>
2179 </term>
2180 <listitem>
2181 <para>
2182 Instructs server to start streaming WAL, starting at
2183 WAL location <replaceable class="parameter">XXX/XXX</replaceable>.
2184 If <literal>TIMELINE</literal> option is specified,
2185 streaming starts on timeline <replaceable class="parameter">tli</replaceable>;
2186 otherwise, the server's current timeline is selected. The server can
2187 reply with an error, for example if the requested section of WAL has already
2188 been recycled. On success, the server responds with a CopyBothResponse
2189 message, and then starts to stream WAL to the frontend.
2190 </para>
2192 <para>
2193 If a slot's name is provided
2194 via <replaceable class="parameter">slot_name</replaceable>, it will be updated
2195 as replication progresses so that the server knows which WAL segments,
2196 and if <varname>hot_standby_feedback</varname> is on which transactions,
2197 are still needed by the standby.
2198 </para>
2200 <para>
2201 If the client requests a timeline that's not the latest but is part of
2202 the history of the server, the server will stream all the WAL on that
2203 timeline starting from the requested start point up to the point where
2204 the server switched to another timeline. If the client requests
2205 streaming at exactly the end of an old timeline, the server skips COPY
2206 mode entirely.
2207 </para>
2209 <para>
2210 After streaming all the WAL on a timeline that is not the latest one,
2211 the server will end streaming by exiting the COPY mode. When the client
2212 acknowledges this by also exiting COPY mode, the server sends a result
2213 set with one row and two columns, indicating the next timeline in this
2214 server's history. The first column is the next timeline's ID (type <type>int8</type>), and the
2215 second column is the WAL location where the switch happened (type <type>text</type>). Usually,
2216 the switch position is the end of the WAL that was streamed, but there
2217 are corner cases where the server can send some WAL from the old
2218 timeline that it has not itself replayed before promoting. Finally, the
2219 server sends two CommandComplete messages (one that ends the CopyData
2220 and the other ends the <literal>START_REPLICATION</literal> itself), and
2221 is ready to accept a new command.
2222 </para>
2224 <para>
2225 WAL data is sent as a series of CopyData messages;
2226 see <xref linkend="protocol-message-types"/> and <xref
2227 linkend="protocol-message-formats"/> for details.
2228 (This allows other information to be intermixed; in particular the server can send
2229 an ErrorResponse message if it encounters a failure after beginning
2230 to stream.) The payload of each CopyData message from server to the
2231 client contains a message of one of the following formats:
2232 </para>
2234 <variablelist>
2235 <varlistentry id="protocol-replication-xlogdata">
2236 <term>XLogData (B)</term>
2237 <listitem>
2238 <variablelist>
2239 <varlistentry>
2240 <term>Byte1('w')</term>
2241 <listitem>
2242 <para>
2243 Identifies the message as WAL data.
2244 </para>
2245 </listitem>
2246 </varlistentry>
2248 <varlistentry>
2249 <term>Int64</term>
2250 <listitem>
2251 <para>
2252 The starting point of the WAL data in this message.
2253 </para>
2254 </listitem>
2255 </varlistentry>
2257 <varlistentry>
2258 <term>Int64</term>
2259 <listitem>
2260 <para>
2261 The current end of WAL on the server.
2262 </para>
2263 </listitem>
2264 </varlistentry>
2266 <varlistentry>
2267 <term>Int64</term>
2268 <listitem>
2269 <para>
2270 The server's system clock at the time of transmission, as
2271 microseconds since midnight on 2000-01-01.
2272 </para>
2273 </listitem>
2274 </varlistentry>
2276 <varlistentry>
2277 <term>Byte<replaceable>n</replaceable></term>
2278 <listitem>
2279 <para>
2280 A section of the WAL data stream.
2281 </para>
2283 <para>
2284 A single WAL record is never split across two XLogData messages.
2285 When a WAL record crosses a WAL page boundary, and is therefore
2286 already split using continuation records, it can be split at the page
2287 boundary. In other words, the first main WAL record and its
2288 continuation records can be sent in different XLogData messages.
2289 </para>
2290 </listitem>
2291 </varlistentry>
2292 </variablelist>
2293 </listitem>
2294 </varlistentry>
2296 <varlistentry id="protocol-replication-primary-keepalive-message">
2297 <term>Primary keepalive message (B)</term>
2298 <listitem>
2299 <variablelist>
2300 <varlistentry>
2301 <term>Byte1('k')</term>
2302 <listitem>
2303 <para>
2304 Identifies the message as a sender keepalive.
2305 </para>
2306 </listitem>
2307 </varlistentry>
2309 <varlistentry>
2310 <term>Int64</term>
2311 <listitem>
2312 <para>
2313 The current end of WAL on the server.
2314 </para>
2315 </listitem>
2316 </varlistentry>
2318 <varlistentry>
2319 <term>Int64</term>
2320 <listitem>
2321 <para>
2322 The server's system clock at the time of transmission, as
2323 microseconds since midnight on 2000-01-01.
2324 </para>
2325 </listitem>
2326 </varlistentry>
2328 <varlistentry>
2329 <term>Byte1</term>
2330 <listitem>
2331 <para>
2332 1 means that the client should reply to this message as soon as
2333 possible, to avoid a timeout disconnect. 0 otherwise.
2334 </para>
2335 </listitem>
2336 </varlistentry>
2337 </variablelist>
2338 </listitem>
2339 </varlistentry>
2340 </variablelist>
2342 <para>
2343 The receiving process can send replies back to the sender at any time,
2344 using one of the following message formats (also in the payload of a
2345 CopyData message):
2346 </para>
2348 <variablelist>
2349 <varlistentry id="protocol-replication-standby-status-update">
2350 <term>Standby status update (F)</term>
2351 <listitem>
2352 <variablelist>
2353 <varlistentry>
2354 <term>Byte1('r')</term>
2355 <listitem>
2356 <para>
2357 Identifies the message as a receiver status update.
2358 </para>
2359 </listitem>
2360 </varlistentry>
2362 <varlistentry>
2363 <term>Int64</term>
2364 <listitem>
2365 <para>
2366 The location of the last WAL byte + 1 received and written to disk
2367 in the standby.
2368 </para>
2369 </listitem>
2370 </varlistentry>
2372 <varlistentry>
2373 <term>Int64</term>
2374 <listitem>
2375 <para>
2376 The location of the last WAL byte + 1 flushed to disk in
2377 the standby.
2378 </para>
2379 </listitem>
2380 </varlistentry>
2382 <varlistentry>
2383 <term>Int64</term>
2384 <listitem>
2385 <para>
2386 The location of the last WAL byte + 1 applied in the standby.
2387 </para>
2388 </listitem>
2389 </varlistentry>
2391 <varlistentry>
2392 <term>Int64</term>
2393 <listitem>
2394 <para>
2395 The client's system clock at the time of transmission, as
2396 microseconds since midnight on 2000-01-01.
2397 </para>
2398 </listitem>
2399 </varlistentry>
2401 <varlistentry>
2402 <term>Byte1</term>
2403 <listitem>
2404 <para>
2405 If 1, the client requests the server to reply to this message
2406 immediately. This can be used to ping the server, to test if
2407 the connection is still healthy.
2408 </para>
2409 </listitem>
2410 </varlistentry>
2411 </variablelist>
2412 </listitem>
2413 </varlistentry>
2415 <varlistentry id="protocol-replication-hot-standby-feedback-message">
2416 <term>Hot standby feedback message (F)</term>
2417 <listitem>
2418 <variablelist>
2419 <varlistentry>
2420 <term>Byte1('h')</term>
2421 <listitem>
2422 <para>
2423 Identifies the message as a hot standby feedback message.
2424 </para>
2425 </listitem>
2426 </varlistentry>
2428 <varlistentry>
2429 <term>Int64</term>
2430 <listitem>
2431 <para>
2432 The client's system clock at the time of transmission, as
2433 microseconds since midnight on 2000-01-01.
2434 </para>
2435 </listitem>
2436 </varlistentry>
2438 <varlistentry>
2439 <term>Int32</term>
2440 <listitem>
2441 <para>
2442 The standby's current global <literal>xmin</literal>, excluding the
2443 <literal>catalog_xmin</literal> from any replication slots. If both
2444 this value and the following <literal>catalog_xmin</literal>
2445 are 0, this is treated as a notification that hot standby feedback
2446 will no longer be sent on this connection. Later non-zero messages
2447 may reinitiate the feedback mechanism.
2448 </para>
2449 </listitem>
2450 </varlistentry>
2452 <varlistentry>
2453 <term>Int32</term>
2454 <listitem>
2455 <para>
2456 The epoch of the global <literal>xmin</literal> xid on the standby.
2457 </para>
2458 </listitem>
2459 </varlistentry>
2461 <varlistentry>
2462 <term>Int32</term>
2463 <listitem>
2464 <para>
2465 The lowest <literal>catalog_xmin</literal> of any replication
2466 slots on the standby. Set to 0 if no <literal>catalog_xmin</literal>
2467 exists on the standby or if hot standby feedback is being
2468 disabled.
2469 </para>
2470 </listitem>
2471 </varlistentry>
2473 <varlistentry>
2474 <term>Int32</term>
2475 <listitem>
2476 <para>
2477 The epoch of the <literal>catalog_xmin</literal> xid on the standby.
2478 </para>
2479 </listitem>
2480 </varlistentry>
2481 </variablelist>
2482 </listitem>
2483 </varlistentry>
2484 </variablelist>
2485 </listitem>
2486 </varlistentry>
2488 <varlistentry id="protocol-replication-start-replication-slot-logical">
2489 <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>
2490 <listitem>
2491 <para>
2492 Instructs server to start streaming WAL for logical replication,
2493 starting at either WAL location <replaceable
2494 class="parameter">XXX/XXX</replaceable> or the slot's
2495 <literal>confirmed_flush_lsn</literal> (see <xref
2496 linkend="view-pg-replication-slots"/>), whichever is greater. This
2497 behavior makes it easier for clients to avoid updating their local LSN
2498 status when there is no data to process. However, starting at a
2499 different LSN than requested might not catch certain kinds of client
2500 errors; so the client may wish to check that
2501 <literal>confirmed_flush_lsn</literal> matches its expectations before
2502 issuing <literal>START_REPLICATION</literal>.
2503 </para>
2505 <para>
2506 The server can reply with an error, for example if the
2507 slot does not exist. On success, the server responds with a CopyBothResponse
2508 message, and then starts to stream WAL to the frontend.
2509 </para>
2511 <para>
2512 The messages inside the CopyBothResponse messages are of the same format
2513 documented for <literal>START_REPLICATION ... PHYSICAL</literal>, including
2514 two CommandComplete messages.
2515 </para>
2517 <para>
2518 The output plugin associated with the selected slot is used
2519 to process the output for streaming.
2520 </para>
2522 <variablelist>
2523 <varlistentry>
2524 <term><literal>SLOT</literal> <replaceable class="parameter">slot_name</replaceable></term>
2525 <listitem>
2526 <para>
2527 The name of the slot to stream changes from. This parameter is required,
2528 and must correspond to an existing logical replication slot created
2529 with <literal>CREATE_REPLICATION_SLOT</literal> in
2530 <literal>LOGICAL</literal> mode.
2531 </para>
2532 </listitem>
2533 </varlistentry>
2535 <varlistentry>
2536 <term><replaceable class="parameter">XXX/XXX</replaceable></term>
2537 <listitem>
2538 <para>
2539 The WAL location to begin streaming at.
2540 </para>
2541 </listitem>
2542 </varlistentry>
2544 <varlistentry>
2545 <term><replaceable class="parameter">option_name</replaceable></term>
2546 <listitem>
2547 <para>
2548 The name of an option passed to the slot's logical decoding plugin.
2549 </para>
2550 </listitem>
2551 </varlistentry>
2553 <varlistentry>
2554 <term><replaceable class="parameter">option_value</replaceable></term>
2555 <listitem>
2556 <para>
2557 Optional value, in the form of a string constant, associated with the
2558 specified option.
2559 </para>
2560 </listitem>
2561 </varlistentry>
2562 </variablelist>
2563 </listitem>
2564 </varlistentry>
2566 <varlistentry id="protocol-replication-drop-replication-slot">
2567 <term>
2568 <literal>DROP_REPLICATION_SLOT</literal> <replaceable class="parameter">slot_name</replaceable> <optional> <literal>WAIT</literal> </optional>
2569 <indexterm><primary>DROP_REPLICATION_SLOT</primary></indexterm>
2570 </term>
2571 <listitem>
2572 <para>
2573 Drops a replication slot, freeing any reserved server-side resources.
2574 If the slot is a logical slot that was created in a database other than
2575 the database the walsender is connected to, this command fails.
2576 </para>
2578 <variablelist>
2579 <varlistentry>
2580 <term><replaceable class="parameter">slot_name</replaceable></term>
2581 <listitem>
2582 <para>
2583 The name of the slot to drop.
2584 </para>
2585 </listitem>
2586 </varlistentry>
2588 <varlistentry>
2589 <term><literal>WAIT</literal></term>
2590 <listitem>
2591 <para>
2592 This option causes the command to wait if the slot is active until
2593 it becomes inactive, instead of the default behavior of raising an
2594 error.
2595 </para>
2596 </listitem>
2597 </varlistentry>
2598 </variablelist>
2599 </listitem>
2600 </varlistentry>
2602 <varlistentry id="protocol-replication-upload-manifest">
2603 <term>
2604 <literal>UPLOAD_MANIFEST</literal>
2605 <indexterm><primary>UPLOAD_MANIFEST</primary></indexterm>
2606 </term>
2607 <listitem>
2608 <para>
2609 Uploads a backup manifest in preparation for taking an incremental
2610 backup.
2611 </para>
2612 </listitem>
2613 </varlistentry>
2615 <varlistentry id="protocol-replication-base-backup" xreflabel="BASE_BACKUP">
2616 <term><literal>BASE_BACKUP</literal> [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ]
2617 <indexterm><primary>BASE_BACKUP</primary></indexterm>
2618 </term>
2619 <listitem>
2620 <para>
2621 Instructs the server to start streaming a base backup.
2622 The system will automatically be put in backup mode before the backup
2623 is started, and taken out of it when the backup is complete. The
2624 following options are accepted:
2626 <variablelist>
2627 <varlistentry>
2628 <term><literal>LABEL</literal> <replaceable>'label'</replaceable></term>
2629 <listitem>
2630 <para>
2631 Sets the label of the backup. If none is specified, a backup label
2632 of <literal>base backup</literal> will be used. The quoting rules
2633 for the label are the same as a standard SQL string with
2634 <xref linkend="guc-standard-conforming-strings"/> turned on.
2635 </para>
2636 </listitem>
2637 </varlistentry>
2639 <varlistentry>
2640 <term><literal>TARGET</literal> <replaceable>'target'</replaceable></term>
2641 <listitem>
2642 <para>
2643 Tells the server where to send the backup. If the target is
2644 <literal>client</literal>, which is the default, the backup data is
2645 sent to the client. If it is <literal>server</literal>, the backup
2646 data is written to the server at the pathname specified by the
2647 <literal>TARGET_DETAIL</literal> option. If it is
2648 <literal>blackhole</literal>, the backup data is not sent
2649 anywhere; it is simply discarded.
2650 </para>
2652 <para>
2653 The <literal>server</literal> target requires superuser privilege or
2654 being granted the <literal>pg_write_server_files</literal> role.
2655 </para>
2656 </listitem>
2657 </varlistentry>
2659 <varlistentry>
2660 <term><literal>TARGET_DETAIL</literal> <replaceable>'detail'</replaceable></term>
2661 <listitem>
2662 <para>
2663 Provides additional information about the backup target.
2664 </para>
2666 <para>
2667 Currently, this option can only be used when the backup target is
2668 <literal>server</literal>. It specifies the server directory
2669 to which the backup should be written.
2670 </para>
2671 </listitem>
2672 </varlistentry>
2674 <varlistentry>
2675 <term><literal>PROGRESS [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2676 <listitem>
2677 <para>
2678 If set to true, request information required to generate a progress
2679 report. This will send back an approximate size in the header of each
2680 tablespace, which can be used to calculate how far along the stream
2681 is done. This is calculated by enumerating all the file sizes once
2682 before the transfer is even started, and might as such have a
2683 negative impact on the performance. In particular, it might take
2684 longer before the first data
2685 is streamed. Since the database files can change during the backup,
2686 the size is only approximate and might both grow and shrink between
2687 the time of approximation and the sending of the actual files.
2688 The default is false.
2689 </para>
2690 </listitem>
2691 </varlistentry>
2693 <varlistentry>
2694 <term><literal>CHECKPOINT { 'fast' | 'spread' }</literal></term>
2695 <listitem>
2696 <para>
2697 Sets the type of checkpoint to be performed at the beginning of the
2698 base backup. The default is <literal>spread</literal>.
2699 </para>
2700 </listitem>
2701 </varlistentry>
2703 <varlistentry>
2704 <term><literal>WAL [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2705 <listitem>
2706 <para>
2707 If set to true, include the necessary WAL segments in the backup.
2708 This will include all the files between start and stop backup in the
2709 <filename>pg_wal</filename> directory of the base directory tar
2710 file. The default is false.
2711 </para>
2712 </listitem>
2713 </varlistentry>
2715 <varlistentry>
2716 <term><literal>WAIT [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2717 <listitem>
2718 <para>
2719 If set to true, the backup will wait until the last required WAL
2720 segment has been archived, or emit a warning if WAL archiving is
2721 not enabled. If false, the backup will neither wait nor warn,
2722 leaving the client responsible for ensuring the required log is
2723 available. The default is true.
2724 </para>
2725 </listitem>
2726 </varlistentry>
2728 <varlistentry>
2729 <term><literal>COMPRESSION</literal> <replaceable>'method'</replaceable></term>
2730 <listitem>
2731 <para>
2732 Instructs the server to compress the backup using the specified
2733 method. Currently, the supported methods are <literal>gzip</literal>,
2734 <literal>lz4</literal>, and <literal>zstd</literal>.
2735 </para>
2736 </listitem>
2737 </varlistentry>
2739 <varlistentry>
2740 <term><literal>COMPRESSION_DETAIL</literal> <replaceable>detail</replaceable></term>
2741 <listitem>
2742 <para>
2743 Specifies details for the chosen compression method. This should only
2744 be used in conjunction with the <literal>COMPRESSION</literal>
2745 option. If the value is an integer, it specifies the compression
2746 level. Otherwise, it should be a comma-separated list of items,
2747 each of the form <replaceable>keyword</replaceable> or
2748 <replaceable>keyword=value</replaceable>. Currently, the supported
2749 keywords are <literal>level</literal>, <literal>long</literal> and
2750 <literal>workers</literal>.
2751 </para>
2753 <para>
2754 The <literal>level</literal> keyword sets the compression level.
2755 For <literal>gzip</literal> the compression level should be an
2756 integer between <literal>1</literal> and <literal>9</literal>
2757 (default <literal>Z_DEFAULT_COMPRESSION</literal> or
2758 <literal>-1</literal>), for <literal>lz4</literal> an integer
2759 between 1 and 12 (default <literal>0</literal> for fast compression
2760 mode), and for <literal>zstd</literal> an integer between
2761 <literal>ZSTD_minCLevel()</literal> (usually <literal>-131072</literal>)
2762 and <literal>ZSTD_maxCLevel()</literal> (usually <literal>22</literal>),
2763 (default <literal>ZSTD_CLEVEL_DEFAULT</literal> or
2764 <literal>3</literal>).
2765 </para>
2767 <para>
2768 The <literal>long</literal> keyword enables long-distance matching
2769 mode, for improved compression ratio, at the expense of higher memory
2770 use. Long-distance mode is supported only for
2771 <literal>zstd</literal>.
2772 </para>
2774 <para>
2775 The <literal>workers</literal> keyword sets the number of threads
2776 that should be used for parallel compression. Parallel compression
2777 is supported only for <literal>zstd</literal>.
2778 </para>
2779 </listitem>
2780 </varlistentry>
2782 <varlistentry>
2783 <term><literal>MAX_RATE</literal> <replaceable>rate</replaceable></term>
2784 <listitem>
2785 <para>
2786 Limit (throttle) the maximum amount of data transferred from server
2787 to client per unit of time. The expected unit is kilobytes per second.
2788 If this option is specified, the value must either be equal to zero
2789 or it must fall within the range from 32 kB through 1 GB (inclusive).
2790 If zero is passed or the option is not specified, no restriction is
2791 imposed on the transfer.
2792 </para>
2793 </listitem>
2794 </varlistentry>
2796 <varlistentry>
2797 <term><literal>TABLESPACE_MAP [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2798 <listitem>
2799 <para>
2800 If true, include information about symbolic links present in the
2801 directory <filename>pg_tblspc</filename> in a file named
2802 <filename>tablespace_map</filename>. The tablespace map file includes
2803 each symbolic link name as it exists in the directory
2804 <filename>pg_tblspc/</filename> and the full path of that symbolic link.
2805 The default is false.
2806 </para>
2807 </listitem>
2808 </varlistentry>
2810 <varlistentry>
2811 <term><literal>VERIFY_CHECKSUMS [ <replaceable class="parameter">boolean</replaceable> ]</literal></term>
2812 <listitem>
2813 <para>
2814 If true, checksums are verified during a base backup if they are
2815 enabled. If false, this is skipped. The default is true.
2816 </para>
2817 </listitem>
2818 </varlistentry>
2820 <varlistentry>
2821 <term><literal>MANIFEST</literal> <replaceable>manifest_option</replaceable></term>
2822 <listitem>
2823 <para>
2824 When this option is specified with a value of <literal>yes</literal>
2825 or <literal>force-encode</literal>, a backup manifest is created
2826 and sent along with the backup. The manifest is a list of every
2827 file present in the backup with the exception of any WAL files that
2828 may be included. It also stores the size, last modification time, and
2829 optionally a checksum for each file.
2830 A value of <literal>force-encode</literal> forces all filenames
2831 to be hex-encoded; otherwise, this type of encoding is performed only
2832 for files whose names are non-UTF8 octet sequences.
2833 <literal>force-encode</literal> is intended primarily for testing
2834 purposes, to be sure that clients which read the backup manifest
2835 can handle this case. For compatibility with previous releases,
2836 the default is <literal>MANIFEST 'no'</literal>.
2837 </para>
2838 </listitem>
2839 </varlistentry>
2841 <varlistentry>
2842 <term><literal>MANIFEST_CHECKSUMS</literal> <replaceable>checksum_algorithm</replaceable></term>
2843 <listitem>
2844 <para>
2845 Specifies the checksum algorithm that should be applied to each file included
2846 in the backup manifest. Currently, the available
2847 algorithms are <literal>NONE</literal>, <literal>CRC32C</literal>,
2848 <literal>SHA224</literal>, <literal>SHA256</literal>,
2849 <literal>SHA384</literal>, and <literal>SHA512</literal>.
2850 The default is <literal>CRC32C</literal>.
2851 </para>
2852 </listitem>
2853 </varlistentry>
2855 <varlistentry>
2856 <term><literal>INCREMENTAL</literal></term>
2857 <listitem>
2858 <para>
2859 Requests an incremental backup. The
2860 <literal>UPLOAD_MANIFEST</literal> command must be executed
2861 before running a base backup with this option.
2862 </para>
2863 </listitem>
2864 </varlistentry>
2865 </variablelist>
2866 </para>
2868 <para>
2869 When the backup is started, the server will first send two
2870 ordinary result sets, followed by one or more CopyOutResponse
2871 results.
2872 </para>
2874 <para>
2875 The first ordinary result set contains the starting position of the
2876 backup, in a single row with two columns. The first column contains
2877 the start position given in XLogRecPtr format, and the second column
2878 contains the corresponding timeline ID.
2879 </para>
2881 <para>
2882 The second ordinary result set has one row for each tablespace.
2883 The fields in this row are:
2885 <variablelist>
2886 <varlistentry>
2887 <term><literal>spcoid</literal> (<type>oid</type>)</term>
2888 <listitem>
2889 <para>
2890 The OID of the tablespace, or null if it's the base
2891 directory.
2892 </para>
2893 </listitem>
2894 </varlistentry>
2896 <varlistentry>
2897 <term><literal>spclocation</literal> (<type>text</type>)</term>
2898 <listitem>
2899 <para>
2900 The full path of the tablespace directory, or null
2901 if it's the base directory.
2902 </para>
2903 </listitem>
2904 </varlistentry>
2906 <varlistentry>
2907 <term><literal>size</literal> (<type>int8</type>)</term>
2908 <listitem>
2909 <para>
2910 The approximate size of the tablespace, in kilobytes (1024 bytes),
2911 if progress report has been requested; otherwise it's null.
2912 </para>
2913 </listitem>
2914 </varlistentry>
2915 </variablelist>
2916 </para>
2918 <para>
2919 After the second regular result set, a CopyOutResponse will be sent.
2920 The payload of each CopyData message will contain a message in one of
2921 the following formats:
2922 </para>
2924 <variablelist>
2925 <varlistentry>
2926 <term>new archive (B)</term>
2927 <listitem>
2928 <variablelist>
2929 <varlistentry>
2930 <term>Byte1('n')</term>
2931 <listitem><para>
2932 Identifies the message as indicating the start of a new archive.
2933 There will be one archive for the main data directory and one
2934 for each additional tablespace; each will use tar format
2935 (following the <quote>ustar interchange format</quote> specified
2936 in the POSIX 1003.1-2008 standard).
2937 </para></listitem>
2938 </varlistentry>
2940 <varlistentry>
2941 <term>String</term>
2942 <listitem><para>
2943 The file name for this archive.
2944 </para></listitem>
2945 </varlistentry>
2947 <varlistentry>
2948 <term>String</term>
2949 <listitem><para>
2950 For the main data directory, an empty string. For other
2951 tablespaces, the full path to the directory from which this
2952 archive was created.
2953 </para></listitem>
2954 </varlistentry>
2955 </variablelist>
2956 </listitem>
2957 </varlistentry>
2959 <varlistentry>
2960 <term>manifest (B)</term>
2961 <listitem>
2962 <variablelist>
2963 <varlistentry>
2964 <term>Byte1('m')</term>
2965 <listitem><para>
2966 Identifies the message as indicating the start of the backup
2967 manifest.
2968 </para></listitem>
2969 </varlistentry>
2970 </variablelist>
2971 </listitem>
2972 </varlistentry>
2974 <varlistentry>
2975 <term>archive or manifest data (B)</term>
2976 <listitem>
2977 <variablelist>
2978 <varlistentry>
2979 <term>Byte1('d')</term>
2980 <listitem><para>
2981 Identifies the message as containing archive or manifest data.
2982 </para></listitem>
2983 </varlistentry>
2985 <varlistentry>
2986 <term>Byte<replaceable>n</replaceable></term>
2987 <listitem><para>
2988 Data bytes.
2989 </para></listitem>
2990 </varlistentry>
2991 </variablelist>
2992 </listitem>
2993 </varlistentry>
2995 <varlistentry>
2996 <term>progress report (B)</term>
2997 <listitem>
2998 <variablelist>
2999 <varlistentry>
3000 <term>Byte1('p')</term>
3001 <listitem><para>
3002 Identifies the message as a progress report.
3003 </para></listitem>
3004 </varlistentry>
3006 <varlistentry>
3007 <term>Int64</term>
3008 <listitem><para>
3009 The number of bytes from the current tablespace for which
3010 processing has been completed.
3011 </para></listitem>
3012 </varlistentry>
3013 </variablelist>
3014 </listitem>
3015 </varlistentry>
3016 </variablelist>
3018 <para>
3019 After the CopyOutResponse, or all such responses, have been sent, a
3020 final ordinary result set will be sent, containing the WAL end position
3021 of the backup, in the same format as the start position.
3022 </para>
3024 <para>
3025 The tar archive for the data directory and each tablespace will contain
3026 all files in the directories, regardless of whether they are
3027 <productname>PostgreSQL</productname> files or other files added to the same
3028 directory. The only excluded files are:
3030 <itemizedlist spacing="compact" mark="bullet">
3031 <listitem>
3032 <para>
3033 <filename>postmaster.pid</filename>
3034 </para>
3035 </listitem>
3036 <listitem>
3037 <para>
3038 <filename>postmaster.opts</filename>
3039 </para>
3040 </listitem>
3041 <listitem>
3042 <para>
3043 <filename>pg_internal.init</filename> (found in multiple directories)
3044 </para>
3045 </listitem>
3046 <listitem>
3047 <para>
3048 Various temporary files and directories created during the operation
3049 of the PostgreSQL server, such as any file or directory beginning
3050 with <filename>pgsql_tmp</filename> and temporary relations.
3051 </para>
3052 </listitem>
3053 <listitem>
3054 <para>
3055 Unlogged relations, except for the init fork which is required to
3056 recreate the (empty) unlogged relation on recovery.
3057 </para>
3058 </listitem>
3059 <listitem>
3060 <para>
3061 <filename>pg_wal</filename>, including subdirectories. If the backup is run
3062 with WAL files included, a synthesized version of <filename>pg_wal</filename> will be
3063 included, but it will only contain the files necessary for the
3064 backup to work, not the rest of the contents.
3065 </para>
3066 </listitem>
3067 <listitem>
3068 <para>
3069 <filename>pg_dynshmem</filename>, <filename>pg_notify</filename>,
3070 <filename>pg_replslot</filename>, <filename>pg_serial</filename>,
3071 <filename>pg_snapshots</filename>, <filename>pg_stat_tmp</filename>, and
3072 <filename>pg_subtrans</filename> are copied as empty directories (even if
3073 they are symbolic links).
3074 </para>
3075 </listitem>
3076 <listitem>
3077 <para>
3078 Files other than regular files and directories, such as symbolic
3079 links (other than for the directories listed above) and special
3080 device files, are skipped. (Symbolic links
3081 in <filename>pg_tblspc</filename> are maintained.)
3082 </para>
3083 </listitem>
3084 </itemizedlist>
3085 Owner, group, and file mode are set if the underlying file system on
3086 the server supports it.
3087 </para>
3088 </listitem>
3089 </varlistentry>
3090 </variablelist>
3091 </para>
3092 </sect1>
3094 <sect1 id="protocol-logical-replication">
3095 <title>Logical Streaming Replication Protocol</title>
3097 <para>
3098 This section describes the logical replication protocol, which is the message
3099 flow started by the <literal>START_REPLICATION</literal>
3100 <literal>SLOT</literal> <replaceable class="parameter">slot_name</replaceable>
3101 <literal>LOGICAL</literal> replication command.
3102 </para>
3104 <para>
3105 The logical streaming replication protocol builds on the primitives of
3106 the physical streaming replication protocol.
3107 </para>
3109 <sect2 id="protocol-logical-replication-params">
3110 <title>Logical Streaming Replication Parameters</title>
3112 <para>
3113 The logical replication <literal>START_REPLICATION</literal> command
3114 accepts following parameters:
3116 <variablelist>
3117 <varlistentry>
3118 <term>
3119 proto_version
3120 </term>
3121 <listitem>
3122 <para>
3123 Protocol version. Currently versions <literal>1</literal>, <literal>2</literal>,
3124 <literal>3</literal>, and <literal>4</literal> are supported.
3125 </para>
3126 <para>
3127 Version <literal>2</literal> is supported only for server version 14
3128 and above, and it allows streaming of large in-progress transactions.
3129 </para>
3130 <para>
3131 Version <literal>3</literal> is supported only for server version 15
3132 and above, and it allows streaming of two-phase commits.
3133 </para>
3134 <para>
3135 Version <literal>4</literal> is supported only for server version 16
3136 and above, and it allows streams of large in-progress transactions to
3137 be applied in parallel.
3138 </para>
3139 </listitem>
3140 </varlistentry>
3142 <varlistentry>
3143 <term>
3144 publication_names
3145 </term>
3146 <listitem>
3147 <para>
3148 Comma separated list of publication names for which to subscribe
3149 (receive changes). The individual publication names are treated
3150 as standard objects names and can be quoted the same as needed.
3151 </para>
3152 </listitem>
3153 </varlistentry>
3154 </variablelist>
3156 </para>
3157 </sect2>
3159 <sect2 id="protocol-logical-messages">
3160 <title>Logical Replication Protocol Messages</title>
3162 <para>
3163 The individual protocol messages are discussed in the following
3164 subsections. Individual messages are described in
3165 <xref linkend="protocol-logicalrep-message-formats"/>.
3166 </para>
3168 <para>
3169 All top-level protocol messages begin with a message type byte.
3170 While represented in code as a character, this is a signed byte with no
3171 associated encoding.
3172 </para>
3174 <para>
3175 Since the streaming replication protocol supplies a message length there
3176 is no need for top-level protocol messages to embed a length in their
3177 header.
3178 </para>
3180 </sect2>
3182 <sect2 id="protocol-logical-messages-flow">
3183 <title>Logical Replication Protocol Message Flow</title>
3185 <para>
3186 With the exception of the <literal>START_REPLICATION</literal> command and
3187 the replay progress messages, all information flows only from the backend
3188 to the frontend.
3189 </para>
3191 <para>
3192 The logical replication protocol sends individual transactions one by one.
3193 This means that all messages between a pair of Begin and Commit messages
3194 belong to the same transaction. Similarly, all messages between a pair of
3195 Begin Prepare and Prepare messages belong to the same transaction.
3196 It also sends changes of large in-progress transactions between a pair of
3197 Stream Start and Stream Stop messages. The last stream of such a transaction
3198 contains a Stream Commit or Stream Abort message.
3199 </para>
3201 <para>
3202 Every sent transaction contains zero or more DML messages (Insert,
3203 Update, Delete). In case of a cascaded setup it can also contain Origin
3204 messages. The origin message indicates that the transaction originated on
3205 different replication node. Since a replication node in the scope of logical
3206 replication protocol can be pretty much anything, the only identifier
3207 is the origin name. It's downstream's responsibility to handle this as
3208 needed (if needed). The Origin message is always sent before any DML
3209 messages in the transaction.
3210 </para>
3212 <para>
3213 Every DML message contains a relation OID, identifying the publisher's
3214 relation that was acted on. Before the first DML message for a given
3215 relation OID, a Relation message will be sent, describing the schema of
3216 that relation. Subsequently, a new Relation message will be sent if
3217 the relation's definition has changed since the last Relation message
3218 was sent for it. (The protocol assumes that the client is capable of
3219 remembering this metadata for as many relations as needed.)
3220 </para>
3222 <para>
3223 Relation messages identify column types by their OIDs. In the case
3224 of a built-in type, it is assumed that the client can look up that
3225 type OID locally, so no additional data is needed. For a non-built-in
3226 type OID, a Type message will be sent before the Relation message,
3227 to provide the type name associated with that OID. Thus, a client that
3228 needs to specifically identify the types of relation columns should
3229 cache the contents of Type messages, and first consult that cache to
3230 see if the type OID is defined there. If not, look up the type OID
3231 locally.
3232 </para>
3233 </sect2>
3234 </sect1>
3236 <sect1 id="protocol-message-types">
3237 <title>Message Data Types</title>
3239 <para>
3240 This section describes the base data types used in messages.
3241 </para>
3243 <variablelist>
3244 <varlistentry>
3245 <term>Int<replaceable>n</replaceable>(<replaceable>i</replaceable>)</term>
3246 <listitem>
3247 <para>
3248 An <replaceable>n</replaceable>-bit integer in network byte
3249 order (most significant byte first).
3250 If <replaceable>i</replaceable> is specified it
3251 is the exact value that will appear, otherwise the value
3252 is variable. Eg. Int16, Int32(42).
3253 </para>
3254 </listitem>
3255 </varlistentry>
3257 <varlistentry>
3258 <term>Int<replaceable>n</replaceable>[<replaceable>k</replaceable>]</term>
3259 <listitem>
3260 <para>
3261 An array of <replaceable>k</replaceable>
3262 <replaceable>n</replaceable>-bit integers, each in network
3263 byte order. The array length <replaceable>k</replaceable>
3264 is always determined by an earlier field in the message.
3265 Eg. Int16[M].
3266 </para>
3267 </listitem>
3268 </varlistentry>
3270 <varlistentry>
3271 <term>String(<replaceable>s</replaceable>)</term>
3272 <listitem>
3273 <para>
3274 A null-terminated string (C-style string). There is no
3275 specific length limitation on strings.
3276 If <replaceable>s</replaceable> is specified it is the exact
3277 value that will appear, otherwise the value is variable.
3278 Eg. String, String("user").
3279 </para>
3281 <note>
3282 <para>
3283 <emphasis>There is no predefined limit</emphasis> on the length of a string
3284 that can be returned by the backend. Good coding strategy for a frontend
3285 is to use an expandable buffer so that anything that fits in memory can be
3286 accepted. If that's not feasible, read the full string and discard trailing
3287 characters that don't fit into your fixed-size buffer.
3288 </para>
3289 </note>
3290 </listitem>
3291 </varlistentry>
3293 <varlistentry>
3294 <term>Byte<replaceable>n</replaceable>(<replaceable>c</replaceable>)</term>
3295 <listitem>
3296 <para>
3297 Exactly <replaceable>n</replaceable> bytes. If the field
3298 width <replaceable>n</replaceable> is not a constant, it is
3299 always determinable from an earlier field in the message.
3300 If <replaceable>c</replaceable> is specified it is the exact
3301 value. Eg. Byte2, Byte1('\n').
3302 </para>
3303 </listitem>
3304 </varlistentry>
3305 </variablelist>
3306 </sect1>
3308 <sect1 id="protocol-message-formats">
3309 <title>Message Formats</title>
3311 <para>
3312 This section describes the detailed format of each message. Each is marked to
3313 indicate that it can be sent by a frontend (F), a backend (B), or both
3314 (F &amp; B).
3315 Notice that although each message includes a byte count at the beginning,
3316 the message format is defined so that the message end can be found without
3317 reference to the byte count. This aids validity checking. (The CopyData
3318 message is an exception, because it forms part of a data stream; the contents
3319 of any individual CopyData message cannot be interpretable on their own.)
3320 </para>
3322 <variablelist>
3323 <varlistentry id="protocol-message-formats-AuthenticationOk">
3324 <term>AuthenticationOk (B)</term>
3325 <listitem>
3326 <variablelist>
3327 <varlistentry>
3328 <term>Byte1('R')</term>
3329 <listitem>
3330 <para>
3331 Identifies the message as an authentication request.
3332 </para>
3333 </listitem>
3334 </varlistentry>
3336 <varlistentry>
3337 <term>Int32(8)</term>
3338 <listitem>
3339 <para>
3340 Length of message contents in bytes, including self.
3341 </para>
3342 </listitem>
3343 </varlistentry>
3345 <varlistentry>
3346 <term>Int32(0)</term>
3347 <listitem>
3348 <para>
3349 Specifies that the authentication was successful.
3350 </para>
3351 </listitem>
3352 </varlistentry>
3353 </variablelist>
3354 </listitem>
3355 </varlistentry>
3357 <varlistentry id="protocol-message-formats-AuthenticationKerberosV5">
3358 <term>AuthenticationKerberosV5 (B)</term>
3359 <listitem>
3361 <variablelist>
3362 <varlistentry>
3363 <term>Byte1('R')</term>
3364 <listitem>
3365 <para>
3366 Identifies the message as an authentication request.
3367 </para>
3368 </listitem>
3369 </varlistentry>
3371 <varlistentry>
3372 <term>Int32(8)</term>
3373 <listitem>
3374 <para>
3375 Length of message contents in bytes, including self.
3376 </para>
3377 </listitem>
3378 </varlistentry>
3380 <varlistentry>
3381 <term>Int32(2)</term>
3382 <listitem>
3383 <para>
3384 Specifies that Kerberos V5 authentication is required.
3385 </para>
3386 </listitem>
3387 </varlistentry>
3388 </variablelist>
3389 </listitem>
3390 </varlistentry>
3392 <varlistentry id="protocol-message-formats-AuthenticationCleartextPassword">
3393 <term>AuthenticationCleartextPassword (B)</term>
3394 <listitem>
3396 <variablelist>
3397 <varlistentry>
3398 <term>Byte1('R')</term>
3399 <listitem>
3400 <para>
3401 Identifies the message as an authentication request.
3402 </para>
3403 </listitem>
3404 </varlistentry>
3406 <varlistentry>
3407 <term>Int32(8)</term>
3408 <listitem>
3409 <para>
3410 Length of message contents in bytes, including self.
3411 </para>
3412 </listitem>
3413 </varlistentry>
3415 <varlistentry>
3416 <term>Int32(3)</term>
3417 <listitem>
3418 <para>
3419 Specifies that a clear-text password is required.
3420 </para>
3421 </listitem>
3422 </varlistentry>
3423 </variablelist>
3424 </listitem>
3425 </varlistentry>
3427 <varlistentry id="protocol-message-formats-AuthenticationMD5Password">
3428 <term>AuthenticationMD5Password (B)</term>
3429 <listitem>
3430 <variablelist>
3431 <varlistentry>
3432 <term>Byte1('R')</term>
3433 <listitem>
3434 <para>
3435 Identifies the message as an authentication request.
3436 </para>
3437 </listitem>
3438 </varlistentry>
3440 <varlistentry>
3441 <term>Int32(12)</term>
3442 <listitem>
3443 <para>
3444 Length of message contents in bytes, including self.
3445 </para>
3446 </listitem>
3447 </varlistentry>
3449 <varlistentry>
3450 <term>Int32(5)</term>
3451 <listitem>
3452 <para>
3453 Specifies that an MD5-encrypted password is required.
3454 </para>
3455 </listitem>
3456 </varlistentry>
3458 <varlistentry>
3459 <term>Byte4</term>
3460 <listitem>
3461 <para>
3462 The salt to use when encrypting the password.
3463 </para>
3464 </listitem>
3465 </varlistentry>
3466 </variablelist>
3467 </listitem>
3468 </varlistentry>
3470 <varlistentry id="protocol-message-formats-AuthenticationGSS">
3471 <term>AuthenticationGSS (B)</term>
3472 <listitem>
3473 <variablelist>
3474 <varlistentry>
3475 <term>Byte1('R')</term>
3476 <listitem>
3477 <para>
3478 Identifies the message as an authentication request.
3479 </para>
3480 </listitem>
3481 </varlistentry>
3482 <varlistentry>
3483 <term>Int32(8)</term>
3484 <listitem>
3485 <para>
3486 Length of message contents in bytes, including self.
3487 </para>
3488 </listitem>
3489 </varlistentry>
3491 <varlistentry>
3492 <term>Int32(7)</term>
3493 <listitem>
3494 <para>
3495 Specifies that GSSAPI authentication is required.
3496 </para>
3497 </listitem>
3498 </varlistentry>
3499 </variablelist>
3500 </listitem>
3501 </varlistentry>
3503 <varlistentry id="protocol-message-formats-AuthenticationGSSContinue">
3504 <term>AuthenticationGSSContinue (B)</term>
3505 <listitem>
3506 <variablelist>
3507 <varlistentry>
3508 <term>Byte1('R')</term>
3509 <listitem>
3510 <para>
3511 Identifies the message as an authentication request.
3512 </para>
3513 </listitem>
3514 </varlistentry>
3516 <varlistentry>
3517 <term>Int32</term>
3518 <listitem>
3519 <para>
3520 Length of message contents in bytes, including self.
3521 </para>
3522 </listitem>
3523 </varlistentry>
3525 <varlistentry>
3526 <term>Int32(8)</term>
3527 <listitem>
3528 <para>
3529 Specifies that this message contains GSSAPI or SSPI data.
3530 </para>
3531 </listitem>
3532 </varlistentry>
3534 <varlistentry>
3535 <term>Byte<replaceable>n</replaceable></term>
3536 <listitem>
3537 <para>
3538 GSSAPI or SSPI authentication data.
3539 </para>
3540 </listitem>
3541 </varlistentry>
3542 </variablelist>
3543 </listitem>
3544 </varlistentry>
3546 <varlistentry id="protocol-message-formats-AuthenticationSSPI">
3547 <term>AuthenticationSSPI (B)</term>
3548 <listitem>
3549 <variablelist>
3550 <varlistentry>
3551 <term>Byte1('R')</term>
3552 <listitem>
3553 <para>
3554 Identifies the message as an authentication request.
3555 </para>
3556 </listitem>
3557 </varlistentry>
3559 <varlistentry>
3560 <term>Int32(8)</term>
3561 <listitem>
3562 <para>
3563 Length of message contents in bytes, including self.
3564 </para>
3565 </listitem>
3566 </varlistentry>
3568 <varlistentry>
3569 <term>Int32(9)</term>
3570 <listitem>
3571 <para>
3572 Specifies that SSPI authentication is required.
3573 </para>
3574 </listitem>
3575 </varlistentry>
3576 </variablelist>
3577 </listitem>
3578 </varlistentry>
3580 <varlistentry id="protocol-message-formats-AuthenticationSASL">
3581 <term>AuthenticationSASL (B)</term>
3582 <listitem>
3583 <variablelist>
3584 <varlistentry>
3585 <term>Byte1('R')</term>
3586 <listitem>
3587 <para>
3588 Identifies the message as an authentication request.
3589 </para>
3590 </listitem>
3591 </varlistentry>
3593 <varlistentry>
3594 <term>Int32</term>
3595 <listitem>
3596 <para>
3597 Length of message contents in bytes, including self.
3598 </para>
3599 </listitem>
3600 </varlistentry>
3602 <varlistentry>
3603 <term>Int32(10)</term>
3604 <listitem>
3605 <para>
3606 Specifies that SASL authentication is required.
3607 </para>
3608 </listitem>
3609 </varlistentry>
3610 </variablelist>
3612 <para>
3613 The message body is a list of SASL authentication mechanisms, in the
3614 server's order of preference. A zero byte is required as terminator after
3615 the last authentication mechanism name. For each mechanism, there is the
3616 following:
3618 <variablelist>
3619 <varlistentry>
3620 <term>String</term>
3621 <listitem>
3622 <para>
3623 Name of a SASL authentication mechanism.
3624 </para>
3625 </listitem>
3626 </varlistentry>
3627 </variablelist>
3628 </para>
3629 </listitem>
3630 </varlistentry>
3632 <varlistentry id="protocol-message-formats-AuthenticationSASLContinue">
3633 <term>AuthenticationSASLContinue (B)</term>
3634 <listitem>
3635 <variablelist>
3636 <varlistentry>
3637 <term>Byte1('R')</term>
3638 <listitem>
3639 <para>
3640 Identifies the message as an authentication request.
3641 </para>
3642 </listitem>
3643 </varlistentry>
3645 <varlistentry>
3646 <term>Int32</term>
3647 <listitem>
3648 <para>
3649 Length of message contents in bytes, including self.
3650 </para>
3651 </listitem>
3652 </varlistentry>
3654 <varlistentry>
3655 <term>Int32(11)</term>
3656 <listitem>
3657 <para>
3658 Specifies that this message contains a SASL challenge.
3659 </para>
3660 </listitem>
3661 </varlistentry>
3663 <varlistentry>
3664 <term>Byte<replaceable>n</replaceable></term>
3665 <listitem>
3666 <para>
3667 SASL data, specific to the SASL mechanism being used.
3668 </para>
3669 </listitem>
3670 </varlistentry>
3671 </variablelist>
3672 </listitem>
3673 </varlistentry>
3675 <varlistentry id="protocol-message-formats-AuthenticationSASLFinal">
3676 <term>AuthenticationSASLFinal (B)</term>
3677 <listitem>
3678 <variablelist>
3679 <varlistentry>
3680 <term>Byte1('R')</term>
3681 <listitem>
3682 <para>
3683 Identifies the message as an authentication request.
3684 </para>
3685 </listitem>
3686 </varlistentry>
3688 <varlistentry>
3689 <term>Int32</term>
3690 <listitem>
3691 <para>
3692 Length of message contents in bytes, including self.
3693 </para>
3694 </listitem>
3695 </varlistentry>
3697 <varlistentry>
3698 <term>Int32(12)</term>
3699 <listitem>
3700 <para>
3701 Specifies that SASL authentication has completed.
3702 </para>
3703 </listitem>
3704 </varlistentry>
3706 <varlistentry>
3707 <term>Byte<replaceable>n</replaceable></term>
3708 <listitem>
3709 <para>
3710 SASL outcome "additional data", specific to the SASL mechanism
3711 being used.
3712 </para>
3713 </listitem>
3714 </varlistentry>
3715 </variablelist>
3716 </listitem>
3717 </varlistentry>
3719 <varlistentry id="protocol-message-formats-BackendKeyData">
3720 <term>BackendKeyData (B)</term>
3721 <listitem>
3722 <variablelist>
3723 <varlistentry>
3724 <term>Byte1('K')</term>
3725 <listitem>
3726 <para>
3727 Identifies the message as cancellation key data.
3728 The frontend must save these values if it wishes to be
3729 able to issue CancelRequest messages later.
3730 </para>
3731 </listitem>
3732 </varlistentry>
3734 <varlistentry>
3735 <term>Int32(12)</term>
3736 <listitem>
3737 <para>
3738 Length of message contents in bytes, including self.
3739 </para>
3740 </listitem>
3741 </varlistentry>
3743 <varlistentry>
3744 <term>Int32</term>
3745 <listitem>
3746 <para>
3747 The process ID of this backend.
3748 </para>
3749 </listitem>
3750 </varlistentry>
3752 <varlistentry>
3753 <term>Int32</term>
3754 <listitem>
3755 <para>
3756 The secret key of this backend.
3757 </para>
3758 </listitem>
3759 </varlistentry>
3760 </variablelist>
3761 </listitem>
3762 </varlistentry>
3764 <varlistentry id="protocol-message-formats-Bind">
3765 <term>Bind (F)</term>
3766 <listitem>
3767 <variablelist>
3768 <varlistentry>
3769 <term>Byte1('B')</term>
3770 <listitem>
3771 <para>
3772 Identifies the message as a Bind command.
3773 </para>
3774 </listitem>
3775 </varlistentry>
3777 <varlistentry>
3778 <term>Int32</term>
3779 <listitem>
3780 <para>
3781 Length of message contents in bytes, including self.
3782 </para>
3783 </listitem>
3784 </varlistentry>
3786 <varlistentry>
3787 <term>String</term>
3788 <listitem>
3789 <para>
3790 The name of the destination portal
3791 (an empty string selects the unnamed portal).
3792 </para>
3793 </listitem>
3794 </varlistentry>
3796 <varlistentry>
3797 <term>String</term>
3798 <listitem>
3799 <para>
3800 The name of the source prepared statement
3801 (an empty string selects the unnamed prepared statement).
3802 </para>
3803 </listitem>
3804 </varlistentry>
3806 <varlistentry>
3807 <term>Int16</term>
3808 <listitem>
3809 <para>
3810 The number of parameter format codes that follow
3811 (denoted <replaceable>C</replaceable> below).
3812 This can be zero to indicate that there are no parameters
3813 or that the parameters all use the default format (text);
3814 or one, in which case the specified format code is applied
3815 to all parameters; or it can equal the actual number of
3816 parameters.
3817 </para>
3818 </listitem>
3819 </varlistentry>
3821 <varlistentry>
3822 <term>Int16[<replaceable>C</replaceable>]</term>
3823 <listitem>
3824 <para>
3825 The parameter format codes. Each must presently be
3826 zero (text) or one (binary).
3827 </para>
3828 </listitem>
3829 </varlistentry>
3831 <varlistentry>
3832 <term>Int16</term>
3833 <listitem>
3834 <para>
3835 The number of parameter values that follow (possibly zero).
3836 This must match the number of parameters needed by the query.
3837 </para>
3838 </listitem>
3839 </varlistentry>
3840 </variablelist>
3842 <para>
3843 Next, the following pair of fields appear for each parameter:
3844 </para>
3846 <variablelist>
3847 <varlistentry>
3848 <term>Int32</term>
3849 <listitem>
3850 <para>
3851 The length of the parameter value, in bytes (this count
3852 does not include itself). Can be zero.
3853 As a special case, -1 indicates a NULL parameter value.
3854 No value bytes follow in the NULL case.
3855 </para>
3856 </listitem>
3857 </varlistentry>
3859 <varlistentry>
3860 <term>Byte<replaceable>n</replaceable></term>
3861 <listitem>
3862 <para>
3863 The value of the parameter, in the format indicated by the
3864 associated format code.
3865 <replaceable>n</replaceable> is the above length.
3866 </para>
3867 </listitem>
3868 </varlistentry>
3869 </variablelist>
3871 <para>
3872 After the last parameter, the following fields appear:
3873 </para>
3875 <variablelist>
3876 <varlistentry>
3877 <term>Int16</term>
3878 <listitem>
3879 <para>
3880 The number of result-column format codes that follow
3881 (denoted <replaceable>R</replaceable> below).
3882 This can be zero to indicate that there are no result columns
3883 or that the result columns should all use the default format
3884 (text);
3885 or one, in which case the specified format code is applied
3886 to all result columns (if any); or it can equal the actual
3887 number of result columns of the query.
3888 </para>
3889 </listitem>
3890 </varlistentry>
3892 <varlistentry>
3893 <term>Int16[<replaceable>R</replaceable>]</term>
3894 <listitem>
3895 <para>
3896 The result-column format codes. Each must presently be
3897 zero (text) or one (binary).
3898 </para>
3899 </listitem>
3900 </varlistentry>
3901 </variablelist>
3902 </listitem>
3903 </varlistentry>
3905 <varlistentry id="protocol-message-formats-BindComplete">
3906 <term>BindComplete (B)</term>
3907 <listitem>
3908 <variablelist>
3909 <varlistentry>
3910 <term>Byte1('2')</term>
3911 <listitem>
3912 <para>
3913 Identifies the message as a Bind-complete indicator.
3914 </para>
3915 </listitem>
3916 </varlistentry>
3918 <varlistentry>
3919 <term>Int32(4)</term>
3920 <listitem>
3921 <para>
3922 Length of message contents in bytes, including self.
3923 </para>
3924 </listitem>
3925 </varlistentry>
3927 </variablelist>
3928 </listitem>
3929 </varlistentry>
3931 <varlistentry id="protocol-message-formats-CancelRequest">
3932 <term>CancelRequest (F)</term>
3933 <listitem>
3934 <variablelist>
3935 <varlistentry>
3936 <term>Int32(16)</term>
3937 <listitem>
3938 <para>
3939 Length of message contents in bytes, including self.
3940 </para>
3941 </listitem>
3942 </varlistentry>
3944 <varlistentry>
3945 <term>Int32(80877102)</term>
3946 <listitem>
3947 <para>
3948 The cancel request code. The value is chosen to contain
3949 <literal>1234</literal> in the most significant 16 bits, and <literal>5678</literal> in the
3950 least significant 16 bits. (To avoid confusion, this code
3951 must not be the same as any protocol version number.)
3952 </para>
3953 </listitem>
3954 </varlistentry>
3956 <varlistentry>
3957 <term>Int32</term>
3958 <listitem>
3959 <para>
3960 The process ID of the target backend.
3961 </para>
3962 </listitem>
3963 </varlistentry>
3965 <varlistentry>
3966 <term>Int32</term>
3967 <listitem>
3968 <para>
3969 The secret key for the target backend.
3970 </para>
3971 </listitem>
3972 </varlistentry>
3973 </variablelist>
3974 </listitem>
3975 </varlistentry>
3977 <varlistentry id="protocol-message-formats-Close">
3978 <term>Close (F)</term>
3979 <listitem>
3980 <variablelist>
3981 <varlistentry>
3982 <term>Byte1('C')</term>
3983 <listitem>
3984 <para>
3985 Identifies the message as a Close command.
3986 </para>
3987 </listitem>
3988 </varlistentry>
3990 <varlistentry>
3991 <term>Int32</term>
3992 <listitem>
3993 <para>
3994 Length of message contents in bytes, including self.
3995 </para>
3996 </listitem>
3997 </varlistentry>
3999 <varlistentry>
4000 <term>Byte1</term>
4001 <listitem>
4002 <para>
4003 '<literal>S</literal>' to close a prepared statement; or
4004 '<literal>P</literal>' to close a portal.
4005 </para>
4006 </listitem>
4007 </varlistentry>
4009 <varlistentry>
4010 <term>String</term>
4011 <listitem>
4012 <para>
4013 The name of the prepared statement or portal to close
4014 (an empty string selects the unnamed prepared statement
4015 or portal).
4016 </para>
4017 </listitem>
4018 </varlistentry>
4019 </variablelist>
4020 </listitem>
4021 </varlistentry>
4023 <varlistentry id="protocol-message-formats-CloseComplete">
4024 <term>CloseComplete (B)</term>
4025 <listitem>
4026 <variablelist>
4027 <varlistentry>
4028 <term>Byte1('3')</term>
4029 <listitem>
4030 <para>
4031 Identifies the message as a Close-complete indicator.
4032 </para>
4033 </listitem>
4034 </varlistentry>
4036 <varlistentry>
4037 <term>Int32(4)</term>
4038 <listitem>
4039 <para>
4040 Length of message contents in bytes, including self.
4041 </para>
4042 </listitem>
4043 </varlistentry>
4044 </variablelist>
4045 </listitem>
4046 </varlistentry>
4048 <varlistentry id="protocol-message-formats-CommandComplete">
4049 <term>CommandComplete (B)</term>
4050 <listitem>
4051 <variablelist>
4052 <varlistentry>
4053 <term>Byte1('C')</term>
4054 <listitem>
4055 <para>
4056 Identifies the message as a command-completed response.
4057 </para>
4058 </listitem>
4059 </varlistentry>
4061 <varlistentry>
4062 <term>Int32</term>
4063 <listitem>
4064 <para>
4065 Length of message contents in bytes, including self.
4066 </para>
4067 </listitem>
4068 </varlistentry>
4070 <varlistentry>
4071 <term>String</term>
4072 <listitem>
4073 <para>
4074 The command tag. This is usually a single
4075 word that identifies which SQL command was completed.
4076 </para>
4078 <para>
4079 For an <command>INSERT</command> command, the tag is
4080 <literal>INSERT <replaceable>oid</replaceable>
4081 <replaceable>rows</replaceable></literal>, where
4082 <replaceable>rows</replaceable> is the number of rows
4083 inserted. <replaceable>oid</replaceable> used to be the object ID
4084 of the inserted row if <replaceable>rows</replaceable> was 1
4085 and the target table had OIDs, but OIDs system columns are
4086 not supported anymore; therefore <replaceable>oid</replaceable>
4087 is always 0.
4088 </para>
4090 <para>
4091 For a <command>DELETE</command> command, the tag is
4092 <literal>DELETE <replaceable>rows</replaceable></literal> where
4093 <replaceable>rows</replaceable> is the number of rows deleted.
4094 </para>
4096 <para>
4097 For an <command>UPDATE</command> command, the tag is
4098 <literal>UPDATE <replaceable>rows</replaceable></literal> where
4099 <replaceable>rows</replaceable> is the number of rows updated.
4100 </para>
4102 <para>
4103 For a <command>MERGE</command> command, the tag is
4104 <literal>MERGE <replaceable>rows</replaceable></literal> where
4105 <replaceable>rows</replaceable> is the number of rows inserted,
4106 updated, or deleted.
4107 </para>
4109 <para>
4110 For a <command>SELECT</command> or <command>CREATE TABLE AS</command>
4111 command, the tag is <literal>SELECT <replaceable>rows</replaceable></literal>
4112 where <replaceable>rows</replaceable> is the number of rows retrieved.
4113 </para>
4115 <para>
4116 For a <command>MOVE</command> command, the tag is
4117 <literal>MOVE <replaceable>rows</replaceable></literal> where
4118 <replaceable>rows</replaceable> is the number of rows the
4119 cursor's position has been changed by.
4120 </para>
4122 <para>
4123 For a <command>FETCH</command> command, the tag is
4124 <literal>FETCH <replaceable>rows</replaceable></literal> where
4125 <replaceable>rows</replaceable> is the number of rows that
4126 have been retrieved from the cursor.
4127 </para>
4129 <para>
4130 For a <command>COPY</command> command, the tag is
4131 <literal>COPY <replaceable>rows</replaceable></literal> where
4132 <replaceable>rows</replaceable> is the number of rows copied.
4133 (Note: the row count appears only in
4134 <productname>PostgreSQL</productname> 8.2 and later.)
4135 </para>
4136 </listitem>
4137 </varlistentry>
4138 </variablelist>
4139 </listitem>
4140 </varlistentry>
4142 <varlistentry id="protocol-message-formats-CopyData">
4143 <term>CopyData (F &amp; B)</term>
4144 <listitem>
4145 <variablelist>
4146 <varlistentry>
4147 <term>Byte1('d')</term>
4148 <listitem>
4149 <para>
4150 Identifies the message as <command>COPY</command> data.
4151 </para>
4152 </listitem>
4153 </varlistentry>
4155 <varlistentry>
4156 <term>Int32</term>
4157 <listitem>
4158 <para>
4159 Length of message contents in bytes, including self.
4160 </para>
4161 </listitem>
4162 </varlistentry>
4164 <varlistentry>
4165 <term>Byte<replaceable>n</replaceable></term>
4166 <listitem>
4167 <para>
4168 Data that forms part of a <command>COPY</command> data stream. Messages sent
4169 from the backend will always correspond to single data rows,
4170 but messages sent by frontends might divide the data stream
4171 arbitrarily.
4172 </para>
4173 </listitem>
4174 </varlistentry>
4175 </variablelist>
4176 </listitem>
4177 </varlistentry>
4179 <varlistentry id="protocol-message-formats-CopyDone">
4180 <term>CopyDone (F &amp; B)</term>
4181 <listitem>
4182 <variablelist>
4183 <varlistentry>
4184 <term>Byte1('c')</term>
4185 <listitem>
4186 <para>
4187 Identifies the message as a <command>COPY</command>-complete indicator.
4188 </para>
4189 </listitem>
4190 </varlistentry>
4192 <varlistentry>
4193 <term>Int32(4)</term>
4194 <listitem>
4195 <para>
4196 Length of message contents in bytes, including self.
4197 </para>
4198 </listitem>
4199 </varlistentry>
4200 </variablelist>
4201 </listitem>
4202 </varlistentry>
4204 <varlistentry id="protocol-message-formats-CopyFail">
4205 <term>CopyFail (F)</term>
4206 <listitem>
4207 <variablelist>
4208 <varlistentry>
4209 <term>Byte1('f')</term>
4210 <listitem>
4211 <para>
4212 Identifies the message as a <command>COPY</command>-failure indicator.
4213 </para>
4214 </listitem>
4215 </varlistentry>
4216 <varlistentry>
4217 <term>Int32</term>
4218 <listitem>
4219 <para>
4220 Length of message contents in bytes, including self.
4221 </para>
4222 </listitem>
4223 </varlistentry>
4225 <varlistentry>
4226 <term>String</term>
4227 <listitem>
4228 <para>
4229 An error message to report as the cause of failure.
4230 </para>
4231 </listitem>
4232 </varlistentry>
4233 </variablelist>
4234 </listitem>
4235 </varlistentry>
4237 <varlistentry id="protocol-message-formats-CopyInResponse">
4238 <term>CopyInResponse (B)</term>
4239 <listitem>
4240 <variablelist>
4241 <varlistentry>
4242 <term>Byte1('G')</term>
4243 <listitem>
4244 <para>
4245 Identifies the message as a Start Copy In response.
4246 The frontend must now send copy-in data (if not
4247 prepared to do so, send a CopyFail message).
4248 </para>
4249 </listitem>
4250 </varlistentry>
4252 <varlistentry>
4253 <term>Int32</term>
4254 <listitem>
4255 <para>
4256 Length of message contents in bytes, including self.
4257 </para>
4258 </listitem>
4259 </varlistentry>
4261 <varlistentry>
4262 <term>Int8</term>
4263 <listitem>
4264 <para>
4265 0 indicates the overall <command>COPY</command> format is textual (rows
4266 separated by newlines, columns separated by separator
4267 characters, etc.).
4268 1 indicates the overall copy format is binary (similar
4269 to DataRow format).
4270 See <xref linkend="sql-copy"/>
4271 for more information.
4272 </para>
4273 </listitem>
4274 </varlistentry>
4276 <varlistentry>
4277 <term>Int16</term>
4278 <listitem>
4279 <para>
4280 The number of columns in the data to be copied
4281 (denoted <replaceable>N</replaceable> below).
4282 </para>
4283 </listitem>
4284 </varlistentry>
4286 <varlistentry>
4287 <term>Int16[<replaceable>N</replaceable>]</term>
4288 <listitem>
4289 <para>
4290 The format codes to be used for each column.
4291 Each must presently be zero (text) or one (binary).
4292 All must be zero if the overall copy format is textual.
4293 </para>
4294 </listitem>
4295 </varlistentry>
4296 </variablelist>
4297 </listitem>
4298 </varlistentry>
4300 <varlistentry id="protocol-message-formats-CopyOutResponse">
4301 <term>CopyOutResponse (B)</term>
4302 <listitem>
4303 <variablelist>
4304 <varlistentry>
4305 <term>Byte1('H')</term>
4306 <listitem>
4307 <para>
4308 Identifies the message as a Start Copy Out response.
4309 This message will be followed by copy-out data.
4310 </para>
4311 </listitem>
4312 </varlistentry>
4314 <varlistentry>
4315 <term>Int32</term>
4316 <listitem>
4317 <para>
4318 Length of message contents in bytes, including self.
4319 </para>
4320 </listitem>
4321 </varlistentry>
4323 <varlistentry>
4324 <term>Int8</term>
4325 <listitem>
4326 <para>
4327 0 indicates the overall <command>COPY</command> format
4328 is textual (rows separated by newlines, columns
4329 separated by separator characters, etc.). 1 indicates
4330 the overall copy format is binary (similar to DataRow
4331 format). See <xref linkend="sql-copy"/> for more information.
4332 </para>
4333 </listitem>
4334 </varlistentry>
4336 <varlistentry>
4337 <term>Int16</term>
4338 <listitem>
4339 <para>
4340 The number of columns in the data to be copied
4341 (denoted <replaceable>N</replaceable> below).
4342 </para>
4343 </listitem>
4344 </varlistentry>
4346 <varlistentry>
4347 <term>Int16[<replaceable>N</replaceable>]</term>
4348 <listitem>
4349 <para>
4350 The format codes to be used for each column.
4351 Each must presently be zero (text) or one (binary).
4352 All must be zero if the overall copy format is textual.
4353 </para>
4354 </listitem>
4355 </varlistentry>
4356 </variablelist>
4357 </listitem>
4358 </varlistentry>
4360 <varlistentry id="protocol-message-formats-CopyBothResponse">
4361 <term>CopyBothResponse (B)</term>
4362 <listitem>
4363 <variablelist>
4364 <varlistentry>
4365 <term>Byte1('W')</term>
4366 <listitem>
4367 <para>
4368 Identifies the message as a Start Copy Both response.
4369 This message is used only for Streaming Replication.
4370 </para>
4371 </listitem>
4372 </varlistentry>
4374 <varlistentry>
4375 <term>Int32</term>
4376 <listitem>
4377 <para>
4378 Length of message contents in bytes, including self.
4379 </para>
4380 </listitem>
4381 </varlistentry>
4383 <varlistentry>
4384 <term>Int8</term>
4385 <listitem>
4386 <para>
4387 0 indicates the overall <command>COPY</command> format
4388 is textual (rows separated by newlines, columns
4389 separated by separator characters, etc.). 1 indicates
4390 the overall copy format is binary (similar to DataRow
4391 format). See <xref linkend="sql-copy"/> for more information.
4392 </para>
4393 </listitem>
4394 </varlistentry>
4396 <varlistentry>
4397 <term>Int16</term>
4398 <listitem>
4399 <para>
4400 The number of columns in the data to be copied
4401 (denoted <replaceable>N</replaceable> below).
4402 </para>
4403 </listitem>
4404 </varlistentry>
4406 <varlistentry>
4407 <term>Int16[<replaceable>N</replaceable>]</term>
4408 <listitem>
4409 <para>
4410 The format codes to be used for each column.
4411 Each must presently be zero (text) or one (binary).
4412 All must be zero if the overall copy format is textual.
4413 </para>
4414 </listitem>
4415 </varlistentry>
4416 </variablelist>
4417 </listitem>
4418 </varlistentry>
4420 <varlistentry id="protocol-message-formats-DataRow">
4421 <term>DataRow (B)</term>
4422 <listitem>
4423 <variablelist>
4424 <varlistentry>
4425 <term>Byte1('D')</term>
4426 <listitem>
4427 <para>
4428 Identifies the message as a data row.
4429 </para>
4430 </listitem>
4431 </varlistentry>
4433 <varlistentry>
4434 <term>Int32</term>
4435 <listitem>
4436 <para>
4437 Length of message contents in bytes, including self.
4438 </para>
4439 </listitem>
4440 </varlistentry>
4442 <varlistentry>
4443 <term>Int16</term>
4444 <listitem>
4445 <para>
4446 The number of column values that follow (possibly zero).
4447 </para>
4448 </listitem>
4449 </varlistentry>
4450 </variablelist>
4452 <para>
4453 Next, the following pair of fields appear for each column:
4454 </para>
4456 <variablelist>
4457 <varlistentry>
4458 <term>Int32</term>
4459 <listitem>
4460 <para>
4461 The length of the column value, in bytes (this count
4462 does not include itself). Can be zero.
4463 As a special case, -1 indicates a NULL column value.
4464 No value bytes follow in the NULL case.
4465 </para>
4466 </listitem>
4467 </varlistentry>
4469 <varlistentry>
4470 <term>Byte<replaceable>n</replaceable></term>
4471 <listitem>
4472 <para>
4473 The value of the column, in the format indicated by the
4474 associated format code.
4475 <replaceable>n</replaceable> is the above length.
4476 </para>
4477 </listitem>
4478 </varlistentry>
4479 </variablelist>
4480 </listitem>
4481 </varlistentry>
4483 <varlistentry id="protocol-message-formats-Describe">
4484 <term>Describe (F)</term>
4485 <listitem>
4486 <variablelist>
4487 <varlistentry>
4488 <term>Byte1('D')</term>
4489 <listitem>
4490 <para>
4491 Identifies the message as a Describe command.
4492 </para>
4493 </listitem>
4494 </varlistentry>
4496 <varlistentry>
4497 <term>Int32</term>
4498 <listitem>
4499 <para>
4500 Length of message contents in bytes, including self.
4501 </para>
4502 </listitem>
4503 </varlistentry>
4505 <varlistentry>
4506 <term>Byte1</term>
4507 <listitem>
4508 <para>
4509 '<literal>S</literal>' to describe a prepared statement; or
4510 '<literal>P</literal>' to describe a portal.
4511 </para>
4512 </listitem>
4513 </varlistentry>
4515 <varlistentry>
4516 <term>String</term>
4517 <listitem>
4518 <para>
4519 The name of the prepared statement or portal to describe
4520 (an empty string selects the unnamed prepared statement
4521 or portal).
4522 </para>
4523 </listitem>
4524 </varlistentry>
4525 </variablelist>
4526 </listitem>
4527 </varlistentry>
4529 <varlistentry id="protocol-message-formats-EmptyQueryResponse">
4530 <term>EmptyQueryResponse (B)</term>
4531 <listitem>
4532 <variablelist>
4533 <varlistentry>
4534 <term>Byte1('I')</term>
4535 <listitem>
4536 <para>
4537 Identifies the message as a response to an empty query string.
4538 (This substitutes for CommandComplete.)
4539 </para>
4540 </listitem>
4541 </varlistentry>
4543 <varlistentry>
4544 <term>Int32(4)</term>
4545 <listitem>
4546 <para>
4547 Length of message contents in bytes, including self.
4548 </para>
4549 </listitem>
4550 </varlistentry>
4551 </variablelist>
4552 </listitem>
4553 </varlistentry>
4555 <varlistentry id="protocol-message-formats-ErrorResponse">
4556 <term>ErrorResponse (B)</term>
4557 <listitem>
4558 <variablelist>
4559 <varlistentry>
4560 <term>Byte1('E')</term>
4561 <listitem>
4562 <para>
4563 Identifies the message as an error.
4564 </para>
4565 </listitem>
4566 </varlistentry>
4568 <varlistentry>
4569 <term>Int32</term>
4570 <listitem>
4571 <para>
4572 Length of message contents in bytes, including self.
4573 </para>
4574 </listitem>
4575 </varlistentry>
4576 </variablelist>
4578 <para>
4579 The message body consists of one or more identified fields,
4580 followed by a zero byte as a terminator. Fields can appear in
4581 any order. For each field there is the following:
4582 </para>
4584 <variablelist>
4585 <varlistentry>
4586 <term>Byte1</term>
4587 <listitem>
4588 <para>
4589 A code identifying the field type; if zero, this is
4590 the message terminator and no string follows.
4591 The presently defined field types are listed in
4592 <xref linkend="protocol-error-fields"/>.
4593 Since more field types might be added in future,
4594 frontends should silently ignore fields of unrecognized
4595 type.
4596 </para>
4597 </listitem>
4598 </varlistentry>
4600 <varlistentry>
4601 <term>String</term>
4602 <listitem>
4603 <para>
4604 The field value.
4605 </para>
4606 </listitem>
4607 </varlistentry>
4608 </variablelist>
4609 </listitem>
4610 </varlistentry>
4612 <varlistentry id="protocol-message-formats-Execute">
4613 <term>Execute (F)</term>
4614 <listitem>
4615 <variablelist>
4616 <varlistentry>
4617 <term>Byte1('E')</term>
4618 <listitem>
4619 <para>
4620 Identifies the message as an Execute command.
4621 </para>
4622 </listitem>
4623 </varlistentry>
4625 <varlistentry>
4626 <term>Int32</term>
4627 <listitem>
4628 <para>
4629 Length of message contents in bytes, including self.
4630 </para>
4631 </listitem>
4632 </varlistentry>
4634 <varlistentry>
4635 <term>String</term>
4636 <listitem>
4637 <para>
4638 The name of the portal to execute
4639 (an empty string selects the unnamed portal).
4640 </para>
4641 </listitem>
4642 </varlistentry>
4644 <varlistentry>
4645 <term>Int32</term>
4646 <listitem>
4647 <para>
4648 Maximum number of rows to return, if portal contains
4649 a query that returns rows (ignored otherwise). Zero
4650 denotes <quote>no limit</quote>.
4651 </para>
4652 </listitem>
4653 </varlistentry>
4654 </variablelist>
4655 </listitem>
4656 </varlistentry>
4658 <varlistentry id="protocol-message-formats-Flush">
4659 <term>Flush (F)</term>
4660 <listitem>
4661 <variablelist>
4662 <varlistentry>
4663 <term>Byte1('H')</term>
4664 <listitem>
4665 <para>
4666 Identifies the message as a Flush command.
4667 </para>
4668 </listitem>
4669 </varlistentry>
4671 <varlistentry>
4672 <term>Int32(4)</term>
4673 <listitem>
4674 <para>
4675 Length of message contents in bytes, including self.
4676 </para>
4677 </listitem>
4678 </varlistentry>
4679 </variablelist>
4680 </listitem>
4681 </varlistentry>
4683 <varlistentry id="protocol-message-formats-FunctionCall">
4684 <term>FunctionCall (F)</term>
4685 <listitem>
4686 <variablelist>
4687 <varlistentry>
4688 <term>Byte1('F')</term>
4689 <listitem>
4690 <para>
4691 Identifies the message as a function call.
4692 </para>
4693 </listitem>
4694 </varlistentry>
4696 <varlistentry>
4697 <term>Int32</term>
4698 <listitem>
4699 <para>
4700 Length of message contents in bytes, including self.
4701 </para>
4702 </listitem>
4703 </varlistentry>
4705 <varlistentry>
4706 <term>Int32</term>
4707 <listitem>
4708 <para>
4709 Specifies the object ID of the function to call.
4710 </para>
4711 </listitem>
4712 </varlistentry>
4714 <varlistentry>
4715 <term>Int16</term>
4716 <listitem>
4717 <para>
4718 The number of argument format codes that follow
4719 (denoted <replaceable>C</replaceable> below).
4720 This can be zero to indicate that there are no arguments
4721 or that the arguments all use the default format (text);
4722 or one, in which case the specified format code is applied
4723 to all arguments; or it can equal the actual number of
4724 arguments.
4725 </para>
4726 </listitem>
4727 </varlistentry>
4729 <varlistentry>
4730 <term>Int16[<replaceable>C</replaceable>]</term>
4731 <listitem>
4732 <para>
4733 The argument format codes. Each must presently be
4734 zero (text) or one (binary).
4735 </para>
4736 </listitem>
4737 </varlistentry>
4739 <varlistentry>
4740 <term>Int16</term>
4741 <listitem>
4742 <para>
4743 Specifies the number of arguments being supplied to the
4744 function.
4745 </para>
4746 </listitem>
4747 </varlistentry>
4748 </variablelist>
4750 <para>
4751 Next, the following pair of fields appear for each argument:
4752 </para>
4754 <variablelist>
4755 <varlistentry>
4756 <term>Int32</term>
4757 <listitem>
4758 <para>
4759 The length of the argument value, in bytes (this count
4760 does not include itself). Can be zero.
4761 As a special case, -1 indicates a NULL argument value.
4762 No value bytes follow in the NULL case.
4763 </para>
4764 </listitem>
4765 </varlistentry>
4767 <varlistentry>
4768 <term>Byte<replaceable>n</replaceable></term>
4769 <listitem>
4770 <para>
4771 The value of the argument, in the format indicated by the
4772 associated format code.
4773 <replaceable>n</replaceable> is the above length.
4774 </para>
4775 </listitem>
4776 </varlistentry>
4777 </variablelist>
4779 <para>
4780 After the last argument, the following field appears:
4781 </para>
4783 <variablelist>
4784 <varlistentry>
4785 <term>Int16</term>
4786 <listitem>
4787 <para>
4788 The format code for the function result. Must presently be
4789 zero (text) or one (binary).
4790 </para>
4791 </listitem>
4792 </varlistentry>
4793 </variablelist>
4794 </listitem>
4795 </varlistentry>
4797 <varlistentry id="protocol-message-formats-FunctionCallResponse">
4798 <term>FunctionCallResponse (B)</term>
4799 <listitem>
4800 <variablelist>
4801 <varlistentry>
4802 <term>Byte1('V')</term>
4803 <listitem>
4804 <para>
4805 Identifies the message as a function call result.
4806 </para>
4807 </listitem>
4808 </varlistentry>
4810 <varlistentry>
4811 <term>Int32</term>
4812 <listitem>
4813 <para>
4814 Length of message contents in bytes, including self.
4815 </para>
4816 </listitem>
4817 </varlistentry>
4819 <varlistentry>
4820 <term>Int32</term>
4821 <listitem>
4822 <para>
4823 The length of the function result value, in bytes (this count
4824 does not include itself). Can be zero.
4825 As a special case, -1 indicates a NULL function result.
4826 No value bytes follow in the NULL case.
4827 </para>
4828 </listitem>
4829 </varlistentry>
4831 <varlistentry>
4832 <term>Byte<replaceable>n</replaceable></term>
4833 <listitem>
4834 <para>
4835 The value of the function result, in the format indicated by
4836 the associated format code.
4837 <replaceable>n</replaceable> is the above length.
4838 </para>
4839 </listitem>
4840 </varlistentry>
4841 </variablelist>
4842 </listitem>
4843 </varlistentry>
4845 <varlistentry id="protocol-message-formats-GSSENCRequest">
4846 <term>GSSENCRequest (F)</term>
4847 <listitem>
4848 <variablelist>
4849 <varlistentry>
4850 <term>Int32(8)</term>
4851 <listitem>
4852 <para>
4853 Length of message contents in bytes, including self.
4854 </para>
4855 </listitem>
4856 </varlistentry>
4858 <varlistentry>
4859 <term>Int32(80877104)</term>
4860 <listitem>
4861 <para>
4862 The <acronym>GSSAPI</acronym> Encryption request code. The value is chosen to contain
4863 <literal>1234</literal> in the most significant 16 bits, and <literal>5680</literal> in the
4864 least significant 16 bits. (To avoid confusion, this code
4865 must not be the same as any protocol version number.)
4866 </para>
4867 </listitem>
4868 </varlistentry>
4869 </variablelist>
4870 </listitem>
4871 </varlistentry>
4873 <varlistentry id="protocol-message-formats-GSSResponse">
4874 <term>GSSResponse (F)</term>
4875 <listitem>
4876 <variablelist>
4877 <varlistentry>
4878 <term>Byte1('p')</term>
4879 <listitem>
4880 <para>
4881 Identifies the message as a GSSAPI or SSPI response. Note that
4882 this is also used for SASL and password response messages.
4883 The exact message type can be deduced from the context.
4884 </para>
4885 </listitem>
4886 </varlistentry>
4888 <varlistentry>
4889 <term>Int32</term>
4890 <listitem>
4891 <para>
4892 Length of message contents in bytes, including self.
4893 </para>
4894 </listitem>
4895 </varlistentry>
4897 <varlistentry>
4898 <term>Byte<replaceable>n</replaceable></term>
4899 <listitem>
4900 <para>
4901 GSSAPI/SSPI specific message data.
4902 </para>
4903 </listitem>
4904 </varlistentry>
4905 </variablelist>
4906 </listitem>
4907 </varlistentry>
4909 <varlistentry id="protocol-message-formats-NegotiateProtocolVersion">
4910 <term>NegotiateProtocolVersion (B)</term>
4911 <listitem>
4912 <variablelist>
4913 <varlistentry>
4914 <term>Byte1('v')</term>
4915 <listitem>
4916 <para>
4917 Identifies the message as a protocol version negotiation
4918 message.
4919 </para>
4920 </listitem>
4921 </varlistentry>
4923 <varlistentry>
4924 <term>Int32</term>
4925 <listitem>
4926 <para>
4927 Length of message contents in bytes, including self.
4928 </para>
4929 </listitem>
4930 </varlistentry>
4932 <varlistentry>
4933 <term>Int32</term>
4934 <listitem>
4935 <para>
4936 Newest minor protocol version supported by the server
4937 for the major protocol version requested by the client.
4938 </para>
4939 </listitem>
4940 </varlistentry>
4942 <varlistentry>
4943 <term>Int32</term>
4944 <listitem>
4945 <para>
4946 Number of protocol options not recognized by the server.
4947 </para>
4948 </listitem>
4949 </varlistentry>
4950 </variablelist>
4952 <para>
4953 Then, for protocol option not recognized by the server, there
4954 is the following:
4955 </para>
4957 <variablelist>
4958 <varlistentry>
4959 <term>String</term>
4960 <listitem>
4961 <para>
4962 The option name.
4963 </para>
4964 </listitem>
4965 </varlistentry>
4966 </variablelist>
4967 </listitem>
4968 </varlistentry>
4970 <varlistentry id="protocol-message-formats-NoData">
4971 <term>NoData (B)</term>
4972 <listitem>
4973 <variablelist>
4974 <varlistentry>
4975 <term>Byte1('n')</term>
4976 <listitem>
4977 <para>
4978 Identifies the message as a no-data indicator.
4979 </para>
4980 </listitem>
4981 </varlistentry>
4983 <varlistentry>
4984 <term>Int32(4)</term>
4985 <listitem>
4986 <para>
4987 Length of message contents in bytes, including self.
4988 </para>
4989 </listitem>
4990 </varlistentry>
4991 </variablelist>
4992 </listitem>
4993 </varlistentry>
4995 <varlistentry id="protocol-message-formats-NoticeResponse">
4996 <term>NoticeResponse (B)</term>
4997 <listitem>
4998 <variablelist>
4999 <varlistentry>
5000 <term>Byte1('N')</term>
5001 <listitem>
5002 <para>
5003 Identifies the message as a notice.
5004 </para>
5005 </listitem>
5006 </varlistentry>
5008 <varlistentry>
5009 <term>Int32</term>
5010 <listitem>
5011 <para>
5012 Length of message contents in bytes, including self.
5013 </para>
5014 </listitem>
5015 </varlistentry>
5016 </variablelist>
5018 <para>
5019 The message body consists of one or more identified fields,
5020 followed by a zero byte as a terminator. Fields can appear in
5021 any order. For each field there is the following:
5022 </para>
5024 <variablelist>
5025 <varlistentry>
5026 <term>Byte1</term>
5027 <listitem>
5028 <para>
5029 A code identifying the field type; if zero, this is
5030 the message terminator and no string follows.
5031 The presently defined field types are listed in
5032 <xref linkend="protocol-error-fields"/>.
5033 Since more field types might be added in future,
5034 frontends should silently ignore fields of unrecognized
5035 type.
5036 </para>
5037 </listitem>
5038 </varlistentry>
5040 <varlistentry>
5041 <term>String</term>
5042 <listitem>
5043 <para>
5044 The field value.
5045 </para>
5046 </listitem>
5047 </varlistentry>
5048 </variablelist>
5049 </listitem>
5050 </varlistentry>
5052 <varlistentry id="protocol-message-formats-NotificationResponse">
5053 <term>NotificationResponse (B)</term>
5054 <listitem>
5055 <variablelist>
5056 <varlistentry>
5057 <term>Byte1('A')</term>
5058 <listitem>
5059 <para>
5060 Identifies the message as a notification response.
5061 </para>
5062 </listitem>
5063 </varlistentry>
5065 <varlistentry>
5066 <term>Int32</term>
5067 <listitem>
5068 <para>
5069 Length of message contents in bytes, including self.
5070 </para>
5071 </listitem>
5072 </varlistentry>
5074 <varlistentry>
5075 <term>Int32</term>
5076 <listitem>
5077 <para>
5078 The process ID of the notifying backend process.
5079 </para>
5080 </listitem>
5081 </varlistentry>
5083 <varlistentry>
5084 <term>String</term>
5085 <listitem>
5086 <para>
5087 The name of the channel that the notify has been raised on.
5088 </para>
5089 </listitem>
5090 </varlistentry>
5092 <varlistentry>
5093 <term>String</term>
5094 <listitem>
5095 <para>
5096 The <quote>payload</quote> string passed from the notifying process.
5097 </para>
5098 </listitem>
5099 </varlistentry>
5100 </variablelist>
5101 </listitem>
5102 </varlistentry>
5104 <varlistentry id="protocol-message-formats-ParameterDescription">
5105 <term>ParameterDescription (B)</term>
5106 <listitem>
5107 <variablelist>
5108 <varlistentry>
5109 <term>Byte1('t')</term>
5110 <listitem>
5111 <para>
5112 Identifies the message as a parameter description.
5113 </para>
5114 </listitem>
5115 </varlistentry>
5117 <varlistentry>
5118 <term>Int32</term>
5119 <listitem>
5120 <para>
5121 Length of message contents in bytes, including self.
5122 </para>
5123 </listitem>
5124 </varlistentry>
5126 <varlistentry>
5127 <term>Int16</term>
5128 <listitem>
5129 <para>
5130 The number of parameters used by the statement
5131 (can be zero).
5132 </para>
5133 </listitem>
5134 </varlistentry>
5135 </variablelist>
5137 <para>
5138 Then, for each parameter, there is the following:
5139 </para>
5141 <variablelist>
5142 <varlistentry>
5143 <term>Int32</term>
5144 <listitem>
5145 <para>
5146 Specifies the object ID of the parameter data type.
5147 </para>
5148 </listitem>
5149 </varlistentry>
5150 </variablelist>
5151 </listitem>
5152 </varlistentry>
5154 <varlistentry id="protocol-message-formats-ParameterStatus">
5155 <term>ParameterStatus (B)</term>
5156 <listitem>
5157 <variablelist>
5158 <varlistentry>
5159 <term>Byte1('S')</term>
5160 <listitem>
5161 <para>
5162 Identifies the message as a run-time parameter status report.
5163 </para>
5164 </listitem>
5165 </varlistentry>
5167 <varlistentry>
5168 <term>Int32</term>
5169 <listitem>
5170 <para>
5171 Length of message contents in bytes, including self.
5172 </para>
5173 </listitem>
5174 </varlistentry>
5176 <varlistentry>
5177 <term>String</term>
5178 <listitem>
5179 <para>
5180 The name of the run-time parameter being reported.
5181 </para>
5182 </listitem>
5183 </varlistentry>
5185 <varlistentry>
5186 <term>String</term>
5187 <listitem>
5188 <para>
5189 The current value of the parameter.
5190 </para>
5191 </listitem>
5192 </varlistentry>
5193 </variablelist>
5194 </listitem>
5195 </varlistentry>
5197 <varlistentry id="protocol-message-formats-Parse">
5198 <term>Parse (F)</term>
5199 <listitem>
5200 <variablelist>
5201 <varlistentry>
5202 <term>Byte1('P')</term>
5203 <listitem>
5204 <para>
5205 Identifies the message as a Parse command.
5206 </para>
5207 </listitem>
5208 </varlistentry>
5210 <varlistentry>
5211 <term>Int32</term>
5212 <listitem>
5213 <para>
5214 Length of message contents in bytes, including self.
5215 </para>
5216 </listitem>
5217 </varlistentry>
5219 <varlistentry>
5220 <term>String</term>
5221 <listitem>
5222 <para>
5223 The name of the destination prepared statement
5224 (an empty string selects the unnamed prepared statement).
5225 </para>
5226 </listitem>
5227 </varlistentry>
5229 <varlistentry>
5230 <term>String</term>
5231 <listitem>
5232 <para>
5233 The query string to be parsed.
5234 </para>
5235 </listitem>
5236 </varlistentry>
5238 <varlistentry>
5239 <term>Int16</term>
5240 <listitem>
5241 <para>
5242 The number of parameter data types specified
5243 (can be zero). Note that this is not an indication of
5244 the number of parameters that might appear in the
5245 query string, only the number that the frontend wants to
5246 prespecify types for.
5247 </para>
5248 </listitem>
5249 </varlistentry>
5250 </variablelist>
5252 <para>
5253 Then, for each parameter, there is the following:
5254 </para>
5256 <variablelist>
5257 <varlistentry>
5258 <term>Int32</term>
5259 <listitem>
5260 <para>
5261 Specifies the object ID of the parameter data type.
5262 Placing a zero here is equivalent to leaving the type
5263 unspecified.
5264 </para>
5265 </listitem>
5266 </varlistentry>
5267 </variablelist>
5268 </listitem>
5269 </varlistentry>
5271 <varlistentry id="protocol-message-formats-ParseComplete">
5272 <term>ParseComplete (B)</term>
5273 <listitem>
5274 <variablelist>
5275 <varlistentry>
5276 <term>Byte1('1')</term>
5277 <listitem>
5278 <para>
5279 Identifies the message as a Parse-complete indicator.
5280 </para>
5281 </listitem>
5282 </varlistentry>
5284 <varlistentry>
5285 <term>Int32(4)</term>
5286 <listitem>
5287 <para>
5288 Length of message contents in bytes, including self.
5289 </para>
5290 </listitem>
5291 </varlistentry>
5292 </variablelist>
5293 </listitem>
5294 </varlistentry>
5296 <varlistentry id="protocol-message-formats-PasswordMessage">
5297 <term>PasswordMessage (F)</term>
5298 <listitem>
5299 <variablelist>
5300 <varlistentry>
5301 <term>Byte1('p')</term>
5302 <listitem>
5303 <para>
5304 Identifies the message as a password response. Note that
5305 this is also used for GSSAPI, SSPI and SASL response messages.
5306 The exact message type can be deduced from the context.
5307 </para>
5308 </listitem>
5309 </varlistentry>
5311 <varlistentry>
5312 <term>Int32</term>
5313 <listitem>
5314 <para>
5315 Length of message contents in bytes, including self.
5316 </para>
5317 </listitem>
5318 </varlistentry>
5320 <varlistentry>
5321 <term>String</term>
5322 <listitem>
5323 <para>
5324 The password (encrypted, if requested).
5325 </para>
5326 </listitem>
5327 </varlistentry>
5328 </variablelist>
5329 </listitem>
5330 </varlistentry>
5332 <varlistentry id="protocol-message-formats-PortalSuspended">
5333 <term>PortalSuspended (B)</term>
5334 <listitem>
5335 <variablelist>
5336 <varlistentry>
5337 <term>Byte1('s')</term>
5338 <listitem>
5339 <para>
5340 Identifies the message as a portal-suspended indicator.
5341 Note this only appears if an Execute message's row-count limit
5342 was reached.
5343 </para>
5344 </listitem>
5345 </varlistentry>
5347 <varlistentry>
5348 <term>Int32(4)</term>
5349 <listitem>
5350 <para>
5351 Length of message contents in bytes, including self.
5352 </para>
5353 </listitem>
5354 </varlistentry>
5355 </variablelist>
5356 </listitem>
5357 </varlistentry>
5359 <varlistentry id="protocol-message-formats-Query">
5360 <term>Query (F)</term>
5361 <listitem>
5362 <variablelist>
5363 <varlistentry>
5364 <term>Byte1('Q')</term>
5365 <listitem>
5366 <para>
5367 Identifies the message as a simple query.
5368 </para>
5369 </listitem>
5370 </varlistentry>
5372 <varlistentry>
5373 <term>Int32</term>
5374 <listitem>
5375 <para>
5376 Length of message contents in bytes, including self.
5377 </para>
5378 </listitem>
5379 </varlistentry>
5381 <varlistentry>
5382 <term>String</term>
5383 <listitem>
5384 <para>
5385 The query string itself.
5386 </para>
5387 </listitem>
5388 </varlistentry>
5389 </variablelist>
5390 </listitem>
5391 </varlistentry>
5393 <varlistentry id="protocol-message-formats-ReadyForQuery">
5394 <term>ReadyForQuery (B)</term>
5395 <listitem>
5396 <variablelist>
5397 <varlistentry>
5398 <term>Byte1('Z')</term>
5399 <listitem>
5400 <para>
5401 Identifies the message type. ReadyForQuery is sent
5402 whenever the backend is ready for a new query cycle.
5403 </para>
5404 </listitem>
5405 </varlistentry>
5407 <varlistentry>
5408 <term>Int32(5)</term>
5409 <listitem>
5410 <para>
5411 Length of message contents in bytes, including self.
5412 </para>
5413 </listitem>
5414 </varlistentry>
5416 <varlistentry>
5417 <term>Byte1</term>
5418 <listitem>
5419 <para>
5420 Current backend transaction status indicator.
5421 Possible values are '<literal>I</literal>' if idle (not in
5422 a transaction block); '<literal>T</literal>' if in a transaction
5423 block; or '<literal>E</literal>' if in a failed transaction
5424 block (queries will be rejected until block is ended).
5425 </para>
5426 </listitem>
5427 </varlistentry>
5428 </variablelist>
5429 </listitem>
5430 </varlistentry>
5432 <varlistentry id="protocol-message-formats-RowDescription">
5433 <term>RowDescription (B)</term>
5434 <listitem>
5435 <variablelist>
5436 <varlistentry>
5437 <term>Byte1('T')</term>
5438 <listitem>
5439 <para>
5440 Identifies the message as a row description.
5441 </para>
5442 </listitem>
5443 </varlistentry>
5445 <varlistentry>
5446 <term>Int32</term>
5447 <listitem>
5448 <para>
5449 Length of message contents in bytes, including self.
5450 </para>
5451 </listitem>
5452 </varlistentry>
5454 <varlistentry>
5455 <term>Int16</term>
5456 <listitem>
5457 <para>
5458 Specifies the number of fields in a row (can be zero).
5459 </para>
5460 </listitem>
5461 </varlistentry>
5462 </variablelist>
5464 <para>
5465 Then, for each field, there is the following:
5466 </para>
5468 <variablelist>
5469 <varlistentry>
5470 <term>String</term>
5471 <listitem>
5472 <para>
5473 The field name.
5474 </para>
5475 </listitem>
5476 </varlistentry>
5478 <varlistentry>
5479 <term>Int32</term>
5480 <listitem>
5481 <para>
5482 If the field can be identified as a column of a specific
5483 table, the object ID of the table; otherwise zero.
5484 </para>
5485 </listitem>
5486 </varlistentry>
5488 <varlistentry>
5489 <term>Int16</term>
5490 <listitem>
5491 <para>
5492 If the field can be identified as a column of a specific
5493 table, the attribute number of the column; otherwise zero.
5494 </para>
5495 </listitem>
5496 </varlistentry>
5498 <varlistentry>
5499 <term>Int32</term>
5500 <listitem>
5501 <para>
5502 The object ID of the field's data type.
5503 </para>
5504 </listitem>
5505 </varlistentry>
5507 <varlistentry>
5508 <term>Int16</term>
5509 <listitem>
5510 <para>
5511 The data type size (see <varname>pg_type.typlen</varname>).
5512 Note that negative values denote variable-width types.
5513 </para>
5514 </listitem>
5515 </varlistentry>
5517 <varlistentry>
5518 <term>Int32</term>
5519 <listitem>
5520 <para>
5521 The type modifier (see <varname>pg_attribute.atttypmod</varname>).
5522 The meaning of the modifier is type-specific.
5523 </para>
5524 </listitem>
5525 </varlistentry>
5527 <varlistentry>
5528 <term>Int16</term>
5529 <listitem>
5530 <para>
5531 The format code being used for the field. Currently will
5532 be zero (text) or one (binary). In a RowDescription
5533 returned from the statement variant of Describe, the
5534 format code is not yet known and will always be zero.
5535 </para>
5536 </listitem>
5537 </varlistentry>
5538 </variablelist>
5539 </listitem>
5540 </varlistentry>
5542 <varlistentry id="protocol-message-formats-SASLInitialResponse">
5543 <term>SASLInitialResponse (F)</term>
5544 <listitem>
5545 <variablelist>
5546 <varlistentry>
5547 <term>Byte1('p')</term>
5548 <listitem>
5549 <para>
5550 Identifies the message as an initial SASL response. Note that
5551 this is also used for GSSAPI, SSPI and password response messages.
5552 The exact message type is deduced from the context.
5553 </para>
5554 </listitem>
5555 </varlistentry>
5557 <varlistentry>
5558 <term>Int32</term>
5559 <listitem>
5560 <para>
5561 Length of message contents in bytes, including self.
5562 </para>
5563 </listitem>
5564 </varlistentry>
5566 <varlistentry>
5567 <term>String</term>
5568 <listitem>
5569 <para>
5570 Name of the SASL authentication mechanism that the client
5571 selected.
5572 </para>
5573 </listitem>
5574 </varlistentry>
5576 <varlistentry>
5577 <term>Int32</term>
5578 <listitem>
5579 <para>
5580 Length of SASL mechanism specific "Initial Client Response" that
5581 follows, or -1 if there is no Initial Response.
5582 </para>
5583 </listitem>
5584 </varlistentry>
5586 <varlistentry>
5587 <term>Byte<replaceable>n</replaceable></term>
5588 <listitem>
5589 <para>
5590 SASL mechanism specific "Initial Response".
5591 </para>
5592 </listitem>
5593 </varlistentry>
5594 </variablelist>
5595 </listitem>
5596 </varlistentry>
5598 <varlistentry id="protocol-message-formats-SASLResponse">
5599 <term>SASLResponse (F)</term>
5600 <listitem>
5601 <variablelist>
5602 <varlistentry>
5603 <term>Byte1('p')</term>
5604 <listitem>
5605 <para>
5606 Identifies the message as a SASL response. Note that
5607 this is also used for GSSAPI, SSPI and password response messages.
5608 The exact message type can be deduced from the context.
5609 </para>
5610 </listitem>
5611 </varlistentry>
5613 <varlistentry>
5614 <term>Int32</term>
5615 <listitem>
5616 <para>
5617 Length of message contents in bytes, including self.
5618 </para>
5619 </listitem>
5620 </varlistentry>
5622 <varlistentry>
5623 <term>Byte<replaceable>n</replaceable></term>
5624 <listitem>
5625 <para>
5626 SASL mechanism specific message data.
5627 </para>
5628 </listitem>
5629 </varlistentry>
5630 </variablelist>
5631 </listitem>
5632 </varlistentry>
5634 <varlistentry id="protocol-message-formats-SSLRequest">
5635 <term>SSLRequest (F)</term>
5636 <listitem>
5637 <variablelist>
5638 <varlistentry>
5639 <term>Int32(8)</term>
5640 <listitem>
5641 <para>
5642 Length of message contents in bytes, including self.
5643 </para>
5644 </listitem>
5645 </varlistentry>
5647 <varlistentry>
5648 <term>Int32(80877103)</term>
5649 <listitem>
5650 <para>
5651 The <acronym>SSL</acronym> request code. The value is chosen to contain
5652 <literal>1234</literal> in the most significant 16 bits, and <literal>5679</literal> in the
5653 least significant 16 bits. (To avoid confusion, this code
5654 must not be the same as any protocol version number.)
5655 </para>
5656 </listitem>
5657 </varlistentry>
5658 </variablelist>
5659 </listitem>
5660 </varlistentry>
5662 <varlistentry id="protocol-message-formats-StartupMessage">
5663 <term>StartupMessage (F)</term>
5664 <listitem>
5665 <variablelist>
5666 <varlistentry>
5667 <term>Int32</term>
5668 <listitem>
5669 <para>
5670 Length of message contents in bytes, including self.
5671 </para>
5672 </listitem>
5673 </varlistentry>
5675 <varlistentry>
5676 <term>Int32(196608)</term>
5677 <listitem>
5678 <para>
5679 The protocol version number. The most significant 16 bits are
5680 the major version number (3 for the protocol described here).
5681 The least significant 16 bits are the minor version number
5682 (0 for the protocol described here).
5683 </para>
5684 </listitem>
5685 </varlistentry>
5686 </variablelist>
5688 <para>
5689 The protocol version number is followed by one or more pairs of
5690 parameter name and value strings. A zero byte is required as a
5691 terminator after the last name/value pair.
5692 Parameters can appear in any
5693 order. <literal>user</literal> is required, others are optional.
5694 Each parameter is specified as:
5695 </para>
5697 <variablelist>
5698 <varlistentry>
5699 <term>String</term>
5700 <listitem>
5701 <para>
5702 The parameter name. Currently recognized names are:
5704 <variablelist>
5705 <varlistentry>
5706 <term><literal>user</literal></term>
5707 <listitem>
5708 <para>
5709 The database user name to connect as. Required;
5710 there is no default.
5711 </para>
5712 </listitem>
5713 </varlistentry>
5715 <varlistentry>
5716 <term><literal>database</literal></term>
5717 <listitem>
5718 <para>
5719 The database to connect to. Defaults to the user name.
5720 </para>
5721 </listitem>
5722 </varlistentry>
5724 <varlistentry>
5725 <term><literal>options</literal></term>
5726 <listitem>
5727 <para>
5728 Command-line arguments for the backend. (This is
5729 deprecated in favor of setting individual run-time
5730 parameters.) Spaces within this string are
5731 considered to separate arguments, unless escaped with
5732 a backslash (<literal>\</literal>); write <literal>\\</literal> to
5733 represent a literal backslash.
5734 </para>
5735 </listitem>
5736 </varlistentry>
5738 <varlistentry>
5739 <term><literal>replication</literal></term>
5740 <listitem>
5741 <para>
5742 Used to connect in streaming replication mode, where
5743 a small set of replication commands can be issued
5744 instead of SQL statements. Value can be
5745 <literal>true</literal>, <literal>false</literal>, or
5746 <literal>database</literal>, and the default is
5747 <literal>false</literal>. See
5748 <xref linkend="protocol-replication"/> for details.
5749 </para>
5750 </listitem>
5751 </varlistentry>
5752 </variablelist>
5754 In addition to the above, other parameters may be listed.
5755 Parameter names beginning with <literal>_pq_.</literal> are
5756 reserved for use as protocol extensions, while others are
5757 treated as run-time parameters to be set at backend start
5758 time. Such settings will be applied during backend start
5759 (after parsing the command-line arguments if any) and will
5760 act as session defaults.
5761 </para>
5762 </listitem>
5763 </varlistentry>
5765 <varlistentry>
5766 <term>String</term>
5767 <listitem>
5768 <para>
5769 The parameter value.
5770 </para>
5771 </listitem>
5772 </varlistentry>
5773 </variablelist>
5774 </listitem>
5775 </varlistentry>
5777 <varlistentry id="protocol-message-formats-Sync">
5778 <term>Sync (F)</term>
5779 <listitem>
5780 <variablelist>
5781 <varlistentry>
5782 <term>Byte1('S')</term>
5783 <listitem>
5784 <para>
5785 Identifies the message as a Sync command.
5786 </para>
5787 </listitem>
5788 </varlistentry>
5790 <varlistentry>
5791 <term>Int32(4)</term>
5792 <listitem>
5793 <para>
5794 Length of message contents in bytes, including self.
5795 </para>
5796 </listitem>
5797 </varlistentry>
5798 </variablelist>
5799 </listitem>
5800 </varlistentry>
5802 <varlistentry id="protocol-message-formats-Terminate">
5803 <term>Terminate (F)</term>
5804 <listitem>
5805 <variablelist>
5806 <varlistentry>
5807 <term>Byte1('X')</term>
5808 <listitem>
5809 <para>
5810 Identifies the message as a termination.
5811 </para>
5812 </listitem>
5813 </varlistentry>
5815 <varlistentry>
5816 <term>Int32(4)</term>
5817 <listitem>
5818 <para>
5819 Length of message contents in bytes, including self.
5820 </para>
5821 </listitem>
5822 </varlistentry>
5823 </variablelist>
5824 </listitem>
5825 </varlistentry>
5826 </variablelist>
5827 </sect1>
5829 <sect1 id="protocol-error-fields">
5830 <title>Error and Notice Message Fields</title>
5832 <para>
5833 This section describes the fields that can appear in ErrorResponse and
5834 NoticeResponse messages. Each field type has a single-byte identification
5835 token. Note that any given field type should appear at most once per
5836 message.
5837 </para>
5839 <variablelist>
5840 <varlistentry>
5841 <term><literal>S</literal></term>
5842 <listitem>
5843 <para>
5844 Severity: the field contents are
5845 <literal>ERROR</literal>, <literal>FATAL</literal>, or
5846 <literal>PANIC</literal> (in an error message), or
5847 <literal>WARNING</literal>, <literal>NOTICE</literal>, <literal>DEBUG</literal>,
5848 <literal>INFO</literal>, or <literal>LOG</literal> (in a notice message),
5849 or a localized translation of one of these. Always present.
5850 </para>
5851 </listitem>
5852 </varlistentry>
5854 <varlistentry>
5855 <term><literal>V</literal></term>
5856 <listitem>
5857 <para>
5858 Severity: the field contents are
5859 <literal>ERROR</literal>, <literal>FATAL</literal>, or
5860 <literal>PANIC</literal> (in an error message), or
5861 <literal>WARNING</literal>, <literal>NOTICE</literal>, <literal>DEBUG</literal>,
5862 <literal>INFO</literal>, or <literal>LOG</literal> (in a notice message).
5863 This is identical to the <literal>S</literal> field except
5864 that the contents are never localized. This is present only in
5865 messages generated by <productname>PostgreSQL</productname> versions 9.6
5866 and later.
5867 </para>
5868 </listitem>
5869 </varlistentry>
5871 <varlistentry>
5872 <term><literal>C</literal></term>
5873 <listitem>
5874 <para>
5875 Code: the SQLSTATE code for the error (see <xref
5876 linkend="errcodes-appendix"/>). Not localizable. Always present.
5877 </para>
5878 </listitem>
5879 </varlistentry>
5881 <varlistentry>
5882 <term><literal>M</literal></term>
5883 <listitem>
5884 <para>
5885 Message: the primary human-readable error message.
5886 This should be accurate but terse (typically one line).
5887 Always present.
5888 </para>
5889 </listitem>
5890 </varlistentry>
5892 <varlistentry>
5893 <term><literal>D</literal></term>
5894 <listitem>
5895 <para>
5896 Detail: an optional secondary error message carrying more
5897 detail about the problem. Might run to multiple lines.
5898 </para>
5899 </listitem>
5900 </varlistentry>
5902 <varlistentry>
5903 <term><literal>H</literal></term>
5904 <listitem>
5905 <para>
5906 Hint: an optional suggestion what to do about the problem.
5907 This is intended to differ from Detail in that it offers advice
5908 (potentially inappropriate) rather than hard facts.
5909 Might run to multiple lines.
5910 </para>
5911 </listitem>
5912 </varlistentry>
5914 <varlistentry>
5915 <term><literal>P</literal></term>
5916 <listitem>
5917 <para>
5918 Position: the field value is a decimal ASCII integer, indicating
5919 an error cursor position as an index into the original query string.
5920 The first character has index 1, and positions are measured in
5921 characters not bytes.
5922 </para>
5923 </listitem>
5924 </varlistentry>
5926 <varlistentry>
5927 <term><literal>p</literal></term>
5928 <listitem>
5929 <para>
5930 Internal position: this is defined the same as the <literal>P</literal>
5931 field, but it is used when the cursor position refers to an internally
5932 generated command rather than the one submitted by the client.
5933 The <literal>q</literal> field will always appear when this field appears.
5934 </para>
5935 </listitem>
5936 </varlistentry>
5938 <varlistentry>
5939 <term><literal>q</literal></term>
5940 <listitem>
5941 <para>
5942 Internal query: the text of a failed internally-generated command.
5943 This could be, for example, an SQL query issued by a PL/pgSQL function.
5944 </para>
5945 </listitem>
5946 </varlistentry>
5948 <varlistentry>
5949 <term><literal>W</literal></term>
5950 <listitem>
5951 <para>
5952 Where: an indication of the context in which the error occurred.
5953 Presently this includes a call stack traceback of active
5954 procedural language functions and internally-generated queries.
5955 The trace is one entry per line, most recent first.
5956 </para>
5957 </listitem>
5958 </varlistentry>
5960 <varlistentry>
5961 <term><literal>s</literal></term>
5962 <listitem>
5963 <para>
5964 Schema name: if the error was associated with a specific database
5965 object, the name of the schema containing that object, if any.
5966 </para>
5967 </listitem>
5968 </varlistentry>
5970 <varlistentry>
5971 <term><literal>t</literal></term>
5972 <listitem>
5973 <para>
5974 Table name: if the error was associated with a specific table, the
5975 name of the table. (Refer to the schema name field for the name of
5976 the table's schema.)
5977 </para>
5978 </listitem>
5979 </varlistentry>
5981 <varlistentry>
5982 <term><literal>c</literal></term>
5983 <listitem>
5984 <para>
5985 Column name: if the error was associated with a specific table column,
5986 the name of the column. (Refer to the schema and table name fields to
5987 identify the table.)
5988 </para>
5989 </listitem>
5990 </varlistentry>
5992 <varlistentry>
5993 <term><literal>d</literal></term>
5994 <listitem>
5995 <para>
5996 Data type name: if the error was associated with a specific data type,
5997 the name of the data type. (Refer to the schema name field for the
5998 name of the data type's schema.)
5999 </para>
6000 </listitem>
6001 </varlistentry>
6003 <varlistentry>
6004 <term><literal>n</literal></term>
6005 <listitem>
6006 <para>
6007 Constraint name: if the error was associated with a specific
6008 constraint, the name of the constraint. Refer to fields listed above
6009 for the associated table or domain. (For this purpose, indexes are
6010 treated as constraints, even if they weren't created with constraint
6011 syntax.)
6012 </para>
6013 </listitem>
6014 </varlistentry>
6016 <varlistentry>
6017 <term><literal>F</literal></term>
6018 <listitem>
6019 <para>
6020 File: the file name of the source-code location where the error
6021 was reported.
6022 </para>
6023 </listitem>
6024 </varlistentry>
6026 <varlistentry>
6027 <term><literal>L</literal></term>
6028 <listitem>
6029 <para>
6030 Line: the line number of the source-code location where the error
6031 was reported.
6032 </para>
6033 </listitem>
6034 </varlistentry>
6036 <varlistentry>
6037 <term><literal>R</literal></term>
6038 <listitem>
6039 <para>
6040 Routine: the name of the source-code routine reporting the error.
6041 </para>
6042 </listitem>
6043 </varlistentry>
6044 </variablelist>
6046 <note>
6047 <para>
6048 The fields for schema name, table name, column name, data type name, and
6049 constraint name are supplied only for a limited number of error types;
6050 see <xref linkend="errcodes-appendix"/>. Frontends should not assume that
6051 the presence of any of these fields guarantees the presence of another
6052 field. Core error sources observe the interrelationships noted above, but
6053 user-defined functions may use these fields in other ways. In the same
6054 vein, clients should not assume that these fields denote contemporary
6055 objects in the current database.
6056 </para>
6057 </note>
6059 <para>
6060 The client is responsible for formatting displayed information to meet its
6061 needs; in particular it should break long lines as needed. Newline characters
6062 appearing in the error message fields should be treated as paragraph breaks,
6063 not line breaks.
6064 </para>
6065 </sect1>
6067 <sect1 id="protocol-logicalrep-message-formats">
6068 <title>Logical Replication Message Formats</title>
6070 <para>
6071 This section describes the detailed format of each logical replication
6072 message. These messages are either returned by the replication slot SQL
6073 interface or are sent by a walsender. In the case of a walsender, they are
6074 encapsulated inside replication protocol WAL messages as described in
6075 <xref linkend="protocol-replication"/>, and generally obey the same message
6076 flow as physical replication.
6077 </para>
6079 <variablelist>
6080 <varlistentry id="protocol-logicalrep-message-formats-Begin">
6081 <term>Begin</term>
6082 <listitem>
6083 <variablelist>
6084 <varlistentry>
6085 <term>Byte1('B')</term>
6086 <listitem>
6087 <para>
6088 Identifies the message as a begin message.
6089 </para>
6090 </listitem>
6091 </varlistentry>
6093 <varlistentry>
6094 <term>Int64 (XLogRecPtr)</term>
6095 <listitem>
6096 <para>
6097 The final LSN of the transaction.
6098 </para>
6099 </listitem>
6100 </varlistentry>
6102 <varlistentry>
6103 <term>Int64 (TimestampTz)</term>
6104 <listitem>
6105 <para>
6106 Commit timestamp of the transaction. The value is in number
6107 of microseconds since PostgreSQL epoch (2000-01-01).
6108 </para>
6109 </listitem>
6110 </varlistentry>
6112 <varlistentry>
6113 <term>Int32 (TransactionId)</term>
6114 <listitem>
6115 <para>
6116 Xid of the transaction.
6117 </para>
6118 </listitem>
6119 </varlistentry>
6120 </variablelist>
6121 </listitem>
6122 </varlistentry>
6124 <varlistentry id="protocol-logicalrep-message-formats-Message">
6125 <term>Message</term>
6126 <listitem>
6127 <variablelist>
6128 <varlistentry>
6129 <term>Byte1('M')</term>
6130 <listitem>
6131 <para>
6132 Identifies the message as a logical decoding message.
6133 </para>
6134 </listitem>
6135 </varlistentry>
6137 <varlistentry>
6138 <term>Int32 (TransactionId)</term>
6139 <listitem>
6140 <para>
6141 Xid of the transaction (only present for streamed transactions).
6142 This field is available since protocol version 2.
6143 </para>
6144 </listitem>
6145 </varlistentry>
6147 <varlistentry>
6148 <term>Int8</term>
6149 <listitem>
6150 <para>
6151 Flags; Either 0 for no flags or 1 if the logical decoding
6152 message is transactional.
6153 </para>
6154 </listitem>
6155 </varlistentry>
6157 <varlistentry>
6158 <term>Int64 (XLogRecPtr)</term>
6159 <listitem>
6160 <para>
6161 The LSN of the logical decoding message.
6162 </para>
6163 </listitem>
6164 </varlistentry>
6166 <varlistentry>
6167 <term>String</term>
6168 <listitem>
6169 <para>
6170 The prefix of the logical decoding message.
6171 </para>
6172 </listitem>
6173 </varlistentry>
6175 <varlistentry>
6176 <term>Int32</term>
6177 <listitem>
6178 <para>
6179 Length of the content.
6180 </para>
6181 </listitem>
6182 </varlistentry>
6184 <varlistentry>
6185 <term>Byte<replaceable>n</replaceable></term>
6186 <listitem>
6187 <para>
6188 The content of the logical decoding message.
6189 </para>
6190 </listitem>
6191 </varlistentry>
6192 </variablelist>
6193 </listitem>
6194 </varlistentry>
6196 <varlistentry id="protocol-logicalrep-message-formats-Commit">
6197 <term>Commit</term>
6198 <listitem>
6199 <variablelist>
6200 <varlistentry>
6201 <term>Byte1('C')</term>
6202 <listitem>
6203 <para>
6204 Identifies the message as a commit message.
6205 </para>
6206 </listitem>
6207 </varlistentry>
6209 <varlistentry>
6210 <term>Int8(0)</term>
6211 <listitem>
6212 <para>
6213 Flags; currently unused.
6214 </para>
6215 </listitem>
6216 </varlistentry>
6218 <varlistentry>
6219 <term>Int64 (XLogRecPtr)</term>
6220 <listitem>
6221 <para>
6222 The LSN of the commit.
6223 </para>
6224 </listitem>
6225 </varlistentry>
6227 <varlistentry>
6228 <term>Int64 (XLogRecPtr)</term>
6229 <listitem>
6230 <para>
6231 The end LSN of the transaction.
6232 </para>
6233 </listitem>
6234 </varlistentry>
6236 <varlistentry>
6237 <term>Int64 (TimestampTz)</term>
6238 <listitem>
6239 <para>
6240 Commit timestamp of the transaction. The value is in number
6241 of microseconds since PostgreSQL epoch (2000-01-01).
6242 </para>
6243 </listitem>
6244 </varlistentry>
6245 </variablelist>
6246 </listitem>
6247 </varlistentry>
6249 <varlistentry id="protocol-logicalrep-message-formats-Origin">
6250 <term>Origin</term>
6251 <listitem>
6252 <variablelist>
6253 <varlistentry>
6254 <term>Byte1('O')</term>
6255 <listitem>
6256 <para>
6257 Identifies the message as an origin message.
6258 </para>
6259 </listitem>
6260 </varlistentry>
6262 <varlistentry>
6263 <term>Int64 (XLogRecPtr)</term>
6264 <listitem>
6265 <para>
6266 The LSN of the commit on the origin server.
6267 </para>
6268 </listitem>
6269 </varlistentry>
6271 <varlistentry>
6272 <term>String</term>
6273 <listitem>
6274 <para>
6275 Name of the origin.
6276 </para>
6277 </listitem>
6278 </varlistentry>
6279 </variablelist>
6281 <para>
6282 Note that there can be multiple Origin messages inside a single transaction.
6283 </para>
6284 </listitem>
6285 </varlistentry>
6287 <varlistentry id="protocol-logicalrep-message-formats-Relation">
6288 <term>Relation</term>
6289 <listitem>
6290 <variablelist>
6291 <varlistentry>
6292 <term>Byte1('R')</term>
6293 <listitem>
6294 <para>
6295 Identifies the message as a relation message.
6296 </para>
6297 </listitem>
6298 </varlistentry>
6300 <varlistentry>
6301 <term>Int32 (TransactionId)</term>
6302 <listitem>
6303 <para>
6304 Xid of the transaction (only present for streamed transactions).
6305 This field is available since protocol version 2.
6306 </para>
6307 </listitem>
6308 </varlistentry>
6310 <varlistentry>
6311 <term>Int32 (Oid)</term>
6312 <listitem>
6313 <para>
6314 OID of the relation.
6315 </para>
6316 </listitem>
6317 </varlistentry>
6319 <varlistentry>
6320 <term>String</term>
6321 <listitem>
6322 <para>
6323 Namespace (empty string for <literal>pg_catalog</literal>).
6324 </para>
6325 </listitem>
6326 </varlistentry>
6328 <varlistentry>
6329 <term>String</term>
6330 <listitem>
6331 <para>
6332 Relation name.
6333 </para>
6334 </listitem>
6335 </varlistentry>
6337 <varlistentry>
6338 <term>Int8</term>
6339 <listitem>
6340 <para>
6341 Replica identity setting for the relation (same as
6342 <structfield>relreplident</structfield> in <structname>pg_class</structname>).
6343 </para>
6344 </listitem>
6345 </varlistentry>
6347 <varlistentry>
6348 <term>Int16</term>
6349 <listitem>
6350 <para>
6351 Number of columns.
6352 </para>
6353 </listitem>
6354 </varlistentry>
6355 </variablelist>
6357 <para>
6358 Next, the following message part appears for each column included in
6359 the publication (except generated columns):
6360 </para>
6362 <variablelist>
6363 <varlistentry>
6364 <term>Int8</term>
6365 <listitem>
6366 <para>
6367 Flags for the column. Currently can be either 0 for no flags
6368 or 1 which marks the column as part of the key.
6369 </para>
6370 </listitem>
6371 </varlistentry>
6373 <varlistentry>
6374 <term>String</term>
6375 <listitem>
6376 <para>
6377 Name of the column.
6378 </para>
6379 </listitem>
6380 </varlistentry>
6382 <varlistentry>
6383 <term>Int32 (Oid)</term>
6384 <listitem>
6385 <para>
6386 OID of the column's data type.
6387 </para>
6388 </listitem>
6389 </varlistentry>
6391 <varlistentry>
6392 <term>Int32</term>
6393 <listitem>
6394 <para>
6395 Type modifier of the column (<structfield>atttypmod</structfield>).
6396 </para>
6397 </listitem>
6398 </varlistentry>
6399 </variablelist>
6400 </listitem>
6401 </varlistentry>
6403 <varlistentry id="protocol-logicalrep-message-formats-Type">
6404 <term>Type</term>
6405 <listitem>
6406 <variablelist>
6407 <varlistentry>
6408 <term>Byte1('Y')</term>
6409 <listitem>
6410 <para>
6411 Identifies the message as a type message.
6412 </para>
6413 </listitem>
6414 </varlistentry>
6416 <varlistentry>
6417 <term>Int32 (TransactionId)</term>
6418 <listitem>
6419 <para>
6420 Xid of the transaction (only present for streamed transactions).
6421 This field is available since protocol version 2.
6422 </para>
6423 </listitem>
6424 </varlistentry>
6426 <varlistentry>
6427 <term>Int32 (Oid)</term>
6428 <listitem>
6429 <para>
6430 OID of the data type.
6431 </para>
6432 </listitem>
6433 </varlistentry>
6435 <varlistentry>
6436 <term>String</term>
6437 <listitem>
6438 <para>
6439 Namespace (empty string for <literal>pg_catalog</literal>).
6440 </para>
6441 </listitem>
6442 </varlistentry>
6444 <varlistentry>
6445 <term>String</term>
6446 <listitem>
6447 <para>
6448 Name of the data type.
6449 </para>
6450 </listitem>
6451 </varlistentry>
6452 </variablelist>
6453 </listitem>
6454 </varlistentry>
6456 <varlistentry id="protocol-logicalrep-message-formats-Insert">
6457 <term>Insert</term>
6458 <listitem>
6459 <variablelist>
6460 <varlistentry>
6461 <term>Byte1('I')</term>
6462 <listitem>
6463 <para>
6464 Identifies the message as an insert message.
6465 </para>
6466 </listitem>
6467 </varlistentry>
6469 <varlistentry>
6470 <term>Int32 (TransactionId)</term>
6471 <listitem>
6472 <para>
6473 Xid of the transaction (only present for streamed transactions).
6474 This field is available since protocol version 2.
6475 </para>
6476 </listitem>
6477 </varlistentry>
6479 <varlistentry>
6480 <term>Int32 (Oid)</term>
6481 <listitem>
6482 <para>
6483 OID of the relation corresponding to the ID in the relation
6484 message.
6485 </para>
6486 </listitem>
6487 </varlistentry>
6489 <varlistentry>
6490 <term>Byte1('N')</term>
6491 <listitem>
6492 <para>
6493 Identifies the following TupleData message as a new tuple.
6494 </para>
6495 </listitem>
6496 </varlistentry>
6498 <varlistentry>
6499 <term>TupleData</term>
6500 <listitem>
6501 <para>
6502 TupleData message part representing the contents of new tuple.
6503 </para>
6504 </listitem>
6505 </varlistentry>
6506 </variablelist>
6507 </listitem>
6508 </varlistentry>
6510 <varlistentry id="protocol-logicalrep-message-formats-Update">
6511 <term>Update</term>
6512 <listitem>
6513 <variablelist>
6514 <varlistentry>
6515 <term>Byte1('U')</term>
6516 <listitem>
6517 <para>
6518 Identifies the message as an update message.
6519 </para>
6520 </listitem>
6521 </varlistentry>
6523 <varlistentry>
6524 <term>Int32 (TransactionId)</term>
6525 <listitem>
6526 <para>
6527 Xid of the transaction (only present for streamed transactions).
6528 This field is available since protocol version 2.
6529 </para>
6530 </listitem>
6531 </varlistentry>
6533 <varlistentry>
6534 <term>Int32 (Oid)</term>
6535 <listitem>
6536 <para>
6537 OID of the relation corresponding to the ID in the relation
6538 message.
6539 </para>
6540 </listitem>
6541 </varlistentry>
6543 <varlistentry>
6544 <term>Byte1('K')</term>
6545 <listitem>
6546 <para>
6547 Identifies the following TupleData submessage as a key.
6548 This field is optional and is only present if
6549 the update changed data in any of the column(s) that are
6550 part of the REPLICA IDENTITY index.
6551 </para>
6552 </listitem>
6553 </varlistentry>
6555 <varlistentry>
6556 <term>Byte1('O')</term>
6557 <listitem>
6558 <para>
6559 Identifies the following TupleData submessage as an old tuple.
6560 This field is optional and is only present if table in which
6561 the update happened has REPLICA IDENTITY set to FULL.
6562 </para>
6563 </listitem>
6564 </varlistentry>
6566 <varlistentry>
6567 <term>TupleData</term>
6568 <listitem>
6569 <para>
6570 TupleData message part representing the contents of the old tuple
6571 or primary key. Only present if the previous 'O' or 'K' part
6572 is present.
6573 </para>
6574 </listitem>
6575 </varlistentry>
6577 <varlistentry>
6578 <term>Byte1('N')</term>
6579 <listitem>
6580 <para>
6581 Identifies the following TupleData message as a new tuple.
6582 </para>
6583 </listitem>
6584 </varlistentry>
6586 <varlistentry>
6587 <term>TupleData</term>
6588 <listitem>
6589 <para>
6590 TupleData message part representing the contents of a new tuple.
6591 </para>
6592 </listitem>
6593 </varlistentry>
6594 </variablelist>
6596 <para>
6597 The Update message may contain either a 'K' message part or an 'O' message part
6598 or neither of them, but never both of them.
6599 </para>
6600 </listitem>
6601 </varlistentry>
6603 <varlistentry id="protocol-logicalrep-message-formats-Delete">
6604 <term>Delete</term>
6605 <listitem>
6606 <variablelist>
6607 <varlistentry>
6608 <term>Byte1('D')</term>
6609 <listitem>
6610 <para>
6611 Identifies the message as a delete message.
6612 </para>
6613 </listitem>
6614 </varlistentry>
6616 <varlistentry>
6617 <term>Int32 (TransactionId)</term>
6618 <listitem>
6619 <para>
6620 Xid of the transaction (only present for streamed transactions).
6621 This field is available since protocol version 2.
6622 </para>
6623 </listitem>
6624 </varlistentry>
6626 <varlistentry>
6627 <term>Int32 (Oid)</term>
6628 <listitem>
6629 <para>
6630 OID of the relation corresponding to the ID in the relation
6631 message.
6632 </para>
6633 </listitem>
6634 </varlistentry>
6636 <varlistentry>
6637 <term>Byte1('K')</term>
6638 <listitem>
6639 <para>
6640 Identifies the following TupleData submessage as a key.
6641 This field is present if the table in which the delete has
6642 happened uses an index as REPLICA IDENTITY.
6643 </para>
6644 </listitem>
6645 </varlistentry>
6647 <varlistentry>
6648 <term>Byte1('O')</term>
6649 <listitem>
6650 <para>
6651 Identifies the following TupleData message as an old tuple.
6652 This field is present if the table in which the delete
6653 happened has REPLICA IDENTITY set to FULL.
6654 </para>
6655 </listitem>
6656 </varlistentry>
6658 <varlistentry>
6659 <term>TupleData</term>
6660 <listitem>
6661 <para>
6662 TupleData message part representing the contents of the old tuple
6663 or primary key, depending on the previous field.
6664 </para>
6665 </listitem>
6666 </varlistentry>
6667 </variablelist>
6669 <para>
6670 The Delete message may contain either a 'K' message part or an 'O' message part,
6671 but never both of them.
6672 </para>
6673 </listitem>
6674 </varlistentry>
6676 <varlistentry id="protocol-logicalrep-message-formats-Truncate">
6677 <term>Truncate</term>
6678 <listitem>
6679 <variablelist>
6680 <varlistentry>
6681 <term>Byte1('T')</term>
6682 <listitem>
6683 <para>
6684 Identifies the message as a truncate message.
6685 </para>
6686 </listitem>
6687 </varlistentry>
6689 <varlistentry>
6690 <term>Int32 (TransactionId)</term>
6691 <listitem>
6692 <para>
6693 Xid of the transaction (only present for streamed transactions).
6694 This field is available since protocol version 2.
6695 </para>
6696 </listitem>
6697 </varlistentry>
6699 <varlistentry>
6700 <term>Int32</term>
6701 <listitem>
6702 <para>
6703 Number of relations
6704 </para>
6705 </listitem>
6706 </varlistentry>
6708 <varlistentry>
6709 <term>Int8</term>
6710 <listitem>
6711 <para>
6712 Option bits for <command>TRUNCATE</command>:
6713 1 for <literal>CASCADE</literal>, 2 for <literal>RESTART IDENTITY</literal>
6714 </para>
6715 </listitem>
6716 </varlistentry>
6718 <varlistentry>
6719 <term>Int32 (Oid)</term>
6720 <listitem>
6721 <para>
6722 OID of the relation corresponding to the ID in the relation
6723 message. This field is repeated for each relation.
6724 </para>
6725 </listitem>
6726 </varlistentry>
6727 </variablelist>
6728 </listitem>
6729 </varlistentry>
6730 </variablelist>
6732 <para>
6733 The following messages (Stream Start, Stream Stop, Stream Commit, and
6734 Stream Abort) are available since protocol version 2.
6735 </para>
6737 <variablelist>
6738 <varlistentry id="protocol-logicalrep-message-formats-Stream-Start">
6739 <term>Stream Start</term>
6740 <listitem>
6741 <variablelist>
6742 <varlistentry>
6743 <term>Byte1('S')</term>
6744 <listitem>
6745 <para>
6746 Identifies the message as a stream start message.
6747 </para>
6748 </listitem>
6749 </varlistentry>
6751 <varlistentry>
6752 <term>Int32 (TransactionId)</term>
6753 <listitem>
6754 <para>
6755 Xid of the transaction.
6756 </para>
6757 </listitem>
6758 </varlistentry>
6760 <varlistentry>
6761 <term>Int8</term>
6762 <listitem>
6763 <para>
6764 A value of 1 indicates this is the first stream segment for
6765 this XID, 0 for any other stream segment.
6766 </para>
6767 </listitem>
6768 </varlistentry>
6769 </variablelist>
6770 </listitem>
6771 </varlistentry>
6773 <varlistentry id="protocol-logicalrep-message-formats-Stream-Stop">
6774 <term>Stream Stop</term>
6775 <listitem>
6776 <variablelist>
6777 <varlistentry>
6778 <term>Byte1('E')</term>
6779 <listitem>
6780 <para>
6781 Identifies the message as a stream stop message.
6782 </para>
6783 </listitem>
6784 </varlistentry>
6785 </variablelist>
6786 </listitem>
6787 </varlistentry>
6789 <varlistentry id="protocol-logicalrep-message-formats-Stream-Commit">
6790 <term>Stream Commit</term>
6791 <listitem>
6792 <variablelist>
6793 <varlistentry>
6794 <term>Byte1('c')</term>
6795 <listitem>
6796 <para>
6797 Identifies the message as a stream commit message.
6798 </para>
6799 </listitem>
6800 </varlistentry>
6802 <varlistentry>
6803 <term>Int32 (TransactionId)</term>
6804 <listitem>
6805 <para>
6806 Xid of the transaction.
6807 </para>
6808 </listitem>
6809 </varlistentry>
6811 <varlistentry>
6812 <term>Int8(0)</term>
6813 <listitem>
6814 <para>
6815 Flags; currently unused.
6816 </para>
6817 </listitem>
6818 </varlistentry>
6820 <varlistentry>
6821 <term>Int64 (XLogRecPtr)</term>
6822 <listitem>
6823 <para>
6824 The LSN of the commit.
6825 </para>
6826 </listitem>
6827 </varlistentry>
6829 <varlistentry>
6830 <term>Int64 (XLogRecPtr)</term>
6831 <listitem>
6832 <para>
6833 The end LSN of the transaction.
6834 </para>
6835 </listitem>
6836 </varlistentry>
6838 <varlistentry>
6839 <term>Int64 (TimestampTz)</term>
6840 <listitem>
6841 <para>
6842 Commit timestamp of the transaction. The value is in number
6843 of microseconds since PostgreSQL epoch (2000-01-01).
6844 </para>
6845 </listitem>
6846 </varlistentry>
6847 </variablelist>
6848 </listitem>
6849 </varlistentry>
6851 <varlistentry id="protocol-logicalrep-message-formats-Stream-Abort">
6852 <term>Stream Abort</term>
6853 <listitem>
6854 <variablelist>
6855 <varlistentry>
6856 <term>Byte1('A')</term>
6857 <listitem>
6858 <para>
6859 Identifies the message as a stream abort message.
6860 </para>
6861 </listitem>
6862 </varlistentry>
6864 <varlistentry>
6865 <term>Int32 (TransactionId)</term>
6866 <listitem>
6867 <para>
6868 Xid of the transaction.
6869 </para>
6870 </listitem>
6871 </varlistentry>
6873 <varlistentry>
6874 <term>Int32 (TransactionId)</term>
6875 <listitem>
6876 <para>
6877 Xid of the subtransaction (will be same as xid of the transaction for top-level
6878 transactions).
6879 </para>
6880 </listitem>
6881 </varlistentry>
6883 <varlistentry>
6884 <term>Int64 (XLogRecPtr)</term>
6885 <listitem>
6886 <para>
6887 The LSN of the abort. This field is available since protocol version
6889 </para>
6890 </listitem>
6891 </varlistentry>
6893 <varlistentry>
6894 <term>Int64 (TimestampTz)</term>
6895 <listitem>
6896 <para>
6897 Abort timestamp of the transaction. The value is in number
6898 of microseconds since PostgreSQL epoch (2000-01-01). This field is
6899 available since protocol version 4.
6900 </para>
6901 </listitem>
6902 </varlistentry>
6904 </variablelist>
6905 </listitem>
6906 </varlistentry>
6907 </variablelist>
6909 <para>
6910 The following messages (Begin Prepare, Prepare, Commit Prepared, Rollback Prepared, Stream Prepare)
6911 are available since protocol version 3.
6912 </para>
6914 <variablelist>
6915 <varlistentry id="protocol-logicalrep-message-formats-Begin-Prepare">
6916 <term>Begin Prepare</term>
6917 <listitem>
6918 <variablelist>
6919 <varlistentry>
6920 <term>Byte1('b')</term>
6921 <listitem>
6922 <para>
6923 Identifies the message as the beginning of a prepared transaction message.
6924 </para>
6925 </listitem>
6926 </varlistentry>
6928 <varlistentry>
6929 <term>Int64 (XLogRecPtr)</term>
6930 <listitem>
6931 <para>
6932 The LSN of the prepare.
6933 </para>
6934 </listitem>
6935 </varlistentry>
6937 <varlistentry>
6938 <term>Int64 (XLogRecPtr)</term>
6939 <listitem>
6940 <para>
6941 The end LSN of the prepared transaction.
6942 </para>
6943 </listitem>
6944 </varlistentry>
6946 <varlistentry>
6947 <term>Int64 (TimestampTz)</term>
6948 <listitem>
6949 <para>
6950 Prepare timestamp of the transaction. The value is in number
6951 of microseconds since PostgreSQL epoch (2000-01-01).
6952 </para>
6953 </listitem>
6954 </varlistentry>
6956 <varlistentry>
6957 <term>Int32 (TransactionId)</term>
6958 <listitem>
6959 <para>
6960 Xid of the transaction.
6961 </para>
6962 </listitem>
6963 </varlistentry>
6965 <varlistentry>
6966 <term>String</term>
6967 <listitem>
6968 <para>
6969 The user defined GID of the prepared transaction.
6970 </para>
6971 </listitem>
6972 </varlistentry>
6973 </variablelist>
6974 </listitem>
6975 </varlistentry>
6977 <varlistentry id="protocol-logicalrep-message-formats-Prepare">
6978 <term>Prepare</term>
6979 <listitem>
6980 <variablelist>
6981 <varlistentry>
6982 <term>Byte1('P')</term>
6983 <listitem>
6984 <para>
6985 Identifies the message as a prepared transaction message.
6986 </para>
6987 </listitem>
6988 </varlistentry>
6990 <varlistentry>
6991 <term>Int8(0)</term>
6992 <listitem>
6993 <para>
6994 Flags; currently unused.
6995 </para>
6996 </listitem>
6997 </varlistentry>
6999 <varlistentry>
7000 <term>Int64 (XLogRecPtr)</term>
7001 <listitem>
7002 <para>
7003 The LSN of the prepare.
7004 </para>
7005 </listitem>
7006 </varlistentry>
7008 <varlistentry>
7009 <term>Int64 (XLogRecPtr)</term>
7010 <listitem>
7011 <para>
7012 The end LSN of the prepared transaction.
7013 </para>
7014 </listitem>
7015 </varlistentry>
7017 <varlistentry>
7018 <term>Int64 (TimestampTz)</term>
7019 <listitem>
7020 <para>
7021 Prepare timestamp of the transaction. The value is in number
7022 of microseconds since PostgreSQL epoch (2000-01-01).
7023 </para>
7024 </listitem>
7025 </varlistentry>
7027 <varlistentry>
7028 <term>Int32 (TransactionId)</term>
7029 <listitem>
7030 <para>
7031 Xid of the transaction.
7032 </para>
7033 </listitem>
7034 </varlistentry>
7036 <varlistentry>
7037 <term>String</term>
7038 <listitem>
7039 <para>
7040 The user defined GID of the prepared transaction.
7041 </para>
7042 </listitem>
7043 </varlistentry>
7044 </variablelist>
7045 </listitem>
7046 </varlistentry>
7048 <varlistentry id="protocol-logicalrep-message-formats-Commit-Prepared">
7049 <term>Commit Prepared</term>
7050 <listitem>
7051 <variablelist>
7052 <varlistentry>
7053 <term>Byte1('K')</term>
7054 <listitem>
7055 <para>
7056 Identifies the message as the commit of a prepared transaction message.
7057 </para>
7058 </listitem>
7059 </varlistentry>
7061 <varlistentry>
7062 <term>Int8(0)</term>
7063 <listitem>
7064 <para>
7065 Flags; currently unused.
7066 </para>
7067 </listitem>
7068 </varlistentry>
7070 <varlistentry>
7071 <term>Int64 (XLogRecPtr)</term>
7072 <listitem>
7073 <para>
7074 The LSN of the commit of the prepared transaction.
7075 </para>
7076 </listitem>
7077 </varlistentry>
7079 <varlistentry>
7080 <term>Int64 (XLogRecPtr)</term>
7081 <listitem>
7082 <para>
7083 The end LSN of the commit of the prepared transaction.
7084 </para>
7085 </listitem>
7086 </varlistentry>
7088 <varlistentry>
7089 <term>Int64 (TimestampTz)</term>
7090 <listitem>
7091 <para>
7092 Commit timestamp of the transaction. The value is in number
7093 of microseconds since PostgreSQL epoch (2000-01-01).
7094 </para>
7095 </listitem>
7096 </varlistentry>
7098 <varlistentry>
7099 <term>Int32 (TransactionId)</term>
7100 <listitem>
7101 <para>
7102 Xid of the transaction.
7103 </para>
7104 </listitem>
7105 </varlistentry>
7107 <varlistentry>
7108 <term>String</term>
7109 <listitem>
7110 <para>
7111 The user defined GID of the prepared transaction.
7112 </para>
7113 </listitem>
7114 </varlistentry>
7115 </variablelist>
7116 </listitem>
7117 </varlistentry>
7119 <varlistentry id="protocol-logicalrep-message-formats-Rollback-Prepared">
7120 <term>Rollback Prepared</term>
7121 <listitem>
7122 <variablelist>
7123 <varlistentry>
7124 <term>Byte1('r')</term>
7125 <listitem>
7126 <para>
7127 Identifies the message as the rollback of a prepared transaction message.
7128 </para>
7129 </listitem>
7130 </varlistentry>
7132 <varlistentry>
7133 <term>Int8(0)</term>
7134 <listitem>
7135 <para>
7136 Flags; currently unused.
7137 </para>
7138 </listitem>
7139 </varlistentry>
7141 <varlistentry>
7142 <term>Int64 (XLogRecPtr)</term>
7143 <listitem>
7144 <para>
7145 The end LSN of the prepared transaction.
7146 </para>
7147 </listitem>
7148 </varlistentry>
7150 <varlistentry>
7151 <term>Int64 (XLogRecPtr)</term>
7152 <listitem>
7153 <para>
7154 The end LSN of the rollback of the prepared transaction.
7155 </para>
7156 </listitem>
7157 </varlistentry>
7159 <varlistentry>
7160 <term>Int64 (TimestampTz)</term>
7161 <listitem>
7162 <para>
7163 Prepare timestamp of the transaction. The value is in number
7164 of microseconds since PostgreSQL epoch (2000-01-01).
7165 </para>
7166 </listitem>
7167 </varlistentry>
7169 <varlistentry>
7170 <term>Int64 (TimestampTz)</term>
7171 <listitem>
7172 <para>
7173 Rollback timestamp of the transaction. The value is in number
7174 of microseconds since PostgreSQL epoch (2000-01-01).
7175 </para>
7176 </listitem>
7177 </varlistentry>
7179 <varlistentry>
7180 <term>Int32 (TransactionId)</term>
7181 <listitem>
7182 <para>
7183 Xid of the transaction.
7184 </para>
7185 </listitem>
7186 </varlistentry>
7188 <varlistentry>
7189 <term>String</term>
7190 <listitem>
7191 <para>
7192 The user defined GID of the prepared transaction.
7193 </para>
7194 </listitem>
7195 </varlistentry>
7196 </variablelist>
7197 </listitem>
7198 </varlistentry>
7200 <varlistentry id="protocol-logicalrep-message-formats-Stream-Prepare">
7201 <term>Stream Prepare</term>
7202 <listitem>
7203 <variablelist>
7204 <varlistentry>
7205 <term>Byte1('p')</term>
7206 <listitem>
7207 <para>
7208 Identifies the message as a stream prepared transaction message.
7209 </para>
7210 </listitem>
7211 </varlistentry>
7213 <varlistentry>
7214 <term>Int8(0)</term>
7215 <listitem>
7216 <para>
7217 Flags; currently unused.
7218 </para>
7219 </listitem>
7220 </varlistentry>
7222 <varlistentry>
7223 <term>Int64 (XLogRecPtr)</term>
7224 <listitem>
7225 <para>
7226 The LSN of the prepare.
7227 </para>
7228 </listitem>
7229 </varlistentry>
7231 <varlistentry>
7232 <term>Int64 (XLogRecPtr)</term>
7233 <listitem>
7234 <para>
7235 The end LSN of the prepared transaction.
7236 </para>
7237 </listitem>
7238 </varlistentry>
7240 <varlistentry>
7241 <term>Int64 (TimestampTz)</term>
7242 <listitem>
7243 <para>
7244 Prepare timestamp of the transaction. The value is in number
7245 of microseconds since PostgreSQL epoch (2000-01-01).
7246 </para>
7247 </listitem>
7248 </varlistentry>
7250 <varlistentry>
7251 <term>Int32 (TransactionId)</term>
7252 <listitem>
7253 <para>
7254 Xid of the transaction.
7255 </para>
7256 </listitem>
7257 </varlistentry>
7259 <varlistentry>
7260 <term>String</term>
7261 <listitem>
7262 <para>
7263 The user defined GID of the prepared transaction.
7264 </para>
7265 </listitem>
7266 </varlistentry>
7267 </variablelist>
7268 </listitem>
7269 </varlistentry>
7270 </variablelist>
7272 <para>
7273 The following message parts are shared by the above messages.
7274 </para>
7276 <variablelist>
7277 <varlistentry id="protocol-logicalrep-message-formats-TupleData">
7278 <term>TupleData</term>
7279 <listitem>
7280 <variablelist>
7281 <varlistentry>
7282 <term>Int16</term>
7283 <listitem>
7284 <para>
7285 Number of columns.
7286 </para>
7287 </listitem>
7288 </varlistentry>
7289 </variablelist>
7291 <para>
7292 Next, one of the following submessages appears for each column (except generated columns):
7294 <variablelist>
7295 <varlistentry>
7296 <term>Byte1('n')</term>
7297 <listitem>
7298 <para>
7299 Identifies the data as NULL value.
7300 </para>
7301 </listitem>
7302 </varlistentry>
7303 </variablelist>
7305 <variablelist>
7306 <varlistentry>
7307 <term>Byte1('u')</term>
7308 <listitem>
7309 <para>
7310 Identifies unchanged TOASTed value (the actual value is not
7311 sent).
7312 </para>
7313 </listitem>
7314 </varlistentry>
7315 </variablelist>
7317 <variablelist>
7318 <varlistentry>
7319 <term>Byte1('t')</term>
7320 <listitem>
7321 <para>
7322 Identifies the data as text formatted value.
7323 </para>
7324 </listitem>
7325 </varlistentry>
7326 </variablelist>
7328 <variablelist>
7329 <varlistentry>
7330 <term>Byte1('b')</term>
7331 <listitem>
7332 <para>
7333 Identifies the data as binary formatted value.
7334 </para>
7335 </listitem>
7336 </varlistentry>
7338 <varlistentry>
7339 <term>Int32</term>
7340 <listitem>
7341 <para>
7342 Length of the column value.
7343 </para>
7344 </listitem>
7345 </varlistentry>
7347 <varlistentry>
7348 <term>Byte<replaceable>n</replaceable></term>
7349 <listitem>
7350 <para>
7351 The value of the column, either in binary or in text format.
7352 (As specified in the preceding format byte).
7353 <replaceable>n</replaceable> is the above length.
7354 </para>
7355 </listitem>
7356 </varlistentry>
7357 </variablelist>
7358 </para>
7359 </listitem>
7360 </varlistentry>
7361 </variablelist>
7362 </sect1>
7364 <sect1 id="protocol-changes">
7365 <title>Summary of Changes since Protocol 2.0</title>
7367 <para>
7368 This section provides a quick checklist of changes, for the benefit of
7369 developers trying to update existing client libraries to protocol 3.0.
7370 </para>
7372 <para>
7373 The initial startup packet uses a flexible list-of-strings format
7374 instead of a fixed format. Notice that session default values for run-time
7375 parameters can now be specified directly in the startup packet. (Actually,
7376 you could do that before using the <literal>options</literal> field, but given the
7377 limited width of <literal>options</literal> and the lack of any way to quote
7378 whitespace in the values, it wasn't a very safe technique.)
7379 </para>
7381 <para>
7382 All messages now have a length count immediately following the message type
7383 byte (except for startup packets, which have no type byte). Also note that
7384 PasswordMessage now has a type byte.
7385 </para>
7387 <para>
7388 ErrorResponse and NoticeResponse ('<literal>E</literal>' and '<literal>N</literal>')
7389 messages now contain multiple fields, from which the client code can
7390 assemble an error message of the desired level of verbosity. Note that
7391 individual fields will typically not end with a newline, whereas the single
7392 string sent in the older protocol always did.
7393 </para>
7395 <para>
7396 The ReadyForQuery ('<literal>Z</literal>') message includes a transaction status
7397 indicator.
7398 </para>
7400 <para>
7401 The distinction between BinaryRow and DataRow message types is gone; the
7402 single DataRow message type serves for returning data in all formats.
7403 Note that the layout of DataRow has changed to make it easier to parse.
7404 Also, the representation of binary values has changed: it is no longer
7405 directly tied to the server's internal representation.
7406 </para>
7408 <para>
7409 There is a new <quote>extended query</quote> sub-protocol, which adds the frontend
7410 message types Parse, Bind, Execute, Describe, Close, Flush, and Sync, and the
7411 backend message types ParseComplete, BindComplete, PortalSuspended,
7412 ParameterDescription, NoData, and CloseComplete. Existing clients do not
7413 have to concern themselves with this sub-protocol, but making use of it
7414 might allow improvements in performance or functionality.
7415 </para>
7417 <para>
7418 <command>COPY</command> data is now encapsulated into CopyData and CopyDone messages. There
7419 is a well-defined way to recover from errors during <command>COPY</command>. The special
7420 <quote><literal>\.</literal></quote> last line is not needed anymore, and is not sent
7421 during <command>COPY OUT</command>.
7422 (It is still recognized as a terminator during <command>COPY IN</command>, but its use is
7423 deprecated and will eventually be removed.) Binary <command>COPY</command> is supported.
7424 The CopyInResponse and CopyOutResponse messages include fields indicating
7425 the number of columns and the format of each column.
7426 </para>
7428 <para>
7429 The layout of FunctionCall and FunctionCallResponse messages has changed.
7430 FunctionCall can now support passing NULL arguments to functions. It also
7431 can handle passing parameters and retrieving results in either text or
7432 binary format. There is no longer any reason to consider FunctionCall a
7433 potential security hole, since it does not offer direct access to internal
7434 server data representations.
7435 </para>
7437 <para>
7438 The backend sends ParameterStatus ('<literal>S</literal>') messages during connection
7439 startup for all parameters it considers interesting to the client library.
7440 Subsequently, a ParameterStatus message is sent whenever the active value
7441 changes for any of these parameters.
7442 </para>
7444 <para>
7445 The RowDescription ('<literal>T</literal>') message carries new table OID and column
7446 number fields for each column of the described row. It also shows the format
7447 code for each column.
7448 </para>
7450 <para>
7451 The CursorResponse ('<literal>P</literal>') message is no longer generated by
7452 the backend.
7453 </para>
7455 <para>
7456 The NotificationResponse ('<literal>A</literal>') message has an additional string
7457 field, which can carry a <quote>payload</quote> string passed
7458 from the <command>NOTIFY</command> event sender.
7459 </para>
7461 <para>
7462 The EmptyQueryResponse ('<literal>I</literal>') message used to include an empty
7463 string parameter; this has been removed.
7464 </para>
7465 </sect1>
7466 </chapter>