Skip to content

Instantly share code, notes, and snippets.

@stvkoch
Created May 20, 2021 16:15
Show Gist options
  • Save stvkoch/e3508095ab23a215075f7843fe59c5c7 to your computer and use it in GitHub Desktop.
Save stvkoch/e3508095ab23a215075f7843fe59c5c7 to your computer and use it in GitHub Desktop.
Don't lock the table while migrate your db
-- dont force update all table with default value
ALTER TABLE TB_A
ADD COLUMN new_coumn boolean;
-- add constaint for new records
ALTER TABLE public.TB_A
ALTER COLUMN new_coumn SET DEFAULT false;
-- while not return 0 update the table
do $$
counter interger := 1;
begin
while counter > 0 loop
UPDATE TB_A SET new_coumn = false WHERE id IN (SELECT id FROM TB_A WHERE new_coumn is NULL LIMIT 10000);
counter := row_count
end loop;
end$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment