Axisdream
Written and Published byAxisdream·

Why Read-Only Database Roles Fail to Secure AI Agents

Security diagram showing how read-only SQL queries can exhaust database memory or bypass intended confidentiality boundaries

Key Takeaways

  • •Read-only database roles fail to protect systems from AI agents generating resource-heavy queries, reading confidential data, or invoking complex functions.
  • •Examines the availability threat: denial of service via read queries, analyzing the structural bottlenecks and trade-offs in real-world deployments.
  • •Provides engineering benchmarks and verification criteria for the confidentiality hazard: broad schema access.

On 9 November 2026, security researchers at Vericto published reproducible tests on PostgreSQL 17 showing that standard read-only database accounts fail to protect production clusters from AI agents, which can crash database instances through unconstrained query complexity and exfiltrate confidential data across granted tables. Restricting an autonomous agent to SELECT privileges prevents data writes while leaving database availability and confidentiality entirely unprotected.

Connecting language models directly to databases has become a common pattern in text-to-SQL assistants, analytical agents, and Model Context Protocol servers. Engineering teams frequently assume that creating a dedicated PostgreSQL user with the default_transaction_read_only parameter and granting SELECT permissions on the public schema establishes a secure sandbox. In practice, read operations carry severe operational hazards that standard relational role permissions cannot mitigate.

The availability threat: denial of service via read queries

A read-only role prevents INSERT, UPDATE, DELETE, and destructive DDL commands such as DROP TABLE. However, read queries can easily exhaust database cluster resources:

  • Unindexed table scans: an agent attempting to answer a vague business question frequently generates SQL queries that join multiple large tables without leveraging existing indexes. Sequential scans across millions of rows force disks into heavy read saturation, stalling concurrent application transactions.
  • Cartesian cross joins: models frequently omit join predicates when writing complex multi-table queries. A cross join between three modest tables containing ten thousand rows each forces the database engine to construct a trillion-row intermediate result set in memory, triggering out-of-memory kernel panics.
  • Recursive common table expressions: an agent can generate recursive queries with unbounded termination conditions. These queries consume 100 percent of allocated CPU cores, starving production workloads of execution capacity.
  • Function execution and sleep locks: in PostgreSQL, the SELECT statement can invoke arbitrary built-in functions. An agent can execute commands such as SELECT pg_sleep(3600), holding connection pool slots open indefinitely until connection limits are exhausted and backend services fail.
text axisdream
┌────────────────────────────────────────────────────────┐
│             The Read-Only Illusion                     │
│                                                        │
│  [Agent SELECT Query] ──> [Bypasses Write Guardrails]  │
│                                │                       │
│                                ├─> Memory Exhaustion   │
│                                ├─> CPU Starvation      │
│                                ├─> Connection Locking  │
│                                └─> Sensitive Data Leak │
└────────────────────────────────────────────────────────┘

Because database engines execute valid SELECT statements without evaluating economic or performance sanity, an agent equipped with read-only permissions can take down a production database cluster within seconds.

The confidentiality hazard: broad schema access

Database permissions operate at the table and schema level, whereas data confidentiality operates at the row and column level. Granting an agent SELECT permissions on an application schema exposes massive volumes of sensitive business data:

  • Exfiltration of credential tables: if an agent holds read access across an operational schema, it can query internal authentication tables, dumping password hashes, salt parameters, and active session tokens into model context.
  • Personal data exposure: queries selecting customer tables expose personally identifiable information, billing records, and private communications. Once read into the context window, prompt injection payloads can exfiltrate this information across external network interfaces.
  • Metadata enumeration: an agent querying PostgreSQL system catalogs such as pg_catalog, pg_tables, and pg_roles can map internal network topologies, service accounts, and database extension versions, providing attackers with detailed reconnaissance for targeted exploits.
A read-only account prevents the agent from changing records, but gives it full authority to read every secret stored within granted schemas.

Multi-layered database defense for autonomous agents

Securing database access for autonomous agents requires moving beyond basic role grants to implement defense-in-depth at the engine and network layers:

  1. Strict query execution timeouts: configure mandatory statement_timeout parameters on the agent database role. Setting statement_timeout to five seconds terminates runaway Cartesian joins and recursive loops before they impact system performance.
  2. Memory and resource bounding: set strict work_mem limits on the agent role, preventing individual queries from allocating gigabytes of RAM during sort and hash operations.
  3. Column-level permissions and curated views: never grant direct SELECT access to base tables. Create dedicated reporting views that expose only non-sensitive columns, explicitly omitting password fields, internal keys, and personal identifiers.
  4. Row-Level Security (RLS): enable PostgreSQL Row-Level Security policies to restrict the agent to specific tenant or organizational boundaries. Even if the agent issues a broad SELECT * FROM accounts query, the RLS policy automatically appends filtering predicates, preventing cross-tenant data exposure.
sql axisdream
-- Secure PostgreSQL agent role configuration
ALTER ROLE ai_agent_role SET default_transaction_read_only = on;
ALTER ROLE ai_agent_role SET statement_timeout = '3000ms';
ALTER ROLE ai_agent_role SET work_mem = '16MB';

-- Revoke catalog access and enforce strict view permissions
REVOKE ALL ON SCHEMA public FROM ai_agent_role;
GRANT USAGE ON SCHEMA agent_reporting TO ai_agent_role;
GRANT SELECT ON ALL TABLES IN SCHEMA agent_reporting TO ai_agent_role;

Intermediary query proxies and pre-execution validation

Production architectures should avoid direct database connections between language models and database engines. An intermediary query proxy must inspect and validate every SQL string before execution.

The proxy enforces essential security checks:

  • Abstract Syntax Tree (AST) parsing: parse incoming SQL to verify that queries contain only approved SELECT constructs, blocking session-level parameter adjustments such as SET search_path.
  • Query plan inspection: run an EXPLAIN pass on the query before executing it. If the query planner estimates an execution cost exceeding pre-set thresholds, the proxy rejects the query immediately, returning cost diagnostics to the agent.
  • Output row limits: enforce rigid LIMIT constraints on all queries, preventing the agent from dumping hundreds of thousands of records into the network stream.
Read-only database roles represent an incomplete security boundary. Autonomous agents require strict runtime resource caps, curated reporting views, row-level security, and intermediary query proxies to ensure that natural language queries cannot compromise database availability or expose confidential enterprise data.

Sources

Related Dispatches