Created
September 18, 2023 14:49
-
-
Save ahobson/451f1a5c34a5b96c8c8e8958550e4bad to your computer and use it in GitHub Desktop.
code golf for batch update in postgresql
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
-- this is a naive way that seems plenty fast | |
DO $$ | |
DECLARE | |
rec record; | |
BEGIN | |
RAISE INFO 'Starting %', now(); | |
FOR rec IN SELECT id | |
FROM addresses | |
WHERE country = 'United States' | |
LOOP | |
UPDATE addresses SET country = 'US' WHERE id = rec.id; | |
END LOOP; | |
RAISE INFO 'Finishing %', now(); | |
END $$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trying to batch with a cursor doesn't seem any faster