Linux Bash and SQL diagnostic toolkit plus an inactive replication-slot guard for PostgreSQL.
Built for direct DBRE/SRE incident triage with zero PowerShell or external agent dependencies, using read-only psql transactions and parameterized slot cleanup.
Every push runs shellcheck and an end-to-end suite against a live postgres:16-alpine container exercising WAL retention slots, row lock chains, pg_toast growth, scram-sha-256 HBA checks, and EXPLAIN (ANALYZE, BUFFERS).
docker compose up -d --wait
export PGHOST=127.0.0.1
export PGPORT=5432
export PGUSER=diag
export PGPASSWORD=diag_test_password
export PGDATABASE=diagdb
./bin/pg-diag.sh --check all --format table
./bin/pg-slot-guard.sh --threshold-mb 1024Run the full live integration suite:
./tests/integration.sh
docker compose down -vRuns read-only diagnostic queries (default_transaction_read_only=on) using standard libpq environment variables (PGHOST, PGPORT, PGUSER, PGDATABASE, PGPASSWORD, .pgpass).
--check all|slots|locks|toast|auth(default:all)--format table|csv|json(default:table)-h,--help
Examples:
./bin/pg-diag.sh --check slots --format json
./bin/pg-diag.sh --check locks --format table
./bin/pg-diag.sh --check toast --format csv
./bin/pg-diag.sh --check auth --format jsonFinds inactive replication slots retaining WAL above a MiB threshold. Runs in dry-run mode by default.
--threshold-mb <N>(default:1024, integer>= 0)--slot <name>(optional exact slot filter)--apply-drop(explicit opt-in to executepg_drop_replication_slot())-h,--help
Examples:
# Dry-run preview (default)
./bin/pg-slot-guard.sh --threshold-mb 1024
# Target a single inactive slot and drop it
./bin/pg-slot-guard.sh --threshold-mb 512 --slot analytics_cdc --apply-drop- Static checks:
shellcheck bin/*.sh tests/*.shpasses with zero warnings. - WAL & slot guard: Creates an inactive
test_decodingslot (orphaned_test_slot), generates WAL +pg_switch_wal(), verifiespg-diag.sh --check slots --format jsondetectsretained_wal_bytes > 0, confirms dry-run preserves the slot, and confirms--apply-dropremoves it. - Lock wait chains: Forces a real row-level
FOR UPDATEcontention between twopsqlsessions and verifiespg_blocking_pids()reportsblocker_pidandblocked_pid. - TOAST vs heap growth: Inserts 20 incompressible >64 KB payloads into
diag_toast_testand confirmstoast_bytes > 0while excluding system schemas. - Auth & HBA posture: Verifies
password_encryption == "scram-sha-256"alongsidepg_hba_file_rulesrisk classification. - Plan regression benchmark: Loads 100,000 rows into
diag_events, comparesEXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)before and after indexing(tenant_id, created_at), and asserts the sequential scan is replaced by an index scan with fewer shared blocks and lower execution time.
pg-diag.shsetsPGOPTIONS="-c default_transaction_read_only=on"so diagnostic queries cannot mutate database state.pg-slot-guard.shis dry-run by default, passes slot names via parameterizedpsql -vvariables (:'slot_name') to prevent SQL injection, and enforcesactive = falsein both discovery and drop SQL.- Neither script ever accesses
pg_walon disk or invokespg_resetwal.