perf: connection pooling, query consolidation, inline chart data, batch milestones
This commit is contained in:
40
app/db.py
40
app/db.py
@@ -1,33 +1,41 @@
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
import psycopg2.pool
|
||||
from flask import g, current_app
|
||||
|
||||
# Module-level connection pool (initialised once per process)
|
||||
_pool = None
|
||||
|
||||
|
||||
def init_db(app):
|
||||
"""Test the database connection on startup."""
|
||||
"""Initialise the connection pool on startup."""
|
||||
global _pool
|
||||
try:
|
||||
conn = psycopg2.connect(app.config["DATABASE_URL"])
|
||||
conn.close()
|
||||
print(" * Database connection OK")
|
||||
_pool = psycopg2.pool.SimpleConnectionPool(
|
||||
minconn=2,
|
||||
maxconn=10,
|
||||
dsn=app.config["DATABASE_URL"],
|
||||
)
|
||||
print(" * Database connection pool OK (2–10 connections)")
|
||||
except Exception as e:
|
||||
print(f" * Database connection FAILED: {e}")
|
||||
print(f" * Database connection pool FAILED: {e}")
|
||||
|
||||
|
||||
def get_db():
|
||||
"""Get a database connection for the current request."""
|
||||
"""Get a pooled database connection for the current request."""
|
||||
if "db" not in g:
|
||||
g.db = psycopg2.connect(
|
||||
current_app.config["DATABASE_URL"],
|
||||
cursor_factory=psycopg2.extras.RealDictCursor,
|
||||
)
|
||||
g.db = _pool.getconn()
|
||||
g.db.cursor_factory = psycopg2.extras.RealDictCursor
|
||||
return g.db
|
||||
|
||||
|
||||
def close_db(exception=None):
|
||||
"""Close database connection at end of request."""
|
||||
"""Return database connection to the pool at end of request."""
|
||||
db = g.pop("db", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
if exception:
|
||||
db.rollback()
|
||||
_pool.putconn(db)
|
||||
|
||||
|
||||
def query(sql, params=None):
|
||||
@@ -62,3 +70,11 @@ def execute_returning(sql, params=None):
|
||||
row = cur.fetchone()
|
||||
db.commit()
|
||||
return row
|
||||
|
||||
|
||||
def execute_many(sql, params_list):
|
||||
"""Execute a batch INSERT/UPDATE/DELETE and commit."""
|
||||
db = get_db()
|
||||
with db.cursor() as cur:
|
||||
cur.executemany(sql, params_list)
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user