title: Add column with default in postgresql without table level lock
body: <p>Have such a problem - table with over 20mln rows.</p>
  
  <p>When i add new column with default - postgresql lock table for over 40 minutes so my application stop working for this time.</p>
  
  <p>So instead of</p>
  
  <pre><code>ALTER TABLE "test" ADD COLUMN "field" boolean DEFAULT True NOT NULL;
  </code></pre>
  
  <p>I do</p>
  
  <pre><code>ALTER TABLE "test" ADD COLUMN "field" boolean NULL;
  ALTER TABLE "test" ALTER COLUMN "field" SET DEFAULT true;
  </code></pre>
  
  <p>after which every new row will be with true by default, so now I need to update 20mln current rows.</p>
  
  <p>I update them in batches:</p>
  
  <pre><code>WITH cte AS (
  SELECT id as pk
  FROM "test"
  WHERE  "field" is null
  LIMIT  10000
  )
  UPDATE "test" table_
  SET "field" = true
  FROM   cte
  WHERE  table_.id = cte.pk
  </code></pre>
  
  <p>after which i do </p>
  
  <pre><code>ALTER TABLE "test" ALTER COLUMN "field" SET NOT NULL;
  </code></pre>
  
  <p>And everything is alright but when I update rows is too slow. Can you give my some advice about improving speed of update?</p>
  
  <p>Currently it update 10000 in about 2 minutes.
  I try decreasing size to 1000 - it was better (3.5 minutes for 10000) but it still to slow.</p>
  
  <p>I also try create index before update, but it doesnt give better results (as i understand it will give better result but when huge path of table will be updated).</p>