Skip to content

Connection pool docs inconsistency? #1165

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

Closed
elliotwaite opened this issue Jul 13, 2024 · 2 comments
Closed

Connection pool docs inconsistency? #1165

elliotwaite opened this issue Jul 13, 2024 · 2 comments

Comments

@elliotwaite
Copy link

What happens to a connection's prepared statements when a connection is released?

The Connection Pool docs say prepared statements become invalid once a connection is released:

Prepared statements and cursors returned by Connection.prepare() and Connection.cursor() become invalid once the connection is released.

But the Pool docs say that prepared statements are not reset when a connection is released:

Once a connection is released, it’s reset to close all open cursors and other resources except prepared statements.

Is there a difference between a prepared statement becoming invalid and being reset, or are the docs incorrect in one of these places, or am I misunderstanding something?

@elprans
Copy link
Member

elprans commented Jul 17, 2024

The former point refers to prepared statement objects returned explicitly from Connection.prepare(). Automatic prepared statements created and managed by asyncpg are not deallocated when a connection is released.

@elliotwaite
Copy link
Author

Oh, I see. Thanks for the explanation. I also read through more of the code to try to improve my understanding, and my current understanding is:

  • Prepared statements created by Connection.prepare() are not stored in the connection's cache and are closed when the connection is closed or when all references to that prepared statement are garbage collected.
  • On the other hand, when using the Connection methods .execute(), .executemany(), .fetch(), fetchrow(), .fetchval(), or .cursor(), an implicit prepared statement is created, and if the connection's cache is enabled, that prepared statement is stored in the connection's cache, otherwise the prepared statement will not be stored in the cache and it will be an unnamed prepared statement, which is by definition is single-use only and is invalidated upon use.

Please let me know if any of that is incorrect.

Also, I'll close this issue since I was just misunderstanding the docs.

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

No branches or pull requests

2 participants