<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mudit Kapoor</title>
    <description>The latest articles on DEV Community by Mudit Kapoor (@mudit_builds).</description>
    <link>https://dev.to/mudit_builds</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4169771%2Fe67b7a67-c647-4f7a-bc57-e39f34e779bf.jpg</url>
      <title>DEV Community: Mudit Kapoor</title>
      <link>https://dev.to/mudit_builds</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mudit_builds"/>
    <language>en</language>
    <item>
      <title>Your agent's worst query is read-only</title>
      <dc:creator>Mudit Kapoor</dc:creator>
      <pubDate>Fri, 09 Oct 2026 20:19:02 +0000</pubDate>
      <link>https://dev.to/mudit_builds/your-agents-worst-query-is-read-only-4n0j</link>
      <guid>https://dev.to/mudit_builds/your-agents-worst-query-is-read-only-4n0j</guid>
      <description>&lt;p&gt;In April an AI agent deleted a company's production database and HN spent 1,000 comments on it. The details matter: the agent was in Cursor, it found an API token that could do anything to staging &lt;em&gt;and&lt;/em&gt; production, and it sent one &lt;code&gt;curl&lt;/code&gt; that deleted the volume. The backups lived on the same volume.&lt;/p&gt;

&lt;p&gt;HN's verdict wasn't "the model was bad". It was: &lt;strong&gt;you can't prompt an agent into safety. The limit has to live in something the agent can't talk its way past.&lt;/strong&gt; Scoped credentials, a permission layer, a door with a lock on it.&lt;/p&gt;

&lt;p&gt;That lesson has a quieter SQL version that doesn't make HN, because nothing gets deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dangerous query is a SELECT
&lt;/h2&gt;

&lt;p&gt;Point an agent at a shared Trino cluster with a read-only user and you've handled the headline risks: no &lt;code&gt;DROP&lt;/code&gt;, no &lt;code&gt;DELETE&lt;/code&gt;. What's left is SQL that is perfectly valid and perfectly read-only:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a missing partition filter that turns into a full scan and ties up every worker;&lt;/li&gt;
&lt;li&gt;a join on the wrong column, or no column at all, that builds hundreds of millions of rows in worker memory while everyone else's queries wait behind it;&lt;/li&gt;
&lt;li&gt;a retry loop that runs it again because the first attempt "timed out".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On self-hosted Trino nobody gets a $500 bill for that. What happens is worse to explain: the dashboards are slow, the other team's pipeline misses its window, and the cause was a question someone asked a chatbot.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Doesn't Trino already limit this?"
&lt;/h2&gt;

&lt;p&gt;It does, and you should keep those limits. But look at &lt;em&gt;when&lt;/em&gt; they act:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Acts&lt;/th&gt;
&lt;th&gt;Catches a cross join that reads little&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;query.max-scan-physical-bytes&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;terminates the query once it has scanned that much&lt;/td&gt;
&lt;td&gt;no: it counts bytes read, not rows built&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;query.max-execution-time&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;terminates it after it has run that long&lt;/td&gt;
&lt;td&gt;only after it held the cluster that long&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resource groups (&lt;code&gt;hardPhysicalDataScanLimit&lt;/code&gt;, &lt;code&gt;softMemoryLimit&lt;/code&gt;, …)&lt;/td&gt;
&lt;td&gt;queue the group's &lt;em&gt;next&lt;/em&gt; queries once it's over its share&lt;/td&gt;
&lt;td&gt;no: the running query continues&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All three act once the query is already on the cluster. And a scan cap can't see the worst shape at all. Here's one measured on Trino 476:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orderkey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;partkey&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tpch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tiny&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tpch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tiny&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lineitem&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By Trino's own plan it reads &lt;strong&gt;676,575 bytes&lt;/strong&gt;, so any scan limit above 0.7 MB lets it through. It would build &lt;strong&gt;902,625,000 rows&lt;/strong&gt;. The &lt;code&gt;LIMIT 10&lt;/code&gt; doesn't help: the rows are built before anything is returned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pricing the query before it runs
&lt;/h2&gt;

