Skip to content

syahiidkamil/Software-Engineer-AI-Agent-Atlas / .claude/skills/supabase-postgres-best-practices

supabase-postgres-best-practices

Verified

Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pg_cron, pgmq), vector/semantic search (pgvector), and restoring dumps (pg_restore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.

FileKindSize
CHANGELOG.mdmarkdown5.6 KB
LICENSEother1.0 KB
SKILL.mdskill md3.2 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/schema-constraints.md

View on GitHub
---
title: Add Constraints Safely in Migrations
impact: HIGH
impactDescription: Prevents migration failures and enables idempotent schema changes
tags: constraints, migrations, schema, alter-table
---

## Add Constraints Safely in Migrations

PostgreSQL does not support `ADD CONSTRAINT IF NOT EXISTS`. Migrations using this syntax will fail.

**Incorrect (causes syntax error):**

```sql
-- ERROR: syntax error at or near "not" (SQLSTATE 42601)
alter table public.profiles
add constraint if not exists profiles_birthchart_id_unique unique (birthchart_id);
```

**Correct (idempotent constraint creation):**

```sql
-- Use DO block to check before adding
do $$
begin
  if not exists (
    select 1 from pg_constraint
    where conname = 'profiles_birthchart_id_unique'
    and conrelid = 'public.profiles'::regclass
  ) then
    alter table public.profiles
    add constraint profiles_birthchart_id_unique unique (birthchart_id);
  end if;
end $$;
```

For all constraint types:

```sql
-- Check constraints
do $$
begin
  if not exists (
    select 1 from pg_constraint
    where conname = 'check_age_positive'
  ) then
    alter table users add constraint check_age_positive check (age > 0);
  end if;
end $$;

-- Foreign keys
do $$
begin
  if not exists (
    select 1 from pg_constraint
    where conname = 'profiles_birthchart_id_fkey'
  ) then
    alter table profiles
    add constraint profiles_birthchart_id_fkey
    foreign key (birthchart_id) references birthcharts(id);
  end if;
end $$;
```

Check if constraint exists:

```sql
-- Query to check constraint existence
select conname, contype, pg_get_constraintdef(oid)
from pg_constraint
where conrelid = 'public.profiles'::regclass;

-- contype values:
-- 'p' = PRIMARY KEY
-- 'f' = FOREIGN KEY
-- 'u' = UNIQUE
-- 'c' = CHECK
```

Reference: [Constraints](https://www.postgresql.org/docs/current/ddl-constraints.html)
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall