# Postgres migration locked a big table in production, how do I add a column or index safely next time?

Source: https://gitshowcase.com/forum/discuss/postgres-migration-locked-a-big-table-in-production-how-do-i-add/
Site: GitShowcase Forum, category Databases and Vector Stores
Published: 2026-12-02. Updated: 2026-12-02. Replies: 1.

## Question by Teodor M., 2026-12-02

Embarrassing one. Last week a routine migration on our `events` table (a few hundred million rows) blocked writes for several minutes during business hours. The migration was two statements:

```sql
ALTER TABLE events ADD COLUMN source text NOT NULL DEFAULT 'web';
CREATE INDEX events_source_idx ON events (source);
```

Our migration tool wraps each file in a transaction. While it ran, inserts piled up in the application, connections filled and the on call person had a bad hour. We are on a managed Postgres, a recent major version.

I know in general that some DDL takes heavy locks, but I never internalised which forms are safe and which are not. I would like a reliable rule set for the common cases:

1. Adding a column, with and without a default.
2. Adding a NOT NULL constraint to an existing column.
3. Adding a foreign key or check constraint.
4. Creating an index.

I would also like to know how to make a migration fail fast instead of queuing behind a long transaction and then blocking everyone behind it. What is your checklist before running DDL on a hot table?

## Reply 1 by Anand R., 2026-12-02

Set a short `lock_timeout` for the migration session so it gives up instead of waiting forever for its lock. A DDL statement that queues behind a long running transaction also blocks every query that arrives after it, and that is usually the real outage. Fail, retry in a quiet moment, and alert on long transactions.

---
Cite as: GitShowcase Forum, "Postgres migration locked a big table in production, how do I add a column or index safely next time?", https://gitshowcase.com/forum/discuss/postgres-migration-locked-a-big-table-in-production-how-do-i-add/, accessed 2026-12-02.
