Added order by to PostgreSQL and SQLite SQL queries in readStream to ensure messages ordering #241
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.
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