Skip to content

root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks - #23013

Merged
rissson merged 8 commits into
goauthentik:mainfrom
ryanpesek:postgresql-direct-endpoint
Jul 15, 2026
Merged

rissson merged 8 commits into
goauthentik:mainfrom
ryanpesek:postgresql-direct-endpoint

Conversation

@ryanpesek

Copy link
Copy Markdown
Contributor

Why

authentik has three subsystems that hold state on a single PostgreSQL
backend across multiple statements: the Channels Postgres layer (LISTEN
on a long-lived connection), the dramatiq worker (LISTEN + session-scoped
pg_advisory_lock), and the startup migration's pg_advisory_lock(1000).
Behind a transaction-pooling pooler (PgBouncer pool_mode=transaction,
Cloud SQL managed pooling) all three break silently — the pooler returns
the backend to the pool at every COMMIT, severing the LISTEN registration
and releasing the lock without anyone telling the app.

This blocks deployments from using a transaction-mode pooler in front of
authentik at all, even though such a pooler is the standard way to deal
with PG connection exhaustion in production. Today operators run direct
connections from every app thread, hitting the max_connections ceiling
under load.

What

Adds an optional postgresql.direct.* config block. When set, a Django
database alias named direct is added; the three session-stable
subsystems route through it while ORM traffic continues through the
default alias (which can now safely point at a transaction-pooling
pooler).

Safety

Opt-in. When postgresql.direct.* is unset, postgresql_direct_db_enabled()
returns False, no Django alias is added, and all subsystems use default
as before. Identical behavior to the current codebase.


Checklist

  • The project has been linted, built, and tested (make all)
  • The documentation has been updated and formatted (make docs)

@ryanpesek
ryanpesek requested review from a team as code owners June 11, 2026 18:39
@netlify

netlify Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit e4a8307
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6a576fcc59eee600089e628f
😎 Deploy Preview https://deploy-preview-23013--authentik-integrations.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jun 11, 2026

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 5b1bc10
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6a2b00fe415c320008c35265
😎 Deploy Preview https://deploy-preview-23013--authentik-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit e4a8307
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a576fccd1112d0007e28c8c
😎 Deploy Preview https://deploy-preview-23013--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.12281% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.68%. Comparing base (65a581b) to head (e4a8307).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
authentik/lib/config.py 95.12% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23013      +/-   ##
==========================================
- Coverage   92.69%   92.68%   -0.01%     
==========================================
  Files        1045     1047       +2     
  Lines       61414    61637     +223     
  Branches      424      424              
==========================================
+ Hits        56927    57129     +202     
- Misses       4487     4508      +21     
Flag Coverage Δ
conformance 36.02% <9.21%> (-0.11%) ⬇️
e2e 41.16% <9.21%> (-0.12%) ⬇️
integration 32.41% <9.21%> (-0.14%) ⬇️
rust 0.00% <ø> (ø)
unit 91.83% <99.12%> (+0.02%) ⬆️
unit-migrate 91.85% <99.12%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ryanpesek
ryanpesek force-pushed the postgresql-direct-endpoint branch from 5b1bc10 to b1c2ae3 Compare June 11, 2026 19:48
@BeryJu BeryJu changed the title PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks Jun 12, 2026
@rissson rissson self-assigned this Jun 15, 2026
@rissson

rissson commented Jun 17, 2026

Copy link
Copy Markdown
Member

Yes! In principle I agree and want this change to make it in, but I have a bit of thinking to do about how this will affect future plans of moving this stuff out of Python.

Also, I don't see this affecting advisory locks everywhere, like where we use pglock instead of raw SQL.

@ryanpesek

Copy link
Copy Markdown
Contributor Author

Yes! In principle I agree and want this change to make it in, but I have a bit of thinking to do about how this will affect future plans of moving this stuff out of Python.

Also, I don't see this affecting advisory locks everywhere, like where we use pglock instead of raw SQL.

@rissson Good point about pglock. I just pushed a commit that I think covers all of that now.

@rissson

rissson commented Jun 18, 2026

Copy link
Copy Markdown
Member

Thanks! I'll take care of implementing this in packages/ak-common/src/db.rs as well

rissson added 3 commits July 9, 2026 16:53
Signed-off-by: Marc 'risson' Schmitt <[email protected]>
Signed-off-by: Marc 'risson' Schmitt <[email protected]>
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Jul 9, 2026
@rissson

rissson commented Jul 9, 2026

Copy link
Copy Markdown
Member

@ryanpesek do you think it makes sense to force migrations to go through the direct connection if configured?

@ryanpesek

Copy link
Copy Markdown
Contributor Author

@ryanpesek do you think it makes sense to force migrations to go through the direct connection if configured?

That seems like the safest option to me as that would avoid any quirks that a pooler could introduce.

@rissson
rissson merged commit c6b8a20 into goauthentik:main Jul 15, 2026
116 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Jul 15, 2026
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
* main:
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
* main: (104 commits)
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
  ...
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
…card-2

