Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/listen.sgml`
862ce4e993045d29e2cb5c21753be98eb83cb5f93932c4850000000100000d6c
 <para>
   The method a client application must use to detect notification events depends on
   which <productname>PostgreSQL</productname> application programming interface it
   uses.  With the <application>libpq</application> library, the application issues
   <command>LISTEN</command> as an ordinary SQL command, and then must
   periodically call the function <function>PQnotifies</function> to find out
   whether any notification events have been received.  Other interfaces such as
   <application>libpgtcl</application> provide higher-level methods for handling notify events; indeed,
   with <application>libpgtcl</application> the application programmer should not even issue
   <command>LISTEN</command> or <command>UNLISTEN</command> directly.  See the
   documentation for the interface you are using for more details.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">channel</replaceable></term>
    <listitem>
     <para>
      Name of a notification channel (any identifier).
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   <command>LISTEN</command> takes effect at transaction commit.
   If <command>LISTEN</command> or <command>UNLISTEN</command> is executed
   within a transaction that later rolls back, the set of notification
   channels being listened to is unchanged.
  </para>

  <para>
   A transaction that has executed <command>LISTEN</command> cannot be
   prepared for two-phase commit.
  </para>

  <para>
   There is a race condition when first setting up a listening session:
   if concurrently-committing transactions are sending notify events,
   exactly which of those will the newly listening session receive?
   The answer is that the session will receive all events committed after
   an instant during the transaction's commit step.  But that is slightly
   later than any database state that the transaction could have observed
   in queries.  This leads to the following rule for
   using <command>LISTEN</command>: first execute (and commit!) that
   command, then in a new transaction inspect the database state as needed
   by the application logic, then rely on notifications to find out about
   subsequent changes to the database state.  The first few received
   notifications might refer to updates already observed in the initial
   database inspection, but this is usually harmless.
  </para>

  <para>
   <xref linkend="sql-notify"/>
   contains a more extensive
   discussion of the use of <command>LISTEN</command> and
   <command>NOTIFY</command>.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Configure and execute a listen/notify sequence from
   <application>psql</application>:

<programlisting>
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
</programlisting></para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>LISTEN</command> statement in the SQL
   standard.
  </para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-notify"/></member>
   <member><xref linkend="sql-unlisten"/></member>
   <member><xref linkend="guc-max-notify-queue-pages"/></member>
  </simplelist>
 </refsect1>
</refentry>

Title: LISTEN Parameters, Notes, Examples, and Compatibility
Summary
This section details the parameters for the LISTEN command, which include the name of the notification channel. It also provides notes on transaction behavior, potential race conditions, and best practices for using LISTEN. An example demonstrates a listen/notify sequence in psql. The command is not part of the SQL standard. Related commands are NOTIFY and UNLISTEN.