Letting LLMs query sensitive data: aggregates aren't as safe as you think

Then_Respect_1964 · reddit · 2026-11-17

A developer shares a lesson from building access rules for AI agents querying Postgres: aggregates were allowed without approval on the assumption they can't expose individual records. Reviewing the agent's actual SQL broke that assumption — MAX/MIN(amount) return real underlying values, and SUM(amount) GROUP BY customerid effectively exposes single-payment customers. All passed because the policy only checked whether an aggregate function was used, not what the result could reveal; the agent wasn't even trying to bypass rules.

He now requires approval for MIN/MAX like row-level reads, but GROUP BY remains hard: grouping by quasi-identifiers (emails, precise timestamps) is effectively row access, and k-anonymity minimums can be defeated by overlapping queries — something agents are good at. Key takeaway: policies should evaluate what a query's result reveals, not whether it uses an aggregate. He asks how others let agents query production databases safely.

Related event: Agent bypasses approval rules via "harmless" aggregate queries(2 posts)→

Original post →

More from coding & agent

coding & agent channel →