Re: Updating a large table - Mailing list pgsql-performance

From Sergei Kornilov
Subject Re: Updating a large table
Date
Msg-id 1037971515502424@web26o.yandex.ru
Whole thread Raw
In response to Re: Updating a large table  (Timokhin Maxim <ncx2@yandex.com>)
Responses Re: Updating a large table
List pgsql-performance
Hello

> 1. ALTER TABLE clusters ADD COLUMN "is_paid" BOOLEAN DEFAULT FALSE;
this is wrong. To avoid large table lock you need DEFAULT NULL:
ALTER TABLE clusters ADD COLUMN "is_paid" BOOLEAN DEFAULT NULL;
Default null changes only system catalog, default with any non-null value will rewrite all rows. After adding column
youcan set default value - it applied only for future inserts:
 
ALTER TABLE clusters ALTER COLUMN "is_paid" SET DEFAULT FALSE;

And then you can update all old rows in table by small chunks. Finally, when here is no NULL values you can set not
null:
ALTER TABLE clusters ALTER COLUMN "is_paid" SET NOT NULL;
But unfortunately this locks table for some time - smaller what rewrite time, but time of full seqscan. I hope my patch
[1]will be merged and not null can be set in future by temporary adding check constraint (not valid, then validate) -
whichnot require large table lock
 

[1] https://www.postgresql.org/message-id/flat/81911511895540@web58j.yandex.ru#81911511895540@web58j.yandex.ru

Regards, Sergei


pgsql-performance by date:

Previous
From: Timokhin Maxim
Date:
Subject: Re: Updating a large table
Next
From: Timokhin Maxim
Date:
Subject: Re: Updating a large table