I'm trying to fire a pg_notify
whenever a new row gets inserted into a table, but instead of getting a notification for every newly-inserted row, I only get one notification in total.
The trigger is
CREATE TRIGGER "testNotify"
AFTER INSERT ON mine.test
FOR EACH ROW EXECUTE PROCEDURE mine."testNotify"()
The function is
CREATE OR REPLACE FUNCTION mine."testNotify"()
RETURNS TRIGGER AS $$
DECLARE
BEGIN
PERFORM pg_notify('testNotify', 'test payload');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
The insert is
INSERT INTO mine.test (one, two, three)
SELECT one, two, three FROM temptable
ON CONFLICT DO NOTHING
I've successfully inserted about new 100,000 rows this way, so I would expect to see that many notifications if my application does LISTEN "testNotify"
, but instead it gets only 1, with a channel of testNotify
and a payload of test payload
. Am I doing something wrong above?
Quote from the manual
If the same channel name is signaled multiple times from the same transaction with identical payload strings, the database server can decide to deliver a single notification only.
(emphasis mine)
And:
On the other hand, notifications with distinct payload strings will always be delivered as distinct notifications
So, if you want to make sure you get one notification for each row, include the primary key of the row in the payload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With