Repository navigation
GH-5346 PostgreSQL scheduled-message poll gets the index SQL Server has - #5347
Merged
Merged
Conversation
…Server has The scheduled-message poll (`status = 'Scheduled' and execution_time <= now order by execution_time limit N`) runs every ScheduledJobPollingTime inside the advisory-lock transaction, and none of the PostgreSQL inbox indexes covers it -- so every cycle was a sequential scan plus a sort of an inbox dominated by retained Handled rows. SQL Server got a filtered idx_wolverine_incoming_envelopes_scheduled index in JasperFxGH-4318; PostgreSQL got the recover and keep_until partial indexes in JasperFxGH-4316 but not this one. On a 302k-row inbox (300k Handled, 2k Scheduled) the poll drops from a 135ms seq scan + top-N sort (27,473 buffers) to a 0.05ms index scan with no sort (12 buffers), and the idle poll with nothing due to a 2-buffer probe. The index is partial on the Scheduled slice, so it stays small and costs writes only for rows entering, leaving or being rescheduled within that slice. The durability pass's due-scheduled load and count and the scheduled-message admin listing filter on the same predicate. Existing databases pick it up on their next migration. Tests extend the JasperFxGH-4316 delta-stability tests: the index is declared, exists after apply, and FindDeltaAsync reports None both unpartitioned and with inbox partitioning, so the predicate round-trips through pg_get_indexdef. A new test EXPLAINs the poll's own statement against an analyzed inbox of mostly Handled rows and asserts an index scan on the new index with no seq scan and no sort. Fixes JasperFx#5346 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VMMyc4rK7Q8FxoEMdiqcYU
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the PostgreSQL scheduled-message index change.
No open issues 🎉
What changed in this PR
Adds the missing PostgreSQL scheduled-message index, bringing it in line with SQL Server and allowing scheduled-message polling to avoid full inbox scans and sorting.
Changes:
- Adds a partial index on
execution_timefor rows withstatus = 'Scheduled'. - Extends index-existence and migration-stability checks for partitioned and unpartitioned inboxes.
- Adds an execution-plan regression test using the actual scheduled-poll SQL.
| File | Description |
|---|---|
src/Persistence/Wolverine.Postgresql/Schema/IncomingEnvelopeTable.cs |
Declares the partial scheduled-message index. |
src/Persistence/PostgresqlTests/EnvelopeTables_recovery_index_creation.cs |
Verifies index creation, migration stability, and polling index usage. |
Review effort: Balanced
This was referenced Nov 6, 2026
This was referenced Nov 16, 2026
This was referenced Nov 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On PostgreSQL the scheduled-message poll scans the whole incoming envelope table on every polling cycle. This adds a partial index,
idx_wolverine_incoming_envelopes_scheduledon(execution_time) where status = 'Scheduled', so the poll becomes a small index range scan with no sort.SQL Server already has this index (GH-4318, #4346). PostgreSQL got the
recoverandkeep_untilpartial indexes in GH-4316 (#4336), but not this one.Fixes #5346
Why
PostgresqlMessageStore.WriteLoadScheduledEnvelopeSql, run byPollForScheduledMessagesAsyncinside thepg_try_advisory_xact_locktransaction, asks:None of the existing inbox indexes (primary key, owner, recover, keep_until) can serve it, so the plan is a sequential scan plus a sort. On a busy system most of the inbox is retained Handled rows, so the cost grows with message rate, not with the number of scheduled messages. The poll runs every
ScheduledJobPollingTime(5 seconds by default) per message database.Evidence
EXPLAIN (ANALYZE, BUFFERS) on PostgreSQL 18.4, with a copy of the table holding 302k rows (300k Handled, 2k Scheduled, half of them due). The full repro and plans are in #5346.
$1, generic plan (as Npgsql sends it)On a small inbox (about 500 rows) the planner keeps the sequential scan, which is the right choice, so the index costs nothing there.
The change
src/Persistence/Wolverine.Postgresql/Schema/IncomingEnvelopeTable.cs: one newIndexDefinitionafter the keep_until index, named throughPostgresqlIdentifier.Shortenlike its neighbours. The predicate has the samestatus = '<literal>'shape as keep_until.EnableInboxPartitioningit behaves like the existing partial indexes: it is declared on the partitioned parent and PostgreSQL creates it on each partition. Only thescheduledpartition holds entries.LoadDueScheduledAsync/CountDueScheduledAsyncand the scheduled-message admin listing, which filter onstatus = 'Scheduled'.statusis already in the recover and keep_until predicates, so status changes were already not HOT-eligible.Tests
All in
src/Persistence/PostgresqlTests/EnvelopeTables_recovery_index_creation.cs, extending the GH-4316 tests:incoming_recovery_indexes_are_created_and_stable: the table declares the scheduled index, the index exists after apply, andFindDeltaAsyncreportsNone.incoming_recovery_indexes_are_stable_with_inbox_partitioning: the same existence check, andNonefromFindDeltaAsync, withEnableInboxPartitioning.the_scheduled_poll_uses_the_scheduled_index(new): loads 20k Handled and 200 Scheduled rows, runs ANALYZE, then EXPLAINs the statementWriteLoadScheduledEnvelopeSqlbuilds. It asserts an Index Scan on the new index with no Seq Scan and no Sort. It runs the real poll statement, so a later change to the query that stops matching the index fails the test. The SQLitequeue_ordering_is_opt_intests check their indexes the same way, with EXPLAIN QUERY PLAN.Without the index change, the three tests fail; with it, all four in the class pass on net9.0 and net10.0. I also ran the related PostgreSQL classes on net9.0 (message store, partitioned compliance, partitioned scheduled promotion, durable scheduled messages, fetch counts, dead letter indexes, IdAndDestination identity, basic functionality): 309 passed, 0 failed.
Not in this PR
FetchCountsAsyncon an unpartitioned PostgreSQL inbox still does an exactgroup by statusscan every metrics tick (107 ms on the same 302k-row table). With this index in place, PostgreSQL could do what GH-4318 did for SQL Server: estimate Scheduled from this index'sreltuplesand Handled from the keep_until index's. Those estimates are sampled rather than exact, so the GH-3885 small-table fallback would still be needed. I've kept that for a separate PR.🤖 Generated with Claude Code
https://claude.ai/code/session_01VMMyc4rK7Q8FxoEMdiqcYU