fix(projection): prevent skipped events written within the same microsecond (#10710)

This PR fixes a bug where projections could skip events if they were
written within the same microsecond, which can occur during high load on
different transactions.

## Problem

The event query ordering was not fully deterministic. Events created at
the exact same time (same `position`) and in the same transaction
(`in_tx_order`) were not guaranteed to be returned in the same order on
subsequent queries. This could lead to some events being skipped by the
projection logic.

## Solution

To solve this, the `ORDER BY` clause for event queries has been extended
to include `instance_id`, `aggregate_type`, and `aggregate_id`. This
ensures a stable and deterministic ordering for all events, even if they
share the same timestamp.

## Additionally changes:

* Replaced a manual slice search with the more idiomatic
`slices.Contains` to skip already projected instances.
* Changed the handling of already locked projections to log a debug
message and skip execution instead of returning an error.
* Ensures the database transaction is explicitly committed.

(cherry picked from commit 25ab6b2397)
This commit is contained in:
Silvan
2025-09-12 13:26:03 +02:00
committed by Livio Spring
parent a32208118b
commit 358a0d93d0
3 changed files with 12 additions and 17 deletions

View File

@@ -91,9 +91,9 @@ func (db *Postgres) orderByEventSequence(desc, shouldOrderBySequence, useV1 bool
}
if desc {
return ` ORDER BY "position" DESC, in_tx_order DESC`
return ` ORDER BY "position" DESC, in_tx_order DESC, instance_id, aggregate_type, aggregate_id`
}
return ` ORDER BY "position", in_tx_order`
return ` ORDER BY "position", in_tx_order, instance_id, aggregate_type, aggregate_id`
}
func (db *Postgres) eventQuery(useV1 bool) string {