Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Postgres adapter optimization for add_column method

#6422

Looking at postgres adapter I found that add_column works not optimal. First it executes ALTER TABLE ADD COLUMN query and only then sets defaults and NOT NULL if they are needed. But in the real production environment it takes hours to run such migrations.

For example we run:

add_column :users, :failed_attempts, :integer, :null=>false, :default=>0

this migration executes:

UPDATE "users" SET "failed_attempts"=0 WHERE "failed_attempts" IS NULL

which took more then one hour to complete for the table with 2M+ records.

But:

ALTER TABLE users ADD COLUMN failed_attempts INTEGER NOT NULL DEFAULT 0;

took just 2 minutes to complete for the same table.

So such migration will lock the users table for an hour (or even more) and so stop the whole application for hours instead of just 2 minutes.

In the assigned patch I've fixed it in the manner it was done for the mysql.

Reported by 2kan · February 12th, 2011 @ 06:50 PM

State: committed
Milestone: 3.1
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. 2kan
    2kan
    • Tag set to patch, postgresql

    February 12th, 2011 @ 06:50 PM

  2. Vijay Dev
    Vijay Dev
    • Title changed from [PACTH] Postgres adapter optimization for add_column method to [PATCH] Postgres adapter optimization for add_column method

    February 12th, 2011 @ 07:15 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • Milestone set to 3.1
    • Assigned user changed from Santiago Pastorino to Aaron Patterson
    • Importance changed from to Low

    February 12th, 2011 @ 07:17 PM

  4. Aaron Patterson
    Aaron Patterson
    • State changed from new to committed

    seems good. Merged and pushed.

    February 15th, 2011 @ 06:55 PM