Thread
-
Re: Optimize LISTEN/NOTIFY
Joel Jacobson <joel@compiler.org> — 2025-11-07T18:59:41Z
On Thu, Nov 6, 2025, at 09:33, Joel Jacobson wrote: > On Thu, Nov 6, 2025, at 00:21, Chao Li wrote: >> That’s what we don’t know. We now lack a performance test for >> evaluating how “direct advancement” efficiently helps if it only >> handles sleeping listeners. So what I was suggesting is that we should >> first create some tests, maybe also add a few more statistics, so that >> we can evaluate different solutions. If a simple implementation that >> only handles sleeping listeners would have performed good enough, of >> course we can take it; otherwise we may need to either pursue a better >> solution. Changes since v23: * The advancingPos flag has been split into two fields: bool isAdvancing: indicates if a backend is currently advancing QueuePosition advancingPos: the target position the backend will advance to * The logic in SignalBackends has been reworked and simplified, thanks to the new isAdvancing and advancingPos fields. I now think it's finally easy to reason about why each branch in SignalBackends must be correct. I've also attached 0003-optimize_listen_notify-v24.txt that adds instrumentation that can then be used together with the benchmark/correctness tool pg_async_notify_test-v24.c. This has been very helpful to me, to develop an intuition for its concurrency behavior. I hope it can help others as well. The 0003 patch is only for testing and not part of the patchset, hence the .txt. % gcc -Wall -Wextra -O2 -pthread \ -I$(pg_config --includedir-server) \ -I$(pg_config --includedir) \ -L$(pg_config --libdir) \ -o pg_async_notify_test-v24 pg_async_notify_test-v24.c \ -lpq -pthread -lm % ./pg_async_notify_test-v24 --listeners 10 --notifiers 10 --channels 10 --duration 10 10 s: 301622 sent (29409/s), 3015940 received (294185/s) Notification Latency Distribution: 0.00-0.01ms 0 (0.0%) avg: 0.000ms 0.01-0.10ms # 25 (0.0%) avg: 0.074ms 0.10-1.00ms # 289 (0.0%) avg: 0.461ms 1.00-10.00ms ######### 2824257 (93.6%) avg: 4.193ms 10.00-100.00ms # 189923 (6.3%) avg: 24.893ms >100.00ms # 1453 (0.0%) avg: 109.662ms SignalBackends Statistics: signaled_targeted # 1251569 (9.4%) advancing_behind # 108505 (0.8%) advancing_ahead # 207494 (1.6%) idle_behind # 408641 (3.1%) avoided_wakeups ####### 10589740 (79.6%) already_ahead # 744087 (5.6%) asyncQueueReadAllNotifications Statistics: necessary_wakeups ######## 1525695 (86.3%) unnecessary_wakeups # 242106 (13.7%) /Joel