| From: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi> |
|---|---|
| To: | "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | Fix two race conditions in processing interrupts |
| Date: | 2026-11-16 22:32:28 |
| Message-ID: | 42b2c4f9-511e-4b5c-8edd-b474019ddf29@iki.fi |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
While I was working on my Latches vs Interrupts patch set, AI review
pointed out two race conditions that are present on master, too:
1. If a new catchup signal arrives while we're processing a previous
catchup signal, the new signal can be lost.
This is a pretty straightforward race condition.
ReceiveSharedInvalidMessages() clears catchupInterruptPending, after it
has processed all the messages. If another catchup interrupt arrives in
between, catchupInterruptPending is cleared even though there are now
more messages pending, and the "signaled" flag in shared memory is set
again.
This is relatively harmless: the consequence is that the sinval queue
might overflow, causing a cache reset, if nothing else wakes up the
process to read the invalidations. It's also very unlikely to occur in
practice; it requires the process to receive another catchup signal just
after it has caught up. That requires lots of new invalidations, and
even then, some other process is much more likely to be the laggard in
that situation.
But the fix is also pretty simple, see attached patch 0001.
2. Assertion failure if IDLE_STATS_UPDATE_TIMEOUT fires while processing
a catchup signal
I bumped into this while testing the first issue, and had a backend
paused in the middle of processing a catchup signal:
TRAP: failed Assert("s->state == TRANS_DEFAULT || s->state ==
TRANS_COMMIT || s->state == TRANS_ABORT || s->state == TRANS_PREPARE"),
File: "../master-wt/src/backend/access/transam/xact.c", Line: 902, PID:
23381
0 postgres ExceptionalCondition + 216
1 postgres GetCurrentTransactionStopTimestamp + 128
2 postgres pgstat_index_flush_cb + 204
3 postgres pgstat_flush_pending_entries + 248
4 postgres pgstat_report_stat + 436
5 postgres ProcessInterrupts + 3340
6 postgres errfinish + 840
7 postgres ReceiveSharedInvalidMessages + 572
8 postgres AcceptInvalidationMessages + 52
9 postgres AtStart_Cache + 28
10 postgres StartTransaction + 900
11 postgres StartTransactionCommand + 84
12 postgres ProcessCatchupInterrupt + 256
13 postgres ProcessClientReadInterrupt + 112
14 postgres secure_read + 456
15 postgres pq_recvbuf + 260
16 postgres pq_getbyte + 96
17 postgres SocketBackend + 44
18 postgres ReadCommand + 44
19 postgres PostgresMain + 2860
20 postgres BackendInitialize + 0
21 postgres postmaster_child_launch + 464
22 postgres BackendStartup + 304
23 postgres ServerLoop + 372
24 postgres PostmasterMain + 6560
25 postgres main + 924
In this particular case, CHECK_FOR_INTERRUPTS() was called from
errfinish(), which is only called because of a debugging message at
level DEBUG4, but there can be many other CHECK_FOR_INTERRUPTS() calls
in processing invalidations.
In ProcessInterrupts(), the condition for whether it's safe to process
the idle stats timeout looks like this:
> /*
> * If there are pending stats updates and we currently are truly idle
> * (matching the conditions in PostgresMain(), report stats now.
> */
> if (IdleStatsUpdateTimeoutPending &&
> DoingCommandRead && !IsTransactionOrTransactionBlock())
> {
> IdleStatsUpdateTimeoutPending = false;
> pgstat_report_stat(true);
> }
Currently, IsTransactionOrTransactionBlock() returns false when we are
in the middle of StartTransaction(). That doesn't feel right to me, I
don't think a backend should be considered as "truly idle" when it's in
the middle of StartTransaction(). The attached patch 0002 changes
IsTransactionOrTransactionBlock() to return true (meaning not truly
idle) in that state.
Patch 0003 is a test that exercises both of those cases. It's AI
generated and I don't intend to commit it, I'm merely including it so
you can reproduce the failures if you want to. I don't like it because
it's pretty tightly coupled with injection points and the exact code
structure.
Any comments / objections on patches 0001 and 0002?
- Heikki
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Fix-losing-a-sinval-catchup-signal-that-arrives-duri.patch | text/x-patch | 5.0 KB |
| 0002-Don-t-consider-backend-as-fully-idle-when-it-s-start.patch | text/x-patch | 3.1 KB |
| 0003-Add-a-test-for-losing-a-sinval-catchup-signal.patch | text/x-patch | 7.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zsolt Parragi | 2026-11-16 22:41:54 | Re: Dropping a composite attribute causes data integrity violations |
| Previous Message | Zsolt Parragi | 2026-11-16 22:23:18 | Re: [PATCH] Fix interval input at minimum signed field values (BUG #19746) |