Delete duplicate records from postgres table
suggest changeDELETE FROM <SCHEMA_NAME>.<TABLE_NAME>
WHERE ctid NOT IN (
SELECT MAX(ctid)
FROM <SCHEMA_NAME>.<TABLE_NAME>
GROUP BY <column_list>
);
ctid identifies the physical row for this statement only. GROUP BY needs the columns that define a duplicate; table.* is not valid there. One row in each group is kept. Add a WHERE clause inside the subquery if only some duplicates should be removed.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents