Row Level Security (RLS) Architecture & Policy Mastery

Row Level Security (RLS) Architecture & Policy Mastery in Supabase: Because client applications connect directly to Supabase over HTTPS via PostgREST, Row…

Because client applications connect directly to Supabase over HTTPS via PostgREST, Row Level Security (RLS) is the primary security boundary of your entire application stack.


How RLS Works at the PostgreSQL Kernel Level

When RLS is active on a table, PostgreSQL's query rewriter intercepts every SQL query generated by PostgREST and injects security policy predicates into the query's WHERE clause before execution. Even if a client requests SELECT * FROM sensitive_documents, PostgreSQL evaluates the policy rules against each row.

Client Request: GET /rest/v1/documents
                    |
                    v
          [ PostgREST Gateway ]
                    |
                    v
        [ PostgreSQL Query Rewriter ]
        Original: SELECT * FROM documents;
        Rewritten: SELECT * FROM documents 
                   WHERE owner_id = (SELECT auth.uid());
                    |
                    v
        [ Kernel Evaluates Data Blocks ]
        (Only permissible rows are ever returned)

Step-by-Step Policy Configuration

-- 1. Enable RLS on the table (locks table down completely; 0 access by default)
ALTER TABLE public.documents ENABLE ROW LEVEL SECURITY;

-- 2. Force RLS for table owners too (prevents accidental bypass)
ALTER TABLE public.documents FORCE ROW LEVEL SECURITY;

-- 3. Read Policy (USING clause)
CREATE POLICY "Users can view their own documents or public documents"
ON public.documents
FOR SELECT
TO authenticated
USING (
    owner_id = (SELECT auth.uid()) OR is_public = true
);

-- 4. Insert Policy (WITH CHECK clause)
CREATE POLICY "Users can create documents assigned to themselves"
ON public.documents
FOR INSERT
TO authenticated
WITH CHECK (
    owner_id = (SELECT auth.uid())
);

-- 5. Update Policy (BOTH USING and WITH CHECK)
CREATE POLICY "Users can update their own documents"
ON public.documents
FOR UPDATE
TO authenticated
USING (
    owner_id = (SELECT auth.uid()) -- Must own the row to target it
)
WITH CHECK (
    owner_id = (SELECT auth.uid()) -- Cannot re-assign ownership during update!
);

-- 6. Delete Policy
CREATE POLICY "Users can delete their own documents"
ON public.documents
FOR DELETE
TO authenticated
USING (
    owner_id = (SELECT auth.uid())
);

Performance Optimization: The (SELECT auth.uid()) Pattern

CRITICAL PERFORMANCE TIP: Notice the syntax (SELECT auth.uid()) instead of a bare auth.uid(). In PostgreSQL, calling owner_id = auth.uid() causes the database engine to re-invoke the auth.uid() function for every single row evaluated during a table scan. Wrapping it in a scalar subquery (SELECT auth.uid()) tells the query planner that the result is constant for the duration of the query, evaluating it exactly once and enabling standard B-Tree index scans.


Inspecting Claims with auth.jwt()

Supabase JWTs carry custom metadata in the raw_app_meta_data and raw_user_meta_data fields. You can inspect these directly inside SQL policies without querying other tables:

-- Admin-only policy checking custom claims in the encrypted JWT
CREATE POLICY "Superadmins have full system access"
ON public.system_settings
FOR ALL
TO authenticated
USING (
    ((SELECT auth.jwt()) -> 'app_metadata' ->> 'is_superadmin')::boolean = true
);

Exercises

  1. Write an RLS policy for an order_items table where access is granted only if the user owns the parent orders record.
  2. Explain the difference between the USING clause and the WITH CHECK clause in an UPDATE policy.
  3. Benchmark the execution plan of a query using auth.uid() vs (SELECT auth.uid()) on a table with 100,000 rows.