First, identify the duplicates using a query such as the following:
CREATE UNLOGGED TABLE dup_cust ASSELECT *FROM custWHERE customerid IN (SELECT customerid FROM cust GROUP BY customerid HAVING count(*) > 1);
We save the list of duplicates in a separate table because the query can be very slow if the table is big, so we don't want to run it more than once.