Re: Difference between "add column" and "add column" with default - Mailing list pgsql-general

From Tom Lane
Subject Re: Difference between "add column" and "add column" with default
Date
Msg-id 8402.1142893892@sss.pgh.pa.us
Whole thread Raw
In response to Difference between "add column" and "add column" with default  (Guido Neitzer <guido.neitzer@pharmaline.de>)
List pgsql-general
Guido Neitzer <guido.neitzer@pharmaline.de> writes:
> What is the technical difference between adding a column to a table
> and then apply a "set value = ..." to all columns and adding a column
> with a default value = ...?

"ADD COLUMN DEFAULT ..." is implemented via a full-table rewrite,
so you end up with a version of the table that has no dead space.
Unfortunately this requires an exclusive table lock while the rewrite
happens, so you lock out other processes from the table for a
considerably longer period of time than the UPDATE approach.  IIRC it's
also not completely MVCC-safe --- committed-dead rows will get removed
even if there are old open transactions that should still see those rows
as current.  Bottom line: there's no free lunch.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: ambuild parameters
Next
From: Bruno Wolff III
Date:
Subject: Re: How I can get the real data type result instead of integer data type?