LISTEN

Name

LISTEN  --  Listen for a notification event.

Synopsis

LISTEN name
  

Parameters

name

This parameter takes the name of the notify condition to which the backend will be listening for.

Results

LISTEN

This message is displayed if the command is successful and the backend process is now listening for notification.

NOTICE Async_Listen: We are already listening on name

This message is displayed if the backend is already listening for the specified notify condition.

Description

The NOTIFY and LISTEN commands work together to create a system for PostgreSQL components to communicate between themselves. Use the LISTEN command to start the backend process listening for a notification named name. Notifications are passed by the NOTIFY command, which is usually placed in rules related to changes of various database components; in this way, notifications are sent out to listeners when things are changed.

All currently running backend processes that are listening for a specific notification will be sent that notification when a NOTIFY command is issued for it by any of the processes. Once a backend process receives that notification, it sends it to the connected frontend application (such as psql), which then handles the notification however its application programming interfact (API) is setup to handle them. For more information about this method of IPC (inter-process communication), see NOTIFY.

You can use any valid string no longer than 31 characters for the name of the notification to listen to (this is also true for the NOTIFY command); you do not need to specify the name of a table as the name of your notification. To stop a backend process from listening for notifications, use the UNLISTEN command.

Examples

Configure and execute a listen/notify sequence from psql:

booktown=# LISTEN PUBLISHER_UPDATE;
LISTEN
booktown=# NOTIFY PUBLISHER_UPDATE;
Asynchronous NOTIFY 'publisher_update' from backend with pid '16864' received.