Skip to content

packages/django-dramatiq-postgres/broker: use positive state filter for pending messages - #24074

Merged
rissson merged 1 commit into
goauthentik:mainfrom
EasyAsABC123:optimize-task-consumer-query
Jul 17, 2026
Merged

rissson merged 1 commit into
goauthentik:mainfrom
EasyAsABC123:optimize-task-consumer-query

Conversation

@EasyAsABC123

@EasyAsABC123 EasyAsABC123 commented Jul 15, 2026 •

Copy link
Copy Markdown

Details

  • Change the Postgres task consumer pending-message lookup from a negative terminal-state exclusion to a positive consumable-state filter.
  • Let PostgreSQL use the existing (queue_name, state, eta) index for queue polling.
  • Add a regression test that asserts the consumer applies the positive state filter.

What does this PR change?

Changes query to be positive and thereby near instant

Why is this change needed?

Seek over all rows in tasks, including completed tasks is slow.

How was this tested?

  • git diff --check
  • Built and published a container image from this branch.
  • Deployed the image to a Kubernetes Authentik worker/server pair.
  • Verified the running worker contains .filter(state__in=CONSUMABLE_TASK_STATES).
  • Verified PostgreSQL plans the positive-state query with authentik_t_queue_n_099d0d_idx.

On a production-like task table with about 409k rows, mostly completed tasks, the old query shape planned as a parallel sequential scan:

Parallel Seq Scan on authentik_tasks_task
cost ~= 253678

The new query shape uses the existing index:

Index Scan using authentik_t_queue_n_099d0d_idx on authentik_tasks_task
cost ~= 346

Linked issues

None opened


Checklist

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

@EasyAsABC123
EasyAsABC123 requested a review from a team as a code owner July 15, 2026 16:51
@BeryJu
BeryJu requested a review from rissson July 15, 2026 16:52
@netlify

netlify Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 94658e5
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6a58ea69df4eb7000818d39a
😎 Deploy Preview https://deploy-preview-24074--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 Jul 15, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit 94658e5
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6a58ea69fc0482000853d0a4
😎 Deploy Preview https://deploy-preview-24074--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 Jul 15, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 94658e5
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6a58ea69fd262b0008b6193f
😎 Deploy Preview https://deploy-preview-24074--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.

@EasyAsABC123
EasyAsABC123 force-pushed the optimize-task-consumer-query branch from 4211525 to ceaf6aa Compare July 15, 2026 16:58
@rissson rissson changed the title tasks: use positive state filter for pending messages packages/django-dramatiq-postgres/broker: use positive state filter for pending messages Jul 15, 2026
@rissson rissson self-assigned this Jul 15, 2026
@rissson rissson added area:backend backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5 labels Jul 15, 2026
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in authentik Core Jul 15, 2026
@EasyAsABC123
EasyAsABC123 force-pushed the optimize-task-consumer-query branch from ceaf6aa to eee6779 Compare July 15, 2026 17:17
@EasyAsABC123

Copy link
Copy Markdown
Author

@rissson all resolved, ready for another pass, when you have time

@EasyAsABC123
EasyAsABC123 force-pushed the optimize-task-consumer-query branch from eee6779 to d4ef266 Compare July 15, 2026 20:34
@codecov

codecov Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.66%. Comparing base (0c237e8) to head (e491415).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24074      +/-   ##
==========================================
- Coverage   92.83%   92.66%   -0.17%     
==========================================
  Files        1056     1056              
  Lines       62751    62765      +14     
  Branches      424      424              
==========================================
- Hits        58256    58164      -92     
- Misses       4495     4601     +106     
Flag Coverage Δ
conformance 35.55% <0.00%> (-0.01%) ⬇️
e2e 40.32% <0.00%> (-0.28%) ⬇️
integration 31.97% <0.00%> (-0.05%) ⬇️
rust 0.00% <ø> (ø)
unit 92.00% <100.00%> (+<0.01%) ⬆️
unit-migrate 92.03% <100.00%> (+<0.01%) ⬆️

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.

@EasyAsABC123
EasyAsABC123 force-pushed the optimize-task-consumer-query branch from d4ef266 to 94658e5 Compare July 16, 2026 14:27
@EasyAsABC123
EasyAsABC123 force-pushed the optimize-task-consumer-query branch from 94658e5 to e491415 Compare July 16, 2026 14:30
@rissson

rissson commented Jul 16, 2026

Copy link
Copy Markdown
Member

@EasyAsABC123 you do not need to keep rebasing on main. When you do we need to re-run the CI every time for no good reason, and we need to re-check your changes. Thanks.

@EasyAsABC123

EasyAsABC123 commented Jul 16, 2026 via email

Copy link
Copy Markdown
Author

@rissson

rissson commented Jul 17, 2026

