~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/protocol.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

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