PostgreSQL LISTEN/NOTIFY number of notifications per transaction with identical payloads

Viewed 534

In the PostgreSQL manual it says:

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.

Do you know how this "decision" is made?

1 Answers

That's an interesting question. Perhaps the documentation is unclear but in my experience duplicated notifications are sent only within subtransactions.

To not just guess here, let's open the PostgreSQL source code. Notification function has a test of duplicates:

/* no point in making duplicate entries in the list ... */
if (AsyncExistsPendingNotify(channel, payload))
    return;

Ok, but it does not explain the possibility of duplicates. So, we can move forward and inspect the AsyncExistsPendingNotify function. Somewhere inside this function, we found our answer in a comment:

 /* 
 * As we are not checking our parents' lists, we can still get duplicates
 * in combination with subtransactions, like in:
 *
 * begin;
 * notify foo '1';
 * savepoint foo;
 * notify foo '1';
 * commit; 
 */

So, that's it. We can have duplicated notifications when using subtransactions. The documentation could be clearer, but perhaps PostgreSQL's folks made it intentionally. Therefore I can conclude that avoiding duplicates, in this case, is not a strict requirement.

Related