]> begriffs open source - ai-pg/blob - full-docs/txt/sql-notify.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / sql-notify.txt
1
2 NOTIFY
3
4    NOTIFY — generate a notification
5
6 Synopsis
7
8 NOTIFY channel [ , payload ]
9
10 Description
11
12    The NOTIFY command sends a notification event together with an optional
13    “payload” string to each client application that has previously
14    executed LISTEN channel for the specified channel name in the current
15    database. Notifications are visible to all users.
16
17    NOTIFY provides a simple interprocess communication mechanism for a
18    collection of processes accessing the same PostgreSQL database. A
19    payload string can be sent along with the notification, and
20    higher-level mechanisms for passing structured data can be built by
21    using tables in the database to pass additional data from notifier to
22    listener(s).
23
24    The information passed to the client for a notification event includes
25    the notification channel name, the notifying session's server process
26    PID, and the payload string, which is an empty string if it has not
27    been specified.
28
29    It is up to the database designer to define the channel names that will
30    be used in a given database and what each one means. Commonly, the
31    channel name is the same as the name of some table in the database, and
32    the notify event essentially means, “I changed this table, take a look
33    at it to see what's new”. But no such association is enforced by the
34    NOTIFY and LISTEN commands. For example, a database designer could use
35    several different channel names to signal different sorts of changes to
36    a single table. Alternatively, the payload string could be used to
37    differentiate various cases.
38
39    When NOTIFY is used to signal the occurrence of changes to a particular
40    table, a useful programming technique is to put the NOTIFY in a
41    statement trigger that is triggered by table updates. In this way,
42    notification happens automatically when the table is changed, and the
43    application programmer cannot accidentally forget to do it.
44
45    NOTIFY interacts with SQL transactions in some important ways. Firstly,
46    if a NOTIFY is executed inside a transaction, the notify events are not
47    delivered until and unless the transaction is committed. This is
48    appropriate, since if the transaction is aborted, all the commands
49    within it have had no effect, including NOTIFY. But it can be
50    disconcerting if one is expecting the notification events to be
51    delivered immediately. Secondly, if a listening session receives a
52    notification signal while it is within a transaction, the notification
53    event will not be delivered to its connected client until just after
54    the transaction is completed (either committed or aborted). Again, the
55    reasoning is that if a notification were delivered within a transaction
56    that was later aborted, one would want the notification to be undone
57    somehow — but the server cannot “take back” a notification once it has
58    sent it to the client. So notification events are only delivered
59    between transactions. The upshot of this is that applications using
60    NOTIFY for real-time signaling should try to keep their transactions
61    short.
62
63    If the same channel name is signaled multiple times with identical
64    payload strings within the same transaction, only one instance of the
65    notification event is delivered to listeners. On the other hand,
66    notifications with distinct payload strings will always be delivered as
67    distinct notifications. Similarly, notifications from different
68    transactions will never get folded into one notification. Except for
69    dropping later instances of duplicate notifications, NOTIFY guarantees
70    that notifications from the same transaction get delivered in the order
71    they were sent. It is also guaranteed that messages from different
72    transactions are delivered in the order in which the transactions
73    committed.
74
75    It is common for a client that executes NOTIFY to be listening on the
76    same notification channel itself. In that case it will get back a
77    notification event, just like all the other listening sessions.
78    Depending on the application logic, this could result in useless work,
79    for example, reading a database table to find the same updates that
80    that session just wrote out. It is possible to avoid such extra work by
81    noticing whether the notifying session's server process PID (supplied
82    in the notification event message) is the same as one's own session's
83    PID (available from libpq). When they are the same, the notification
84    event is one's own work bouncing back, and can be ignored.
85
86 Parameters
87
88    channel
89           Name of the notification channel to be signaled (any
90           identifier).
91
92    payload
93           The “payload” string to be communicated along with the
94           notification. This must be specified as a simple string literal.
95           In the default configuration it must be shorter than 8000 bytes.
96           (If binary data or large amounts of information need to be
97           communicated, it's best to put it in a database table and send
98           the key of the record.)
99
100 Notes
101
102    There is a queue that holds notifications that have been sent but not
103    yet processed by all listening sessions. If this queue becomes full,
104    transactions calling NOTIFY will fail at commit. The queue is quite
105    large (8GB in a standard installation) and should be sufficiently sized
106    for almost every use case. However, no cleanup can take place if a
107    session executes LISTEN and then enters a transaction for a very long
108    time. Once the queue is half full you will see warnings in the log file
109    pointing you to the session that is preventing cleanup. In this case
110    you should make sure that this session ends its current transaction so
111    that cleanup can proceed.
112
113    The function pg_notification_queue_usage returns the fraction of the
114    queue that is currently occupied by pending notifications. See
115    Section 9.27 for more information.
116
117    A transaction that has executed NOTIFY cannot be prepared for two-phase
118    commit.
119
120 pg_notify
121
122    To send a notification you can also use the function pg_notify(text,
123    text). The function takes the channel name as the first argument and
124    the payload as the second. The function is much easier to use than the
125    NOTIFY command if you need to work with non-constant channel names and
126    payloads.
127
128 Examples
129
130    Configure and execute a listen/notify sequence from psql:
131 LISTEN virtual;
132 NOTIFY virtual;
133 Asynchronous notification "virtual" received from server process with PID 8448.
134 NOTIFY virtual, 'This is the payload';
135 Asynchronous notification "virtual" with payload "This is the payload" received
136 from server process with PID 8448.
137
138 LISTEN foo;
139 SELECT pg_notify('fo' || 'o', 'pay' || 'load');
140 Asynchronous notification "foo" with payload "payload" received from server proc
141 ess with PID 14728.
142
143 Compatibility
144
145    There is no NOTIFY statement in the SQL standard.
146
147 See Also
148
149    LISTEN, UNLISTEN, max_notify_queue_pages