Skip to content

Instantly share code, notes, and snippets.

@varunchitale
Created March 25, 2019 06:56
Show Gist options
  • Save varunchitale/39f137da733f418c5d9939e29c28242c to your computer and use it in GitHub Desktop.
Save varunchitale/39f137da733f418c5d9939e29c28242c to your computer and use it in GitHub Desktop.
Efficiently delete duplicates rows from a table with a set of specific constraints.
DELETE FROM <table> a USING (
SELECT MIN(ctid) as ctid, var1, var2
FROM <same_table> b
GROUP BY 2,3 HAVING COUNT(*) > 1
) b
WHERE a.var1 = b.var1
and a.var2 = b.var2
AND a.ctid <> b.ctid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment