<?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: Mubeen Ahmad Khan</title>
    <description>The latest articles on DEV Community by Mubeen Ahmad Khan (@mubeenahmadkhan).</description>
    <link>https://dev.to/mubeenahmadkhan</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%2F4096913%2F44ac480e-9b37-45a2-8e30-a02b3f63d396.jpg</url>
      <title>DEV Community: Mubeen Ahmad Khan</title>
      <link>https://dev.to/mubeenahmadkhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mubeenahmadkhan"/>
    <language>en</language>
    <item>
      <title>Why Supabase RLS is Slow (and How to Fix It)</title>
      <dc:creator>Mubeen Ahmad Khan</dc:creator>
      <pubDate>Thu, 27 Aug 2026 07:38:34 +0000</pubDate>
      <link>https://dev.to/mubeenahmadkhan/why-supabase-rls-is-slow-and-how-to-fix-it-2oi6</link>
      <guid>https://dev.to/mubeenahmadkhan/why-supabase-rls-is-slow-and-how-to-fix-it-2oi6</guid>
      <description>&lt;p&gt;If you are seeing query performance degrade after enabling Row Level Security (RLS) on multi-tenant Supabase tables, the culprit is almost always per-row subquery evaluation.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem: Per-Row Evaluation
&lt;/h3&gt;

&lt;p&gt;When you write policies using subqueries:&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;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="nv"&gt;"Tenant isolation"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;user_tenants&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL re-evaluates that subquery across every single scanned row rather than resolving it once upfront.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Fix: Custom JWT Claims with InitPlan Wrapping
&lt;/h3&gt;

&lt;p&gt;Inject the &lt;code&gt;tenant_id&lt;/code&gt; into the user's JWT metadata on login and read it through a wrapped &lt;code&gt;(SELECT ...)&lt;/code&gt; subquery:&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;CREATE&lt;/span&gt; &lt;span class="n"&gt;POLICY&lt;/span&gt; &lt;span class="nv"&gt;"Fast tenant isolation"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;tenant_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'app_metadata'&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'tenant_id'&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Wrapping &lt;code&gt;auth.jwt()&lt;/code&gt; inside &lt;code&gt;(SELECT ...)&lt;/code&gt; is essential. Without the subquery wrapper, PostgreSQL evaluates the JSON claim extraction repeatedly for every row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For the complete benchmark data and EXPLAIN (ANALYZE, BUFFERS) execution plans, read the full deep dive on my blog:&lt;br&gt;
👉 &lt;strong&gt;&lt;a href="https://blog.rowistan.com/2026/08/supabase-rls-custom-jwt-claims.html" rel="noopener noreferrer"&gt;Read Full Benchmark &amp;amp; Guide on blog.rowistan.com&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>supabase</category>
      <category>postgres</category>
      <category>database</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
