Skip to content

Fix sequence number check when inserting new events#515

Merged
erikrozendaal merged 1 commit intomasterfrom
514-bug-store_events_v03sql-uses-text-min-on-sequence_number-causing-false-optimistic-locking-failures
Feb 26, 2026
Merged

Fix sequence number check when inserting new events#515
erikrozendaal merged 1 commit intomasterfrom
514-bug-store_events_v03sql-uses-text-min-on-sequence_number-causing-false-optimistic-locking-failures

Conversation

@erikrozendaal
Copy link
Member

The MIN aggregate function was sorting the sequence number using the text datatype, which does not correctly find the minium. Cast to integer to fix this.

See #514.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a critical bug in the store_events procedure where the MIN() aggregate function was performing lexicographic (text) comparison instead of numeric comparison on sequence numbers. This caused false optimistic locking failures when committing batches of 10+ events, as MIN('9', '10') returns '10' lexicographically instead of '9' numerically.

Changes:

  • Fixed the MIN() aggregate to cast sequence_number to integer before comparison in store_events_v05.sql and db/structure.sql
  • Added a test case that reproduces the bug scenario (committing events with sequence numbers crossing digit boundaries)
  • Created a migration to apply the fix to existing databases

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
db/migrate/sequent/store_events_v05.sql New version of store_events procedure with the MIN() bug fix, changing from text extraction (->>) to JSONB extraction with integer cast (->' ... '::integer)
db/structure.sql Updated to reflect the fixed store_events procedure and added the new migration version
db/migrate/20260226121600_sequent_fix_sequence_number_check.rb Migration to apply the fix, but contains a critical bug in the down method
spec/lib/sequent/core/event_store_spec.rb Added regression test that commits 8 events followed by events 9 and 10 to verify the fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The `MIN` aggregate function was sorting the sequence number using the
`text` datatype, which does not correctly find the minium. Cast to
`integer` to fix this.

See #514.
@erikrozendaal erikrozendaal force-pushed the 514-bug-store_events_v03sql-uses-text-min-on-sequence_number-causing-false-optimistic-locking-failures branch from 1269c1b to 77d6f01 Compare February 26, 2026 12:14
@erikrozendaal erikrozendaal merged commit f654f12 into master Feb 26, 2026
7 checks passed
@erikrozendaal erikrozendaal deleted the 514-bug-store_events_v03sql-uses-text-min-on-sequence_number-causing-false-optimistic-locking-failures branch February 26, 2026 15:19
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.

Bug: store_events_v03.sql uses TEXT MIN() on sequence_number, causing false optimistic locking failures

2 participants