Skip to content

Instantly share code, notes, and snippets.

@beanieboi
Last active March 11, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beanieboi/837e2a3a7ab5afd3835332cad5f3fd85 to your computer and use it in GitHub Desktop.
Save beanieboi/837e2a3a7ab5afd3835332cad5f3fd85 to your computer and use it in GitHub Desktop.
My Postgres Cheatsheet
DO LANGUAGE plpgsql $$
DECLARE counter INTEGER = 1;
DECLARE tick INTEGER = 1;
BEGIN
WHILE counter > 0 LOOP
UPDATE table
SET field = NULL
WHERE id IN (
SELECT id
FROM table
WHERE
field IS NOT NULL
LIMIT 100000
);
GET DIAGNOSTICS counter = ROW_COUNT;
raise notice 'Updated: %', counter;
raise notice 'Overall: %', tick*counter;
tick = tick + 1;
COMMIT;
END LOOP;
END$$;
ALTER TABLE table ALTER COLUMN id type uuid using id::uuid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment