tools / database.py
PostgreSQL administration with parameterised queries, schema introspection, plan analysis, and pooled connections.
Tool: postgres_admin
| Action | Behaviour |
|---|---|
query | Run arbitrary SQL with a 15 s statement timeout (configurable). Output clamped to 300 rows. Returns tabulated results from a RealDictCursor. |
schema | Dump information_schema.columns for public tables. Filterable by table_name via parameterised query. |
explain_analyze | Prepend EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) if not already present; runs with the same timeout. |
activity | Selects from pg_stat_activity; bounded to 50 longest-running rows. |
Security
- All schema introspection uses parameterised queries (
cur.execute(sql, (table_name,))). - SQL is passed through
extract_code_from_markdown()first so fenced SQL blocks are stripped cleanly. - Connection pool with autocommit; broken connections are closed and discarded; the next call opens a fresh one.
DSN
Default DSN comes from --default-db / GHOST_DEFAULT_DB. The tool can be invoked with an alternative DSN per-call.