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 running backend processes that are listening for a specific notification are sent that notification when a NOTIFY command is issued for it by any of the processes. When a backend process receives a notification, that notification is sent to the client application (such as
psql
), which then handles the notification in whatever manner it is written for. 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 for (this is also true for the NOTIFY command). To stop a backend process from listening for notifications, use the UNLISTEN command.
Example
The following example configures and executes 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.