-
Notifications
You must be signed in to change notification settings - Fork 419
Prepared statements being recreated on every call of fetch #453
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
Comments
Further experimentation proved that the problem lies in setting a custom type codec after getting a connection from the pool. Minimal reproducer (it should not matter what postgres you connect to or which table you fetch from):
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the issue with a local PostgreSQL install?: I use the official Docker image with custom schema and test data.
uvloop?: Yes
Hello,
I am facing a peculiar problem with the way prepared statements are handled. I use the following architecture:
aiohttp application, which initializes a pool of 1 to 20 db connections on init.
Data is periodically refreshed from the DB (once in a few minutes for most tables). I have a special class which handles the loading of data from DB and caches them to memory and to Redis (since multiple containers of the same app are running and I would like to minimize fetches from DB). This class is instantiated by a factory method which creates (besides other arguments) a
load
coroutine, which gets query passed into it by the factory.The queries have no parameters and are static through out the runtime.
load
functions works by getting a connection from the pool, and callingconnection.fetch
on the given query. As per my understanding, the query should then be turned into a prepared statement, cached into a builtin LRU cache, and reused in later calls. However, it seems that each call toload
(which is periodic) gets a new LRU cache for some reason, creating the prepared statements anew. But when I runconnection.fetch
onSELECT * FROM pg_prepared_statements
I see that the number of prepared statements held by the connection increases in each call offetch
.Indeed, adding some prints to
connection.py
I found out that the statements get recreated and put into the cache on each call, since the cache is empty. I thought that perhaps it is because the connections I get from the pool differ, but sincepg_prepared_statements
is local to a session (a connection?) I think this is not the case. Indeed, limiting the size of the pool tomax_size=1
did not solve this issue.This causes my Postgres to slowly drain more and more memory until the connections are reset. Disabling the LRU cache with
statement_cache_size=0
avoids this, but I believe that this behaviour is not intended.I tried to make a minimal reproducer but haven't yet succeeded.
The text was updated successfully, but these errors were encountered: