| From: | Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com> |
|---|---|
| To: | Xuneng Zhou <xunengzhou(at)gmail(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Small WAL replay perf improvements (smgropen()+reduce membarriers for LSN waiters) |
| Date: | 2026-12-02 08:20:34 |
| Message-ID: | CAKZiRmzPf7WCZqLpo4jYuzT2Ad41hWz_SYQoQz2O4qoYS3vy8w@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Mon, Nov 30, 2026 at 2:50 PM Xuneng Zhou <xunengzhou(at)gmail(dot)com> wrote:
>
> > > > 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.
Ok, done that under 0004, I hope I got right flags there.
> > > 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+ZC5Khw@mail.gmail.com
> > > [2] https://www.postgresql.org/message-id/CABPTF7UkddAnG=6X0JLHEOVWO-xoG3XYXgc1uWhbjj5eHRjipw@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.
OK I see, it's risky to me too, but then this sounds like we can leave
everything mostly as it was in the 0003 patch and we do not need to move
it to ApplyWalRecord() - as on 2nd second look to me to me that if we are
not doing this optimization by Heikki, having it explictly in the "main
redo loop" makes a lot of sense - mainly: feature code visibliity in top
code section, so I've left it as it was.
So re-sending the full patchset and including the new 0004. Thanks!
-J.
| Attachment | Content-Type | Size |
|---|---|---|
| v02122026-0001-Avoid-redundant-smgropen-in-XLogReadBuffer.patch | text/x-patch | 3.1 KB |
| v02122026-0002-Cache-the-most-recently-opened-relation-in.patch | text/x-patch | 2.2 KB |
| v02122026-0004-WAIT-FOR-LSN-avoid-WaitLSNWakeupStandby-du.patch | text/x-patch | 1.7 KB |
| v02122026-0003-WAIT-FOR-LSN-reduce-per-record-overhead-du.patch | text/x-patch | 3.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Michael Paquier | 2026-12-02 08:03:10 | Re: Fix reversed slot types in copy_replication_slot() error messages |