gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

894 line
60KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>52.2. Message Flow</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="protocol-overview.html" title="52.1. Overview" /><link rel="next" href="sasl-authentication.html" title="52.3. SASL Authentication" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">52.2. Message Flow</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="protocol-overview.html" title="52.1. Overview">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="protocol.html" title="Chapter 52. Frontend/Backend Protocol">Up</a></td><th width="60%" align="center">Chapter 52. Frontend/Backend Protocol</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sasl-authentication.html" title="52.3. SASL Authentication">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PROTOCOL-FLOW"><div class="titlepage"><div><div><h2 class="title" style="clear: both">52.2. Message Flow</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.3">52.2.1. Start-up</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.4">52.2.2. Simple Query</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY">52.2.3. Extended Query</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.6">52.2.4. Function Call</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#PROTOCOL-COPY">52.2.5. COPY Operations</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#PROTOCOL-ASYNC">52.2.6. Asynchronous Operations</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.9">52.2.7. Canceling Requests in Progress</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.10">52.2.8. Termination</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.11">52.2.9. <acronym class="acronym">SSL</acronym> Session Encryption</a></span></dt><dt><span class="sect2"><a href="protocol-flow.html#id-1.10.5.7.12">52.2.10. <acronym class="acronym">GSSAPI</acronym> Session Encryption</a></span></dt></dl></div><p>
  3. This section describes the message flow and the semantics of each
  4. message type. (Details of the exact representation of each message
  5. appear in <a class="xref" href="protocol-message-formats.html" title="52.7. Message Formats">Section 52.7</a>.) There are
  6. several different sub-protocols depending on the state of the
  7. connection: start-up, query, function call,
  8. <code class="command">COPY</code>, and termination. There are also special
  9. provisions for asynchronous operations (including notification
  10. responses and command cancellation), which can occur at any time
  11. after the start-up phase.
  12. </p><div class="sect2" id="id-1.10.5.7.3"><div class="titlepage"><div><div><h3 class="title">52.2.1. Start-up</h3></div></div></div><p>
  13. To begin a session, a frontend opens a connection to the server and sends
  14. a startup message. This message includes the names of the user and of the
  15. database the user wants to connect to; it also identifies the particular
  16. protocol version to be used. (Optionally, the startup message can include
  17. additional settings for run-time parameters.)
  18. The server then uses this information and
  19. the contents of its configuration files (such as
  20. <code class="filename">pg_hba.conf</code>) to determine
  21. whether the connection is provisionally acceptable, and what additional
  22. authentication is required (if any).
  23. </p><p>
  24. The server then sends an appropriate authentication request message,
  25. to which the frontend must reply with an appropriate authentication
  26. response message (such as a password).
  27. For all authentication methods except GSSAPI, SSPI and SASL, there is at
  28. most one request and one response. In some methods, no response
  29. at all is needed from the frontend, and so no authentication request
  30. occurs. For GSSAPI, SSPI and SASL, multiple exchanges of packets may be
  31. needed to complete the authentication.
  32. </p><p>
  33. The authentication cycle ends with the server either rejecting the
  34. connection attempt (ErrorResponse), or sending AuthenticationOk.
  35. </p><p>
  36. The possible messages from the server in this phase are:
  37. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">ErrorResponse</span></dt><dd><p>
  38. The connection attempt has been rejected.
  39. The server then immediately closes the connection.
  40. </p></dd><dt><span class="term">AuthenticationOk</span></dt><dd><p>
  41. The authentication exchange is successfully completed.
  42. </p></dd><dt><span class="term">AuthenticationKerberosV5</span></dt><dd><p>
  43. The frontend must now take part in a Kerberos V5
  44. authentication dialog (not described here, part of the
  45. Kerberos specification) with the server. If this is
  46. successful, the server responds with an AuthenticationOk,
  47. otherwise it responds with an ErrorResponse. This is no
  48. longer supported.
  49. </p></dd><dt><span class="term">AuthenticationCleartextPassword</span></dt><dd><p>
  50. The frontend must now send a PasswordMessage containing the
  51. password in clear-text form. If
  52. this is the correct password, the server responds with an
  53. AuthenticationOk, otherwise it responds with an ErrorResponse.
  54. </p></dd><dt><span class="term">AuthenticationMD5Password</span></dt><dd><p>
  55. The frontend must now send a PasswordMessage containing the
  56. password (with user name) encrypted via MD5, then encrypted
  57. again using the 4-byte random salt specified in the
  58. AuthenticationMD5Password message. If this is the correct
  59. password, the server responds with an AuthenticationOk,
  60. otherwise it responds with an ErrorResponse. The actual
  61. PasswordMessage can be computed in SQL as <code class="literal">concat('md5',
  62. md5(concat(md5(concat(password, username)), random-salt)))</code>.
  63. (Keep in mind the <code class="function">md5()</code> function returns its
  64. result as a hex string.)
  65. </p></dd><dt><span class="term">AuthenticationSCMCredential</span></dt><dd><p>
  66. This response is only possible for local Unix-domain connections
  67. on platforms that support SCM credential messages. The frontend
  68. must issue an SCM credential message and then send a single data
  69. byte. (The contents of the data byte are uninteresting; it's
  70. only used to ensure that the server waits long enough to receive
  71. the credential message.) If the credential is acceptable,
  72. the server responds with an
  73. AuthenticationOk, otherwise it responds with an ErrorResponse.
  74. (This message type is only issued by pre-9.1 servers. It may
  75. eventually be removed from the protocol specification.)
  76. </p></dd><dt><span class="term">AuthenticationGSS</span></dt><dd><p>
  77. The frontend must now initiate a GSSAPI negotiation. The frontend
  78. will send a GSSResponse message with the first part of the GSSAPI
  79. data stream in response to this. If further messages are needed,
  80. the server will respond with AuthenticationGSSContinue.
  81. </p></dd><dt><span class="term">AuthenticationSSPI</span></dt><dd><p>
  82. The frontend must now initiate a SSPI negotiation. The frontend
  83. will send a GSSResponse with the first part of the SSPI
  84. data stream in response to this. If further messages are needed,
  85. the server will respond with AuthenticationGSSContinue.
  86. </p></dd><dt><span class="term">AuthenticationGSSContinue</span></dt><dd><p>
  87. This message contains the response data from the previous step
  88. of GSSAPI or SSPI negotiation (AuthenticationGSS, AuthenticationSSPI
  89. or a previous AuthenticationGSSContinue). If the GSSAPI
  90. or SSPI data in this message
  91. indicates more data is needed to complete the authentication,
  92. the frontend must send that data as another GSSResponse message. If
  93. GSSAPI or SSPI authentication is completed by this message, the server
  94. will next send AuthenticationOk to indicate successful authentication
  95. or ErrorResponse to indicate failure.
  96. </p></dd><dt><span class="term">AuthenticationSASL</span></dt><dd><p>
  97. The frontend must now initiate a SASL negotiation, using one of the
  98. SASL mechanisms listed in the message. The frontend will send a
  99. SASLInitialResponse with the name of the selected mechanism, and the
  100. first part of the SASL data stream in response to this. If further
  101. messages are needed, the server will respond with
  102. AuthenticationSASLContinue. See <a class="xref" href="sasl-authentication.html" title="52.3. SASL Authentication">Section 52.3</a>
  103. for details.
  104. </p></dd><dt><span class="term">AuthenticationSASLContinue</span></dt><dd><p>
  105. This message contains challenge data from the previous step of SASL
  106. negotiation (AuthenticationSASL, or a previous
  107. AuthenticationSASLContinue). The frontend must respond with a
  108. SASLResponse message.
  109. </p></dd><dt><span class="term">AuthenticationSASLFinal</span></dt><dd><p>
  110. SASL authentication has completed with additional mechanism-specific
  111. data for the client. The server will next send AuthenticationOk to
  112. indicate successful authentication, or an ErrorResponse to indicate
  113. failure. This message is sent only if the SASL mechanism specifies
  114. additional data to be sent from server to client at completion.
  115. </p></dd><dt><span class="term">NegotiateProtocolVersion</span></dt><dd><p>
  116. The server does not support the minor protocol version requested
  117. by the client, but does support an earlier version of the protocol;
  118. this message indicates the highest supported minor version. This
  119. message will also be sent if the client requested unsupported protocol
  120. options (i.e. beginning with <code class="literal">_pq_.</code>) in the
  121. startup packet. This message will be followed by an ErrorResponse or
  122. a message indicating the success or failure of authentication.
  123. </p></dd></dl></div><p>
  124. </p><p>
  125. If the frontend does not support the authentication method
  126. requested by the server, then it should immediately close the
  127. connection.
  128. </p><p>
  129. After having received AuthenticationOk, the frontend must wait
  130. for further messages from the server. In this phase a backend process
  131. is being started, and the frontend is just an interested bystander.
  132. It is still possible for the startup attempt
  133. to fail (ErrorResponse) or the server to decline support for the requested
  134. minor protocol version (NegotiateProtocolVersion), but in the normal case
  135. the backend will send some ParameterStatus messages, BackendKeyData, and
  136. finally ReadyForQuery.
  137. </p><p>
  138. During this phase the backend will attempt to apply any additional
  139. run-time parameter settings that were given in the startup message.
  140. If successful, these values become session defaults. An error causes
  141. ErrorResponse and exit.
  142. </p><p>
  143. The possible messages from the backend in this phase are:
  144. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">BackendKeyData</span></dt><dd><p>
  145. This message provides secret-key data that the frontend must
  146. save if it wants to be able to issue cancel requests later.
  147. The frontend should not respond to this message, but should
  148. continue listening for a ReadyForQuery message.
  149. </p></dd><dt><span class="term">ParameterStatus</span></dt><dd><p>
  150. This message informs the frontend about the current (initial)
  151. setting of backend parameters, such as <a class="xref" href="runtime-config-client.html#GUC-CLIENT-ENCODING">client_encoding</a> or <a class="xref" href="runtime-config-client.html#GUC-DATESTYLE">DateStyle</a>.
  152. The frontend can ignore this message, or record the settings
  153. for its future use; see <a class="xref" href="protocol-flow.html#PROTOCOL-ASYNC" title="52.2.6. Asynchronous Operations">Section 52.2.6</a> for
  154. more details. The frontend should not respond to this
  155. message, but should continue listening for a ReadyForQuery
  156. message.
  157. </p></dd><dt><span class="term">ReadyForQuery</span></dt><dd><p>
  158. Start-up is completed. The frontend can now issue commands.
  159. </p></dd><dt><span class="term">ErrorResponse</span></dt><dd><p>
  160. Start-up failed. The connection is closed after sending this
  161. message.
  162. </p></dd><dt><span class="term">NoticeResponse</span></dt><dd><p>
  163. A warning message has been issued. The frontend should
  164. display the message but continue listening for ReadyForQuery
  165. or ErrorResponse.
  166. </p></dd></dl></div><p>
  167. </p><p>
  168. The ReadyForQuery message is the same one that the backend will
  169. issue after each command cycle. Depending on the coding needs of
  170. the frontend, it is reasonable to consider ReadyForQuery as
  171. starting a command cycle, or to consider ReadyForQuery as ending the
  172. start-up phase and each subsequent command cycle.
  173. </p></div><div class="sect2" id="id-1.10.5.7.4"><div class="titlepage"><div><div><h3 class="title">52.2.2. Simple Query</h3></div></div></div><p>
  174. A simple query cycle is initiated by the frontend sending a Query message
  175. to the backend. The message includes an SQL command (or commands)
  176. expressed as a text string.
  177. The backend then sends one or more response
  178. messages depending on the contents of the query command string,
  179. and finally a ReadyForQuery response message. ReadyForQuery
  180. informs the frontend that it can safely send a new command.
  181. (It is not actually necessary for the frontend to wait for
  182. ReadyForQuery before issuing another command, but the frontend must
  183. then take responsibility for figuring out what happens if the earlier
  184. command fails and already-issued later commands succeed.)
  185. </p><p>
  186. The possible response messages from the backend are:
  187. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">CommandComplete</span></dt><dd><p>
  188. An SQL command completed normally.
  189. </p></dd><dt><span class="term">CopyInResponse</span></dt><dd><p>
  190. The backend is ready to copy data from the frontend to a
  191. table; see <a class="xref" href="protocol-flow.html#PROTOCOL-COPY" title="52.2.5. COPY Operations">Section 52.2.5</a>.
  192. </p></dd><dt><span class="term">CopyOutResponse</span></dt><dd><p>
  193. The backend is ready to copy data from a table to the
  194. frontend; see <a class="xref" href="protocol-flow.html#PROTOCOL-COPY" title="52.2.5. COPY Operations">Section 52.2.5</a>.
  195. </p></dd><dt><span class="term">RowDescription</span></dt><dd><p>
  196. Indicates that rows are about to be returned in response to
  197. a <code class="command">SELECT</code>, <code class="command">FETCH</code>, etc query.
  198. The contents of this message describe the column layout of the rows.
  199. This will be followed by a DataRow message for each row being returned
  200. to the frontend.
  201. </p></dd><dt><span class="term">DataRow</span></dt><dd><p>
  202. One of the set of rows returned by
  203. a <code class="command">SELECT</code>, <code class="command">FETCH</code>, etc query.
  204. </p></dd><dt><span class="term">EmptyQueryResponse</span></dt><dd><p>
  205. An empty query string was recognized.
  206. </p></dd><dt><span class="term">ErrorResponse</span></dt><dd><p>
  207. An error has occurred.
  208. </p></dd><dt><span class="term">ReadyForQuery</span></dt><dd><p>
  209. Processing of the query string is complete. A separate
  210. message is sent to indicate this because the query string might
  211. contain multiple SQL commands. (CommandComplete marks the
  212. end of processing one SQL command, not the whole string.)
  213. ReadyForQuery will always be sent, whether processing
  214. terminates successfully or with an error.
  215. </p></dd><dt><span class="term">NoticeResponse</span></dt><dd><p>
  216. A warning message has been issued in relation to the query.
  217. Notices are in addition to other responses, i.e., the backend
  218. will continue processing the command.
  219. </p></dd></dl></div><p>
  220. </p><p>
  221. The response to a <code class="command">SELECT</code> query (or other queries that
  222. return row sets, such as <code class="command">EXPLAIN</code> or <code class="command">SHOW</code>)
  223. normally consists of RowDescription, zero or more
  224. DataRow messages, and then CommandComplete.
  225. <code class="command">COPY</code> to or from the frontend invokes special protocol
  226. as described in <a class="xref" href="protocol-flow.html#PROTOCOL-COPY" title="52.2.5. COPY Operations">Section 52.2.5</a>.
  227. All other query types normally produce only
  228. a CommandComplete message.
  229. </p><p>
  230. Since a query string could contain several queries (separated by
  231. semicolons), there might be several such response sequences before the
  232. backend finishes processing the query string. ReadyForQuery is issued
  233. when the entire string has been processed and the backend is ready to
  234. accept a new query string.
  235. </p><p>
  236. If a completely empty (no contents other than whitespace) query string
  237. is received, the response is EmptyQueryResponse followed by ReadyForQuery.
  238. </p><p>
  239. In the event of an error, ErrorResponse is issued followed by
  240. ReadyForQuery. All further processing of the query string is aborted by
  241. ErrorResponse (even if more queries remained in it). Note that this
  242. might occur partway through the sequence of messages generated by an
  243. individual query.
  244. </p><p>
  245. In simple Query mode, the format of retrieved values is always text,
  246. except when the given command is a <code class="command">FETCH</code> from a cursor
  247. declared with the <code class="literal">BINARY</code> option. In that case, the
  248. retrieved values are in binary format. The format codes given in
  249. the RowDescription message tell which format is being used.
  250. </p><p>
  251. A frontend must be prepared to accept ErrorResponse and
  252. NoticeResponse messages whenever it is expecting any other type of
  253. message. See also <a class="xref" href="protocol-flow.html#PROTOCOL-ASYNC" title="52.2.6. Asynchronous Operations">Section 52.2.6</a> concerning messages
  254. that the backend might generate due to outside events.
  255. </p><p>
  256. Recommended practice is to code frontends in a state-machine style
  257. that will accept any message type at any time that it could make sense,
  258. rather than wiring in assumptions about the exact sequence of messages.
  259. </p><div class="sect3" id="PROTOCOL-FLOW-MULTI-STATEMENT"><div class="titlepage"><div><div><h4 class="title">52.2.2.1. Multiple Statements in a Simple Query</h4></div></div></div><p>
  260. When a simple Query message contains more than one SQL statement
  261. (separated by semicolons), those statements are executed as a single
  262. transaction, unless explicit transaction control commands are included
  263. to force a different behavior. For example, if the message contains
  264. </p><pre class="programlisting">
  265. INSERT INTO mytable VALUES(1);
  266. SELECT 1/0;
  267. INSERT INTO mytable VALUES(2);
  268. </pre><p>
  269. then the divide-by-zero failure in the <code class="command">SELECT</code> will force
  270. rollback of the first <code class="command">INSERT</code>. Furthermore, because
  271. execution of the message is abandoned at the first error, the second
  272. <code class="command">INSERT</code> is never attempted at all.
  273. </p><p>
  274. If instead the message contains
  275. </p><pre class="programlisting">
  276. BEGIN;
  277. INSERT INTO mytable VALUES(1);
  278. COMMIT;
  279. INSERT INTO mytable VALUES(2);
  280. SELECT 1/0;
  281. </pre><p>
  282. then the first <code class="command">INSERT</code> is committed by the
  283. explicit <code class="command">COMMIT</code> command. The second <code class="command">INSERT</code>
  284. and the <code class="command">SELECT</code> are still treated as a single transaction,
  285. so that the divide-by-zero failure will roll back the
  286. second <code class="command">INSERT</code>, but not the first one.
  287. </p><p>
  288. This behavior is implemented by running the statements in a
  289. multi-statement Query message in an <em class="firstterm">implicit transaction
  290. block</em> unless there is some explicit transaction block for them to
  291. run in. The main difference between an implicit transaction block and
  292. a regular one is that an implicit block is closed automatically at the
  293. end of the Query message, either by an implicit commit if there was no
  294. error, or an implicit rollback if there was an error. This is similar
  295. to the implicit commit or rollback that happens for a statement
  296. executed by itself (when not in a transaction block).
  297. </p><p>
  298. If the session is already in a transaction block, as a result of
  299. a <code class="command">BEGIN</code> in some previous message, then the Query message
  300. simply continues that transaction block, whether the message contains
  301. one statement or several. However, if the Query message contains
  302. a <code class="command">COMMIT</code> or <code class="command">ROLLBACK</code> closing the existing
  303. transaction block, then any following statements are executed in an
  304. implicit transaction block.
  305. Conversely, if a <code class="command">BEGIN</code> appears in a multi-statement Query
  306. message, then it starts a regular transaction block that will only be
  307. terminated by an explicit <code class="command">COMMIT</code> or <code class="command">ROLLBACK</code>,
  308. whether that appears in this Query message or a later one.
  309. If the <code class="command">BEGIN</code> follows some statements that were executed as
  310. an implicit transaction block, those statements are not immediately
  311. committed; in effect, they are retroactively included into the new
  312. regular transaction block.
  313. </p><p>
  314. A <code class="command">COMMIT</code> or <code class="command">ROLLBACK</code> appearing in an implicit
  315. transaction block is executed as normal, closing the implicit block;
  316. however, a warning will be issued since a <code class="command">COMMIT</code>
  317. or <code class="command">ROLLBACK</code> without a previous <code class="command">BEGIN</code> might
  318. represent a mistake. If more statements follow, a new implicit
  319. transaction block will be started for them.
  320. </p><p>
  321. Savepoints are not allowed in an implicit transaction block, since
  322. they would conflict with the behavior of automatically closing the
  323. block upon any error.
  324. </p><p>
  325. Remember that, regardless of any transaction control commands that may
  326. be present, execution of the Query message stops at the first error.
  327. Thus for example given
  328. </p><pre class="programlisting">
  329. BEGIN;
  330. SELECT 1/0;
  331. ROLLBACK;
  332. </pre><p>
  333. in a single Query message, the session will be left inside a failed
  334. regular transaction block, since the <code class="command">ROLLBACK</code> is not
  335. reached after the divide-by-zero error. Another <code class="command">ROLLBACK</code>
  336. will be needed to restore the session to a usable state.
  337. </p><p>
  338. Another behavior of note is that initial lexical and syntactic
  339. analysis is done on the entire query string before any of it is
  340. executed. Thus simple errors (such as a misspelled keyword) in later
  341. statements can prevent execution of any of the statements. This
  342. is normally invisible to users since the statements would all roll
  343. back anyway when done as an implicit transaction block. However,
  344. it can be visible when attempting to do multiple transactions within a
  345. multi-statement Query. For instance, if a typo turned our previous
  346. example into
  347. </p><pre class="programlisting">
  348. BEGIN;
  349. INSERT INTO mytable VALUES(1);
  350. COMMIT;
  351. INSERT INTO mytable VALUES(2);
  352. SELCT 1/0;
  353. </pre><p>
  354. then none of the statements would get run, resulting in the visible
  355. difference that the first <code class="command">INSERT</code> is not committed.
  356. Errors detected at semantic analysis or later, such as a misspelled
  357. table or column name, do not have this effect.
  358. </p></div></div><div class="sect2" id="PROTOCOL-FLOW-EXT-QUERY"><div class="titlepage"><div><div><h3 class="title">52.2.3. Extended Query</h3></div></div></div><p>
  359. The extended query protocol breaks down the above-described simple
  360. query protocol into multiple steps. The results of preparatory
  361. steps can be re-used multiple times for improved efficiency.
  362. Furthermore, additional features are available, such as the possibility
  363. of supplying data values as separate parameters instead of having to
  364. insert them directly into a query string.
  365. </p><p>
  366. In the extended protocol, the frontend first sends a Parse message,
  367. which contains a textual query string, optionally some information
  368. about data types of parameter placeholders, and the
  369. name of a destination prepared-statement object (an empty string
  370. selects the unnamed prepared statement). The response is
  371. either ParseComplete or ErrorResponse. Parameter data types can be
  372. specified by OID; if not given, the parser attempts to infer the
  373. data types in the same way as it would do for untyped literal string
  374. constants.
  375. </p><div class="note"><h3 class="title">Note</h3><p>
  376. A parameter data type can be left unspecified by setting it to zero,
  377. or by making the array of parameter type OIDs shorter than the
  378. number of parameter symbols (<code class="literal">$</code><em class="replaceable"><code>n</code></em>)
  379. used in the query string. Another special case is that a parameter's
  380. type can be specified as <code class="type">void</code> (that is, the OID of the
  381. <code class="type">void</code> pseudo-type). This is meant to allow parameter symbols
  382. to be used for function parameters that are actually OUT parameters.
  383. Ordinarily there is no context in which a <code class="type">void</code> parameter
  384. could be used, but if such a parameter symbol appears in a function's
  385. parameter list, it is effectively ignored. For example, a function
  386. call such as <code class="literal">foo($1,$2,$3,$4)</code> could match a function with
  387. two IN and two OUT arguments, if <code class="literal">$3</code> and <code class="literal">$4</code>
  388. are specified as having type <code class="type">void</code>.
  389. </p></div><div class="note"><h3 class="title">Note</h3><p>
  390. The query string contained in a Parse message cannot include more
  391. than one SQL statement; else a syntax error is reported. This
  392. restriction does not exist in the simple-query protocol, but it
  393. does exist in the extended protocol, because allowing prepared
  394. statements or portals to contain multiple commands would complicate
  395. the protocol unduly.
  396. </p></div><p>
  397. If successfully created, a named prepared-statement object lasts till
  398. the end of the current session, unless explicitly destroyed. An unnamed
  399. prepared statement lasts only until the next Parse statement specifying
  400. the unnamed statement as destination is issued. (Note that a simple
  401. Query message also destroys the unnamed statement.) Named prepared
  402. statements must be explicitly closed before they can be redefined by
  403. another Parse message, but this is not required for the unnamed statement.
  404. Named prepared statements can also be created and accessed at the SQL
  405. command level, using <code class="command">PREPARE</code> and <code class="command">EXECUTE</code>.
  406. </p><p>
  407. Once a prepared statement exists, it can be readied for execution using a
  408. Bind message. The Bind message gives the name of the source prepared
  409. statement (empty string denotes the unnamed prepared statement), the name
  410. of the destination portal (empty string denotes the unnamed portal), and
  411. the values to use for any parameter placeholders present in the prepared
  412. statement. The
  413. supplied parameter set must match those needed by the prepared statement.
  414. (If you declared any <code class="type">void</code> parameters in the Parse message,
  415. pass NULL values for them in the Bind message.)
  416. Bind also specifies the format to use for any data returned
  417. by the query; the format can be specified overall, or per-column.
  418. The response is either BindComplete or ErrorResponse.
  419. </p><div class="note"><h3 class="title">Note</h3><p>
  420. The choice between text and binary output is determined by the format
  421. codes given in Bind, regardless of the SQL command involved. The
  422. <code class="literal">BINARY</code> attribute in cursor declarations is irrelevant when
  423. using extended query protocol.
  424. </p></div><p>
  425. Query planning typically occurs when the Bind message is processed.
  426. If the prepared statement has no parameters, or is executed repeatedly,
  427. the server might save the created plan and re-use it during subsequent
  428. Bind messages for the same prepared statement. However, it will do so
  429. only if it finds that a generic plan can be created that is not much
  430. less efficient than a plan that depends on the specific parameter values
  431. supplied. This happens transparently so far as the protocol is concerned.
  432. </p><p>
  433. If successfully created, a named portal object lasts till the end of the
  434. current transaction, unless explicitly destroyed. An unnamed portal is
  435. destroyed at the end of the transaction, or as soon as the next Bind
  436. statement specifying the unnamed portal as destination is issued. (Note
  437. that a simple Query message also destroys the unnamed portal.) Named
  438. portals must be explicitly closed before they can be redefined by another
  439. Bind message, but this is not required for the unnamed portal.
  440. Named portals can also be created and accessed at the SQL
  441. command level, using <code class="command">DECLARE CURSOR</code> and <code class="command">FETCH</code>.
  442. </p><p>
  443. Once a portal exists, it can be executed using an Execute message.
  444. The Execute message specifies the portal name (empty string denotes the
  445. unnamed portal) and
  446. a maximum result-row count (zero meaning <span class="quote">“<span class="quote">fetch all rows</span>”</span>).
  447. The result-row count is only meaningful for portals
  448. containing commands that return row sets; in other cases the command is
  449. always executed to completion, and the row count is ignored.
  450. The possible
  451. responses to Execute are the same as those described above for queries
  452. issued via simple query protocol, except that Execute doesn't cause
  453. ReadyForQuery or RowDescription to be issued.
  454. </p><p>
  455. If Execute terminates before completing the execution of a portal
  456. (due to reaching a nonzero result-row count), it will send a
  457. PortalSuspended message; the appearance of this message tells the frontend
  458. that another Execute should be issued against the same portal to
  459. complete the operation. The CommandComplete message indicating
  460. completion of the source SQL command is not sent until
  461. the portal's execution is completed. Therefore, an Execute phase is
  462. always terminated by the appearance of exactly one of these messages:
  463. CommandComplete, EmptyQueryResponse (if the portal was created from
  464. an empty query string), ErrorResponse, or PortalSuspended.
  465. </p><p>
  466. At completion of each series of extended-query messages, the frontend
  467. should issue a Sync message. This parameterless message causes the
  468. backend to close the current transaction if it's not inside a
  469. <code class="command">BEGIN</code>/<code class="command">COMMIT</code> transaction block (<span class="quote">“<span class="quote">close</span>”</span>
  470. meaning to commit if no error, or roll back if error). Then a
  471. ReadyForQuery response is issued. The purpose of Sync is to provide
  472. a resynchronization point for error recovery. When an error is detected
  473. while processing any extended-query message, the backend issues
  474. ErrorResponse, then reads and discards messages until a Sync is reached,
  475. then issues ReadyForQuery and returns to normal message processing.
  476. (But note that no skipping occurs if an error is detected
  477. <span class="emphasis"><em>while</em></span> processing Sync — this ensures that there is one
  478. and only one ReadyForQuery sent for each Sync.)
  479. </p><div class="note"><h3 class="title">Note</h3><p>
  480. Sync does not cause a transaction block opened with <code class="command">BEGIN</code>
  481. to be closed. It is possible to detect this situation since the
  482. ReadyForQuery message includes transaction status information.
  483. </p></div><p>
  484. In addition to these fundamental, required operations, there are several
  485. optional operations that can be used with extended-query protocol.
  486. </p><p>
  487. The Describe message (portal variant) specifies the name of an existing
  488. portal (or an empty string for the unnamed portal). The response is a
  489. RowDescription message describing the rows that will be returned by
  490. executing the portal; or a NoData message if the portal does not contain a
  491. query that will return rows; or ErrorResponse if there is no such portal.
  492. </p><p>
  493. The Describe message (statement variant) specifies the name of an existing
  494. prepared statement (or an empty string for the unnamed prepared
  495. statement). The response is a ParameterDescription message describing the
  496. parameters needed by the statement, followed by a RowDescription message
  497. describing the rows that will be returned when the statement is eventually
  498. executed (or a NoData message if the statement will not return rows).
  499. ErrorResponse is issued if there is no such prepared statement. Note that
  500. since Bind has not yet been issued, the formats to be used for returned
  501. columns are not yet known to the backend; the format code fields in the
  502. RowDescription message will be zeroes in this case.
  503. </p><div class="tip"><h3 class="title">Tip</h3><p>
  504. In most scenarios the frontend should issue one or the other variant
  505. of Describe before issuing Execute, to ensure that it knows how to
  506. interpret the results it will get back.
  507. </p></div><p>
  508. The Close message closes an existing prepared statement or portal
  509. and releases resources. It is not an error to issue Close against
  510. a nonexistent statement or portal name. The response is normally
  511. CloseComplete, but could be ErrorResponse if some difficulty is
  512. encountered while releasing resources. Note that closing a prepared
  513. statement implicitly closes any open portals that were constructed
  514. from that statement.
  515. </p><p>
  516. The Flush message does not cause any specific output to be generated,
  517. but forces the backend to deliver any data pending in its output
  518. buffers. A Flush must be sent after any extended-query command except
  519. Sync, if the frontend wishes to examine the results of that command before
  520. issuing more commands. Without Flush, messages returned by the backend
  521. will be combined into the minimum possible number of packets to minimize
  522. network overhead.
  523. </p><div class="note"><h3 class="title">Note</h3><p>
  524. The simple Query message is approximately equivalent to the series Parse,
  525. Bind, portal Describe, Execute, Close, Sync, using the unnamed prepared
  526. statement and portal objects and no parameters. One difference is that
  527. it will accept multiple SQL statements in the query string, automatically
  528. performing the bind/describe/execute sequence for each one in succession.
  529. Another difference is that it will not return ParseComplete, BindComplete,
  530. CloseComplete, or NoData messages.
  531. </p></div></div><div class="sect2" id="id-1.10.5.7.6"><div class="titlepage"><div><div><h3 class="title">52.2.4. Function Call</h3></div></div></div><p>
  532. The Function Call sub-protocol allows the client to request a direct
  533. call of any function that exists in the database's
  534. <code class="structname">pg_proc</code> system catalog. The client must have
  535. execute permission for the function.
  536. </p><div class="note"><h3 class="title">Note</h3><p>
  537. The Function Call sub-protocol is a legacy feature that is probably best
  538. avoided in new code. Similar results can be accomplished by setting up
  539. a prepared statement that does <code class="literal">SELECT function($1, ...)</code>.
  540. The Function Call cycle can then be replaced with Bind/Execute.
  541. </p></div><p>
  542. A Function Call cycle is initiated by the frontend sending a
  543. FunctionCall message to the backend. The backend then sends one
  544. or more response messages depending on the results of the function
  545. call, and finally a ReadyForQuery response message. ReadyForQuery
  546. informs the frontend that it can safely send a new query or
  547. function call.
  548. </p><p>
  549. The possible response messages from the backend are:
  550. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">ErrorResponse</span></dt><dd><p>
  551. An error has occurred.
  552. </p></dd><dt><span class="term">FunctionCallResponse</span></dt><dd><p>
  553. The function call was completed and returned the result given
  554. in the message.
  555. (Note that the Function Call protocol can only handle a single
  556. scalar result, not a row type or set of results.)
  557. </p></dd><dt><span class="term">ReadyForQuery</span></dt><dd><p>
  558. Processing of the function call is complete. ReadyForQuery
  559. will always be sent, whether processing terminates
  560. successfully or with an error.
  561. </p></dd><dt><span class="term">NoticeResponse</span></dt><dd><p>
  562. A warning message has been issued in relation to the function
  563. call. Notices are in addition to other responses, i.e., the
  564. backend will continue processing the command.
  565. </p></dd></dl></div><p>
  566. </p></div><div class="sect2" id="PROTOCOL-COPY"><div class="titlepage"><div><div><h3 class="title">52.2.5. COPY Operations</h3></div></div></div><p>
  567. The <code class="command">COPY</code> command allows high-speed bulk data transfer
  568. to or from the server. Copy-in and copy-out operations each switch
  569. the connection into a distinct sub-protocol, which lasts until the
  570. operation is completed.
  571. </p><p>
  572. Copy-in mode (data transfer to the server) is initiated when the
  573. backend executes a <code class="command">COPY FROM STDIN</code> SQL statement. The backend
  574. sends a CopyInResponse message to the frontend. The frontend should
  575. then send zero or more CopyData messages, forming a stream of input
  576. data. (The message boundaries are not required to have anything to do
  577. with row boundaries, although that is often a reasonable choice.)
  578. The frontend can terminate the copy-in mode by sending either a CopyDone
  579. message (allowing successful termination) or a CopyFail message (which
  580. will cause the <code class="command">COPY</code> SQL statement to fail with an
  581. error). The backend then reverts to the command-processing mode it was
  582. in before the <code class="command">COPY</code> started, which will be either simple or
  583. extended query protocol. It will next send either CommandComplete
  584. (if successful) or ErrorResponse (if not).
  585. </p><p>
  586. In the event of a backend-detected error during copy-in mode (including
  587. receipt of a CopyFail message), the backend will issue an ErrorResponse
  588. message. If the <code class="command">COPY</code> command was issued via an extended-query
  589. message, the backend will now discard frontend messages until a Sync
  590. message is received, then it will issue ReadyForQuery and return to normal
  591. processing. If the <code class="command">COPY</code> command was issued in a simple
  592. Query message, the rest of that message is discarded and ReadyForQuery
  593. is issued. In either case, any subsequent CopyData, CopyDone, or CopyFail
  594. messages issued by the frontend will simply be dropped.
  595. </p><p>
  596. The backend will ignore Flush and Sync messages received during copy-in
  597. mode. Receipt of any other non-copy message type constitutes an error
  598. that will abort the copy-in state as described above. (The exception for
  599. Flush and Sync is for the convenience of client libraries that always
  600. send Flush or Sync after an Execute message, without checking whether
  601. the command to be executed is a <code class="command">COPY FROM STDIN</code>.)
  602. </p><p>
  603. Copy-out mode (data transfer from the server) is initiated when the
  604. backend executes a <code class="command">COPY TO STDOUT</code> SQL statement. The backend
  605. sends a CopyOutResponse message to the frontend, followed by
  606. zero or more CopyData messages (always one per row), followed by CopyDone.
  607. The backend then reverts to the command-processing mode it was
  608. in before the <code class="command">COPY</code> started, and sends CommandComplete.
  609. The frontend cannot abort the transfer (except by closing the connection
  610. or issuing a Cancel request),
  611. but it can discard unwanted CopyData and CopyDone messages.
  612. </p><p>
  613. In the event of a backend-detected error during copy-out mode,
  614. the backend will issue an ErrorResponse message and revert to normal
  615. processing. The frontend should treat receipt of ErrorResponse as
  616. terminating the copy-out mode.
  617. </p><p>
  618. It is possible for NoticeResponse and ParameterStatus messages to be
  619. interspersed between CopyData messages; frontends must handle these cases,
  620. and should be prepared for other asynchronous message types as well (see
  621. <a class="xref" href="protocol-flow.html#PROTOCOL-ASYNC" title="52.2.6. Asynchronous Operations">Section 52.2.6</a>). Otherwise, any message type other than
  622. CopyData or CopyDone may be treated as terminating copy-out mode.
  623. </p><p>
  624. There is another Copy-related mode called copy-both, which allows
  625. high-speed bulk data transfer to <span class="emphasis"><em>and</em></span> from the server.
  626. Copy-both mode is initiated when a backend in walsender mode
  627. executes a <code class="command">START_REPLICATION</code> statement. The
  628. backend sends a CopyBothResponse message to the frontend. Both
  629. the backend and the frontend may then send CopyData messages
  630. until either end sends a CopyDone message. After the client
  631. sends a CopyDone message, the connection goes from copy-both mode to
  632. copy-out mode, and the client may not send any more CopyData messages.
  633. Similarly, when the server sends a CopyDone message, the connection
  634. goes into copy-in mode, and the server may not send any more CopyData
  635. messages. After both sides have sent a CopyDone message, the copy mode
  636. is terminated, and the backend reverts to the command-processing mode.
  637. In the event of a backend-detected error during copy-both mode,
  638. the backend will issue an ErrorResponse message, discard frontend messages
  639. until a Sync message is received, and then issue ReadyForQuery and return
  640. to normal processing. The frontend should treat receipt of ErrorResponse
  641. as terminating the copy in both directions; no CopyDone should be sent
  642. in this case. See <a class="xref" href="protocol-replication.html" title="52.4. Streaming Replication Protocol">Section 52.4</a> for more
  643. information on the subprotocol transmitted over copy-both mode.
  644. </p><p>
  645. The CopyInResponse, CopyOutResponse and CopyBothResponse messages
  646. include fields that inform the frontend of the number of columns
  647. per row and the format codes being used for each column. (As of
  648. the present implementation, all columns in a given <code class="command">COPY</code>
  649. operation will use the same format, but the message design does not
  650. assume this.)
  651. </p></div><div class="sect2" id="PROTOCOL-ASYNC"><div class="titlepage"><div><div><h3 class="title">52.2.6. Asynchronous Operations</h3></div></div></div><p>
  652. There are several cases in which the backend will send messages that
  653. are not specifically prompted by the frontend's command stream.
  654. Frontends must be prepared to deal with these messages at any time,
  655. even when not engaged in a query.
  656. At minimum, one should check for these cases before beginning to
  657. read a query response.
  658. </p><p>
  659. It is possible for NoticeResponse messages to be generated due to
  660. outside activity; for example, if the database administrator commands
  661. a <span class="quote">“<span class="quote">fast</span>”</span> database shutdown, the backend will send a NoticeResponse
  662. indicating this fact before closing the connection. Accordingly,
  663. frontends should always be prepared to accept and display NoticeResponse
  664. messages, even when the connection is nominally idle.
  665. </p><p>
  666. ParameterStatus messages will be generated whenever the active
  667. value changes for any of the parameters the backend believes the
  668. frontend should know about. Most commonly this occurs in response
  669. to a <code class="command">SET</code> SQL command executed by the frontend, and
  670. this case is effectively synchronous — but it is also possible
  671. for parameter status changes to occur because the administrator
  672. changed a configuration file and then sent the
  673. <span class="systemitem">SIGHUP</span> signal to the server. Also,
  674. if a <code class="command">SET</code> command is rolled back, an appropriate
  675. ParameterStatus message will be generated to report the current
  676. effective value.
  677. </p><p>
  678. At present there is a hard-wired set of parameters for which
  679. ParameterStatus will be generated: they are
  680. <code class="varname">server_version</code>,
  681. <code class="varname">server_encoding</code>,
  682. <code class="varname">client_encoding</code>,
  683. <code class="varname">application_name</code>,
  684. <code class="varname">is_superuser</code>,
  685. <code class="varname">session_authorization</code>,
  686. <code class="varname">DateStyle</code>,
  687. <code class="varname">IntervalStyle</code>,
  688. <code class="varname">TimeZone</code>,
  689. <code class="varname">integer_datetimes</code>, and
  690. <code class="varname">standard_conforming_strings</code>.
  691. (<code class="varname">server_encoding</code>, <code class="varname">TimeZone</code>, and
  692. <code class="varname">integer_datetimes</code> were not reported by releases before 8.0;
  693. <code class="varname">standard_conforming_strings</code> was not reported by releases
  694. before 8.1;
  695. <code class="varname">IntervalStyle</code> was not reported by releases before 8.4;
  696. <code class="varname">application_name</code> was not reported by releases before 9.0.)
  697. Note that
  698. <code class="varname">server_version</code>,
  699. <code class="varname">server_encoding</code> and
  700. <code class="varname">integer_datetimes</code>
  701. are pseudo-parameters that cannot change after startup.
  702. This set might change in the future, or even become configurable.
  703. Accordingly, a frontend should simply ignore ParameterStatus for
  704. parameters that it does not understand or care about.
  705. </p><p>
  706. If a frontend issues a <code class="command">LISTEN</code> command, then the
  707. backend will send a NotificationResponse message (not to be
  708. confused with NoticeResponse!) whenever a
  709. <code class="command">NOTIFY</code> command is executed for the same
  710. channel name.
  711. </p><div class="note"><h3 class="title">Note</h3><p>
  712. At present, NotificationResponse can only be sent outside a
  713. transaction, and thus it will not occur in the middle of a
  714. command-response series, though it might occur just before ReadyForQuery.
  715. It is unwise to design frontend logic that assumes that, however.
  716. Good practice is to be able to accept NotificationResponse at any
  717. point in the protocol.
  718. </p></div></div><div class="sect2" id="id-1.10.5.7.9"><div class="titlepage"><div><div><h3 class="title">52.2.7. Canceling Requests in Progress</h3></div></div></div><p>
  719. During the processing of a query, the frontend might request
  720. cancellation of the query. The cancel request is not sent
  721. directly on the open connection to the backend for reasons of
  722. implementation efficiency: we don't want to have the backend
  723. constantly checking for new input from the frontend during query
  724. processing. Cancel requests should be relatively infrequent, so
  725. we make them slightly cumbersome in order to avoid a penalty in
  726. the normal case.
  727. </p><p>
  728. To issue a cancel request, the frontend opens a new connection to
  729. the server and sends a CancelRequest message, rather than the
  730. StartupMessage message that would ordinarily be sent across a new
  731. connection. The server will process this request and then close
  732. the connection. For security reasons, no direct reply is made to
  733. the cancel request message.
  734. </p><p>
  735. A CancelRequest message will be ignored unless it contains the
  736. same key data (PID and secret key) passed to the frontend during
  737. connection start-up. If the request matches the PID and secret
  738. key for a currently executing backend, the processing of the
  739. current query is aborted. (In the existing implementation, this is
  740. done by sending a special signal to the backend process that is
  741. processing the query.)
  742. </p><p>
  743. The cancellation signal might or might not have any effect — for
  744. example, if it arrives after the backend has finished processing
  745. the query, then it will have no effect. If the cancellation is
  746. effective, it results in the current command being terminated
  747. early with an error message.
  748. </p><p>
  749. The upshot of all this is that for reasons of both security and
  750. efficiency, the frontend has no direct way to tell whether a
  751. cancel request has succeeded. It must continue to wait for the
  752. backend to respond to the query. Issuing a cancel simply improves
  753. the odds that the current query will finish soon, and improves the
  754. odds that it will fail with an error message instead of
  755. succeeding.
  756. </p><p>
  757. Since the cancel request is sent across a new connection to the
  758. server and not across the regular frontend/backend communication
  759. link, it is possible for the cancel request to be issued by any
  760. process, not just the frontend whose query is to be canceled.
  761. This might provide additional flexibility when building
  762. multiple-process applications. It also introduces a security
  763. risk, in that unauthorized persons might try to cancel queries.
  764. The security risk is addressed by requiring a dynamically
  765. generated secret key to be supplied in cancel requests.
  766. </p></div><div class="sect2" id="id-1.10.5.7.10"><div class="titlepage"><div><div><h3 class="title">52.2.8. Termination</h3></div></div></div><p>
  767. The normal, graceful termination procedure is that the frontend
  768. sends a Terminate message and immediately closes the connection.
  769. On receipt of this message, the backend closes the connection and
  770. terminates.
  771. </p><p>
  772. In rare cases (such as an administrator-commanded database shutdown)
  773. the backend might disconnect without any frontend request to do so.
  774. In such cases the backend will attempt to send an error or notice message
  775. giving the reason for the disconnection before it closes the connection.
  776. </p><p>
  777. Other termination scenarios arise from various failure cases, such as core
  778. dump at one end or the other, loss of the communications link, loss of
  779. message-boundary synchronization, etc. If either frontend or backend sees
  780. an unexpected closure of the connection, it should clean
  781. up and terminate. The frontend has the option of launching a new backend
  782. by recontacting the server if it doesn't want to terminate itself.
  783. Closing the connection is also advisable if an unrecognizable message type
  784. is received, since this probably indicates loss of message-boundary sync.
  785. </p><p>
  786. For either normal or abnormal termination, any open transaction is
  787. rolled back, not committed. One should note however that if a
  788. frontend disconnects while a non-<code class="command">SELECT</code> query
  789. is being processed, the backend will probably finish the query
  790. before noticing the disconnection. If the query is outside any
  791. transaction block (<code class="command">BEGIN</code> ... <code class="command">COMMIT</code>
  792. sequence) then its results might be committed before the
  793. disconnection is recognized.
  794. </p></div><div class="sect2" id="id-1.10.5.7.11"><div class="titlepage"><div><div><h3 class="title">52.2.9. <acronym class="acronym">SSL</acronym> Session Encryption</h3></div></div></div><p>
  795. If <span class="productname">PostgreSQL</span> was built with
  796. <acronym class="acronym">SSL</acronym> support, frontend/backend communications
  797. can be encrypted using <acronym class="acronym">SSL</acronym>. This provides
  798. communication security in environments where attackers might be
  799. able to capture the session traffic. For more information on
  800. encrypting <span class="productname">PostgreSQL</span> sessions with
  801. <acronym class="acronym">SSL</acronym>, see <a class="xref" href="ssl-tcp.html" title="18.9. Secure TCP/IP Connections with SSL">Section 18.9</a>.
  802. </p><p>
  803. To initiate an <acronym class="acronym">SSL</acronym>-encrypted connection, the
  804. frontend initially sends an SSLRequest message rather than a
  805. StartupMessage. The server then responds with a single byte
  806. containing <code class="literal">S</code> or <code class="literal">N</code>, indicating that it is
  807. willing or unwilling to perform <acronym class="acronym">SSL</acronym>,
  808. respectively. The frontend might close the connection at this point
  809. if it is dissatisfied with the response. To continue after
  810. <code class="literal">S</code>, perform an <acronym class="acronym">SSL</acronym> startup handshake
  811. (not described here, part of the <acronym class="acronym">SSL</acronym>
  812. specification) with the server. If this is successful, continue
  813. with sending the usual StartupMessage. In this case the
  814. StartupMessage and all subsequent data will be
  815. <acronym class="acronym">SSL</acronym>-encrypted. To continue after
  816. <code class="literal">N</code>, send the usual StartupMessage and proceed without
  817. encryption.
  818. </p><p>
  819. The frontend should also be prepared to handle an ErrorMessage
  820. response to SSLRequest from the server. This would only occur if
  821. the server predates the addition of <acronym class="acronym">SSL</acronym> support
  822. to <span class="productname">PostgreSQL</span>. (Such servers are now very ancient,
  823. and likely do not exist in the wild anymore.)
  824. In this case the connection must
  825. be closed, but the frontend might choose to open a fresh connection
  826. and proceed without requesting <acronym class="acronym">SSL</acronym>.
  827. </p><p>
  828. An initial SSLRequest can also be used in a connection that is being
  829. opened to send a CancelRequest message.
  830. </p><p>
  831. While the protocol itself does not provide a way for the server to
  832. force <acronym class="acronym">SSL</acronym> encryption, the administrator can
  833. configure the server to reject unencrypted sessions as a byproduct
  834. of authentication checking.
  835. </p></div><div class="sect2" id="id-1.10.5.7.12"><div class="titlepage"><div><div><h3 class="title">52.2.10. <acronym class="acronym">GSSAPI</acronym> Session Encryption</h3></div></div></div><p>
  836. If <span class="productname">PostgreSQL</span> was built with
  837. <acronym class="acronym">GSSAPI</acronym> support, frontend/backend communications
  838. can be encrypted using <acronym class="acronym">GSSAPI</acronym>. This provides
  839. communication security in environments where attackers might be
  840. able to capture the session traffic. For more information on
  841. encrypting <span class="productname">PostgreSQL</span> sessions with
  842. <acronym class="acronym">GSSAPI</acronym>, see <a class="xref" href="gssapi-enc.html" title="18.10. Secure TCP/IP Connections with GSSAPI Encryption">Section 18.10</a>.
  843. </p><p>
  844. To initiate a <acronym class="acronym">GSSAPI</acronym>-encrypted connection, the
  845. frontend initially sends a GSSENCRequest message rather than a
  846. StartupMessage. The server then responds with a single byte
  847. containing <code class="literal">G</code> or <code class="literal">N</code>, indicating that it
  848. is willing or unwilling to perform <acronym class="acronym">GSSAPI</acronym> encryption,
  849. respectively. The frontend might close the connection at this point
  850. if it is dissatisfied with the response. To continue after
  851. <code class="literal">G</code>, using the GSSAPI C bindings as discussed in RFC2744
  852. or equivalent, perform a <acronym class="acronym">GSSAPI</acronym> initialization by
  853. calling <code class="function">gss_init_sec_context()</code> in a loop and sending
  854. the result to the server, starting with an empty input and then with each
  855. result from the server, until it returns no output. When sending the
  856. results of <code class="function">gss_init_sec_context()</code> to the server,
  857. prepend the length of the message as a four byte integer in network byte
  858. order. If this is successful, then use <code class="function">gss_wrap()</code> to
  859. encrypt the usual StartupMessage and all subsequent data, prepending the
  860. length of the result from <code class="function">gss_wrap()</code> as a four byte
  861. integer in network byte order to the actual encrypted payload. Note that
  862. the server will only accept encrypted packets from the client which are less
  863. than 16kB; <code class="function">gss_wrap_size_limit()</code> should be used by the
  864. client to determine the size of the unencrypted message which will fit
  865. within this limit and larger messages should be broken up into multiple
  866. <code class="function">gss_wrap()</code> calls. Typical segments are 8kB of
  867. unencrypted data, resulting in encrypted packets of slightly larger than 8kB
  868. but well within the 16kB maximum. The server can be expected to not send
  869. encrypted packets of larger than 16kB to the client. To continue after
  870. <code class="literal">N</code>, send the usual StartupMessage and proceed without
  871. encryption.
  872. </p><p>
  873. The frontend should also be prepared to handle an ErrorMessage
  874. response to GSSENCRequest from the server. This would only occur if
  875. the server predates the addition of <acronym class="acronym">GSSAPI</acronym> encryption
  876. support to <span class="productname">PostgreSQL</span>. In this case the
  877. connection must be closed, but the frontend might choose to open a fresh
  878. connection and proceed without requesting <acronym class="acronym">GSSAPI</acronym>
  879. encryption. Given the length limits specified above, the ErrorMessage can
  880. not be confused with a proper response from the server with an appropriate
  881. length.
  882. </p><p>
  883. An initial GSSENCRequest can also be used in a connection that is being
  884. opened to send a CancelRequest message.
  885. </p><p>
  886. While the protocol itself does not provide a way for the server to
  887. force <acronym class="acronym">GSSAPI</acronym> encryption, the administrator can
  888. configure the server to reject unencrypted sessions as a byproduct
  889. of authentication checking.
  890. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="protocol-overview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="protocol.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sasl-authentication.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">52.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 52.3. SASL Authentication</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1