Source login: `SourceMapper` `select_subclasses()` query triggers ~600 ms of PostgreSQL JIT compilation on every login
Describe the bug
Every login through a SAML source spends about 0.5–0.6 s of database time on one query that returns a single row. The time is spent by PostgreSQL JIT-compiling the query, not executing it. Under login load this becomes the largest single consumer of database CPU.
The query comes from SourceMapper.get_manager() in authentik/core/sources/mapper.py:
qs = self.source.user_property_mappings.all().select_subclasses() # and group_property_mappings for groups
select_subclasses() without arguments LEFT JOINs authentik_core_propertymapping with every PropertyMapping subclass table, about 15 of them, plus the M2M table. Most of these tables are tiny (0–50 rows) and rarely change, so autovacuum never analyzes them. With no statistics, the planner's estimates compound across the LEFT JOINs. In our case it estimated 165,622,615 rows and a total cost of about 8.3 million, for a query that returns 1 row. That is far above jit_above_cost (100,000) and the inline/optimize thresholds (500,000), so PostgreSQL runs full JIT with inlining and optimization on every execution. With CONN_MAX_AGE=0, each request has a new backend, so nothing is reused.
The query runs twice per source login, once for user mappings and once for group mappings (authentik/core/sources/flow_manager.py, get_manager(Group, ...)).
Measurements (EXPLAIN ANALYZE from ak shell, source with 1 user property mapping)
| Variant | Planning | Execution |
|---|---|---|
default (jit = on) |
9–11 ms | 540–660 ms |
| JIT section of that plan | Generation 5 ms, Inlining 54 ms, Optimization 278 ms, Emission 274 ms, Total 611 ms (94 functions) | |
SET jit = off |
7–9 ms | 0.7–0.8 ms |
select_subclasses(SAMLSourcePropertyMapping) |
0.3 ms | 0.2–0.4 ms |
SET geqo = off |
~9 ms | ~500 ms (no change) |
join_collapse_limit = from_collapse_limit = 20 |
90–108 ms | 1–7 ms |
In EXPLAIN, the JIT time shows up as the startup time of the first node that evaluates compiled code (here a seq scan of the 1-page M2M table). That makes it look like an I/O problem until you read the JIT: section.
To reproduce
- Create a SAML (or other) source with one user property mapping, on a database where the property mapping tables have never been analyzed (
pg_stat_user_tables.last_analyze/last_autoanalyzeare null). - In
ak shell:s = SAMLSource.objects.get(slug="<slug>") print(s.user_property_mappings.all().select_subclasses().order_by("name").explain(analyze=True)) - Check the top-level cost estimate and the
JIT:section at the end of the plan.
Expected behavior
Loading a source's property mappings should take about a millisecond and should not trigger JIT.
Impact we observed (load testing simulated SAML-source logins)
- The source ACS step had a fixed floor of ~0.5 s per login. After disabling JIT, its median dropped from 1.3 s to 0.48 s under load.
- Postgres went from using more CPU than all web workers combined to roughly the same as the web workers.
Version and deployment info
- authentik 2026.8.5, docker compose, PostgreSQL 16 (official image), default
AUTHENTIK_POSTGRESQL__CONN_MAX_AGE=0.
What we did (workaround)
ALTER SYSTEM SET jit = off;
SELECT pg_reload_conf();
ALTER DATABASE authentik SET jit = off;
ANALYZE;
ALTER SYSTEMsurvives a dump and restore and applies to already-open connections after a reload. The database-level setting is lost ondropdb/createdb.ANALYZEalone probably brings the estimate below the JIT threshold, but it is fragile: autovacuum will not re-analyze these small tables, statistics are lost on restore or major upgrade, and new subclass tables start without statistics.
Suggested fixes
- In
SourceMapper.get_manager(), narrow the join to the subclass the source actually uses:
This looks behaviour-preserving:qs = self.source.user_property_mappings.all().select_subclasses(self.source.property_mapping_type)PropertyMappingManager.compile()(authentik/lib/sync/mapper.py) already skips any mapping that is not an instance ofmapping_subclass. It also cuts planning from ~10 ms to ~0.3 ms. - Consider recommending
jit = offfor authentik databases in the PostgreSQL docs, or setting it per connection by default (for exampleoptions=-c jit=offin the connection options). authentik's workload is short OLTP queries, where JIT does not help. Other unrestrictedselect_subclasses()calls on hot paths may be affected the same way, for example inauthentik/policies/engine.py. We have not measured those.
Related observations from the same investigation (happy to file separately if useful)
- Notification rules fan out on every event.
event_post_save_notificationqueuesevent_trigger_dispatchfor every event, which queues oneevent_trigger_handlerperNotificationRule, each running a full policy engine. This happens even when a rule has no destination group (the admin UI shows these as "Enabled: No"). With the 4 default rules, every event costs 5 background tasks. With one worker process this created a backlog of thousands of tasks after a login burst. Skipping dispatch when no rule exists, or when a rule has no destination, would avoid this. - Deleting a
NotificationRulewith a long task history hangs the request.remove_tasks_on_rel_obj_deletion(authentik/tasks/signals.py) deletes every linked task one by one, with its logs, inside the web request's transaction. With about 100k tasks per rule, the request ran for over 10 minutes, held locks that blocked other requests, and had to be terminated. A bulk delete, or a background task, would help. AUTHENTIK_POSTGRESQL__CONN_MAX_AGE > 0leaks connections under load. As the docs warn for ASGI, connections grew from ~20 to themax_connectionslimit (400) within a few minutes at ~2 logins/s, causing HTTP 500s. Not a bug as such, but a stronger warning in the docs might save others from trying it.
We're disabling postgres JIT with 2026.11. Can you please re-test once it's released and report back.