pgsql: Use a hash table to de-duplicate NOTIFY events faster. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Use a hash table to de-duplicate NOTIFY events faster.
Date
Msg-id E1hyIW7-0002lw-KJ@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Use a hash table to de-duplicate NOTIFY events faster.

Previously, async.c got rid of duplicate notifications by scanning
the list of pending events to compare each one to the proposed new
event.  This works okay for very small numbers of distinct events,
but degrades as O(N^2) for many events.  We can improve matters by
using a hash table to probe for duplicates.  So as not to add a
lot of overhead for the simple cases that the code did handle well
before, create the hash table only once a (sub)transaction has
queued more than 16 distinct notify events.

A downside is that we now have to do per-event work to propagate
a successful subtransaction's notify events up to its parent.
(But this isn't significant unless the subtransaction had many
events, in which case the O(N^2) behavior would have been in
play already, so we still come out ahead.)

We can make some lemonade out of this lemon, though: since we must
examine each event anyway, it's now possible to de-duplicate events
fully, rather than skipping that for events merged up from
subtransactions.  Hence, remove the old weasel wording in notify.sgml
about whether de-duplication happens or not, and adjust the test
case in async-notify.spec that exhibited the old behavior.

While at it, rearrange the definition of struct Notification to make
it more compact and require just one palloc per event, rather than
two or three.  This saves space when there are a lot of events,
in fact more than enough to buy back the space needed for the hash
table.

Patch by me, based on discussions around a different patch
submitted by Filip Rembiałkowski.

Discussion: https://postgr.es/m/17822.1564186806@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/bb5ae8f6c4161e1742a90f27b697eeb14812e65f

Modified Files
--------------
doc/src/sgml/ref/notify.sgml                 |   6 +-
src/backend/commands/async.c                 | 331 ++++++++++++++++++++-------
src/test/isolation/expected/async-notify.out |   2 -
3 files changed, 250 insertions(+), 89 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Doc: improve documentation about postgresql.auto.conf.
Next
From: Tom Lane
Date:
Subject: pgsql: Fix plpgsql to re-look-up composite type names at need.