[Django] #37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1

21 views
Skip to first unread message

Django

unread,
May 29, 2026, 12:27:48 PMMay 29
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type:
| Cleanup/optimization
Status: new | Component: Core
| (Cache system)
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
The db cache backend deletes expired entries according to a
`CULL_FREQUENCY` denominator after expired entries are deleted. For
example: `2`, means delete 50%. This generates SQL like:

{{{#!sql
SELECT cache_key from ... ORDER_BY cache_key LIMIT 1 OFFSET N
DELETE FROM ... WHERE cache_key < result of query 1
}}}

But where N is 0 (`cull_num` in the code), the SELECT is unnecessary. I'm
suggesting there should be another fast path where `cull_num` is 0 that
simply exits.

I spotted this in a real user's logs, see #32785.

This would happen when `CULL_FREQUENCY` > `MAX_ENTRIES` + 1. I imagine the
use cases for that configuration would be:
- a culling strategy that is age-based instead of random, see
[https://forum.djangoproject.com/t/django-database-cache-untouched-
expired-entries-are-never-culled-unless-max-entries-is-reached/22679
forum]
- in tests, overriding `MAX_ENTRIES` to 0 or 1 for determinism, which
would then fall below the default `CULL_FREQUENCY` of 3.
--
Ticket URL: <https://code.djangoproject.com/ticket/37130>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 30, 2026, 8:36:50 AMMay 30
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vishy):

* owner: (none) => Vishy
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:1>

Django

unread,
Jun 1, 2026, 3:35:49 AMJun 1
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
--------------------------------------+------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: Cleanup/optimization | Status: assigned
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:2>

Django

unread,
Jun 7, 2026, 10:17:56 AMJun 7
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
--------------------------------------+------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: Cleanup/optimization | Status: assigned
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Vishy):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:3>

Django

unread,
Jun 8, 2026, 12:05:10 PMJun 8
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
--------------------------------------+------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: Cleanup/optimization | Status: assigned
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:4>

Django

unread,
Jun 9, 2026, 1:38:50 PMJun 9
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
--------------------------------------+------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: Cleanup/optimization | Status: assigned
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Vishy):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:5>

Django

unread,
Jun 9, 2026, 2:31:09 PMJun 9
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:6>

Django

unread,
Jun 9, 2026, 2:53:40 PMJun 9
to django-...@googlegroups.com
#37130: Unnecessary query in db cache backend when CULL_FREQUENCY > MAX_ENTRIES + 1
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Vishy
Type: | Status: closed
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"867c7c0451f2e67e715da5b55b5bf6696747fdc3" 867c7c0]:
{{{#!CommitTicketReference repository=""
revision="867c7c0451f2e67e715da5b55b5bf6696747fdc3"
Fixed #37130 -- Skipped DB cache deletion when culling offset is zero.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/37130#comment:7>
Reply all
Reply to author
Forward
0 new messages