-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Refs #33497 -- Added connection pool support for PostgreSQL. #17594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2867c32
to
e5884b8
Compare
e5884b8
to
96afd8c
Compare
Lovely, thank you for picking up my work. |
9933116
to
bf048c2
Compare
bf048c2
to
4af72c8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this @sarahboyce. I left some minor comments, but it looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One subtlety with this trick that I checked is that pool.open
is thread safe; luckily it is 👍
4af72c8
to
372ff92
Compare
0a24043
to
003b3e2
Compare
003b3e2
to
1c9a7fa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update, I left a few more replies and comments.
f975d56
to
14c65fd
Compare
Fwiw, while it is nice if the test suite runs with a pool enabled I don't think it is a requirement to get this merged. The test suite is rather special after all. Nevertheless it might also show us bugs in the implementation.
…On Wed, Dec 13, 2023, at 22:26, Sarah Boyce wrote:
***@***.**** commented on this pull request.
In tests/backends/postgresql/tests.py
<#17594 (comment)>:
> @@ -223,6 +223,82 @@ def test_connect_non_autocommit(self):
finally:
new_connection.close()
+ @unittest.skipUnless(is_psycopg3, "psycopg3 specific test")
+ def test_connect_pool(self):
+ from psycopg_pool import PoolTimeout
+
+ new_connection = connection.copy()
+ self.assertIsNone(new_connection.pool)
+
+ new_connection.settings_dict["OPTIONS"]["pool"] = {
+ "min_size": 2,
+ "timeout": 0.1,
+ }
+ new_connection._connection_pools = {}
I investigated and `cache.tests.DBCacheWithTimeZoneTests` was failing
because it was running `createcachetable` management command on test
setUp (which then creates a database connection in the pool). If I add
similar logic to `_close_all_connections` which I am running on on the
test database cloning/creating methods, then this all passes.
I think these issues are more around test state clean up which I guess
I'm still trying to figure out.
I can add tests around the timezone setting so I don't remove the logic again 😁
—
Reply to this email directly, view it on GitHub
<#17594 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAT5C3RKLYVTNGHM6G6C2TYJIMRLAVCNFSM6AAAAABAPRHCS6VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTOOBQGUYTOMJRGY>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
8a4741f
to
6e7dbdc
Compare
Merged in 6e520d9. |
Thanks for all the work on this! Is there any update on when it will be merged in and included in a Django Release? |
No, we don't give any ETAs since we are doing this in our free time. If you want to get this in faster, please test it and review the code.
…On Wed, Jan 31, 2024, at 16:45, Rohan Challa wrote:
Thanks for all the work on this! Is there any update on when it will be
merged in and included in a Django Release?
—
Reply to this email directly, view it on GitHub
<#17594 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAT5C7FYRA6EPESGIZFDOTYRJRKHAVCNFSM6AAAAABAPRHCS6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJZGM3TKMJWG4>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I'm finding this does the trick to limit PG connections when running under ASGI (in that not every request grabs a new connection). Awesome work; much less complicated than pg_bouncer for most needs Here's my test repo using the patch branch if anyone wants to extend: https://github.com/gerardrbentley/demo-django |
django/test/utils.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why this can't be moved to the Postgres backend destroy_test_db
method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing specific no, should be doable 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got rid of it completely, couldn't find the reason why it was there :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I found it. When tests do fail it leaves the pool open. I wonder if we should add a hook to Django to prepare the base backend for something like close_pool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this hook already exist in the form of destroy_test_db
? Why can't it be moved there?
In other words, why can't destroy_test_db
simply do
if hasattr(self.connection, "close_pool"):
connection.close_pool()
For the time being
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because there are more locations where I might wanna do some closing, like here:
self.connection.close() |
But yes, for now I will probably add it inside destroy_test_db
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made it postgres specific in _destroy_test_db
for now.
fc7078b
to
5b9263c
Compare
Hi! I'm looking to use this thread pool in the future, in combination with Is this something that can/will be considered in the requirements? Is this threadpool even something that can be used with gevent in the first place? Thank you 🙏 |
As far as I know Django doesn't support |
d03886a
to
74f2407
Compare
I think gevent should work without any problem (and correctly) with this implementation, can you provide a way to reproduce what you're seeing? |
74f2407
to
340e848
Compare
I started working on this patch .... 🚧 |
Fair enough:)
…On Thu, Feb 22, 2024, at 15:31, Mariusz Felisiak wrote:
***@***.**** commented on this pull request.
In django/db/backends/postgresql/base.py
<#17594 (comment)>:
> @@ -256,7 +255,9 @@ def get_database_version(self):
def get_connection_params(self):
settings_dict = self.settings_dict
# None may be used to connect to the default 'postgres' db
- if settings_dict["NAME"] == "" and not
settings_dict["OPTIONS"].get("service"):
+ if settings_dict["NAME"] == "" and not
settings_dict.get("OPTIONS", {}).get(
Sure, maybe it's not needed. However, it has nothing to do with
connection pooling, so we should merge it as a separate cleanup.
—
Reply to this email directly, view it on GitHub
<#17594 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAT5C6BXZ6YMBD6SZ5VKGDYU5JCLAVCNFSM6AAAAABAPRHCS6VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQOJWGAYDMMJXGE>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
ab62fda
to
856058e
Compare
0d8ea9d
to
a691ea1
Compare
a691ea1
to
107aa76
Compare
Superseded by #17914. |
@apollo13 has done all the hard work in this PR: #16881 hoping to push it over the line.
Also hope the tests are testing the right thing, tried to take inspiration from
psycopg_pool
tests: https://github.com/psycopg/psycopg/blob/52ed68a9d699c1e43e07a4d04441534197552de5/tests/pool/test_pool.py#L103But let me know what I'm missing - very new space for me 👍
Related ticket: https://code.djangoproject.com/ticket/33497