Skip to content

Commit c738259

Browse files
committed
Add snapshot.sh — dump all reports as clean text for LLMs and automation
- Runs all safe reports non-interactively, outputs clean plain text - Strips ANSI colors, psql noise, DO/SET lines, NOTICE: prefixes - Skips: interactive (r1/r2), expensive (b3/b4/c2-c4/m1), progress (p1) - --full flag includes expensive reports - Works with any psql connection args or connection string - ~1500 lines / ~430KB for a typical database — fits in LLM context
1 parent 61ead2d commit c738259

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ Tested on **PostgreSQL 13 through 18** via CI on every commit. Older versions (9
115115

116116
Works with the `pg_monitor` role — superuser is not required for most reports (corruption checks need superuser or explicit `GRANT EXECUTE`).
117117

118+
## Snapshot Mode (for LLMs, scripts, automation)
119+
120+
Dump all safe reports as plain text — perfect for feeding to an LLM, saving to a file, or piping into other tools:
121+
122+
```bash
123+
./snapshot.sh -h localhost -U postgres -d mydb # plain text output
124+
./snapshot.sh -d mydb > snapshot.txt # save to file
125+
./snapshot.sh -d mydb | pbcopy # clipboard (macOS)
126+
./snapshot.sh --full -d mydb # include expensive reports
127+
```
128+
129+
Skips interactive and expensive reports by default. Use `--full` to include everything except interactive prompts.
130+
118131
## Adding Custom Reports
119132

120133
Drop a `.sql` file in `sql/`. The filename format is `<id>_<name>.sql`. The first line must be a `--` comment with the description — it becomes the menu entry automatically.

RELEASE_NOTES.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ Robustness:
2323
- Version-conditional: uses appropriate function signatures for PG11–18
2424
- GIN support via `gin_index_check()` on PostgreSQL 18+
2525

26+
### snapshot.sh — LLM-friendly output
27+
28+
New `snapshot.sh` script dumps all safe reports as clean plain text — no ANSI colors, no interactive prompts, no psql noise. Perfect for:
29+
- Feeding database state to an LLM for analysis
30+
- Automated health checks in scripts
31+
- Saving periodic snapshots to files
32+
33+
```bash
34+
./snapshot.sh -d mydb > snapshot.txt
35+
./snapshot.sh --full -d mydb # include expensive reports too
36+
```
37+
2638
### m1 — Buffer cache contents
2739
What's in your `shared_buffers` right now. Shows cached size vs total size, % of cache used per object, and dirty buffer counts. Requires `pg_buffercache` extension.
2840

snapshot.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env bash
2+
#
3+
# postgres_dba snapshot — dump all safe reports as plain text.
4+
# Perfect for feeding to LLMs, saving to files, or piping to tools.
5+
#
6+
# Usage:
7+
# ./snapshot.sh # uses default psql connection
8+
# ./snapshot.sh -h localhost -U postgres -d mydb
9+
# ./snapshot.sh "postgresql://user@host/db"
10+
# ./snapshot.sh -d mydb | pbcopy # copy to clipboard (macOS)
11+
# ./snapshot.sh -d mydb > snapshot.txt # save to file
12+
#
13+
# Skips:
14+
# - Interactive reports (r1, r2 — require user input)
15+
# - Expensive/slow reports (b3, b4, c2, c3, c4, m1 — heavy I/O)
16+
# - Progress reports (p1 — only useful during operations)
17+
#
18+
# To include expensive reports, use --full.
19+
20+
set -euo pipefail
21+
22+
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
23+
24+
FULL=false
25+
PSQL_ARGS=()
26+
27+
for arg in "$@"; do
28+
if [[ "$arg" == "--full" ]]; then
29+
FULL=true
30+
else
31+
PSQL_ARGS+=("$arg")
32+
fi
33+
done
34+
35+
# Reports to skip in normal mode
36+
SKIP_NORMAL="b3 b4 c2 c3 c4 m1 p1 r1 r2"
37+
38+
# Reports to skip even in full mode (interactive only)
39+
SKIP_ALWAYS="r1 r2"
40+
41+
if $FULL; then
42+
SKIP="$SKIP_ALWAYS"
43+
else
44+
SKIP="$SKIP_NORMAL"
45+
fi
46+
47+
# Ensure non-interactive mode for t1
48+
PSQLRC_TMP=$(mktemp)
49+
trap "rm -f $PSQLRC_TMP" EXIT
50+
cat > "$PSQLRC_TMP" <<'EOF'
51+
\set postgres_dba_wide true
52+
\set postgres_dba_interactive_mode false
53+
\pset pager off
54+
\pset footer off
55+
EOF
56+
57+
echo "-- postgres_dba snapshot"
58+
echo "-- Generated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
59+
echo "-- Connection: $(psql "${PSQL_ARGS[@]}" --no-psqlrc -tAc "select format('%s@%s:%s/%s (PostgreSQL %s)', current_user, inet_server_addr(), inet_server_port(), current_database(), version())" 2>/dev/null || echo 'unknown')"
60+
echo "--"
61+
echo ""
62+
63+
for f in "$DIR"/sql/*.sql; do
64+
prefix=$(basename "$f" | sed 's/_.*$//')
65+
66+
# Check skip list
67+
skip=false
68+
for s in $SKIP; do
69+
if [[ "$prefix" == "$s" ]]; then
70+
skip=true
71+
break
72+
fi
73+
done
74+
$skip && continue
75+
76+
desc=$(head -n1 "$f" | sed 's/^--//')
77+
78+
echo "================================================================"
79+
echo "== $prefix$desc"
80+
echo "================================================================"
81+
echo ""
82+
83+
PAGER=cat psql "${PSQL_ARGS[@]}" \
84+
--no-psqlrc \
85+
-f "$DIR/warmup.psql" \
86+
-f "$PSQLRC_TMP" \
87+
-f "$f" \
88+
2>&1 \
89+
| sed 's/^psql:[^ ]* //' \
90+
| sed 's/^NOTICE: //' \
91+
| sed 's/^WARNING: /⚠️ /' \
92+
| grep -v '^Pager ' \
93+
| grep -v '^Null display' \
94+
| grep -v '^Footer is off' \
95+
| grep -v '^DO$' \
96+
| grep -v '^SET$' \
97+
| grep -v '^$' \
98+
| head -500 || true
99+
100+
echo ""
101+
echo ""
102+
done
103+
104+
echo "-- End of snapshot"

0 commit comments

Comments
 (0)