zitadel/internal
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
..
actions
activity feat: trusted (instance) domains (#8369) 2024-07-31 18:00:38 +03:00
admin/repository/eventsourcing
api fix: add default organization as query for org v2 list (#8719) 2024-10-07 09:28:39 +00:00
auth/repository fix: user grants deactivation (#8634) 2024-09-17 12:18:29 +00:00
auth_request/repository
authz fix: internal check of JWT access tokens (#8486) 2024-08-26 09:26:13 +00:00
cache perf(cache): pgx pool connector (#8703) 2024-10-04 13:15:41 +00:00
command fix: twilio code generation and verification (#8728) 2024-10-07 07:12:44 +02:00
config feat(v3alpha): read actions (#8357) 2024-08-12 22:32:01 +02:00
crypto feat(v3alpha): web key resource (#8262) 2024-08-14 14:18:14 +00:00
database perf(cache): pgx pool connector (#8703) 2024-10-04 13:15:41 +00:00
domain fix: ignore projectID and origin check for service accounts (#8704) 2024-10-01 16:38:28 +02:00
eventstore perf(oidc): nest position clause for session terminated query (#8738) 2024-10-07 12:49:55 +00:00
execution feat: add schema user create and remove (#8494) 2024-08-28 19:46:45 +00:00
feature perf(oidc): disable push of user token meta-event (#8691) 2024-09-26 13:55:41 +00:00
form
i18n
iam
id
idp fix(SAML): check on empty nameID (#8714) 2024-10-03 08:17:33 +00:00
integration perf(cache): pgx pool connector (#8703) 2024-10-04 13:15:41 +00:00
logstore
migration
net
notification feat: Add Twilio Verification Service (#8678) 2024-09-26 09:14:33 +02:00
org
project
protoc
qrcode
query perf(oidc): nest position clause for session terminated query (#8738) 2024-10-07 12:49:55 +00:00
renderer
repository fix: twilio code generation and verification (#8728) 2024-10-07 07:12:44 +02:00
static feat: Add Twilio Verification Service (#8678) 2024-09-26 09:14:33 +02:00
statik
telemetry feat: enable application performance profiling (#8442) 2024-08-16 13:26:53 +00:00
test
user feat(oidc): end session by id_token_hint and without cookie (#8542) 2024-09-04 10:14:50 +00:00
v2 fix(eventstore): revert precise decimal (#8527) (#8679) 2024-09-24 18:43:29 +02:00
view/repository
webauthn chore(tests): use a coverage server binary (#8407) 2024-09-06 14:47:57 +02:00
zerrors fix(crypto): reject decrypted strings with non-UTF8 characters. (#8374) 2024-08-02 08:38:37 +00:00