Fix cursor_factory re-wrapping with connection pooling#505
Conversation
|
@asherf I would appreciate your review in your spare time 👍 |
There was a problem hiding this comment.
Pull request overview
This PR fixes a severe performance regression (issue #445) when using django-prometheus with Django 5.1's new PostgreSQL connection pooling. With connection pooling, connections are reused from a pool, but the old code was re-wrapping the cursor_factory in get_new_connection() on every connection reuse. This caused the class's __mro__ to grow unboundedly, degrading performance significantly with each request. The fix moves the cursor_factory wrapping from get_new_connection() to get_connection_params(), which is called once at pool creation time. It also fixes a pre-existing bug in the PostGIS backend where the alias was hardcoded as "postgis" instead of using self.alias.
Changes:
- Move
cursor_factorywrapping fromget_new_connection()toget_connection_params()in both the PostgreSQL and PostGIS backends - Fix PostGIS backend using hardcoded
"postgis"alias instead ofself.alias - Add unit tests for the new
get_connection_params()behavior intest_db_wrapper.py
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
django_prometheus/db/backends/postgresql/base.py |
Moves cursor_factory wrapping to get_connection_params() |
django_prometheus/db/backends/postgis/base.py |
Same fix as postgresql; also corrects hardcoded "postgis" alias to self.alias |
django_prometheus/tests/test_db_wrapper.py |
New unit test file covering the wrapping behavior and the no-re-wrap guarantee |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6ad3a22 to
b4a2ca2
Compare
|
@asherf Fixed the reason for test failures 👍 |
b4a2ca2 to
b2b7422
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@asherf Thanks for your time I checked Copilot review suggestions and responded inline with the reasons why they don't apply You can check them 👍 |
pool: True) with django-prometheuscursor_factorywrapping fromget_new_connection()toget_connection_params()in both postgresql and postgis backends"postgis"alias instead ofself.aliasCloses: #445