Building B2B SaaS applications in Supabase requires multi-tenant isolation where users belong to organizations (workspaces) with granular Role-Based Access Control (RBAC).
Multi-Tenant Schema Architecture
-- Organizations table
CREATE TABLE organizations (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
);
-- Organization Memberships (Join Table)
CREATE TABLE organization_members (
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
role TEXT NOT NULL CHECK (role IN ('OWNER', 'ADMIN', 'MEMBER', 'BILLING')),
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
PRIMARY KEY (organization_id, user_id)
);
-- Application Resource Table
CREATE TABLE workspace_projects (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE NOT NULL,
title TEXT NOT NULL,
budget NUMERIC(10, 2),
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
);
CREATE INDEX idx_workspace_projects_org ON workspace_projects(organization_id);
Writing Secure Multi-Tenant Policies
To allow members of an organization to read workspace projects, the policy must query the organization_members table:
ALTER TABLE workspace_projects ENABLE ROW LEVEL SECURITY;
-- Select Policy for Organization Members
CREATE POLICY "Members can view organization projects"
ON workspace_projects
FOR SELECT
TO authenticated
USING (
organization_id IN (
SELECT om.organization_id
FROM organization_members om
WHERE om.user_id = (SELECT auth.uid())
)
);
-- Write Policy: Only Owners and Admins can create new projects
CREATE POLICY "Admins and Owners can create projects"
ON workspace_projects
FOR INSERT
TO authenticated
WITH CHECK (
EXISTS (
SELECT 1
FROM organization_members om
WHERE om.organization_id = workspace_projects.organization_id
AND om.user_id = (SELECT auth.uid())
AND om.role IN ('OWNER', 'ADMIN')
)
);
Avoiding Recursive Policy Deadlocks (SECURITY DEFINER Functions)
If you enable RLS on organization_members and query it inside a policy on workspace_projects, you can create recursive policy checks that degrade performance.
The solution is to use a SECURITY DEFINER helper function that executes with elevated privileges to check membership cleanly:
-- Function runs with database owner privileges to bypass recursive RLS checks
CREATE OR REPLACE FUNCTION public.is_org_member(p_org_id UUID)
RETURNS BOOLEAN
LANGUAGE sql
SECURITY DEFINER
SET search_path = public -- Security requirement: lock down search path!
STABLE AS $$
SELECT EXISTS (
SELECT 1
FROM organization_members
WHERE organization_id = p_org_id
AND user_id = (SELECT auth.uid())
);
$$;
-- Clean, high-performance policy:
CREATE POLICY "Members view projects via helper"
ON workspace_projects
FOR SELECT
TO authenticated
USING (
is_org_member(organization_id)
);
Exercises
- Create a policy allowing only organization
OWNERmembers to delete a project. - Explain why setting
SET search_path = publicis a mandatory security rule forSECURITY DEFINERfunctions in PostgreSQL. - Design a policy structure that supports role inheritance (
OWNERinherits allADMINandMEMBERprivileges).