Skip to content

Fix cursor_factory re-wrapping with connection pooling#505

Merged
asherf merged 2 commits into
django-commons:masterfrom
karalarmehmet:fix/445-connection-pool-cursor-rewrapping
Mar 5, 2026
Merged

Fix cursor_factory re-wrapping with connection pooling#505
asherf merged 2 commits into
django-commons:masterfrom
karalarmehmet:fix/445-connection-pool-cursor-rewrapping

Conversation

@karalarmehmet

@karalarmehmet karalarmehmet commented Mar 2, 2026

Copy link
Copy Markdown
Contributor
  • Fix severe performance degradation when using Django's PostgreSQL connection pooling (pool: True) with django-prometheus
  • Move cursor_factory wrapping from get_new_connection() to get_connection_params() in both postgresql and postgis backends
  • Fix postgis backend using hardcoded "postgis" alias instead of self.alias

Closes: #445

@karalarmehmet
karalarmehmet marked this pull request as ready for review March 2, 2026 20:14
@karalarmehmet

Copy link
Copy Markdown
Contributor Author

@asherf I would appreciate your review in your spare time 👍

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_factory wrapping from get_new_connection() to get_connection_params() in both the PostgreSQL and PostGIS backends
  • Fix PostGIS backend using hardcoded "postgis" alias instead of self.alias
  • Add unit tests for the new get_connection_params() behavior in test_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.

Comment thread django_prometheus/tests/test_db_wrapper.py
Comment thread django_prometheus/tests/test_db_wrapper.py
@karalarmehmet
karalarmehmet force-pushed the fix/445-connection-pool-cursor-rewrapping branch 2 times, most recently from 6ad3a22 to b4a2ca2 Compare March 5, 2026 07:00
@karalarmehmet

Copy link
Copy Markdown
Contributor Author

@asherf Fixed the reason for test failures 👍

@karalarmehmet
karalarmehmet force-pushed the fix/445-connection-pool-cursor-rewrapping branch from b4a2ca2 to b2b7422 Compare March 5, 2026 19:36
@asherf
asherf requested a review from Copilot March 5, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread django_prometheus/db/backends/postgresql/base.py
Comment thread django_prometheus/db/backends/postgis/base.py
Comment thread django_prometheus/db/backends/postgresql/base.py
@karalarmehmet

karalarmehmet commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@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 👍

@asherf
asherf merged commit 77a983e into django-commons:master Mar 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants