If you look after Postgres every day, you already speak AI. You just use different words. A prompt is a query. A context window is work_mem. A tool call is a function call. And an agent? That's a junior DBA with a runbook.
This is the Postgres version of my Oracle video, written down. No product tour, just a map from what you already know to what's new, and where it can hurt you.
9:12 in the morning, too many connections
The running example: new logins start failing with too_many_connections. The old way, you open your script folder, your notes, the mailing list archive and a search engine. You write the query against pg_stat_activity yourself, run it in psql, and read the result. Every step is yours.
Now you can type the question in plain English. The model drafts the SQL, explains the plan or the lock, and an agent can even run read-only queries through a tool. What didn't change: the database is still the source of truth, and you're still the one accountable for what runs on it.
Your own servers or a managed service, the AI layer is the same. What matters is where the model runs and where your data goes. A local model keeps the data inside. A cloud model is usually stronger, but your prompt leaves the building.
The translation table
| AI term | What you already know | Why it matters |
|---|---|---|
| Prompt | A query with no parser | Postgres stops you with SQLSTATE 42601. A model just guesses. "Which app uses the most connections?" Right now, or held the longest? Write prompts like a good ticket. |
| Tokens | 8 kB pages | The unit of work, cost and every limit. Longer prompts cost more, like more buffers in EXPLAIN (ANALYZE, BUFFERS). |
| Context window | work_mem | Fixed size. When a long chat fills it, the app trims, like a sort spilling to temp files. The middle of a long context gets lost. |
| Stateless API | A fresh backend, or a pooled connection after DISCARD ALL | A chat only seems to remember because the app resends the whole history every time. |
| System prompt | ALTER ROLE ... SET | Kicks in at every request, like role settings at every login. Put the dialect in it: Postgres, not T-SQL. |
| Hallucination | Stale statistics | Confident and wrong. In the video the model invents a column called idle_since. Postgres has state_change. |
| Non-determinism | No plan baseline | Same question, different SQL. Test against a case you already know. |
| Grounding / RAG | Catalog lookup | Feed only the slice it needs: information_schema.columns, your COMMENT ON COLUMN notes, a runbook page. |
| Tool call | Function call | The model only asks. The app executes. The model never touches the database. |
| MCP | Port 5432 + libpq or JDBC | One protocol, many clients and servers. There are open-source MCP servers for Postgres. |
| Workflow vs agent | Nightly cron script vs junior DBA | vacuumdb, reindexdb, pg_dump in fixed order, or an agent that picks its next step from each result. |
| Human-in-the-loop | Change approval | The agent finds the idle transactions. pg_terminate_backend is still your call. |
Two queries, one question
"Which app holds connections it isn't using?" Both generated queries ran fine. One counted state = 'idle', which is mostly connection pools doing their normal thing. The other counted state = 'idle in transaction': an open transaction that's doing nothing. Only that one answers the question. Check the result against a small case you already know.
Where the agent stops
SELECT pid, application_name,
now() - xact_start AS tx_age
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY xact_start;
-- pg_terminate_backend(4127) → you approve
An old open transaction also holds back vacuum, so these sessions hurt twice. Ending them rolls back their work, though, and that's a decision a human makes.
What got better, what it costs
The good part: a first SQL draft in seconds, plain-English explanations of plans, locks and error messages, help on an engine you know less well, and a lot less searching. The bad part: answers can be confidently wrong, the same question can give different results, calls cost tokens and time, and data may leave your network. NIST also names a quieter one, over-reliance. You trust it because it sounds sure, and slowly stop checking.
Security, in DBA words
- Prompt injection is SQL injection without parameters. A value in a table row or a line in the server log can say "ignore your rules". In SQL you'd use
$1parameters. There's no such thing for prompts, so limit what the model can do. OWASP lists it as LLM risk number one. - Sensitive data disclosure is an uncontrolled export. Classify before you paste, mask real values, use a local model for anything you wouldn't send outside.
- Excessive agency is giving an app role superuser. Give the AI its own role, for example
GRANT pg_read_all_data TO ai_ro;, and require human approval for anything high impact. - Avoid the lethal trifecta: private data, untrusted content and a way to send data out. Never all three in one agent.
- Passwords and connection strings never go into the chat.
- Audit it like any other role. With its own role,
log_statement = 'all'for that role shows exactly what it ran. Set its application name in the connection string andpg_stat_activitytells you which session is the AI. - Production last. Sanitized copy or a read-only hot standby, lab first, human approval, and
pg_create_restore_point('before_fix')before any change.
New words, old habits. Least privilege, review before commit, test before production. If you've done this job for a while, you already have most of the instincts you need.
Full video: You Already Speak AI (PostgreSQL edition) on YouTube. PIDs and outputs in the video are illustrative.
Sources
- OWASP Top 10 for LLM Applications 2025: genai.owasp.org/llm-top-10
- Model Context Protocol specification: modelcontextprotocol.io/specification
- Anthropic, Building effective agents: anthropic.com
- Simon Willison, The lethal trifecta for AI agents: simonwillison.net
- Liu et al., Lost in the Middle (2023): arxiv.org/abs/2307.03172
- NIST AI 600-1, Generative AI Profile: doi.org/10.6028/NIST.AI.600-1
- PostgreSQL 17 docs: pg_stat_activity, admin functions, work_mem, DISCARD, ALTER ROLE, predefined roles, logging, hot standby, routine vacuuming
Comments
Post a Comment