Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve consecutive Event ordering in EF inserts #73

Merged
merged 3 commits into from
May 20, 2022

Conversation

BramKraai
Copy link
Contributor

The EF Core DbContext.Add and DbContext.AddRange methods sort entries alphabetically instead of preserving insertion order. This leads to violations in the ConsecutiveIndex constraint in Postgres. This error does not happen in SQL Server. (why not?)

By referencing the previous Event in Applied Events, EF Core can figure out the dependencies between events and correctly preserve the insertion order.

Alternative strategies to fix this issue:

  • Call DbContext.Add + DbContext.SaveChangesAsync for every added Event
    • Very slow, since it requires many round trips to the Database
  • Use a Bulk Insert Library that does preserve insertion order
    • We have to keep the licensing and maintenance in mind when choosing to do so
  • Write custom Bulk Insert Library that does preserve insertion order
    • Will fix this issue and improve performance, but requires many tests to cover all cases.
  • Investigate why SQL Server does work and see if the same configuration can be applied for Postgres

@nielsdisveld
Copy link
Contributor

nielsdisveld commented May 19, 2022

Investigate why SQL Server does work and see if the same configuration can be applied for Postgres

I did a simple test with adding 4 events in a single transaction and watched the logs for the generated SQL commands by EF.

When using Postgres the generated SQL insert commands seem to be a single command for every row.

While for SQL Server the generated commands seem a bit smarter since they try to have a single insert command for multiple rows. (In my example 1 command for 1 row and another command for 3 rows)

After some more testing: It is also an issue for SQL Server.

@BramKraai BramKraai merged commit 4e4c85b into main May 20, 2022
@BramKraai BramKraai deleted the fix/issue/72-consecutive-index-constraint-fails branch May 20, 2022 07:06
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.

ConsecutiveIndex constraint fail when using multiple Apply methods on save entity
3 participants