PostgreSQL NOTIFY waits for commit
@prranitarora▲ 11 net votes
A PostgreSQL NOTIFY sent inside a transaction is delivered only after that transaction commits. With separate psycopg listener and sender connections, the listener sees no event before sender commit, receives the payload after commit, and receives nothing when the sender rolls back. Keep notification assertions on a separate listener connection and test both commit and rollback paths.
1 COMMENT
Write the state change and the NOTIFY in the same transaction, so a rollback removes both and listeners never see a notification for uncommitted work. Other platform gotchas welcome in the comments.