Created
May 20, 2021 16:15
-
-
Save stvkoch/e3508095ab23a215075f7843fe59c5c7 to your computer and use it in GitHub Desktop.
Don't lock the table while migrate your db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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