Skip to content

Parsa-29/loop-config / skills/supabase-postgres-best-practices

supabase-postgres-best-practices

Verified

Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.

FileKindSize
CHANGELOG.mdmarkdown2.0 KB
SKILL.mdskill md2.5 KB
references/_contributing.mdmarkdown4.0 KB
references/_sections.mdmarkdown1.6 KB
references/_template.mdmarkdown1.0 KB
references/advanced-full-text-search.mdmarkdown1.4 KB
references/advanced-jsonb-indexing.mdmarkdown1.4 KB
references/conn-idle-timeout.mdmarkdown1.3 KB
references/conn-limits.mdmarkdown1.3 KB
references/conn-pooling.mdmarkdown1.3 KB
references/conn-prepared-statements.mdmarkdown1.5 KB
references/data-batch-inserts.mdmarkdown1.3 KB
references/data-n-plus-one.mdmarkdown1.3 KB
references/data-pagination.mdmarkdown1.3 KB
references/data-upsert.mdmarkdown1.4 KB
references/lock-advisory.mdmarkdown1.5 KB
references/lock-deadlock-prevention.mdmarkdown1.8 KB
references/lock-short-transactions.mdmarkdown1.3 KB
references/lock-skip-locked.mdmarkdown1.3 KB
references/monitor-explain-analyze.mdmarkdown1.4 KB
references/monitor-pg-stat-statements.mdmarkdown1.4 KB
references/monitor-vacuum-analyze.mdmarkdown1.5 KB
references/query-composite-indexes.mdmarkdown1.4 KB
references/query-covering-indexes.mdmarkdown1.3 KB
references/query-index-types.mdmarkdown1.5 KB
references/query-missing-indexes.mdmarkdown1.2 KB
references/query-partial-indexes.mdmarkdown1.3 KB
references/schema-constraints.mdmarkdown1.8 KB
references/schema-data-types.mdmarkdown1.4 KB
references/schema-foreign-key-indexes.mdmarkdown1.6 KB
references/schema-lowercase-identifiers.mdmarkdown1.7 KB
references/schema-partitioning.mdmarkdown1.5 KB
references/schema-primary-keys.mdmarkdown1.8 KB
references/security-privileges.mdmarkdown1.5 KB
references/security-rls-basics.mdmarkdown1.3 KB
references/security-rls-performance.mdmarkdown2.1 KB

references/security-rls-performance.md

View on GitHub
---
title: Optimize RLS Policies for Performance
impact: HIGH
impactDescription: 5-10x faster RLS queries with proper patterns
tags: rls, performance, security, optimization
---

## Optimize RLS Policies for Performance

Poorly written RLS policies can cause severe performance issues. Use subqueries and indexes strategically.

**Incorrect (function called for every row):**

```sql
create policy orders_policy on orders
  using (auth.uid() = user_id);  -- auth.uid() called per row!

-- With 1M rows, auth.uid() is called 1M times
```

**Correct (wrap functions in SELECT):**

```sql
create policy orders_policy on orders
  using ((select auth.uid()) = user_id);  -- Called once, cached

-- 100x+ faster on large tables
```

Use security definer functions for complex checks:

`SECURITY DEFINER` functions run with the creator's privileges and bypass RLS on any tables they touch — which is what makes them useful for internal lookups, but also what makes them dangerous if misused. Always include an explicit `auth.uid()` check inside the function body, keep them in a non-exposed schema, and revoke `EXECUTE` from any role that shouldn't call them directly.

```sql
-- Create helper function in a private schema
create or replace function private.is_team_member(team_id bigint)
returns boolean
language sql
security definer
set search_path = ''
as $$
  select exists (
    select 1 from public.team_members
    -- always check the calling user's identity inside the function
    where team_id = $1 and user_id = (select auth.uid())
  );
$$;

-- Revoke direct execution from public roles
revoke execute on function private.is_team_member(bigint) from PUBLIC, anon, authenticated, service_role;

-- Use in policy (indexed lookup, not per-row check)
create policy team_orders_policy on orders
  using ((select private.is_team_member(team_id)));
```

Always add indexes on columns used in RLS policies:

```sql
create index orders_user_id_idx on orders (user_id);
```

Reference: [RLS Performance](https://supabase.com/docs/guides/database/postgres/row-level-security#rls-performance-recommendations)
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
Agent Cost Reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall