-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_notes.txt
More file actions
44 lines (36 loc) · 2.63 KB
/
Copy pathsql_notes.txt
File metadata and controls
44 lines (36 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# retrieve the raw SQL statement with variable bindings from a SQLAlchemy query
# https://stackoverflowteams.com/c/alcemy/questions/63
# https://stackoverflow.com/questions/4617291/how-do-i-get-a-raw-compiled-sql-query-from-a-sqlalchemy-expression
from sqlalchemy.dialects import postgresql
print(str(q.statement.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})))
# If that fails due to not finding a literal renderer, just use this:
print(str(q.statement.compile(dialect=postgresql.dialect())))
# Enable SQLAlchemy logging (can help diagnose eager/lazy loading and n+1 queries): put this in main.py:
logging.basicConfig()
logging.getLogger("sqlalchemy.engine").setLevel(logging.DEBUG)
# Create local postgres database:
sudo su - postgres
createdb cube_sidecar
createuser -d cube_sidecar_user
# may be necessary?
psql
grant all privileges on database cube_sidecar to cube_sidecar_user;
ALTER ROLE cube_sidecar_user WITH SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION BYPASSRLS;
# Debugging/timing long SQL queries with explain analyze:
EXPLAIN (ANALYZE, COSTS, BUFFERS, FORMAT JSON) <sql-query>
EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE, FORMAT JSON) <sql-query>
# Probably easiest to construct in psql/pgweb; it can be tricky to do in the code, although using the above trick to get the raw SQL and then manually substituting as necessary should work.
# Write the explain query to a file and then run with psql:
psql -XqAt -f explain.sql > analyze.json
# Paste the contents into https://explain.dalibo.com/
# Check delays in receiving batch data from PCP:
SELECT batch_start_datetime, (system_info->>'transmission_datetime')::timestamptz AS transmission_datetime, created_at, (system_info->>'transmission_datetime')::timestamptz - batch_start_datetime AS transmission_delay, created_at - (system_info->>'transmission_datetime')::timestamptz AS ingest_delay FROM batch WHERE slug LIKE 'rohrdorfer__%' AND created_at > '2025-05-13'
# Check delays in Emmy aggregation processing:
SELECT delivery.slug, delivery.created_at AS delivery_created, aggregated_delivery.created_at AS agg_created, aggregated_delivery.created_at - delivery.created_at AS processing_time FROM delivery JOIN aggregated_delivery ON delivery.slug = aggregated_delivery.slug WHERE delivery.slug LIKE 'rohrdorfer__%' AND delivery.created_at > '2025-05-13'
# Check current database activity (can get log queries):
SELECT * FROM pg_stat_activity;
# Kill a process:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = '8361';
# Run SQL directly in pycharm's Evaluate Expression box:
from sqlalchemy import text
session.execute(text("SELECT * FROM user_")).fetchall()