<?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: Raymond Hani</title>
    <description>The latest articles on DEV Community by Raymond Hani (@raymondhani).</description>
    <link>https://dev.to/raymondhani</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%2F4246436%2F8d5fd36f-06c0-46a4-999b-bc0f836c830b.jpg</url>
      <title>DEV Community: Raymond Hani</title>
      <link>https://dev.to/raymondhani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raymondhani"/>
    <language>en</language>
    <item>
      <title>Compiling Postgres C-Parser to WebAssembly in Go: Building a Layer-4 Guardrail for AI Agents</title>
      <dc:creator>Raymond Hani</dc:creator>
      <pubDate>Sun, 15 Nov 2026 19:06:55 +0000</pubDate>
      <link>https://dev.to/raymondhani/compiling-postgres-c-parser-to-webassembly-in-go-building-a-layer-4-guardrail-for-ai-agents-1j56</link>
      <guid>https://dev.to/raymondhani/compiling-postgres-c-parser-to-webassembly-in-go-building-a-layer-4-guardrail-for-ai-agents-1j56</guid>
      <description>&lt;p&gt;If you've ever wired an LLM agent or a text-to-SQL script up to a Postgres database, you know the anxiety: one bad hallucination and the model runs a &lt;code&gt;DROP TABLE&lt;/code&gt;, wipes rows with an unconstrained &lt;code&gt;DELETE&lt;/code&gt;, or gets stuck in a loop that burns through your compute budget.&lt;/p&gt;

&lt;p&gt;Most teams try to fix this with regex filters or by asking a second LLM to check the query first. In practice, regex breaks the moment a query has inline comments or nested CTEs, and using an LLM-as-a-judge adds hundreds of milliseconds of latency while still being non-deterministic.&lt;/p&gt;

&lt;p&gt;I wanted a deterministic guardrail at the network layer, so I built &lt;strong&gt;Aegis&lt;/strong&gt;—an open-source Layer-4 Postgres proxy written in Go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why WebAssembly (wazero) Instead of cgo?
&lt;/h3&gt;

&lt;p&gt;To parse Postgres queries properly, you really want Postgres's actual C parser (&lt;code&gt;libpg_query&lt;/code&gt;). The problem is that calling C from Go requires &lt;code&gt;cgo&lt;/code&gt;, which makes cross-compiling painful, adds cgo boundary overhead under concurrency, and pushes memory management outside the Go runtime.&lt;/p&gt;

&lt;p&gt;To keep Aegis 100% pure Go, it runs &lt;code&gt;libpg_query&lt;/code&gt; compiled to WebAssembly via &lt;code&gt;wazero&lt;/code&gt; (&lt;code&gt;wasilibs/go-pgquery&lt;/code&gt;). You get a single static Go binary with zero C dependencies, while still parsing the exact same Abstract Syntax Tree (AST) that Postgres uses internally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;guardrail&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;pg_query&lt;/span&gt; &lt;span class="s2"&gt;"github.com/wasilibs/go-pgquery"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;ValidateQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rawSQL&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;pg_query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rawSQL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="c1"&gt;// Block malformed SQL by default&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetStmts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;stmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetStmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetDropStmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetTruncateStmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ErrDestructiveQueryBlocked&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;deleteStmt&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetDeleteStmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;deleteStmt&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;deleteStmt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhereClause&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ErrUnconstrainedDeleteBlocked&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How the "SQL Guillotine" Works on the Wire
&lt;/h3&gt;

&lt;p&gt;Aegis sits between your app and your Postgres database, listening on port &lt;code&gt;:5433&lt;/code&gt; for TCP traffic (and &lt;code&gt;:5434&lt;/code&gt; for admin and metrics):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Zero code changes:&lt;/strong&gt; Point your existing client (&lt;code&gt;psycopg2&lt;/code&gt;, &lt;code&gt;pgx&lt;/code&gt;, SQLAlchemy, etc.) to &lt;code&gt;:5433&lt;/code&gt; instead of &lt;code&gt;:5432&lt;/code&gt; without changing a line of database code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AST inspection and local rate limiting:&lt;/strong&gt; Aegis parses incoming query packets in memory using the Wasm AST parser and enforces local RPM rate limits from a &lt;code&gt;policies.yaml&lt;/code&gt; file so runaway agent loops get throttled locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Guillotine:&lt;/strong&gt; Safe &lt;code&gt;SELECT&lt;/code&gt; queries are forwarded over TLS to your database (e.g., Neon Postgres or RDS). If Aegis spots a &lt;code&gt;DROP TABLE&lt;/code&gt; or a &lt;code&gt;DELETE&lt;/code&gt; without a &lt;code&gt;WHERE&lt;/code&gt; clause, it drops the connection immediately and returns a fatal error packet to the client. The database never even sees the query.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Try It Out
&lt;/h3&gt;

&lt;p&gt;The proxy and companion Python SDK (&lt;code&gt;aegis-sdk&lt;/code&gt; on PyPI) are open-source (AGPLv3) and run completely offline in Docker with total data privacy.&lt;/p&gt;

&lt;p&gt;GitHub: &lt;a href="https://github.com/raymondhani/aegis-proxy" rel="noopener noreferrer"&gt;https://github.com/raymondhani/aegis-proxy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you work with Go or Postgres internals, I'd love for you to take a look and let me know if you can think of any weird SQL edge cases that might slip past the AST check!&lt;/p&gt;

</description>
      <category>go</category>
      <category>postgres</category>
      <category>llm</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
