1NOTIFY(7) SQL Commands NOTIFY(7)
2
3
4
6 NOTIFY - generate a notification
7
8
10 NOTIFY name
11
12
14 The NOTIFY command sends a notification event to each client applica‐
15 tion that has previously executed LISTEN name for the specified notifi‐
16 cation name in the current database.
17
18 NOTIFY provides a simple form of signal or interprocess communication
19 mechanism for a collection of processes accessing the same PostgreSQL
20 database. Higher-level mechanisms can be built by using tables in the
21 database to pass additional data (beyond a mere notification name) from
22 notifier to listener(s).
23
24 The information passed to the client for a notification event includes
25 the notification name and the notifying session's server process PID.
26 It is up to the database designer to define the notification names that
27 will be used in a given database and what each one means.
28
29 Commonly, the notification name is the same as the name of some table
30 in the database, and the notify event essentially means, ``I changed
31 this table, take a look at it to see what's new''. But no such associa‐
32 tion is enforced by the NOTIFY and LISTEN commands. For example, a
33 database designer could use several different notification names to
34 signal different sorts of changes to a single table.
35
36 When NOTIFY is used to signal the occurrence of changes to a particular
37 table, a useful programming technique is to put the NOTIFY in a rule
38 that is triggered by table updates. In this way, notification happens
39 automatically when the table is changed, and the application programmer
40 cannot accidentally forget to do it.
41
42 NOTIFY interacts with SQL transactions in some important ways. Firstly,
43 if a NOTIFY is executed inside a transaction, the notify events are not
44 delivered until and unless the transaction is committed. This is appro‐
45 priate, since if the transaction is aborted, all the commands within it
46 have had no effect, including NOTIFY. But it can be disconcerting if
47 one is expecting the notification events to be delivered immediately.
48 Secondly, if a listening session receives a notification signal while
49 it is within a transaction, the notification event will not be deliv‐
50 ered to its connected client until just after the transaction is com‐
51 pleted (either committed or aborted). Again, the reasoning is that if a
52 notification were delivered within a transaction that was later
53 aborted, one would want the notification to be undone somehow — but the
54 server cannot ``take back'' a notification once it has sent it to the
55 client. So notification events are only delivered between transac‐
56 tions. The upshot of this is that applications using NOTIFY for real-
57 time signaling should try to keep their transactions short.
58
59 NOTIFY behaves like Unix signals in one important respect: if the same
60 notification name is signaled multiple times in quick succession,
61 recipients might get only one notification event for several executions
62 of NOTIFY. So it is a bad idea to depend on the number of notifications
63 received. Instead, use NOTIFY to wake up applications that need to pay
64 attention to something, and use a database object (such as a sequence)
65 to keep track of what happened or how many times it happened.
66
67 It is common for a client that executes NOTIFY to be listening on the
68 same notification name itself. In that case it will get back a notifi‐
69 cation event, just like all the other listening sessions. Depending on
70 the application logic, this could result in useless work, for example,
71 reading a database table to find the same updates that that session
72 just wrote out. It is possible to avoid such extra work by noticing
73 whether the notifying session's server process PID (supplied in the
74 notification event message) is the same as one's own session's PID
75 (available from libpq). When they are the same, the notification event
76 is one's own work bouncing back, and can be ignored. (Despite what was
77 said in the preceding paragraph, this is a safe technique. PostgreSQL
78 keeps self-notifications separate from notifications arriving from
79 other sessions, so you cannot miss an outside notification by ignoring
80 your own notifications.)
81
83 name Name of the notification to be signaled (any identifier).
84
86 Configure and execute a listen/notify sequence from psql:
87
88 LISTEN virtual;
89 NOTIFY virtual;
90 Asynchronous notification "virtual" received from server process with PID 8448.
91
92
94 There is no NOTIFY statement in the SQL standard.
95
97 LISTEN [listen(7)], UNLISTEN [unlisten(7)]
98
99
100
101SQL - Language Statements 2011-09-22 NOTIFY(7)