&lt;p&gt;I built &lt;a href="https://github.com/lagaam-ai/lagaam" rel="noopener noreferrer"&gt;Lagaam&lt;/a&gt; to be the lock on that door. It's an MCP server, and the agent's only way to reach the engine is through its three tools (&lt;code&gt;list_catalogs&lt;/code&gt;, &lt;code&gt;describe_table&lt;/code&gt;, &lt;code&gt;query_data&lt;/code&gt;). Before any SQL runs, Lagaam asks the engine what it would cost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;EXPLAIN (TYPE IO)&lt;/code&gt; for the bytes it would scan,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXPLAIN (TYPE LOGICAL)&lt;/code&gt; for the &lt;strong&gt;widest row count any step would build&lt;/strong&gt;, which is the number a cross join blows up and a &lt;code&gt;LIMIT&lt;/code&gt; can't hide.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Over budget, it's refused. What the agent gets back for the query above:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This query would build 902,625,000 rows at its widest step, over your budget of 50,000,000. These are rows the engine materializes internally, not rows returned, so a LIMIT will not help — join on a column with more distinct values, or filter each side before the join.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That last sentence is the point. The agent can read the refusal, fix the SQL and try again. It isn't a stack trace for a human to decode at 2am.&lt;/p&gt;

&lt;p&gt;The rest is the boring gate you'd want anyway:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read-only in the syntax tree. Exactly one &lt;code&gt;SELECT&lt;/code&gt;, no DDL/DML, no &lt;code&gt;SELECT *&lt;/code&gt;, and a &lt;code&gt;LIMIT&lt;/code&gt; injected when missing. Validated SQL is re-rendered, so what runs is exactly what was checked.&lt;/li&gt;
&lt;li&gt;A per-agent table grant. Tables outside it are refused, and it won't start without a grant.&lt;/li&gt;
&lt;li&gt;Fail closed. No table statistics means no estimate, and no estimate means no query.&lt;/li&gt;
&lt;li&gt;An audit line per call: who asked what, allowed or denied, and why.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apache Pinot gets the same gate. It was harder, because Pinot's planner has no cost estimate (every scan says &lt;code&gt;rowcount 100&lt;/code&gt;), so the Pinot quote is built from segment metadata and the broker's own segment-pruning counts. That's a post of its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does it catch what agents actually write?
&lt;/h2&gt;

&lt;p&gt;I wrote 11 queries an agent plausibly sends (full scans, &lt;code&gt;SELECT *&lt;/code&gt;, a &lt;code&gt;DROP&lt;/code&gt; and a &lt;code&gt;DELETE&lt;/code&gt;, multi-statement injection, a write hidden in a CTE, oversized and self-doubling joins, a table outside the grant) plus one well-scoped control query. A raw MCP wrapper that forwards SQL to Trino submits every one of them. Lagaam stops &lt;strong&gt;11 of 11 before execution&lt;/strong&gt;, and the control query still runs. &lt;a href="https://github.com/lagaam-ai/lagaam/blob/main/benchmarks/results.md" rel="noopener noreferrer"&gt;The table and the script to reproduce it are in the repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it doesn't do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It wouldn't have saved that production database: that was an API token, not SQL. Lagaam guards the query path, and your credentials are still your problem.&lt;/li&gt;
&lt;li&gt;It prices from the planner, so it's only as good as your statistics. Run &lt;code&gt;ANALYZE&lt;/code&gt;; tables without stats are refused rather than guessed.&lt;/li&gt;
&lt;li&gt;It's a gate, not a scheduler: keep resource groups as the backstop for everything that does run.&lt;/li&gt;
&lt;li&gt;It doesn't rewrite or retry anything itself. The agent decides what to do with a refusal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;It's on PyPI and in the official MCP Registry (&lt;code&gt;io.github.lagaam-ai/lagaam&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;TRINO_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;your-trino &lt;span class="nv"&gt;LAGAAM_ALLOWED_TABLES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;hive.sales.orders uvx lagaam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Claude Code, install it as a plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/plugin marketplace add lagaam-ai/lagaam
/plugin install lagaam@lagaam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or in any MCP client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"mcpServers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"lagaam"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"uvx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"lagaam"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"TRINO_HOST"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"your-trino"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"LAGAAM_ALLOWED_TABLES"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"hive.sales.orders"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apache 2.0, self-hosted, Python. If you're letting agents near a shared Trino or Pinot cluster, I'd like to hear what broke. Issues or DMs are open.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>dataengineering</category>
      <category>trino</category>
    </item>
  </channel>
</rss>
