Topic 5 of 5 · 5 min read
Auditing the queries agents run
Goal: Read an agent's query log and flag the statements that touched data outside its task.
Log every statement an agent runs with enough identity to tell who asked for what, then review those logs for reads and writes that do not fit the task. If you cannot reconstruct what an agent did, you cannot tell whether it did something wrong.
Check yourself · 1 of 3
Which set of facts makes an agent query log most useful for review?
Why it matters
An agent's work is a stream of queries you did not write. Some will be odd but harmless. A few will read a column it had no reason to read, or run on a table outside its task. Without logs, the first sign is a customer complaint. With logs, you can ask a simple question: did this statement belong to the job?
What to record
For each statement: the role that ran it, a session or task id, the time, the statement text, and the result size where you can get it. Identity comes from the earlier topics: one role per agent means the role name already says which agent. Add the task with the connection's application_name, which appears in logs and pg_stat_activity:
SET application_name = 'agent_reports:task-7421';
Where to log
- Server query logs.
log_statement = 'all'for the agent role (set withALTER ROLE ... SET) records every statement, withlog_line_prefixshowing user and application. Straightforward, but the text is raw. - pgaudit. The extension logs in a structured format. Session audit records classes of statements for chosen roles. Object audit records access to specific tables and columns, which suits the question "who read
customers.email?". - The tool layer. Your own wrapper can log the task, the user's request and the SQL together, which the database cannot know.
Keep logs where the agent role cannot edit or delete them, and keep them for as long as your incident process needs.
How to review
- Filter by role and time window for the task.
- List the tables and columns touched and compare with what the task required.
- Flag sensitive reads: emails, tokens, payment fields, anything on a list you maintain.
- Look at shape: a sudden
SELECT *, a scan of a huge table, repeated failed permission errors.
Before and after
A log excerpt for the task "weekly revenue by region":
agent_reports:task-7421 SELECT region, sum(total) FROM reporting.orders_summary GROUP BY region
agent_reports:task-7421 SELECT email, phone FROM public.customers LIMIT 500
The second line is outside the task. The fix is not to trust the review forever: remove that grant so the statement fails, then keep the log as the alarm.
What goes wrong
- Logging after the fact. Turn it on before access, not after the incident.
- Secrets in logs. Literals in statements can contain tokens or personal data. Use parameters and restrict log access.
- No one reads it. Alert on a small set of patterns instead of expecting a full read.
Examples
Object audit on a sensitive column
The team enables pgaudit object auditing for the customers table. A week later a query for email by role agent_reports appears, though that agent only reports revenue. The finding leads to two actions: revoke the grant that allowed it and check what else that role read that week.
Tool-layer log beats database log for intent
The database log shows SELECT * FROM refunds WHERE amount > 500. It cannot say why. The agent wrapper also logs the user's request, "find unusually large refunds for the audit", so a reviewer can see the query fits the request.
Key points
- Record the role, a task or session id, the time and the statement text for every agent query.
- Set application_name per task so logs and pg_stat_activity show which job a query belonged to.
- pgaudit gives structured session and object audit logging, which suits questions about who read a given column.
- Review by comparing the tables and columns touched with what the task needed, and flag sensitive reads.
- Turn logging on before granting access, and keep logs where the agent role cannot change them.
Common mistakes
- Enabling logging only after something has gone wrong.
- Sharing one database role across agents, so the log cannot say which agent ran a statement.
- Logging statements with real values inside them and leaving the log readable by everyone.
- Treating the log as the control, instead of removing the grant that let the query run.
Check yourself · 2 of 3
You want to know which roles read customers.email. Which tool fits best?
Check yourself · 3 of 3
A log for the task 'weekly revenue by region' shows SELECT email, phone FROM public.customers LIMIT 500 from the same agent role. What is the best response?
Practice items, not counted towards your mastery.
Start here
- PostgreSQL privileges overview (opens in a new tab)DocsFree
GRANT, REVOKE and default privileges: the mechanism that decides what an agent role can touch.
- PostgreSQL: Row security policies (opens in a new tab)DocsFree
How CREATE POLICY limits the rows a role sees, including the owner-bypass rule.
- Supabase: Model Context Protocol (opens in a new tab)DocsFree
The hosted MCP server's project scoping, read-only mode and feature groups.
More for this topic
- pgaudit (PostgreSQL Audit Extension) (opens in a new tab)ToolFree
Session and object audit logging of the statements a role runs.
- PostgreSQL client settings (statement_timeout) (opens in a new tab)DocsFree
Reference for statement_timeout, idle_in_transaction_session_timeout and default_transaction_read_only.