PostgreSQL Notify
I don't know much about PostgreSQL's NOTIFY functionality, but from what I think it is, I think it might work well with server sent events (and I just want to learn about it in general).
References
Notes
NOTIFY
- generate a notification
NOTIFY channel [ , payload ]
TheNOTIFY
command sends a notification event together with an optionalpayloadstring to each client application that has previously executedLISTEN
channel for the specified channel name in the current database. Notifications are visible to all users.
NOTIFY
provides a simple interprocess communication mechanism for a collection of processes accessing the same PostgreSQL database. A payload string can be sent along with the notification, and higher-level mechanisms for passing structured data can be built by using tables in the database to pass additional data from notifier to listener(s).
The information passed to the client for a notification event includes the notification channel name, the notifying session's server process PID, and the payload string, which is an empty string if it has not been specified.
It is up to the database designer to define the channel names that will be used in a given database and what each one means. Commonly, the channel name is the same as the name of some table in the database, and the notify event essentially means, I changed this table, take a look to see what's new
.
WhenNOTIFY
is used to signal the occurrence of changes to a particular table, a useful programming technique is to put theNOTIFY
in a statement trigger that is triggered by table updates. In this way, notification happens automatically when the table is changed, and the application programmer cannot accidentally forget to do it.
If a NOTIFY
is executed inside a transaction, the notify events are not delivered until and unless the transaction is committed. This is appropriate, since if the transaction is aborted, all the commands within it have no effect, including NOTIFY
. But it can be disconcerting if one is expecting the notification events to be delivered immediately.
If a listening session receives a notification signal while it is within a transaction, the notification event will not be delivered to its connected client until just after the transaction is completed (either committed or aborted).
If the same channel name is signaled multiple times with identical payload strings within the same transaction, any one instance of the notification event is delivered to listeners. On the other hand, notifications with distinct payload strings will always be delivered as distinct notifications.
Parameters
channel
- Name of the notification channel to be signaled
payload
- The
payload
string to be communicated along with the notification. This must be specified as a simple string literal. In the default configuration it must be shorter than 8000 bytes.
- The
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 NOTIFY will fail at commit. The queue is quite large (8GB in a standard installation) and should be sufficiently sized for almost every use case.
pg_notify
To send a notification, you can also use the function pg_notify(text, text)
. 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 NOTIFY
command if you need to work with non-constant channel names and payloads.
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.
Asynchronous Notifications
PostgreSQL offers asynchronous notification via the LISTEN
and NOTIFY
commands. A client session registers its interests in a particular notification channel with the LISTEN
command (and can stop listening with the UNLISTEN
command). All sessions listening on a particular channel will be notified asynchronously when a NOTIFY
command with that channel name is executed by any session, A payload
string can be passes to communicate additional data to the listeners.
libpq applications submit LISTEN
, UNLISTEN
, and NOTIFY
commands as ordinary SQL commands. The arrival of `NOTIFY messages can subsequently be detected by calling PGnotifies
.
The function PGNotifies
returns the next notification from a list of unhandled notification messages received from the server. It returns a null pointer if there are no pending notifications. Once a notification is returned from PGNotifies
, it is considered handled and will be moved form the list of notifications.
PGnotify *PQnotifies(PGconn *conn);
typedef struct pgNotify
{
char *relname; /* notification channel name */
int be_pid; /* process ID of notifying server process */
char *extra; /* notification payload string */
} PGnotify;
Comments
You have to be logged in to add a comment
User Comments
There are currently no comments for this article.