LISTEN and NOTIFY let different parts of a database talk to each other instantly. This helps programs react quickly when something changes.
LISTEN and NOTIFY for pub-sub in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
LISTEN channel_name;
NOTIFY channel_name, 'optional_message';LISTEN waits for notifications on a named channel.
NOTIFY sends a message to all listeners on that channel.
LISTEN new_order;
NOTIFY new_order, 'Order #123 placed';NOTIFY new_order;
In one session, you run LISTEN to wait for messages on 'new_order'. In another session, you run NOTIFY to send a message. The first session immediately gets the message.
-- Open two database sessions. -- Session 1: Listen for notifications LISTEN new_order; -- Session 2: Send a notification NOTIFY new_order, 'Order #123 placed'; -- Back to Session 1, you will see a notification received message.
LISTEN and NOTIFY are asynchronous; your session must be actively waiting to receive notifications.
Notifications are lightweight and fast but not guaranteed to be delivered if the listener disconnects.
Use LISTEN and NOTIFY to reduce polling and improve real-time responsiveness in your apps.
LISTEN waits for messages on a channel.
NOTIFY sends messages to all listeners on that channel.
This helps different parts of your app communicate instantly through the database.
Practice
LISTEN command do in PostgreSQL's pub-sub system?Solution
Step 1: Understand the role of LISTEN
The LISTEN command makes the current session wait for notifications on a given channel.Step 2: Differentiate from NOTIFY
NOTIFY sends messages, while LISTEN waits to receive them.Final Answer:
It waits for notifications on a specified channel. -> Option DQuick Check:
LISTEN = wait for messages [OK]
- Confusing LISTEN with NOTIFY
- Thinking LISTEN creates channels
- Assuming LISTEN deletes notifications
updates?Solution
Step 1: Recall the syntax for LISTEN
The correct syntax to listen on a channel isLISTEN channel_name;.Step 2: Check each option
LISTEN updates; matches the correct syntax:LISTEN updates;. Others are invalid commands.Final Answer:
LISTEN updates; -> Option BQuick Check:
LISTEN syntax = LISTEN channel_name; [OK]
- Using NOTIFY instead of LISTEN
- Using non-existent commands like SUBSCRIBE
- Missing semicolon at the end
Session 1:
LISTEN channel1;Session 2:
NOTIFY channel1, 'hello';What will Session 1 receive?
Solution
Step 1: Understand LISTEN and NOTIFY interaction
Session 1 listens on channel1, so it waits for notifications on that channel.Step 2: Analyze NOTIFY command
Session 2 sends a notification on channel1 with payload 'hello'. This message is delivered to all listeners on channel1.Final Answer:
A notification on channel1 with payload 'hello' -> Option CQuick Check:
NOTIFY sends payload to LISTENers [OK]
- Thinking LISTEN ignores payloads
- Believing NOTIFY causes errors
- Assuming messages go to wrong channels
NOTIFY 'channel1';What is the problem with this command?
Solution
Step 1: Check NOTIFY syntax
The syntax is NOTIFY channel [ , payload ]; channel can be an unquoted identifier or a string literal.Step 2: Verify the command
NOTIFY 'channel1';is valid and notifies channel 'channel1' with no payload.Final Answer:
The command is correct and will work. -> Option AQuick Check:
NOTIFY accepts string literals for channel [OK]
- Thinking NOTIFY requires a payload
- Believing NOTIFY needs LISTEN first
- Thinking channel names must not be quoted
chan1 and chan2 with the same message 'update'. Which approach correctly sends notifications to both channels?Solution
Step 1: Understand NOTIFY behavior
NOTIFY sends a message to one channel at a time; it cannot notify multiple channels in one command.Step 2: Analyze options
RunNOTIFY chan1, 'update'; NOTIFY chan2, 'update';in one transaction. runs two NOTIFY commands, one per channel, correctly notifying both. Options A, B, and D misuse syntax or commands.Final Answer:
Run NOTIFY chan1, 'update'; NOTIFY chan2, 'update'; in one transaction. -> Option AQuick Check:
One NOTIFY per channel needed [OK]
- Trying to notify multiple channels in one NOTIFY
- Using LISTEN to send messages
- Quoting multiple channels as one string
