8000 Add docs for using Psycopg 3 connection pooling by chrispy-snps · Pull Request #12540 · sqlalchemy/sqlalchemy · GitHub
[go: up one dir, main page]

Skip to content

Add docs for using Psycopg 3 connection pooling #12540

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

chrispy-snps
Copy link

Updates documentation to describe how to use Psycopg 3 connection pooling in SQLAlchemy. This is a follow-up to discussion #12522.

Description

The Postgres > psycopg dialect documentation is updated. I used the Oracle connection pool docs as a reference to ensure consistent documentation style.

Checklist

  • A documentation / typographical / small typing error fix
    • Good to go, no issue or tests are needed

Copy link
Member
@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

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

Thanks, nice addition.

Left a couple of suggestions

downsizing for unused connections), connection health pre-checks, and support
for both synchronous and asynchronous code environments.

To take advantage of ``psycopg``'s pool,
Copy link
Member

Choose a reason for hiding this comment

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

I'm not a fan of this section since we have the example just below, but let's wait mike's opinion on this

Copy link
Author

Choose a reason for hiding this comment

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

@CaselIT - this is patterned after the Oracle docs here. I am happy to make any changes requested.

Copy link
Member

Choose a reason for hiding this comment

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

I just meant the list part. Let's wait mike opinion on this

the Psycopg 3 documentation for ``psycopg_pool.ConnectionPool``,
which provides more information on available configuration parameters,
logging, and more.

Copy link
Member

Choose a reason for hiding this comment

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

since the async version is not 100% the same it may make sense to provide the example example also in async

Copy link
Author

Choose a reason for hiding this comment

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

@CaselIT - I added an incremental example for async code. If you prefer a full example, I can do that too.

@CaselIT CaselIT requested a review from zzzeek April 24, 2025 20:09
Signed-off-by: chrispy <chrispy@synopsys.com>
@@ -117,6 +117,20 @@ def close(self):
creator=mypool.getconn, # Use Psycopg 3 connection pool to obtain connections
)

Usage of ``psycopg_pool.AsyncConnectionPool`` is similar, but it requires the
following ``psycopg.AsyncConnection`` subclass instead:
Copy link
Member

Choose a reason for hiding this comment

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

I think it make sense to mention also the use of the AsyncConnectionPool and async_creator

Copy link
Author

Choose a reason for hiding this comment

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

@CaselIT - good feedback. There were indeed enough differences that a more complete example was needed. It's there now.

Signed-off-by: chrispy <chrispy@synopsys.com>
@chrispy-snps
Copy link
Author
chrispy-snps commented May 3, 2025

@dvarrazzo - when I use the asynchronous code structure in this pull request (complete runnable example here) with logging enabled, I get the following warning:

venv/lib/python3.11/site-packages/psycopg_pool/pool_async.py:142: RuntimeWarning: opening the async pool AsyncConnectionPool in the constructor is deprecated and will not be supported anymore in a future release. Please use `await pool.open()`, or use the pool as context manager using: `async with AsyncConnectionPool(...) as pool: `...

How should the code change to resolve this? It is not clear to me.

@dvarrazzo
Copy link

As I have re-opened psycopg/psycopg#1046 maybe we want to wait for which shape it takes before merging this?

@dvarrazzo
Copy link
dvarrazzo commented May 4, 2025

@dvarrazzo - when I use the asynchronous code structure in this pull request (complete runnable example here) with logging enabled, I get the following warning:

venv/lib/python3.11/site-packages/psycopg_pool/pool_async.py:142: RuntimeWarning: opening the async pool AsyncConnectionPool in the constructor is deprecated and will not be supported anymore in a future release. Please use `await pool.open()`, or use the pool as context manager using: `async with AsyncConnectionPool(...) as pool: `...

How should the code change to resolve this? It is not clear to me.

You must open the pool in closed state and use await pool.open() explicitly. For example:

async def main():
    mypool = psycopg_pool.AsyncConnectionPool(
        connection_class=MyAsyncConnection,
        conninfo=db_url.replace("+psycopg", ""),
        open=False,
    )
    await mypool.open()

This is documented here but maybe it's unclear that open=False is required in the c'tor.

@chrispy-snps
Copy link
Author

@dvarrazzo - thanks! I updated the asynchronous example there to open the pool outside the constructor.

Signed-off-by: chrispy <chrispy@synopsys.com>
@CaselIT
Copy link
Member
CaselIT commented May 4, 2025

As I have re-opened psycopg/psycopg#1046 maybe we want to wait for which shape it takes before merging this?

works for me! Or we can even have two example depending on the pool version.

@chrispy-snps
Copy link
Author

@CaselIT, @dvarrazzo - I am fine with waiting for the psycogp_pool 3.3.0 release, then releasing this documentation update with the more user-friendly example (noting the required psycogp_pool version).

@dvarrazzo
Copy link

As I have re-opened psycopg/psycopg#1046 maybe we want to wait for which shape it takes before merging this?

works for me! Or we can even have two example depending on the pool version.

I doubt that psycopg pool 3.3 will bring any difference that anyone would prefer to use the older version. I think that, because asking to subclass the connection is pretty awkward, I think you can limit the documentation to the easier 3.3.

If someone needs to use an older version, I am adding the subclassing example to our docs too, so I don't think you need to be responsible of presenting it. I will link you the updated docs for review as soon as the branch I am preparing is tested.

@chrispy-snps
Copy link
Author

@dvarrazzo - for completeness, I will also keep both the < 3.3 and >= 3.3 asynchronous examples here as a reference.

@CaselIT
Copy link
Member
CaselIT commented May 5, 2025

@dvarrazzo - for completeness, I will also keep both the < 3.3 and >= 3.3 asynchronous examples here as a reference.

I doubt that psycopg pool 3.3 will bring any difference that anyone would prefer to use the older version. I think that, because asking to subclass the connection is pretty awkward, I think you can limit the documentation to the easier 3.3.

makes sense. At worst we could link to that discussion comment from the documentation simply stating "In older version of psycopg see the example at [...]"

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
0