Carry source f591fa64e525679a31c9db7f942d36c789e1a12f forward from 72a1e57. Preserve lab4 attachment transactions, reconciliation locks, quarantine, and existing schema data through verified legacy adoption. Keep report and evidence work in progress untouched.
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
|