8000 When expiring sessions, check if sessions exist to avoid a lot of extra queries by ahus1 · Pull Request #55014 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

When expiring sessions, check if sessions exist to avoid a lot of extra queries - #55014

Draft
ahus1 wants to merge 3 commits into
keycloak:mainfrom
ahus1:is-54886-skip-expiration-empty-realm
Draft

ahus1 wants to merge 3 commits into
keycloak:mainfrom
ahus1:is-54886-skip-expiration-empty-realm

Conversation

@ahus1
@ahus1 ahus1 commented Nov 10, 2026 •
Copy link
Copy Markdown
Member

Closes #54886

Each expiration run first looks for a single session of the realm and type with a one-row index lookup, and only runs the per-bucket queries if one exists. The lookup is a prefix seek on the existing expiration index, so no schema change is needed and the fix can be backported.

Decision: nothing is remembered between runs. An earlier version recorded the time a realm was found empty and skipped it until the idle timeout passed. That saved the probe query, but it depended on the wall clock, on node-local state in a shared singleton, and on the assumption that new sessions never carry a timestamp older than the observation. It was fragile under time offsets and clock changes, and it needed extra guards and tests. The probe costs one query per realm and type per run instead of about 259, which resolves the issue without that risk. A per-bucket high-water mark is tracked in #55012.

Decision: the probe runs in its own short transaction. The expiration runs in an outer transaction while the deletes use inner transactions, and a read in the outer one could hold shared locks under strict isolation levels that the deletes then wait for.

Assumption: for offline sessions the probe does not filter by REMEMBER_ME, as that column can be true, false or NULL there. The skipped migration of the remember-me flag only concerns regular sessions, which are probed separately.

Assumption: the integration test uses Hibernate statistics, which are shared with the periodic expiration task, so it asserts on query kinds and retries up to three times. It fails without the fix.

…ra queries

Closes keycloak#54886

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
ahus1 and others added 2 commits November 10, 2026 21:04
Closes keycloak#54886

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
…ty realms

Closes keycloak#54886

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session expiration task issues ~37x more SELECTs per realm since 26.8.0 (64 session buckets), causing constant DB load with many realms

1 participant

0