Skip to content

GH-5346 PostgreSQL scheduled-message poll gets the index SQL Server has - #5347

Merged
jeremydmiller merged 1 commit into
JasperFx:mainfrom
uniquelau:lg/pg-scheduled-index
Nov 5, 2026
Merged

jeremydmiller merged 1 commit into
JasperFx:mainfrom
uniquelau:lg/pg-scheduled-index

Conversation

@uniquelau

Copy link
Copy Markdown
Contributor

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_scheduled on (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 recover and keep_until partial indexes in GH-4316 (#4336), but not this one.

Fixes #5346

Why

PostgresqlMessageStore.WriteLoadScheduledEnvelopeSql, run by PollForScheduledMessagesAsync inside the pg_try_advisory_xact_lock transaction, asks:

where status = 'Scheduled' and execution_time <= $1 order by execution_time LIMIT N

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.

Case Plan Buffers Time
No index Seq Scan + top-N sort 27,473 135 ms
With the partial index Index Scan, no sort 12 0.085 ms
With the index, prepared $1, generic plan (as Npgsql sends it) Index Scan 12 0.047 ms
With the index, nothing due (the usual idle poll) Index Scan 2 0.013 ms

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 new IndexDefinition after the keep_until index, named through PostgresqlIdentifier.Shorten like its neighbours. The predicate has the same status = '<literal>' shape as keep_until.
  • With EnableInboxPartitioning it behaves like the existing partial indexes: it is declared on the partitioned parent and PostgreSQL creates it on each partition. Only the scheduled partition holds entries.
  • The same index also serves the durability pass's LoadDueScheduledAsync / CountDueScheduledAsync and the scheduled-message admin listing, which filter on status = 'Scheduled'.
  • Write cost is limited to rows entering, leaving or being rescheduled within the Scheduled slice. status is already in the recover and keep_until predicates, so status changes were already not HOT-eligible.
  • Existing databases pick the index up on their next migration, as they did for Durability recovery poll and handled-message cleanup cannot use any index #4316.

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, and FindDeltaAsync reports None.
  • incoming_recovery_indexes_are_stable_with_inbox_partitioning: the same existence check, and None from FindDeltaAsync, with EnableInboxPartitioning.
  • the_scheduled_poll_uses_the_scheduled_index (new): loads 20k Handled and 200 Scheduled rows, runs ANALYZE, then EXPLAINs the statement WriteLoadScheduledEnvelopeSql builds. 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 SQLite queue_ordering_is_opt_in tests 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

FetchCountsAsync on an unpartitioned PostgreSQL inbox still does an exact group by status scan 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's reltuples and 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

…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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_time for rows with status = '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

@jeremydmiller
jeremydmiller merged commit d3ce77d into JasperFx:main Nov 5, 2026
48 of 49 checks passed
This was referenced Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL: the scheduled-message poll has no supporting index and scans the whole inbox every polling cycle

3 participants