Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 1.13 KB

migration_index_postgresql.md

File metadata and controls

27 lines (21 loc) · 1.13 KB

Add an index on postgresql

By default, Postgres locks writes (but not reads) to a table while creating an index on it. That can result in unacceptable downtime during a production deploy. On a large table, indexing can take hours.

it’s a good idea to isolate concurrent index migrations to their own migration files

The disable_ddl_transaction! method applies only to that migration file. Adjacent migrations still run in their own transactions and roll back automatically if they fail.

class AddIndexToAsksActive < ActiveRecord::Migration
  disable_ddl_transaction!

  def change
    reversible do |direction|
      direction.up   { execute("SET SESSION statement_timeout = 0;") }
      direction.down { execute("SET SESSION statement_timeout = 0;") }
    end
    
    add_index :asks, :active, algorithm: :concurrently
  end
end