zitadel/internal/eventstore
Tim Möhlmann a84b259e8c
perf(oidc): nest position clause for session terminated query (#8738)
# Which Problems Are Solved

Optimize the query that checks for terminated sessions in the access
token verifier. The verifier is used in auth middleware, userinfo and
introspection.


# How the Problems Are Solved

The previous implementation built a query for certain events and then
appended a single `PositionAfter` clause. This caused the postgreSQL
planner to use indexes only for the instance ID, aggregate IDs,
aggregate types and event types. Followed by an expensive sequential
scan for the position. This resulting in internal over-fetching of rows
before the final filter was applied.


![Screenshot_20241007_105803](https://github.com/user-attachments/assets/f2d91976-be87-428b-b604-a211399b821c)

Furthermore, the query was searching for events which are not always
applicable. For example, there was always a session ID search and if
there was a user ID, we would also search for a browser fingerprint in
event payload (expensive). Even if those argument string would be empty.

This PR changes:

1. Nest the position query, so that a full `instance_id, aggregate_id,
aggregate_type, event_type, "position"` index can be matched.
2. Redefine the `es_wm` index to include the `position` column.
3. Only search for events for the IDs that actually have a value. Do not
search (noop) if none of session ID, user ID or fingerpint ID are set.

New query plan:


![Screenshot_20241007_110648](https://github.com/user-attachments/assets/c3234c33-1b76-4b33-a4a9-796f69f3d775)


# Additional Changes

- cleanup how we load multi-statement migrations and make that a bit
more reusable.

# Additional Context

- Related to https://github.com/zitadel/zitadel/issues/7639
2024-10-07 12:49:55 +00:00
..
handler fix(handler): optimise snapshot hanlding (#8652) 2024-10-02 18:34:19 +03:00
repository perf(oidc): nest position clause for session terminated query (#8738) 2024-10-07 12:49:55 +00:00
v1/models fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
v3 fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
aggregate.go feat(v3alpha): web key resource (#8262) 2024-08-14 14:18:14 +00:00
asset.go
bench_payload.txt
config.go feat(eventstore): add search table (#8191) 2024-07-03 15:00:56 +00:00
event_base.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
event.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
eventstore_bench_test.go
eventstore_pusher_test.go fix(crdb): obtain latest sequences when the tx is retried (#7795) 2024-04-18 13:07:05 +00:00
eventstore_querier_test.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
eventstore_test.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
eventstore.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
example_test.go fix(setup): init projections (#7194) 2024-01-25 17:28:20 +01:00
field.go feat(eventstore): add search table (#8191) 2024-07-03 15:00:56 +00:00
local_crdb_test.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
read_model.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
search_query_test.go fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
search_query.go perf(oidc): nest position clause for session terminated query (#8738) 2024-10-07 12:49:55 +00:00
subscription.go
unique_constraint.go
version_test.go
version.go
write_model.go