forked from mtusi2026/mtusi2026lab6
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files. Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
31 lines
1.6 KiB
Bash
Executable File
31 lines
1.6 KiB
Bash
Executable File
#!/bin/sh
|
|
set -eu
|
|
mode=${1:?usage: pg-stats.sh activity <csv> | statements <csv> | size | reset}
|
|
cd "$(dirname "$0")/.."
|
|
psql() {
|
|
docker compose exec -T postgres sh -c 'exec psql -X -q -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "${POSTGRES_DB:-$POSTGRES_USER}" "$@"' psql "$@"
|
|
}
|
|
case $mode in
|
|
reset)
|
|
psql -c 'set client_min_messages = warning' -c 'create extension if not exists pg_stat_statements' -c 'select pg_stat_statements_reset()' >/dev/null
|
|
;;
|
|
size)
|
|
psql -At -F, -c "select pg_database_size(current_database()), coalesce((select n_live_tup from pg_stat_user_tables where relname = 'messages'), 0)"
|
|
;;
|
|
statements)
|
|
psql -c "copy (select calls, round(total_exec_time::numeric, 3) as total_ms, round(mean_exec_time::numeric, 3) as mean_ms, rows, shared_blks_hit, shared_blks_read, left(regexp_replace(query, '\s+', ' ', 'g'), 200) as query from pg_stat_statements where dbid = (select oid from pg_database where datname = current_database()) and query not like '%pg_stat%' order by total_exec_time desc limit ${TOP:-20}) to stdout with csv header" > "${2:?csv}"
|
|
;;
|
|
activity)
|
|
echo 'ts,state,wait_event_type,wait_event,backends,max_connections' > "${2:?csv}"
|
|
while :; do
|
|
ts=$(date +%s)
|
|
psql -At -F, -c "select coalesce(state, ''), coalesce(wait_event_type, ''), coalesce(wait_event, ''), count(*), current_setting('max_connections') from pg_stat_activity where backend_type = 'client backend' and pid <> pg_backend_pid() group by 1, 2, 3" | sed "s/^/$ts,/" >> "$2" || true
|
|
sleep "${INTERVAL:-1}"
|
|
done
|
|
;;
|
|
*)
|
|
echo "unknown mode $mode" >&2
|
|
exit 2
|
|
;;
|
|
esac
|