Write RLS Policies with AI Prompts

Share

TL;DR

These prompts help you write advanced RLS policies for complex authorization scenarios. They cover role-based access, hierarchical permissions, shared resources, and performance-optimized policies.

Role-Based Access Control

RBAC Policies

Create RLS policies with role-based access control.

Roles in my system:

  • admin: full access to everything
  • editor: can read and write content
  • viewer: read-only access

Tables: posts, comments, settings

Create policies where:

  1. Admins can do anything
  2. Editors can CRUD posts and comments they create
  3. Viewers can only SELECT

Store user roles in a profiles table or JWT claims. Show both approaches and explain trade-offs.

Hierarchical Access

Hierarchical Policies

Create RLS for hierarchical data access.

Structure:

  • Organizations have multiple teams
  • Teams have multiple projects
  • Projects have multiple tasks

A user should access:

  • Tasks in projects they belong to
  • Projects in teams they belong to
  • Teams in organizations they belong to

Create efficient policies that don't require multiple subqueries. Consider using helper functions for membership checks.

Shared Resources

Shared Resource Policies

Create RLS for resources that can be shared with other users.

Table: documents Columns: id, owner_id, content

Table: document_shares Columns: document_id, shared_with_user_id, permission (view/edit)

Policies needed:

  1. Owner has full access
  2. Users with 'view' share can SELECT
  3. Users with 'edit' share can SELECT and UPDATE
  4. Only owner can DELETE
  5. Only owner can manage shares

Make sure performance is good with proper indexes.

Watch out for recursive policies: If Policy A references Table B, and Table B has a policy referencing Table A, you can create infinite loops. Use security definer functions to break these cycles.

Performance Optimization

Optimized Policies

Optimize my RLS policies for better performance.

Current issues:

  • Policies are slow with large tables
  • Multiple subqueries in each policy
  • Checking membership in several tables

Help me:

  1. Create a security definer function for membership checks
  2. Use materialized views or caching for role lookups
  3. Add proper indexes for policy conditions
  4. Simplify complex policy logic

Show before/after with EXPLAIN ANALYZE to verify improvement.

Pro tip: Store commonly checked permissions in the user's JWT claims using Supabase custom claims. This avoids database lookups for every policy check.

Should I use one policy per operation or combine them?

Separate policies are clearer and easier to maintain. PostgreSQL combines them with OR logic for the same operation, so having multiple policies doesn't hurt performance.

How do I test RLS policies?

Use Supabase's SQL editor with SET ROLE to test as different users. You can also write automated tests that create test users and verify access.

Verify Your RLS Policies

Scan your database to find policy gaps and security issues.

Start Free Scan
AI Fix Prompts

Write RLS Policies with AI Prompts