PostgreSQL schema design, query optimization, indexing, and administration. Use when working with PostgreSQL, JSONB, partitioning, RLS, CTEs, window functions, or EXPLAIN ANALYZE.
From compound-engineeringnpx claudepluginhub iliaal/compound-engineering-plugin --plugin compound-engineeringThis skill uses the workspace's default tool permissions.
references/concurrency-patterns.mdreferences/full-text-search.mdreferences/operations.mdSearches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Executes pre-written implementation plans: critically reviews, follows bite-sized steps exactly, runs verifications, tracks progress with checkpoints, uses git worktrees, stops on blockers.
| Need | Use | Avoid |
|---|---|---|
| Primary key | BIGINT GENERATED ALWAYS AS IDENTITY | SERIAL, BIGSERIAL |
| Timestamps | TIMESTAMPTZ | TIMESTAMP (loses timezone) |
| Text | TEXT | VARCHAR(n) unless constraint needed |
| Money | NUMERIC(precision, scale) | MONEY, FLOAT |
| Boolean | BOOLEAN with NOT NULL DEFAULT | nullable booleans |
| JSON | JSONB | JSON (no indexing), text JSON |
| UUID | gen_random_uuid() (PG13+) | uuid-ossp extension |
| IP addresses | INET / CIDR | text |
| Ranges | TSTZRANGE, INT4RANGE, etc. | pair of columns |
NOT NULL on every column unless NULL has business meaningCHECK constraints for domain rules at DB levelEXCLUDE constraints for range overlaps: EXCLUDE USING gist (room WITH =, during WITH &&)created_at TIMESTAMPTZ NOT NULL DEFAULT now()updated_at with trigger, never trust app layer aloneBIGINT PKs -- cheaper JOINs than UUID, better index localityCREATE INDEX CONCURRENTLY, add columns with DEFAULT (instant add). Never ALTER TYPE on large tables in-place.NULLS NOT DISTINCT on unique indexes (PG15+) -- treats NULLs as equal for uniquenessREVOKE ALL ON SCHEMA public FROM public| Type | Use When |
|---|---|
| B-tree (default) | Equality, range, sorting, LIKE 'prefix%' |
| GIN | JSONB (@>, ?, ?&), arrays, full-text (tsvector) |
| GiST | Geometry, ranges, full-text (smaller but slower than GIN) |
| BRIN | Large tables with natural ordering (timestamps, serial IDs) |
Index rules:
WHERE status = 'active' -- smaller, fasterINCLUDE (col) -- avoids heap lookupON (lower(email)) -- for function-based WHEREfillfactor = 70-90 on write-heavy tables -- reserves space for HOT updates, reducing index bloatSELECT * FROM pg_stat_user_indexes WHERE idx_scan = 0Detect unindexed foreign keys:
SELECT conrelid::regclass, a.attname
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
WHERE c.contype = 'f'
AND NOT EXISTS (
SELECT 1 FROM pg_index i
WHERE i.indrelid = c.conrelid AND a.attnum = ANY(i.indkey)
);
-- GIN index for containment queries
CREATE INDEX ON items USING gin (metadata);
SELECT * FROM items WHERE metadata @> '{"status": "active"}';
-- Expression index for specific key access
CREATE INDEX ON items ((metadata->>'category'));
SELECT * FROM items WHERE metadata->>'category' = 'electronics';
Prefer typed columns over JSONB for frequently queried, well-structured data. Use JSONB for truly dynamic/variable attributes.
Use jsonb_path_ops operator class for containment-only (@>) queries -- 2-3x smaller index. Use default jsonb_ops when key-existence (?, ?|) is needed.
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders FORCE ROW LEVEL SECURITY; -- applies to table owner too
-- Set session context (generic, no extensions needed)
SET app.current_user_id = '123';
CREATE POLICY orders_user_policy ON orders
FOR ALL
USING (user_id = current_setting('app.current_user_id')::bigint);
Performance: Policy expressions evaluate per row. Wrap function calls in a scalar subquery so PG evaluates once and caches:
-- BAD: called per row
USING (get_current_user() = user_id)
-- GOOD: evaluated once, cached
USING ((SELECT get_current_user()) = user_id)
Always index columns referenced in RLS policies. For complex multi-table checks, use SECURITY DEFINER helper functions.
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) before optimizingpg_stat_statements for slow query detection: SELECT query, mean_exec_time, calls FROM pg_stat_statements WHERE mean_exec_time > 100 ORDER BY mean_exec_time DESC LIMIT 20SELECT relname, n_dead_tup, last_vacuum FROM pg_stat_user_tables WHERE n_dead_tup > 10000 ORDER BY n_dead_tup DESCWHERE for function wrappingrows removed by filter -> index doesn't match predicateMATERIALIZED/NOT MATERIALIZED hints to control optimizationEXISTS over IN for correlated subqueriesLATERAL JOIN when subquery needs outer row referenceWHERE id > $last ORDER BY id LIMIT $n) over OFFSETSELECT reltuples FROM pg_class WHERE relname = 'table' -- avoids full count(*) on large tablesREFRESH MATERIALIZED VIEW CONCURRENTLY (needs unique index). Schedule refresh, not per-query.See concurrency-patterns.md for UPSERT, deadlock prevention, N+1 elimination, batch inserts, and queue processing with SKIP LOCKED.
Use when table exceeds ~100M rows or needs TTL purge:
RANGE -- time-series (by month/year), most commonLIST -- categorical (by region, tenant)HASH -- even distribution when no natural keyPartition key must be in every unique/PK constraint. Create indexes on partitions, not parent.
pg_advisory_xact_lock(key)pg_try_advisory_lock(key) -- returns false instead of waitingSELECT * FROM pg_stat_activity WHERE wait_event_type = 'Lock'SELECT deadlocks FROM pg_stat_database WHERE datname = current_database()See full-text-search.md for weighted tsvector setup, query syntax, highlighting, and when to use PG full-text vs external search.
Always pool in production. Direct connections cost ~10MB each.
transaction mode for most workloadsstatement mode if no session-level features (prepared statements, temp tables, advisory locks)Prepared statement caveat: Named prepared statements are bound to a specific connection. In transaction-mode pooling, the next request may hit a different connection. Use unnamed/extended-query-protocol statements (most ORMs default to this), or deallocate immediately after use.
See operations.md for performance tuning, maintenance/monitoring, WAL, replication, and backup/recovery.
CREATE EXTENSION vector;
ALTER TABLE items ADD COLUMN embedding vector(1536); -- match your model's output dimensions
-- HNSW: better recall, higher memory. Default choice.
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
-- IVFFlat: lower memory for large datasets. Set lists = sqrt(row_count).
CREATE INDEX ON items USING ivfflat (embedding vector_cosine_ops) WITH (lists = 1000);
Always filter BEFORE vector search (use partial indexes or CTEs with pre-filtered rows). Distance operators: <=> cosine, <-> L2, <#> inner product.
| Anti-Pattern | Fix |
|---|---|
SELECT * | List needed columns |
| N+1 queries in application loop | Use JOIN, IN, or batch fetch |
OFFSET for pagination on large tables | Cursor pagination: WHERE id > $last ORDER BY id LIMIT $n |
count(*) on large tables | Approximate: SELECT reltuples FROM pg_class WHERE relname = 'table' |
| Nullable booleans | NOT NULL DEFAULT false -- three-valued logic causes subtle bugs |
| Missing FK indexes | See detection query in Index Strategy above |
ORDER BY RANDOM() | Use TABLESAMPLE or application-side shuffle |
Detection queries:
-- Slow queries (requires pg_stat_statements)
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
WHERE mean_exec_time > 100
ORDER BY mean_exec_time DESC LIMIT 20;
-- Table bloat (dead tuples awaiting vacuum)
SELECT relname, n_dead_tup, last_vacuum, last_autovacuum
FROM pg_stat_user_tables
WHERE n_dead_tup > 10000
ORDER BY n_dead_tup DESC;
-- Unused indexes (candidates for removal)
SELECT schemaname, relname, indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0 AND indexrelname NOT LIKE '%_pkey'
ORDER BY pg_relation_size(indexrelid) DESC;
Run EXPLAIN (ANALYZE, BUFFERS) on changed queries. Confirm no sequential scans on large tables and no unindexed FK columns before declaring done.