postgres-patternslisted
Install: claude install-skill SilantevBitcoin/Base-system-Claude
# PostgreSQL Patterns
Quick reference for PostgreSQL best practices: schema design, indexing, query
optimization, concurrency, and security.
## When to Activate
- Writing SQL queries or migrations
- Designing database schemas
- Troubleshooting slow queries (EXPLAIN ANALYZE)
- Implementing Row Level Security
- Preventing deadlocks / tuning concurrency
- Setting up connection pooling
## Schema Design
### Data Type Quick Reference
| Use Case | Correct Type | Avoid |
|----------|-------------|-------|
| IDs | `bigint` (IDENTITY) or UUIDv7 | `int`, random UUIDv4 as PK |
| Strings | `text` | `varchar(255)` without reason |
| Timestamps | `timestamptz` | `timestamp` (no timezone) |
| Money | `numeric(10,2)` | `float` |
| Flags | `boolean` | `varchar`, `int` |
Rules:
- Use `lowercase_snake_case` identifiers (avoid quoted mixed-case).
- Define constraints explicitly: PK, FK with `ON DELETE`, `NOT NULL`, `CHECK`.
- Index every foreign key — always, no exceptions.
## Indexing
### Index Cheat Sheet
| Query Pattern | Index Type | Example |
|--------------|------------|---------|
| `WHERE col = value` | B-tree (default) | `CREATE INDEX idx ON t (col)` |
| `WHERE col > value` | B-tree | `CREATE INDEX idx ON t (col)` |
| `WHERE a = x AND b > y` | Composite | `CREATE INDEX idx ON t (a, b)` |
| `WHERE jsonb @> '{}'` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
| `WHERE tsv @@ query` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
| Time-series ranges | BRIN | `CREATE INDEX idx