![]() | ![]() | ![]() | ![]() |
30.7. Asynchronous NotificationPostgreSQL offers asynchronous notification via the LISTEN and NOTIFY commands. A client session registers its interest in a particular notification condition with the LISTEN command (and can stop listening with the UNLISTEN command). All sessions listening on a particular condition will be notified asynchronously when a NOTIFY command with that condition name is executed by any session. No additional information is passed from the notifier to the listener. Thus, typically, any actual data that needs to be communicated is transferred through a database table. Commonly, the condition name is the same as the associated table, but it is not necessary for there to be any associated table. libpq applications submit
LISTEN and UNLISTEN commands as
ordinary SQL commands. The arrival of NOTIFY
messages can subsequently be detected by calling
The function PGnotify *PQnotifies(PGconn *conn);
typedef struct pgNotify {
char *relname; /* notification condition name */
int be_pid; /* process ID of notifying server process */
char *extra; /* notification parameter */
} PGnotify;
After processing a PGnotify object returned
by Example 30-2 gives a sample program that illustrates the use of asynchronous notification. A better way to check for NOTIFY messages when you have no
useful commands to execute is to call
|
||||||||||||