-
Notifications
You must be signed in to change notification settings - Fork 141
Add snapshot.sh — LLM-friendly plain text dump of all reports #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||
| #!/usr/bin/env bash | ||||||
| # | ||||||
| # postgres_dba snapshot — dump all safe reports as plain text. | ||||||
| # Perfect for feeding to LLMs, saving to files, or piping to tools. | ||||||
| # | ||||||
| # Usage: | ||||||
| # ./snapshot.sh # uses default psql connection | ||||||
| # ./snapshot.sh -h localhost -U postgres -d mydb | ||||||
| # ./snapshot.sh "postgresql://user@host/db" | ||||||
| # ./snapshot.sh -d mydb | pbcopy # copy to clipboard (macOS) | ||||||
| # ./snapshot.sh -d mydb > snapshot.txt # save to file | ||||||
| # | ||||||
| # Skips: | ||||||
| # - Interactive reports (r1, r2 — require user input) | ||||||
| # - Expensive/slow reports (b3, b4, c2, c3, c4, m1 — heavy I/O) | ||||||
| # - Progress reports (p1 — only useful during operations) | ||||||
| # | ||||||
| # To include expensive reports, use --full. | ||||||
|
|
||||||
| set -euo pipefail | ||||||
|
|
||||||
| DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||
|
|
||||||
| FULL=false | ||||||
| PSQL_ARGS=() | ||||||
|
|
||||||
| for arg in "$@"; do | ||||||
| if [[ "$arg" == "--full" ]]; then | ||||||
| FULL=true | ||||||
| else | ||||||
| PSQL_ARGS+=("$arg") | ||||||
| fi | ||||||
| done | ||||||
|
|
||||||
| # Reports to skip in normal mode | ||||||
| SKIP_NORMAL="b3 b4 c2 c3 c4 m1 p1 r1 r2" | ||||||
|
|
||||||
| # Reports to skip even in full mode (interactive only) | ||||||
| SKIP_ALWAYS="r1 r2" | ||||||
|
|
||||||
| if $FULL; then | ||||||
| SKIP="$SKIP_ALWAYS" | ||||||
| else | ||||||
| SKIP="$SKIP_NORMAL" | ||||||
| fi | ||||||
|
|
||||||
| # Ensure non-interactive mode for t1 | ||||||
| PSQLRC_TMP=$(mktemp) | ||||||
| trap "rm -f $PSQLRC_TMP" EXIT | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor quoting nit: since this runs in a trap, it’s safer to quote the tmp path to avoid word-splitting/globbing edge cases.
Suggested change
|
||||||
| cat > "$PSQLRC_TMP" <<'EOF' | ||||||
| \set postgres_dba_wide true | ||||||
| \set postgres_dba_interactive_mode false | ||||||
| \pset pager off | ||||||
| \pset footer off | ||||||
| EOF | ||||||
|
|
||||||
| echo "-- postgres_dba snapshot" | ||||||
| echo "-- Generated: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" | ||||||
| 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')" | ||||||
| echo "--" | ||||||
| echo "" | ||||||
|
|
||||||
| for f in "$DIR"/sql/*.sql; do | ||||||
| prefix=$(basename "$f" | sed 's/_.*$//') | ||||||
|
|
||||||
| # Check skip list | ||||||
| skip=false | ||||||
| for s in $SKIP; do | ||||||
| if [[ "$prefix" == "$s" ]]; then | ||||||
| skip=true | ||||||
| break | ||||||
| fi | ||||||
| done | ||||||
| $skip && continue | ||||||
|
|
||||||
| desc=$(head -n1 "$f" | sed 's/^--//') | ||||||
|
|
||||||
| echo "================================================================" | ||||||
| echo "== $prefix —$desc" | ||||||
| echo "================================================================" | ||||||
| echo "" | ||||||
|
|
||||||
| PAGER=cat psql "${PSQL_ARGS[@]}" \ | ||||||
| --no-psqlrc \ | ||||||
| -f "$DIR/warmup.psql" \ | ||||||
| -f "$PSQLRC_TMP" \ | ||||||
| -f "$f" \ | ||||||
| 2>&1 \ | ||||||
| | sed 's/^psql:[^ ]* //' \ | ||||||
| | sed 's/^NOTICE: //' \ | ||||||
| | sed 's/^WARNING: /⚠️ /' \ | ||||||
| | grep -v '^Pager ' \ | ||||||
| | grep -v '^Null display' \ | ||||||
| | grep -v '^Footer is off' \ | ||||||
| | grep -v '^DO$' \ | ||||||
| | grep -v '^SET$' \ | ||||||
| | grep -v '^$' \ | ||||||
| | head -500 || true | ||||||
|
|
||||||
| echo "" | ||||||
| echo "" | ||||||
| done | ||||||
|
|
||||||
| echo "-- End of snapshot" | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone runs this in a checkout without
sql/*.sql(or the glob doesn’t match), bash will iterate the literal pattern and the script will fail underset -e. Enablingnullglobavoids that surprise.