| From: | Bryan Green <dbryan(dot)green(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi> |
| Subject: | LWLock tranches registered by SLRUs are thrown away at startup |
| Date: | 2026-10-28 23:31:47 |
| Message-ID: | b7890f7f-a39c-4f82-80b1-1199589c2f6e@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Greetings,
When an SLRU leaves its tranche IDs at 0, shmem_slru_init() registers
tranches for it at startup. LWLockShmemInit() runs a moment later and
resets the tranche count, so those tranches are forgotten. Nothing
looks wrong at first, because every backend inherits the postmaster's
count. But the next tranche anyone registers is handed the SLRU's ID
and takes over its name. From then on, the SLRU's locks show up under
that name in pg_stat_activity and the trace probes, and looking up the
SLRU's other tranche can fail with "tranche N is not registered".
Once such an SLRU exists, any preloaded library that registers a tranche
is enough to trigger it. No builtin SLRU leaves its IDs at 0, so
nothing shipping hits this yet. That is the only way an extension can
define an SLRU, though, so the first extension that does will hit it.
This came in with 2e0943a8597 and is in REL_19_STABLE.
The attached patch adds a test that registers one more tranche and
checks the names. Without the lwlock.c change, it fails on Linux and
Windows with:
ERROR: tranche 102 is not registered
I ran into this while setting up a TraceLogging (ETW) backend for the
static probes on Windows, which will be a separate patch. Under
EXEC_BACKEND, backends don't inherit the postmaster's count, so with
the LWLock probes on, the first acquisition of one of the SLRU's locks
fails.
The fix initializes the tranche table once, from whichever of
LWLockNewTrancheId() and LWLockShmemInit() gets there first, and never
resets it after that. If you'd rather fix the ordering in shmem.c, I
can rework it that way.
--
Bryan Green
EDB: https://www.enterprisedb.com
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Do-not-lose-LWLock-tranches-allocated-during-shmem-init.patch | text/plain | 6.8 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zsolt Parragi | 2026-10-28 23:48:38 | Re: Logical replication: lost updates/deletes and invalid log messages caused by SnapshotDirty + concurrent updates |
| Previous Message | Tomas Vondra | 2026-10-28 23:05:53 | Re: Fix BRIN numeric_minmax_multi_ops out of range value range merging |