Skip to content

Instantly share code, notes, and snippets.

@varunchitale
Last active March 25, 2019 07:12
Show Gist options
  • Save varunchitale/dd6f1e63f5c3789969061e576e10c24d to your computer and use it in GitHub Desktop.
Save varunchitale/dd6f1e63f5c3789969061e576e10c24d to your computer and use it in GitHub Desktop.
Update a table and skip the conflicting rows/take some action.
DO $$
DECLARE r RECORD;
BEGIN
FOR r IN <query that will return a record set for r to iterate over>
LOOP
BEGIN
RAISE NOTICE 'Dealing with ID: %',r.id;
UPDATE <table> as t1
SET var1 = t2.var1
from <table2> t2
WHERE <constraints>;
--Exception handling
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE 'Skipping conflict at: %', r.id;
--Do something else
END;
END LOOP;
END$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment