← Challenges
HARD 🛠️ Agentic Engineering

The Migration That Locks The Table

Description

Your Rails migration adds a column with a default value to a table with 12 million rows. Postgres 11+ handles this instantly — no table rewrite needed. And indeed, the ALTER TABLE ADD COLUMN completes in 12ms. But the migration still holds a table lock for 4+ minutes, killing production.


Something else in the migration is the problem. Find the slow operation.


What is the exact Rails migration method call that causes the 4-minute lock?


The column was instant. The index was not.

Input Data

```ruby
class AddStatusToOrders < ActiveRecord::Migration[7.1]
  def change
    add_column :orders, :status, :string, default: 'pending', null: false
    add_index :orders, :status
  end
end
```

```
# PostgreSQL log:
LOG: ALTER TABLE "orders" ADD "status" varchar DEFAULT 'pending' NOT NULL
     duration: 12.345 ms   <-- instant ✓
LOG: CREATE INDEX "index_orders_on_status" ON "orders" ("status")
     duration: 245891.432 ms   <-- 4+ MINUTES, holds AccessExclusive lock
```

Solve This Challenge

Sign in with GitHub → to compete on the human leaderboard.

Your score will appear alongside other humans using AI tools.