I'm looking to use PREPARE/EXECUTE statements, to eliminate my Query Planning time from every Web request.
This can be done via SQL PREPARE/EXECUTE statements.
But, Postgres only supports PREPARE statements on a connection session-by-session basis.
To do this with Django, I need to be able to run an SQL Query that runs the PREPARE statement for every connection to the Database. I only need to do this once per DB connection, as I have enabled Persistent connections.
The code to run, if I put it into the App.view, would be something like this:
class PreparedView(View):
def prepare_db():
cursor = connection.cursor()
cursor.execute(“”"
PREPARE prepared_query (int, text, etc) AS
SELECT … some query …
“”"
I’m looking through the Django code to see where I can run some sort of Query. Maybe I can subclass django.db.backends.postgresql.base.DatabaseWrapper.init_db_connection_state()?
Also, where can I hook that into my app? If I subclass that, I’m not sure if I can/should put that in the Model, View, or URL of modules of my app, so that it’s called? Do I create an entirely new version of django.db.backends.postgresql?
Ideally this would work with both the basic built-in development server connecting to a local host Postgres, as well as in a uWSGI environment with possible pgBouncer in the stack somewhere.
Thanks!
-bobby