* web/cleanup/schedule-card-1: (104 commits)
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  web: bump aws-cdk from 2.1128.1 to 2.1129.0 (#24046)
  stages/authenticator_webauthn: Update FIDO MDS3 & Passkey aaguid blobs (#24039)
  packages/django-dramatiq-postgres/broker: close unusable PostgreSQL connections (#24023)
  api/search: add number and boolean support in AKQL queries on JSON fields (#23418) (#24028)
  enterprise: simplify tests (#18404)
  website/docs: Remove Public Release Docs. (#24031)
  website/docs: sys-mgmt: tweak settings wording (#24029)
  website/docs: cleanup 07-12: update release process guidance (#23970)
  website/integrations: add infiscal (#23996)
  ...
kensternberg-authentik added a commit that referenced this pull request Jul 15, 2026
…card-3-problem-children

* web/cleanup/schedule-card-2: (113 commits)
  website/docs: release notes for 2026.5.5 (#24070)
  website/docs: release notes for 2026.2.6 (#24069)
  web, locale: Normalize legacy locale file names (#24024)
  web/bug: harmonize and make routine the display of tasks (part 4: schedule-list cards) (#23855)
  security: automated internal backport of patch 1822.sec.patch to authentik-main (#24064)
  security: automated internal backport of patch 1934.sec.patch to authentik-main (#24067)
  security: automated internal backport of patch 1887.sec.patch to authentik-main (#24065)
  security: automated internal backport of patch 1919.sec.patch to authentik-main (#24066)
  security: automated internal backport of patch 1817.sec.patch to authentik-main (#24063)
  web/flow: revert locale-driven flow re-request from FlowExecutor (#24050)
  web/bug: harmonize and make routine the display of tasks (part 3: problem children) (#23840)
  root: PostgreSQL direct-endpoint support for LISTEN/NOTIFY and advisory locks (#23013)
  docs/social-logins: Add jumpcloud oidc and saml (#24048)
  core, web: update translations (#24037)
  web: bump @goauthentik/eslint-config from 2.0.0 to 2.0.2 in /web in the goauthentik group across 1 directory (#24007)
  ci: bump taiki-e/install-action from 2.82.9 to 2.83.2 in /.github/actions/setup (#24045)
  website: bump openapi-to-postmanv2 from 6.2.0 to 6.3.0 in /website (#24041)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /lifecycle/container (#24042)
  core: bump pnpm/pnpm from 11.11.0 to 11.12.0 in /website (#24043)
  web: bump @sentry/browser from 10.63.0 to 10.64.0 in /web in the sentry group across 1 directory (#24044)
  ...
takwerx added a commit to takwerx/infra-TAK that referenced this pull request Sep 30, 2026
…~30 s to be picked up

Field report, Justin Davis (TN TAK): password-reset emails land late. He proposed a separate
`worker-email` container running a custom poller over `authentik_tasks_task`. The symptom is
real; the cause is not email-specific.

Since v0.9.23 `_ensure_authentik_pgbouncer()` put the server AND the worker behind PgBouncer in
transaction mode. Authentik 2026.x's task broker (django-dramatiq-postgres) needs LISTEN on
`authentik.tasks.default.enqueue` and a session advisory lock per message, and PgBouncer lists
both as "Never" under transaction pooling (https://www.pgbouncer.org/features.html). So the
worker only found work on its CONSUMER_LISTEN_TIMEOUT fallback poll (30 s), and each unlock
landed on a different pooled backend than its lock and leaked it.

Measured from authentik_tasks_tasklog ("queued" -> "being processed"), empty queues:
  test6/test8/test12 (2026.5.6), nuc (2026.8.2, Rocky non-root): p50 17-30 s for every actor;
  513-1123 "you don't own a lock" Postgres warnings per box per day.
Worker alone pointed at Postgres on test12: 0.08-1.5 s pickup, 0 lock warnings (33 the hour
before), worker connections flat at 5-7 over the soak.

- `_ensure_authentik_worker_direct_pg()` (new): on the bundled topology only, moves the WORKER's
  AUTHENTIK_POSTGRESQL__HOST to `postgresql`, recreates the worker alone under the compose
  lock, verifies the live container, rolls back on failure. Runs after the pgbouncer step at
  deploy and on every console boot, so existing boxes get it from a console update. The server
  stays on PgBouncer: the #20714 leak it caps lives in the server's gunicorn workers.
- `_ensure_authentik_pgbouncer()`: "wired" now means the SERVER is on pgbouncer. Requiring the
  worker there too would flip it back and recreate server+worker on every boot.
- `_authentik_pgbouncer_pg_activity_breakdown()`: the worker's connections are `worker_direct`,
  so a fresh install whose server is still booting is not logged "bypassed".

Upstream's proper fix is goauthentik/authentik#23013 (2026.8.0+, AUTHENTIK_POSTGRESQL__DIRECT__*),
which also covers the server's Channels LISTEN; filed to ROADMAP for the 2026.8 vetting.

Validated: local harness on real test8/test12 compose files, 39/39 (existing box, second-boot
no-op, pgbouncer gate no-op, exited-worker rollback, fresh install, external PG, absent); the
old gate fails 7 of them. pre-merge-check clean apart from the pre-existing oversize_files
ratchet; redteam-selfaudit no regressions. Not yet T&E'd.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants