Skip to content

Index Seq on the NServiceBus PostgreSQL queue table - #3205

Merged
jeremydmiller merged 1 commit into
mainfrom
fix/nsb-postgresql-seq-index
Jun 22, 2026
Merged

jeremydmiller merged 1 commit into
mainfrom
fix/nsb-postgresql-seq-index

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

What

Adds a non-unique index on Seq to the NServiceBus PostgreSQL interop queue table (NServiceBusQueueTable). Follow-up to #3201.

Why

The destructive receive orders by Seq (... ORDER BY Seq LIMIT :count FOR UPDATE SKIP LOCKED). When Wolverine provisions/owns the queue table (e.g. its own listening queue), there was no index on Seq, so the ORDER BY degraded to a full sort as a backlog built up.

A soak that let the table grow made this concrete: with an un-indexed table, an unthrottled 5-minute run enqueued ~5.2M messages but the receiver only drained ~71k as throughput collapsed. With this index the same run delivered 4.5M with zero loss (receiver keeps pace) — a ~60× difference.

Why non-unique

Earlier I had a unique index here and removed it: NServiceBus puts a UNIQUE constraint on seq, Weasel can only model a unique index, and the two don't reconcile — Weasel tries to DROP the constraint-backed index (which PostgreSQL refuses) when diffing against an NServiceBus-owned table, breaking startup. A non-unique index avoids that: it has a different name and uniqueness from nsb_seq_key, so Weasel just adds it and leaves NServiceBus's constraint alone. The transport never needs to enforce seq uniqueness itself.

Validation

  • NServiceBus/PostgreSQL interop suite still green — 4/4 against a real NServiceBus.Transport.PostgreSql host (provisioning against the NServiceBus-owned table is unaffected).
  • Soak: un-indexed un­throttled 5-min ≈ 71k delivered → indexed ≈ 4.5M delivered, 0 failures, 0 loss.
  • dotnet build wolverine.slnx -c Release clean (0 warnings, 0 errors).

🤖 Generated with Claude Code

The destructive receive orders by Seq, so a Wolverine-provisioned NServiceBus
queue table needs an index on Seq or the ORDER BY degrades to a full sort once a
backlog builds. An unthrottled 5-minute soak against an un-indexed table saw
receiver throughput collapse (5.2M enqueued, only ~71k delivered as the table
grew); with this index the same soak delivered 4.5M with zero loss.

Use a non-unique index, not a unique one: NServiceBus puts a UNIQUE constraint on
seq, and Weasel can only model a unique index, so a unique index makes Weasel try
to DROP the constraint-backed index (which PostgreSQL refuses) when reconciling
against an NServiceBus-owned table. The non-unique index sidesteps that — the NSB
PostgreSQL interop suite stays green (4/4) against a real NServiceBus host.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jeremydmiller
jeremydmiller merged commit ccd095a into main Jun 22, 2026
26 checks passed
This was referenced Jun 23, 2026
This was referenced Jul 24, 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.

1 participant