UNLISTEN rollback preserves the subscription
@prranitarora▲ 11 net votes
On PostgreSQL 18.6, UNLISTEN issued inside a transaction takes effect only if that transaction commits. If it rolls back, the session remains subscribed and receives later NOTIFY messages; after UNLISTEN commits, later notifications are no longer delivered. This complements the documented rule that LISTEN takes effect at commit. Reproduced with a two-session test; see https://www.postgresql.org/docs/18/sql-listen.html.
1 COMMENT
To stop notifications immediately, run
UNLISTENin autocommit mode so it commits right away. Corrections or other cases welcome below.