Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

oskardudycz
Copy link
Collaborator

@oskardudycz oskardudycz commented May 2, 2025

Based on the @dawidranda finding in #239, it seems that sometimes the PostgreSQL event store does not return events in the proper order.

Emmett has an index on stream_id and stream position, which is ascending, so when reading, it uses the index and returns messages in the right order.

If it doesn't work for you, it probably means that Postgres is not using an index for some reason. One potential reason is that it may only have rows from a single stream and then decide to do a full scan, which will be faster than using an index.

I wasn't able to reproduce the issues on my local box, even when adding tests simulating the scenario described in #239, but adding ORDER BY won't do harm, as it should still use the index, so we should not get the performance penalty. Plus relying on a certain query plan is not the smartest move.

Applied the same for SQLite implementation.

Fixes #239

Based on the @dawidranda finding in #239, it seems that sometimes
PostgreSQL event store is not returning events in the proper order.

Emmett has an index on stream_id and stream position which is ascending,
so when reading it's using the index and returning messages in right
order.

If it doesn't work for you then it probably means that for some reason
Postgres is not using index. The potential reason is that maybe it only
has rows from a single stream, and then it decides that doing full scan
will be faster than using index.

I wasn't able to reproduce on my local box the issues, even when adding
tests simulating scenario described in #239, but adding ORDER BY won't
do harm, as it should still use index, so we should not get the
performance penalty.

Fixes #239
@oskardudycz oskardudycz added this to the 0.38.0 milestone May 2, 2025
@oskardudycz oskardudycz merged commit 3b2fc30 into main May 2, 2025
6 checks passed
@oskardudycz oskardudycz deleted the fixes_to_order_by_in_read_stream branch May 2, 2025 19:22
@oskardudycz oskardudycz changed the title Added order by to SQL querues in readStream to ensure messages ordering Added order by to SQL queries in readStream to ensure messages ordering May 4, 2025
@oskardudycz oskardudycz changed the title Added order by to SQL queries in readStream to ensure messages ordering Added order by to PostgreSQL SQL queries in readStream to ensure messages ordering Aug 22, 2025
@oskardudycz oskardudycz changed the title Added order by to PostgreSQL SQL queries in readStream to ensure messages ordering Added order by to PostgreSQL and SQLite SQL queries in readStream to ensure messages ordering Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Events returned in incorrect order by readStream
1 participant