docs/
Outline
---
title: Manage a database like a tech lead
slug: database-setup
lang: en
published: 2026-11-24
words: 799
lines: 34
size: 5.1 KB
read: 4 min read
reel: Manage a database like a tech lead
---
A column rename can take production down while the old app is still serving requests. The migration changes the name immediately, and the old version keeps querying the name it knows. In episode 4 of Production Ready, I show how we split that change across three deploys.
Rename a column in three deploys
Take a fictional customers table where name is becoming display_name. Both columns hold the same value during the transition.
- Add the new column. Add
display_nameas nullable and keepnameavailable. Make writes tonamereachdisplay_nameeven while older versions are running. A temporary Postgres trigger can copy the value in the same transaction. Backfill existing rows in short batches by primary key, then check that the two columns agree. Include workers, imports, and scheduled jobs when you check who writes to the table. - Move the app. Once the data matches, switch reads to
display_name. While both versions can run, preserve write compatibility. For example, have the new version write both columns in the same transaction and let the trigger handle old writers. Keep this in place while the prior version is still your fallback. Test old and new versions together in staging, including a switch back to the old version after a write from the new one. - Drop the old column. In the third release, ship a write path that uses only
display_name, behind a switch. Keep writing both columns until the old instances and workers are out of rotation. Confirm that your fallback also works with only the new column. Check that any remaining constraints allow the switch and check reports, views, and indexes for dependencies. Turn on the new write path and remove the temporary trigger and old column together.
To verify the copy, check for rows where name IS DISTINCT FROM display_name. That comparison also catches a NULL on one side. On a big table, split the check into ID ranges and run it while the compatibility path is active. PostgreSQL ALTER TABLE
Put checks in the migration runner
Our runner has rules for pre-deploy, during-deploy, and post-deploy. Before deployment, rehearse on a separate staging database and verify that the current and next app versions can use the planned schema. During deployment, run the intended step once and watch errors and lock waits before moving traffic. Afterward, verify the data and exercise the requests that use the new column before scheduling cleanup.
The runner can trigger rollback when a deploy check fails. While the schema supports both app versions, the app can go back without discarding writes. A transaction can undo uncommitted changes. Once a destructive migration commits, recovery can require its own migration or a restore and reconciliation of later writes. Decide which recovery path applies before running the step. Production Ready deploy setup
Backfill in short batches
Pick a stable indexed key, usually the primary key, and process one range per transaction. Save progress only after the transaction commits and make each batch safe to retry. Start with a small batch in staging. Watch lock waits, query latency, and replication lag if you have replicas. If they climb, pause or reduce the batch size. Batching shortens the time row locks are held. Schema changes such as dropping a column still need a separate lock budget. Use a bounded lock_timeout for the DDL so it fails if it cannot acquire the lock promptly. PostgreSQL locking
Separate access, connections, and cache
- Access. Our production database runs on a separate server behind firewall rules. Restrict network access to the services that need it. Give the app a restricted database role and use a separate role for migrations.
- Connections. PgBouncer caps and reuses connections to Postgres. Set the connection budget across all pools and leave room for maintenance. If you use transaction pooling, check your driver and any features that rely on session state. PgBouncer pooling modes
- Cache. Redis takes repeated reads off Postgres. Cache data that tolerates a brief stale window, use a TTL, and define what happens to the cache after the database write commits. The app should handle a cache miss by reading Postgres. Redis cache-aside
Restore a backup before you need it
We regularly restore backups to check they work. Restore into an isolated database and point a test instance of the app at it. Check important records, permissions, and a few critical read and write flows. Record how long recovery took and how recent the recovered data was. If you depend on point-in-time recovery, choose a timestamp and verify the recovered state against a known write. PostgreSQL backup and recovery
Postgres is my default for most projects. When deciding whether the setup needs to grow, measure concurrent writes, query latency, and connection usage on your own workload.
| A column rename can take production down while the old app is still serving requests. The migration changes the name immediately, and the old version keeps querying the name it knows. In episode 4 of Production Ready, I show how we split that change across three deploys. |
| ## Rename a column in three deploys |
| Take a fictional `customers` table where `name` is becoming `display_name`. Both columns hold the same value during the transition. |
| 1. **Add the new column.** Add `display_name` as nullable and keep `name` available. Make writes to `name` reach `display_name` even while older versions are running. A temporary Postgres trigger can copy the value in the same transaction. Backfill existing rows in short batches by primary key, then check that the two columns agree. Include workers, imports, and scheduled jobs when you check who writes to the table. |
| 2. **Move the app.** Once the data matches, switch reads to `display_name`. While both versions can run, preserve write compatibility. For example, have the new version write both columns in the same transaction and let the trigger handle old writers. Keep this in place while the prior version is still your fallback. Test old and new versions together in staging, including a switch back to the old version after a write from the new one. |
| 3. **Drop the old column.** In the third release, ship a write path that uses only `display_name`, behind a switch. Keep writing both columns until the old instances and workers are out of rotation. Confirm that your fallback also works with only the new column. Check that any remaining constraints allow the switch and check reports, views, and indexes for dependencies. Turn on the new write path and remove the temporary trigger and old column together. |
| To verify the copy, check for rows where `name IS DISTINCT FROM display_name`. That comparison also catches a `NULL` on one side. On a big table, split the check into ID ranges and run it while the compatibility path is active. [PostgreSQL ALTER TABLE](https://www.postgresql.org/docs/18/sql-altertable.html) |
| ## Put checks in the migration runner |
| Our runner has rules for `pre-deploy`, `during-deploy`, and `post-deploy`. Before deployment, rehearse on a separate staging database and verify that the current and next app versions can use the planned schema. During deployment, run the intended step once and watch errors and lock waits before moving traffic. Afterward, verify the data and exercise the requests that use the new column before scheduling cleanup. |
| The runner can trigger rollback when a deploy check fails. While the schema supports both app versions, the app can go back without discarding writes. A transaction can undo uncommitted changes. Once a destructive migration commits, recovery can require its own migration or a restore and reconciliation of later writes. Decide which recovery path applies before running the step. [Production Ready deploy setup](/en/docs/deploy-setup) |
| ## Backfill in short batches |
| Pick a stable indexed key, usually the primary key, and process one range per transaction. Save progress only after the transaction commits and make each batch safe to retry. Start with a small batch in staging. Watch lock waits, query latency, and replication lag if you have replicas. If they climb, pause or reduce the batch size. Batching shortens the time row locks are held. Schema changes such as dropping a column still need a separate lock budget. Use a bounded `lock_timeout` for the DDL so it fails if it cannot acquire the lock promptly. [PostgreSQL locking](https://www.postgresql.org/docs/18/explicit-locking.html) |
| ## Separate access, connections, and cache |
| - **Access.** Our production database runs on a separate server behind firewall rules. Restrict network access to the services that need it. Give the app a restricted database role and use a separate role for migrations. |
| - **Connections.** PgBouncer caps and reuses connections to Postgres. Set the connection budget across all pools and leave room for maintenance. If you use transaction pooling, check your driver and any features that rely on session state. [PgBouncer pooling modes](https://www.pgbouncer.org/features.html) |
| - **Cache.** Redis takes repeated reads off Postgres. Cache data that tolerates a brief stale window, use a TTL, and define what happens to the cache after the database write commits. The app should handle a cache miss by reading Postgres. [Redis cache-aside](https://redis.io/docs/latest/develop/use-cases/cache-aside/) |
| ## Restore a backup before you need it |
| We regularly restore backups to check they work. Restore into an isolated database and point a test instance of the app at it. Check important records, permissions, and a few critical read and write flows. Record how long recovery took and how recent the recovered data was. If you depend on point-in-time recovery, choose a timestamp and verify the recovered state against a known write. [PostgreSQL backup and recovery](https://www.postgresql.org/docs/18/backup.html) |
| Postgres is my default for most projects. When deciding whether the setup needs to grow, measure concurrent writes, query latency, and connection usage on your own workload. |
