Opened 5 years ago

Closed 3 years ago

#28595 closed New feature (fixed)

Database "execute hooks" for instrumentation

Reported by: Shai Berger Owned by: Shai Berger
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Adam Johnson, Carl Meyer Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Quoting, editing and summarizing a 5-month old thread from django-developers:

Add the ability to install general instrumentation hooks around the database execute() and executemany() calls.

Such hooks would allow all sorts of interesting features. For one, they could replace the current special-case allowing assertNumQueries() & friends to record queries out of debug mode (it's an ugly hack, really), but they could also support other use cases:

  • Prevent database access during execution of render() calls and serialization, to make sure all database access is being done explicitly in the view.
  • Preventing database access during tests unless specifically requested by the test (per @carljm, pytest-django does this, currently via monkeypatching).
  • Listen in on all executed queries for performance analysis (per @adamchainz, django-perf-rec currently monkey patches connection.ops.last_executed_query to achieve this)

Change History (5)

comment:1 Changed 5 years ago by Shai Berger

Needs documentation: unset
Needs tests: unset

(I was a little confused about the meaning of the "needs tests" and "needs docs" flags)

comment:2 Changed 5 years ago by Tim Graham <timograham@…>

In d612026c:

Refs #28595 -- Added a hook to add execute wrappers for database queries.

Thanks Adam Johnson, Carl Meyer, Anssi Kääriäinen, Mariusz Felisiak,
Michael Manfre, and Tim Graham for discussion and review.

comment:3 Changed 5 years ago by Tim Graham

Has patch: unset

comment:4 Changed 3 years ago by Raphael Gaschignard

I might be missing something here, so I am not changing the state myself. But I believe this instrumentation has been successfully added in 2.0? As found at

comment:5 Changed 3 years ago by Adam Johnson

Resolution: fixed
Status: assignedclosed

Thanks Raphael, yes this feature was merged in 2.0, the ticket was just left in limbo as open. Closing now.

Note: See TracTickets for help on using tickets.
Back to Top