Database engineering
Use when schema, indexes, query plans, locking, or a migration affects correctness or scale.
- Orient before changing anything: engine and version — confirm tests run on the same engine, not SQLite standing in for MySQL/PostgreSQL — the live schema and migration history, existing indexes and their usage stats, the top queries by total time, who reads and writes each affected table (apps, jobs, replicas, reports), its row count, and any long transaction or lock running now. Compare column types and data shapes with production, not just fixtures: a value can be the number 0.01 in tests and the string "0.01" in production.
- Name the invariant you're protecting and encode it in the database as a unique or check constraint, not only in application code.
- Get the plan before touching an index.
EXPLAIN/EXPLAIN ANALYZE first; a query is not "fast" or "slow" without one, before or after.
- Design composite indexes equality columns first, then the range column, then
ORDER BY; lead every tenant-scoped index with tenant_id; check selectivity before adding; remove unused and duplicate indexes.
- Never run a whole-table
UPDATE or a locking DDL directly against a production-sized table. Use expand → chunked backfill → non-locking constraint → switch reads → contract, and state each step's lock impact and rollback. Deploy must have zero downtime: old code has to run on the new schema, and new code on the old.
- Scope every tenant query and composite index by
tenant_id, and check ownership on update/delete, not only on read.
- Re-run the plan after the change and compare before/after; confirm backups and the restore path still work if schema changed.
For availability and recovery work, also check:
- Restore, not just backup. A backup never restored is a hope. Schedule restore tests and record the real time-to-restore against the agreed RPO and RTO.
- Drill failover on a schedule, and fix what the drill exposes. Do not promote a lagging replica without knowing what data would be lost.
- Replication lag is a correctness issue: monitor it, and gate reads that need read-after-write on it.
- Budget locks for each migration: check the engine's actual lock mode (even metadata-only changes can need an exclusive lock), set a short lock timeout, keep scans and backfills out of the locking transaction, and have a retry plan.
- Protect the database from clients: use a pooler and per-service connection limits so one buggy client cannot exhaust connections.
- Heavy or destructive operations (migrations, failover, large deletes) get a written back-out plan and a blast-radius estimate first.
Check before you finish: the migration and changed queries ran against a production data copy (never production) and gave the expected result with no rows missed or broken (ask the user to prepare a copy if there is none), tests ran on the production engine or the gap is stated, every new foreign key on PostgreSQL has an explicit index, no invariant lives only in app code, and no migration shipped without a stated rollback. Walk the scenarios in the shared bar section 3 that can reach the change, and test each one or name it untested, starting with concurrent writers and retries (including a backfill racing app writes).
Index and plan-reading detail, the full online-migration ladder, multi-tenancy shapes, and the operational baseline: reference.md. The shared quality bar: ../../references/checklists/engineering-quality-bar.md.
Availability and recovery checks adapted in part from engineering/engineering-database-reliability-engineer.md (agency-agents, MIT).
1---2name: bosskuai-database-engineering3description: "Use when SQL or NoSQL database design affects correctness or scale (MariaDB, MySQL, PostgreSQL, SQLite, MongoDB): indexing, query plans, transactions and locking, safe online migrations, constraints, multi-tenant schemas, connection pooling, backups."4metadata:5 source: https://skillmd.com/skills/wankimmy/bosskuai-database-engineering6---78# Database engineering910Use when schema, indexes, query plans, locking, or a migration affects correctness or scale.11121. **Orient before changing anything**: engine and version — confirm tests run on the *same* engine, not SQLite standing in for MySQL/PostgreSQL — the live schema and migration history, existing indexes and their usage stats, the top queries by total time, who reads and writes each affected table (apps, jobs, replicas, reports), its row count, and any long transaction or lock running now. Compare column types and data shapes with production, not just fixtures: a value can be the number 0.01 in tests and the string "0.01" in production.132. **Name the invariant you're protecting** and encode it in the database as a unique or check constraint, not only in application code.143. **Get the plan before touching an index.** `EXPLAIN`/`EXPLAIN ANALYZE` first; a query is not "fast" or "slow" without one, before or after.154. **Design composite indexes equality columns first, then the range column, then `ORDER BY`**; lead every tenant-scoped index with `tenant_id`; check selectivity before adding; remove unused and duplicate indexes.165. **Never run a whole-table `UPDATE` or a locking DDL directly against a production-sized table.** Use expand → chunked backfill → non-locking constraint → switch reads → contract, and state each step's lock impact and rollback. Deploy must have zero downtime: old code has to run on the new schema, and new code on the old.176. **Scope every tenant query and composite index by `tenant_id`**, and check ownership on update/delete, not only on read.187. **Re-run the plan after the change** and compare before/after; confirm backups and the restore path still work if schema changed.1920For availability and recovery work, also check:2122- **Restore, not just backup.** A backup never restored is a hope. Schedule restore tests and record the real time-to-restore against the agreed RPO and RTO.23- **Drill failover** on a schedule, and fix what the drill exposes. Do not promote a lagging replica without knowing what data would be lost.24- **Replication lag is a correctness issue:** monitor it, and gate reads that need read-after-write on it.25- **Budget locks for each migration:** check the engine's actual lock mode (even metadata-only changes can need an exclusive lock), set a short lock timeout, keep scans and backfills out of the locking transaction, and have a retry plan.26- **Protect the database from clients:** use a pooler and per-service connection limits so one buggy client cannot exhaust connections.27- **Heavy or destructive operations** (migrations, failover, large deletes) get a written back-out plan and a blast-radius estimate first.2829Check before you finish: the migration and changed queries ran against a production data copy (never production) and gave the expected result with no rows missed or broken (ask the user to prepare a copy if there is none), tests ran on the production engine or the gap is stated, every new foreign key on PostgreSQL has an explicit index, no invariant lives only in app code, and no migration shipped without a stated rollback. Walk the scenarios in the shared bar section 3 that can reach the change, and test each one or name it untested, starting with concurrent writers and retries (including a backfill racing app writes).3031Index and plan-reading detail, the full online-migration ladder, multi-tenancy shapes, and the operational baseline: `reference.md`. The shared quality bar: `../../references/checklists/engineering-quality-bar.md`.3233Availability and recovery checks adapted in part from engineering/engineering-database-reliability-engineer.md (agency-agents, MIT).