Database patterns

From yanacuti1121/Yana-AI · View interactive version

Database patterns is an AI agent skill from the GitHub repository yanacuti1121/Yana-AI. Design and optimize relational database schemas — normalization, indexing strategy, query optimization, N+1 elimination, pagination patterns, and migration safety. Use when asked to "design a schema", "fix slow queries", "add an index", "N+1 problem", "paginate results", "write a migration", or "dat

How to use this skill

Skill file

When to Use


Schema Design

Normalization rules of thumb

Primary keys

-- Prefer: surrogate integer PK for internal tables
id BIGSERIAL PRIMARY KEY

-- Prefer: UUID for distributed systems / exposed APIs (no enumeration)
id UUID DEFAULT gen_random_uuid() PRIMARY KEY

-- Avoid: natural keys as PK (emails change, names change)

Naming conventions


Indexing

When to add an index

-- Single column
CREATE INDEX idx_orders_user_id ON orders(user_id);

-- Composite — supports WHERE status = ? AND created_at > ?
CREATE INDEX idx_orders_status_created ON orders(status, created_at DESC);

-- Partial index — index only the rows you query
CREATE INDEX idx_orders_pending ON orders(created_at) WHERE status = 'pending';

When NOT to add an index


Query Optimization

N+1 problem

-- N+1: 1 query for users + N queries for each user's orders
SELECT * FROM users;
-- then for each user: SELECT * FROM orders WHERE user_id = ?

-- Fix: JOIN or subquery in one round trip
SELECT u.id, u.name, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id, u.name;

ORM fix: use eager loading (include/preload/with) — never lazy-load in a loop.

EXPLAIN ANALYZE

EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
-- Look for: Seq Scan on large tables, high "rows removed by filter"
-- Target: Index Scan or Index Only Scan on hot paths

Pagination

Offset pagination — simple but degrades

-- Works fine for small offsets; O(offset) cost at large pages
SELECT * FROM orders ORDER BY created_at DESC LIMIT 20 OFFSET 1000;

Cursor pagination — scales to millions of rows

-- Client sends last seen id; server returns next page
SELECT * FROM orders
WHERE (created_at, id) < ($last_created_at, $last_id)
ORDER BY created_at DESC, id DESC
LIMIT 20;

Use cursor pagination for: feeds, infinite scroll, any table > 100k rows.


Migration Safety

Rules for migrations on live tables with data:

| Operation | Risk | Safe approach |

|---|---|---|

| Add column NOT NULL no default | Locks table | Add nullable first → backfill → add constraint |

| Add column with DEFAULT | Rewrites table in old PG | Use ADD COLUMN … DEFAULT (PG 11+: instant) |

| Drop column | Irreversible | Soft-remove in app first → wait one deploy → drop |

| Add index | Locks writes | CREATE INDEX CONCURRENTLY |

| Rename column | Breaks app | Add new column → dual-write → migrate reads → drop old |

| Backfill large table | Lock / timeout | Batch: UPDATE … WHERE id BETWEEN ? AND ? LIMIT 1000 |


Anti-Fake-Pass Rules

Before claiming schema or query work is done, you MUST show:

Reference: gates/anti-fake-pass-gate.md

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.