Copy link
Copy Markdown
Member

Thank you for the contribution!

@rissson
rissson merged commit ae6882c into goauthentik:main Jul 17, 2026
116 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in authentik Core Jul 17, 2026
@authentik-cherry-pick

Copy link
Copy Markdown
Contributor

⚠️ Cherry-pick to version-2026.5 has conflicts: #24166

rissson added a commit that referenced this pull request Jul 17, 2026
…or pending messages (cherry-pick #24074 to version-2026.5) (#24166)

* Cherry-pick #24074 to version-2026.5 (with conflicts)

This cherry-pick has conflicts that need manual resolution.

Original PR: #24074
Original commit: ae6882c

* Apply suggestions from code review

Co-authored-by: Marc 'risson' Schmitt <[email protected]>
Signed-off-by: Marc 'risson' Schmitt <[email protected]>

* Apply suggestions from code review

Co-authored-by: Marc 'risson' Schmitt <[email protected]>
Signed-off-by: Marc 'risson' Schmitt <[email protected]>

---------

Signed-off-by: Marc 'risson' Schmitt <[email protected]>
Co-authored-by: Justin Schuhmann <[email protected]>
Co-authored-by: Marc 'risson' Schmitt <[email protected]>
@EasyAsABC123
EasyAsABC123 deleted the optimize-task-consumer-query branch July 17, 2026 17:18
kensternberg-authentik added a commit that referenced this pull request Jul 18, 2026
* main:
  policies: filter policy engine (#24025)
  endpoints/connectors/agent: fix auth schema for device endpoints (#24164)
  core, web: update translations (#24137)
  packages/django-dramatiq-postgres/broker: use positive state filter for pending messages (#24074)
  events: fix typo (#24161)
  root: fix agents test instructions (#24119)
  enterprise: fix enterprise_test skipping tests when used as class decorator (#24138)
  core: bump library/nginx from `ec4ed8b` to `b5a9a3c` in /website (#24146)
  core: bump github.com/pires/go-proxyproto from 0.14.0 to 0.15.0 (#24139)
  core: bump library/node from `ffc7838` to `715e55e` in /lifecycle/container (#24142)
  core: bump pnpm/pnpm from 11.12.0 to 11.13.0 in /lifecycle/container (#24144)
  core: bump library/golang from `ee5a795` to `3f1e064` in /lifecycle/container (#24143)
  core: bump pnpm/pnpm from 11.12.0 to 11.13.0 in /website (#24147)
  core: bump kubernetes from 36.0.2 to 36.0.3 (#24145)
  core: bump docker from 7.1.0 to 7.2.0 (#24149)
  web: bump webcomponent-qr-code from 1.3.0 to 2.0.0 in /web (#24152)
  core: bump regex from 1.12.4 to 1.13.0 (#24150)
  core: bump rust-toolchain from 1.97.0 to 1.97.1 (#24148)
  website/docs: cleanup 07-12: refresh contributor guidance (#23969)
  packages/ak-common/db: ignore empty passwords (#24127)
kensternberg-authentik added a commit that referenced this pull request Jul 18, 2026
* main:
  policies: filter policy engine (#24025)
  endpoints/connectors/agent: fix auth schema for device endpoints (#24164)
  core, web: update translations (#24137)
  packages/django-dramatiq-postgres/broker: use positive state filter for pending messages (#24074)
  events: fix typo (#24161)
  root: fix agents test instructions (#24119)
  enterprise: fix enterprise_test skipping tests when used as class decorator (#24138)
  core: bump library/nginx from `ec4ed8b` to `b5a9a3c` in /website (#24146)
  core: bump github.com/pires/go-proxyproto from 0.14.0 to 0.15.0 (#24139)
  core: bump library/node from `ffc7838` to `715e55e` in /lifecycle/container (#24142)
  core: bump pnpm/pnpm from 11.12.0 to 11.13.0 in /lifecycle/container (#24144)
  core: bump library/golang from `ee5a795` to `3f1e064` in /lifecycle/container (#24143)
  core: bump pnpm/pnpm from 11.12.0 to 11.13.0 in /website (#24147)
  core: bump kubernetes from 36.0.2 to 36.0.3 (#24145)
  core: bump docker from 7.1.0 to 7.2.0 (#24149)
  web: bump webcomponent-qr-code from 1.3.0 to 2.0.0 in /web (#24152)
  core: bump regex from 1.12.4 to 1.13.0 (#24150)
  core: bump rust-toolchain from 1.97.0 to 1.97.1 (#24148)
  website/docs: cleanup 07-12: refresh contributor guidance (#23969)
  packages/ak-common/db: ignore empty passwords (#24127)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend backport/version-2026.5 Add this label to PRs to backport changes to version-2026.5

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants