1NOTIFY(7) PostgreSQL 13.3 Documentation NOTIFY(7)
2
3
4
6 NOTIFY - generate a notification
7
9 NOTIFY channel [ , payload ]
10
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
87 channel
88 Name of the notification channel to be signaled (any identifier).
89
90 payload
91 The “payload” string to be communicated along with the
92 notification. This must be specified as a simple string literal. In
93 the default configuration it must be shorter than 8000 bytes. (If
94 binary data or large amounts of information need to be
95 communicated, it's best to put it in a database table and send the
96 key of the record.)
97
99 There is a queue that holds notifications that have been sent but not
100 yet processed by all listening sessions. If this queue becomes full,
101 transactions calling NOTIFY will fail at commit. The queue is quite
102 large (8GB in a standard installation) and should be sufficiently sized
103 for almost every use case. However, no cleanup can take place if a
104 session executes LISTEN and then enters a transaction for a very long
105 time. Once the queue is half full you will see warnings in the log file
106 pointing you to the session that is preventing cleanup. In this case
107 you should make sure that this session ends its current transaction so
108 that cleanup can proceed.
109
110 The function pg_notification_queue_usage returns the fraction of the
111 queue that is currently occupied by pending notifications. See
112 Section 9.26 for more information.
113
114 A transaction that has executed NOTIFY cannot be prepared for two-phase
115 commit.
116
117 pg_notify
118 To send a notification you can also use the function pg_notify(text,
119 text). The function takes the channel name as the first argument and
120 the payload as the second. The function is much easier to use than the
121 NOTIFY command if you need to work with non-constant channel names and
122 payloads.
123
125 Configure and execute a listen/notify sequence from psql:
126
127 LISTEN virtual;
128 NOTIFY virtual;
129 Asynchronous notification "virtual" received from server process with PID 8448.
130 NOTIFY virtual, 'This is the payload';
131 Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.
132
133 LISTEN foo;
134 SELECT pg_notify('fo' || 'o', 'pay' || 'load');
135 Asynchronous notification "foo" with payload "payload" received from server process with PID 14728.
136
138 There is no NOTIFY statement in the SQL standard.
139
141 LISTEN(7), UNLISTEN(7)
142
143
144
145PostgreSQL 13.3 2021 NOTIFY(7)