> > > Other interesting thing (not pursued covered in patch) is perhaps why do > > > we even check for the WaitLSNWakeups() in case of doing pure (closed) > > > crash > > > recovery (when DB is inaccessible, way before anything could issue > > > register > > > to be waiting for anything)?
I think we can skip the check for crash recovery. Also noted this optimization earlier, but it slipped through my to-dos after prioritizing bug fixes before the release. Please feel free to do a patch if you want. I'll look at it. > > Thanks for your observation. This has been raised and disussed before, > > but not tackled yet [1][2]. I think we also need to move the new > > helper inside ApplyWalRecord() as Heikki suggested. > > > > [1] > > https://www.postgresql.org/message-id/CAPpHfdvu7bPzP1p3eZW3t6QgF39stTce=oyobyn6n2j+zc5...@mail.gmail.com > > [2] > > https://www.postgresql.org/message-id/CABPTF7UkddAnG=6x0jlheovwo-xog3xyxgc1uwhbjj5ehrj...@mail.gmail.com > > Hi Xuneng, > > thanks for Your's work on WAIT FOR LSN. I wasn't aware of this subthread, > which is my fault as I could search for it first :) -- instead I've just tried > simplest optimization as fun experiment (2-4% is not nothing). Anyway I was > just tresspasing this code and happened to notice this stuff accidently, and > I don't have urge to fix it the most optimal way: so the question is do you > want to carry on with longer term plans to of using "hierarchy idea" [2] or > not? (I could just help to bench it there) No worry at all. Thanks for your quest! If you hadn't raised them, I would've almost forgotten these potential improvements. Guess this is the charm of open source :). > Or maybe we could just to stick Heikki's suggested way [1] that once it's > after directly after SpinLockRelase() we could even get rid of this memory > barrier totally(?), but the earlier commit a80a593ab6 / [3] make me feel > hacking around this is preeeety risky thing, especially as I do not have > reodering CPUs at a hand to test it, but that should be OK (?) Back then, I was worried about this possible interleaving: > Without the barrier in WaitLSNWakeup(), this interleaving is possible: > > Initial: > minWaitedLSN = PG_UINT64_MAX > replayLSN = 90 > > Waiter: > stores minWaitedLSN = 100 > reads replayLSN before the waker publishes the new replay position > sees replayLSN = 90 > decides it should sleep > > Waker: > publishes replayLSN = 100 > reads old minWaitedLSN = PG_UINT64_MAX > skips the wakeup However, after thought more, this lost wakeup seems unlikely to occur because of the synchronization of info_lck. The waiter need to acquire info_lck to get the replayLSN to determine whether it needs sleep, which happens after registering its minWaitedLS; and the startup need to hold the info_lck to publish its progress. So the of publication of replayLSN implies its acquire & release of the info_lck, which means that the waiter has already released that lock and published its minWaitedLSN. In this sense, it seems that we don't a barrier for the need of guaranting that the waker's position store becomes globally visible before its subsequent minWaitedLSN read. That said, I'm still lean toward keeping it for the sake of explicitness. If the above reasoning holds, the current safety relies on the getter and pubilsher's use of the info_lck and the specific ordering of code of lsn registeration and checking, which seems somewhat frigile to me. If that change, especially siliently, the issue could be hard to diagnose and reason about. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
