gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

357 lines
22KB

  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>33.4. Asynchronous Command Processing</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="libpq-exec.html" title="33.3. Command Execution Functions" /><link rel="next" href="libpq-single-row-mode.html" title="33.5. Retrieving Query Results Row-by-Row" /></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">33.4. Asynchronous Command Processing</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-exec.html" title="33.3. Command Execution Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</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="libpq-single-row-mode.html" title="33.5. Retrieving Query Results Row-by-Row">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-ASYNC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.4. Asynchronous Command Processing</h2></div></div></div><a id="id-1.7.3.11.2" class="indexterm"></a><p>
  3. The <code class="function">PQexec</code> function is adequate for submitting
  4. commands in normal, synchronous applications. It has a few
  5. deficiencies, however, that can be of importance to some users:
  6. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  7. <code class="function">PQexec</code> waits for the command to be completed.
  8. The application might have other work to do (such as maintaining a
  9. user interface), in which case it won't want to block waiting for
  10. the response.
  11. </p></li><li class="listitem"><p>
  12. Since the execution of the client application is suspended while it
  13. waits for the result, it is hard for the application to decide that
  14. it would like to try to cancel the ongoing command. (It can be done
  15. from a signal handler, but not otherwise.)
  16. </p></li><li class="listitem"><p>
  17. <code class="function">PQexec</code> can return only one
  18. <code class="structname">PGresult</code> structure. If the submitted command
  19. string contains multiple <acronym class="acronym">SQL</acronym> commands, all but
  20. the last <code class="structname">PGresult</code> are discarded by
  21. <code class="function">PQexec</code>.
  22. </p></li><li class="listitem"><p>
  23. <code class="function">PQexec</code> always collects the command's entire result,
  24. buffering it in a single <code class="structname">PGresult</code>. While
  25. this simplifies error-handling logic for the application, it can be
  26. impractical for results containing many rows.
  27. </p></li></ul></div><p>
  28. </p><p>
  29. Applications that do not like these limitations can instead use the
  30. underlying functions that <code class="function">PQexec</code> is built from:
  31. <code class="function">PQsendQuery</code> and <code class="function">PQgetResult</code>.
  32. There are also
  33. <code class="function">PQsendQueryParams</code>,
  34. <code class="function">PQsendPrepare</code>,
  35. <code class="function">PQsendQueryPrepared</code>,
  36. <code class="function">PQsendDescribePrepared</code>, and
  37. <code class="function">PQsendDescribePortal</code>,
  38. which can be used with <code class="function">PQgetResult</code> to duplicate
  39. the functionality of
  40. <code class="function">PQexecParams</code>,
  41. <code class="function">PQprepare</code>,
  42. <code class="function">PQexecPrepared</code>,
  43. <code class="function">PQdescribePrepared</code>, and
  44. <code class="function">PQdescribePortal</code>
  45. respectively.
  46. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQSENDQUERY"><span class="term">
  47. <code class="function">PQsendQuery</code>
  48. <a id="id-1.7.3.11.4.15.1.1.2" class="indexterm"></a>
  49. </span></dt><dd><p>
  50. Submits a command to the server without waiting for the result(s).
  51. 1 is returned if the command was successfully dispatched and 0 if
  52. not (in which case, use <code class="function">PQerrorMessage</code> to get more
  53. information about the failure).
  54. </p><pre class="synopsis">
  55. int PQsendQuery(PGconn *conn, const char *command);
  56. </pre><p>
  57. After successfully calling <code class="function">PQsendQuery</code>, call
  58. <code class="function">PQgetResult</code> one or more times to obtain the
  59. results. <code class="function">PQsendQuery</code> cannot be called again
  60. (on the same connection) until <code class="function">PQgetResult</code>
  61. has returned a null pointer, indicating that the command is done.
  62. </p></dd><dt id="LIBPQ-PQSENDQUERYPARAMS"><span class="term">
  63. <code class="function">PQsendQueryParams</code>
  64. <a id="id-1.7.3.11.4.15.2.1.2" class="indexterm"></a>
  65. </span></dt><dd><p>
  66. Submits a command and separate parameters to the server without
  67. waiting for the result(s).
  68. </p><pre class="synopsis">
  69. int PQsendQueryParams(PGconn *conn,
  70. const char *command,
  71. int nParams,
  72. const Oid *paramTypes,
  73. const char * const *paramValues,
  74. const int *paramLengths,
  75. const int *paramFormats,
  76. int resultFormat);
  77. </pre><p>
  78. This is equivalent to <code class="function">PQsendQuery</code> except that
  79. query parameters can be specified separately from the query string.
  80. The function's parameters are handled identically to
  81. <code class="function">PQexecParams</code>. Like
  82. <code class="function">PQexecParams</code>, it will not work on 2.0-protocol
  83. connections, and it allows only one command in the query string.
  84. </p></dd><dt id="LIBPQ-PQSENDPREPARE"><span class="term">
  85. <code class="function">PQsendPrepare</code>
  86. <a id="id-1.7.3.11.4.15.3.1.2" class="indexterm"></a>
  87. </span></dt><dd><p>
  88. Sends a request to create a prepared statement with the given
  89. parameters, without waiting for completion.
  90. </p><pre class="synopsis">
  91. int PQsendPrepare(PGconn *conn,
  92. const char *stmtName,
  93. const char *query,
  94. int nParams,
  95. const Oid *paramTypes);
  96. </pre><p>
  97. This is an asynchronous version of <code class="function">PQprepare</code>: it
  98. returns 1 if it was able to dispatch the request, and 0 if not.
  99. After a successful call, call <code class="function">PQgetResult</code> to
  100. determine whether the server successfully created the prepared
  101. statement. The function's parameters are handled identically to
  102. <code class="function">PQprepare</code>. Like
  103. <code class="function">PQprepare</code>, it will not work on 2.0-protocol
  104. connections.
  105. </p></dd><dt id="LIBPQ-PQSENDQUERYPREPARED"><span class="term">
  106. <code class="function">PQsendQueryPrepared</code>
  107. <a id="id-1.7.3.11.4.15.4.1.2" class="indexterm"></a>
  108. </span></dt><dd><p>
  109. Sends a request to execute a prepared statement with given
  110. parameters, without waiting for the result(s).
  111. </p><pre class="synopsis">
  112. int PQsendQueryPrepared(PGconn *conn,
  113. const char *stmtName,
  114. int nParams,
  115. const char * const *paramValues,
  116. const int *paramLengths,
  117. const int *paramFormats,
  118. int resultFormat);
  119. </pre><p>
  120. This is similar to <code class="function">PQsendQueryParams</code>, but
  121. the command to be executed is specified by naming a
  122. previously-prepared statement, instead of giving a query string.
  123. The function's parameters are handled identically to
  124. <code class="function">PQexecPrepared</code>. Like
  125. <code class="function">PQexecPrepared</code>, it will not work on
  126. 2.0-protocol connections.
  127. </p></dd><dt id="LIBPQ-PQSENDDESCRIBEPREPARED"><span class="term">
  128. <code class="function">PQsendDescribePrepared</code>
  129. <a id="id-1.7.3.11.4.15.5.1.2" class="indexterm"></a>
  130. </span></dt><dd><p>
  131. Submits a request to obtain information about the specified
  132. prepared statement, without waiting for completion.
  133. </p><pre class="synopsis">
  134. int PQsendDescribePrepared(PGconn *conn, const char *stmtName);
  135. </pre><p>
  136. This is an asynchronous version of <code class="function">PQdescribePrepared</code>:
  137. it returns 1 if it was able to dispatch the request, and 0 if not.
  138. After a successful call, call <code class="function">PQgetResult</code> to
  139. obtain the results. The function's parameters are handled
  140. identically to <code class="function">PQdescribePrepared</code>. Like
  141. <code class="function">PQdescribePrepared</code>, it will not work on
  142. 2.0-protocol connections.
  143. </p></dd><dt id="LIBPQ-PQSENDDESCRIBEPORTAL"><span class="term">
  144. <code class="function">PQsendDescribePortal</code>
  145. <a id="id-1.7.3.11.4.15.6.1.2" class="indexterm"></a>
  146. </span></dt><dd><p>
  147. Submits a request to obtain information about the specified
  148. portal, without waiting for completion.
  149. </p><pre class="synopsis">
  150. int PQsendDescribePortal(PGconn *conn, const char *portalName);
  151. </pre><p>
  152. This is an asynchronous version of <code class="function">PQdescribePortal</code>:
  153. it returns 1 if it was able to dispatch the request, and 0 if not.
  154. After a successful call, call <code class="function">PQgetResult</code> to
  155. obtain the results. The function's parameters are handled
  156. identically to <code class="function">PQdescribePortal</code>. Like
  157. <code class="function">PQdescribePortal</code>, it will not work on
  158. 2.0-protocol connections.
  159. </p></dd><dt id="LIBPQ-PQGETRESULT"><span class="term">
  160. <code class="function">PQgetResult</code>
  161. <a id="id-1.7.3.11.4.15.7.1.2" class="indexterm"></a>
  162. </span></dt><dd><p>
  163. Waits for the next result from a prior
  164. <code class="function">PQsendQuery</code>,
  165. <code class="function">PQsendQueryParams</code>,
  166. <code class="function">PQsendPrepare</code>,
  167. <code class="function">PQsendQueryPrepared</code>,
  168. <code class="function">PQsendDescribePrepared</code>, or
  169. <code class="function">PQsendDescribePortal</code>
  170. call, and returns it.
  171. A null pointer is returned when the command is complete and there
  172. will be no more results.
  173. </p><pre class="synopsis">
  174. PGresult *PQgetResult(PGconn *conn);
  175. </pre><p>
  176. </p><p>
  177. <code class="function">PQgetResult</code> must be called repeatedly until
  178. it returns a null pointer, indicating that the command is done.
  179. (If called when no command is active,
  180. <code class="function">PQgetResult</code> will just return a null pointer
  181. at once.) Each non-null result from
  182. <code class="function">PQgetResult</code> should be processed using the
  183. same <code class="structname">PGresult</code> accessor functions previously
  184. described. Don't forget to free each result object with
  185. <code class="function">PQclear</code> when done with it. Note that
  186. <code class="function">PQgetResult</code> will block only if a command is
  187. active and the necessary response data has not yet been read by
  188. <code class="function">PQconsumeInput</code>.
  189. </p><div class="note"><h3 class="title">Note</h3><p>
  190. Even when <code class="function">PQresultStatus</code> indicates a fatal
  191. error, <code class="function">PQgetResult</code> should be called until it
  192. returns a null pointer, to allow <span class="application">libpq</span> to
  193. process the error information completely.
  194. </p></div></dd></dl></div><p>
  195. </p><p>
  196. Using <code class="function">PQsendQuery</code> and
  197. <code class="function">PQgetResult</code> solves one of
  198. <code class="function">PQexec</code>'s problems: If a command string contains
  199. multiple <acronym class="acronym">SQL</acronym> commands, the results of those commands
  200. can be obtained individually. (This allows a simple form of overlapped
  201. processing, by the way: the client can be handling the results of one
  202. command while the server is still working on later queries in the same
  203. command string.)
  204. </p><p>
  205. Another frequently-desired feature that can be obtained with
  206. <code class="function">PQsendQuery</code> and <code class="function">PQgetResult</code>
  207. is retrieving large query results a row at a time. This is discussed
  208. in <a class="xref" href="libpq-single-row-mode.html" title="33.5. Retrieving Query Results Row-by-Row">Section 33.5</a>.
  209. </p><p>
  210. By itself, calling <code class="function">PQgetResult</code>
  211. will still cause the client to block until the server completes the
  212. next <acronym class="acronym">SQL</acronym> command. This can be avoided by proper
  213. use of two more functions:
  214. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQCONSUMEINPUT"><span class="term">
  215. <code class="function">PQconsumeInput</code>
  216. <a id="id-1.7.3.11.7.3.1.1.2" class="indexterm"></a>
  217. </span></dt><dd><p>
  218. If input is available from the server, consume it.
  219. </p><pre class="synopsis">
  220. int PQconsumeInput(PGconn *conn);
  221. </pre><p>
  222. </p><p>
  223. <code class="function">PQconsumeInput</code> normally returns 1 indicating
  224. <span class="quote">“<span class="quote">no error</span>”</span>, but returns 0 if there was some kind of
  225. trouble (in which case <code class="function">PQerrorMessage</code> can be
  226. consulted). Note that the result does not say whether any input
  227. data was actually collected. After calling
  228. <code class="function">PQconsumeInput</code>, the application can check
  229. <code class="function">PQisBusy</code> and/or
  230. <code class="function">PQnotifies</code> to see if their state has changed.
  231. </p><p>
  232. <code class="function">PQconsumeInput</code> can be called even if the
  233. application is not prepared to deal with a result or notification
  234. just yet. The function will read available data and save it in
  235. a buffer, thereby causing a <code class="function">select()</code>
  236. read-ready indication to go away. The application can thus use
  237. <code class="function">PQconsumeInput</code> to clear the
  238. <code class="function">select()</code> condition immediately, and then
  239. examine the results at leisure.
  240. </p></dd><dt id="LIBPQ-PQISBUSY"><span class="term">
  241. <code class="function">PQisBusy</code>
  242. <a id="id-1.7.3.11.7.3.2.1.2" class="indexterm"></a>
  243. </span></dt><dd><p>
  244. Returns 1 if a command is busy, that is,
  245. <code class="function">PQgetResult</code> would block waiting for input.
  246. A 0 return indicates that <code class="function">PQgetResult</code> can be
  247. called with assurance of not blocking.
  248. </p><pre class="synopsis">
  249. int PQisBusy(PGconn *conn);
  250. </pre><p>
  251. </p><p>
  252. <code class="function">PQisBusy</code> will not itself attempt to read data
  253. from the server; therefore <code class="function">PQconsumeInput</code>
  254. must be invoked first, or the busy state will never end.
  255. </p></dd></dl></div><p>
  256. </p><p>
  257. A typical application using these functions will have a main loop that
  258. uses <code class="function">select()</code> or <code class="function">poll()</code> to wait for
  259. all the conditions that it must respond to. One of the conditions
  260. will be input available from the server, which in terms of
  261. <code class="function">select()</code> means readable data on the file
  262. descriptor identified by <code class="function">PQsocket</code>. When the main
  263. loop detects input ready, it should call
  264. <code class="function">PQconsumeInput</code> to read the input. It can then
  265. call <code class="function">PQisBusy</code>, followed by
  266. <code class="function">PQgetResult</code> if <code class="function">PQisBusy</code>
  267. returns false (0). It can also call <code class="function">PQnotifies</code>
  268. to detect <code class="command">NOTIFY</code> messages (see <a class="xref" href="libpq-notify.html" title="33.8. Asynchronous Notification">Section 33.8</a>).
  269. </p><p>
  270. A client that uses
  271. <code class="function">PQsendQuery</code>/<code class="function">PQgetResult</code>
  272. can also attempt to cancel a command that is still being processed
  273. by the server; see <a class="xref" href="libpq-cancel.html" title="33.6. Canceling Queries in Progress">Section 33.6</a>. But regardless of
  274. the return value of <code class="function">PQcancel</code>, the application
  275. must continue with the normal result-reading sequence using
  276. <code class="function">PQgetResult</code>. A successful cancellation will
  277. simply cause the command to terminate sooner than it would have
  278. otherwise.
  279. </p><p>
  280. By using the functions described above, it is possible to avoid
  281. blocking while waiting for input from the database server. However,
  282. it is still possible that the application will block waiting to send
  283. output to the server. This is relatively uncommon but can happen if
  284. very long SQL commands or data values are sent. (It is much more
  285. probable if the application sends data via <code class="command">COPY IN</code>,
  286. however.) To prevent this possibility and achieve completely
  287. nonblocking database operation, the following additional functions
  288. can be used.
  289. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQSETNONBLOCKING"><span class="term">
  290. <code class="function">PQsetnonblocking</code>
  291. <a id="id-1.7.3.11.10.2.1.1.2" class="indexterm"></a>
  292. </span></dt><dd><p>
  293. Sets the nonblocking status of the connection.
  294. </p><pre class="synopsis">
  295. int PQsetnonblocking(PGconn *conn, int arg);
  296. </pre><p>
  297. </p><p>
  298. Sets the state of the connection to nonblocking if
  299. <em class="parameter"><code>arg</code></em> is 1, or blocking if
  300. <em class="parameter"><code>arg</code></em> is 0. Returns 0 if OK, -1 if error.
  301. </p><p>
  302. In the nonblocking state, calls to
  303. <code class="function">PQsendQuery</code>, <code class="function">PQputline</code>,
  304. <code class="function">PQputnbytes</code>, <code class="function">PQputCopyData</code>,
  305. and <code class="function">PQendcopy</code> will not block but instead return
  306. an error if they need to be called again.
  307. </p><p>
  308. Note that <code class="function">PQexec</code> does not honor nonblocking
  309. mode; if it is called, it will act in blocking fashion anyway.
  310. </p></dd><dt id="LIBPQ-PQISNONBLOCKING"><span class="term">
  311. <code class="function">PQisnonblocking</code>
  312. <a id="id-1.7.3.11.10.2.2.1.2" class="indexterm"></a>
  313. </span></dt><dd><p>
  314. Returns the blocking status of the database connection.
  315. </p><pre class="synopsis">
  316. int PQisnonblocking(const PGconn *conn);
  317. </pre><p>
  318. </p><p>
  319. Returns 1 if the connection is set to nonblocking mode and 0 if
  320. blocking.
  321. </p></dd><dt id="LIBPQ-PQFLUSH"><span class="term">
  322. <code class="function">PQflush</code>
  323. <a id="id-1.7.3.11.10.2.3.1.2" class="indexterm"></a>
  324. </span></dt><dd><p>
  325. Attempts to flush any queued output data to the server. Returns
  326. 0 if successful (or if the send queue is empty), -1 if it failed
  327. for some reason, or 1 if it was unable to send all the data in
  328. the send queue yet (this case can only occur if the connection
  329. is nonblocking).
  330. </p><pre class="synopsis">
  331. int PQflush(PGconn *conn);
  332. </pre><p>
  333. </p></dd></dl></div><p>
  334. </p><p>
  335. After sending any command or data on a nonblocking connection, call
  336. <code class="function">PQflush</code>. If it returns 1, wait for the socket
  337. to become read- or write-ready. If it becomes write-ready, call
  338. <code class="function">PQflush</code> again. If it becomes read-ready, call
  339. <code class="function">PQconsumeInput</code>, then call
  340. <code class="function">PQflush</code> again. Repeat until
  341. <code class="function">PQflush</code> returns 0. (It is necessary to check for
  342. read-ready and drain the input with <code class="function">PQconsumeInput</code>,
  343. because the server can block trying to send us data, e.g. NOTICE
  344. messages, and won't read our data until we read its.) Once
  345. <code class="function">PQflush</code> returns 0, wait for the socket to be
  346. read-ready and then read the response as described above.
  347. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-exec.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-single-row-mode.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.3. Command Execution Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.5. Retrieving Query Results Row-by-Row</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1