Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/ref/notify.sgml`
aeeae65d83db13d0bce05a96668d0c4123dbe2a4ad2219be0000000100000bfd
 string literal.
      In the default configuration it must be shorter than 8000 bytes.
      (If binary data or large amounts of information need to be communicated,
      it's best to put it in a database table and send the key of the record.)
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   There is a queue that holds notifications that have been sent but not
   yet processed by all listening sessions.  If this queue becomes full,
   transactions calling <command>NOTIFY</command> will fail at commit.
   The queue is quite large (8GB in a standard installation) and should be
   sufficiently sized for almost every use case. However, no cleanup can take
   place if a session executes <command>LISTEN</command> and then enters a
   transaction for a very long time. Once the queue is half full you will see
   warnings in the log file pointing you to the session that is preventing
   cleanup. In this case you should make sure that this session ends its
   current transaction so that cleanup can proceed.
  </para>
  <para>
   The function <function>pg_notification_queue_usage</function> returns the
   fraction of the queue that is currently occupied by pending notifications.
   See <xref linkend="functions-info"/> for more information.
  </para>
  <para>
   A transaction that has executed <command>NOTIFY</command> cannot be
   prepared for two-phase commit.
  </para>

  <refsect2>
   <title>pg_notify</title>

   <indexterm>
    <primary>pg_notify</primary>
   </indexterm>

   <para>
    To send a notification you can also use the function
    <literal><function>pg_notify</function>(<type>text</type>,
    <type>text</type>)</literal>. The function takes the channel name as the
    first argument and the payload as the second. The function is much easier
    to use than the <command>NOTIFY</command> command if you need to work with
    non-constant channel names and payloads.
   </para>
  </refsect2>
 </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.
NOTIFY virtual, 'This is the payload';
Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.

LISTEN foo;
SELECT pg_notify('fo' || 'o', 'pay' || 'load');
Asynchronous notification "foo" with payload "payload" received from server process with PID 14728.
</programlisting></para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

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

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

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

Title: NOTIFY: Queue Management, pg_notify Function, Examples, and Compatibility
Summary
The notification queue can fill up, causing NOTIFY transactions to fail at commit. Long-lived LISTEN transactions can block cleanup. The function pg_notification_queue_usage provides queue usage information. Transactions with NOTIFY cannot be prepared for two-phase commit. The function pg_notify(text, text) allows sending notifications with non-constant channel names and payloads. Examples demonstrate LISTEN/NOTIFY sequences and using pg_notify. NOTIFY is not part of the SQL standard. Related commands include LISTEN and UNLISTEN, and max_notify_queue_pages can be adjusted.