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.

72 satır
6.6KB

  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.8. Asynchronous Notification</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-fastpath.html" title="33.7. The Fast-Path Interface" /><link rel="next" href="libpq-copy.html" title="33.9. Functions Associated with the COPY Command" /></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.8. Asynchronous Notification</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-fastpath.html" title="33.7. The Fast-Path Interface">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-copy.html" title="33.9. Functions Associated with the COPY Command">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-NOTIFY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.8. Asynchronous Notification</h2></div></div></div><a id="id-1.7.3.15.2" class="indexterm"></a><p>
  3. <span class="productname">PostgreSQL</span> offers asynchronous notification
  4. via the <code class="command">LISTEN</code> and <code class="command">NOTIFY</code>
  5. commands. A client session registers its interest in a particular
  6. notification channel with the <code class="command">LISTEN</code> command (and
  7. can stop listening with the <code class="command">UNLISTEN</code> command). All
  8. sessions listening on a particular channel will be notified
  9. asynchronously when a <code class="command">NOTIFY</code> command with that
  10. channel name is executed by any session. A <span class="quote">“<span class="quote">payload</span>”</span> string can
  11. be passed to communicate additional data to the listeners.
  12. </p><p>
  13. <span class="application">libpq</span> applications submit
  14. <code class="command">LISTEN</code>, <code class="command">UNLISTEN</code>,
  15. and <code class="command">NOTIFY</code> commands as
  16. ordinary SQL commands. The arrival of <code class="command">NOTIFY</code>
  17. messages can subsequently be detected by calling
  18. <code class="function">PQnotifies</code>.<a id="id-1.7.3.15.4.7" class="indexterm"></a>
  19. </p><p>
  20. The function <code class="function">PQnotifies</code> returns the next notification
  21. from a list of unhandled notification messages received from the server.
  22. It returns a null pointer if there are no pending notifications. Once a
  23. notification is returned from <code class="function">PQnotifies</code>, it is considered
  24. handled and will be removed from the list of notifications.
  25. </p><pre class="synopsis">
  26. PGnotify *PQnotifies(PGconn *conn);
  27. typedef struct pgNotify
  28. {
  29. char *relname; /* notification channel name */
  30. int be_pid; /* process ID of notifying server process */
  31. char *extra; /* notification payload string */
  32. } PGnotify;
  33. </pre><p>
  34. After processing a <code class="structname">PGnotify</code> object returned
  35. by <code class="function">PQnotifies</code>, be sure to free it with
  36. <code class="function">PQfreemem</code>. It is sufficient to free the
  37. <code class="structname">PGnotify</code> pointer; the
  38. <code class="structfield">relname</code> and <code class="structfield">extra</code>
  39. fields do not represent separate allocations. (The names of these fields
  40. are historical; in particular, channel names need not have anything to
  41. do with relation names.)
  42. </p><p>
  43. <a class="xref" href="libpq-example.html#LIBPQ-EXAMPLE-2" title="Example 33.2. libpq Example Program 2">Example 33.2</a> gives a sample program that illustrates
  44. the use of asynchronous notification.
  45. </p><p>
  46. <code class="function">PQnotifies</code> does not actually read data from the
  47. server; it just returns messages previously absorbed by another
  48. <span class="application">libpq</span> function. In ancient releases of
  49. <span class="application">libpq</span>, the only way to ensure timely receipt
  50. of <code class="command">NOTIFY</code> messages was to constantly submit commands, even
  51. empty ones, and then check <code class="function">PQnotifies</code> after each
  52. <code class="function">PQexec</code>. While this still works, it is deprecated
  53. as a waste of processing power.
  54. </p><p>
  55. A better way to check for <code class="command">NOTIFY</code> messages when you have no
  56. useful commands to execute is to call
  57. <code class="function">PQconsumeInput</code>, then check
  58. <code class="function">PQnotifies</code>. You can use
  59. <code class="function">select()</code> to wait for data to arrive from the
  60. server, thereby using no <acronym class="acronym">CPU</acronym> power unless there is
  61. something to do. (See <code class="function">PQsocket</code> to obtain the file
  62. descriptor number to use with <code class="function">select()</code>.) Note that
  63. this will work OK whether you submit commands with
  64. <code class="function">PQsendQuery</code>/<code class="function">PQgetResult</code> or
  65. simply use <code class="function">PQexec</code>. You should, however, remember
  66. to check <code class="function">PQnotifies</code> after each
  67. <code class="function">PQgetResult</code> or <code class="function">PQexec</code>, to
  68. see if any notifications came in during the processing of the command.
  69. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-fastpath.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-copy.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.7. The Fast-Path Interface </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.9. Functions Associated with the <code class="command">COPY</code> Command</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1