For using COPY FROM
with protection against duplicates in the target table as well as in the source file (validated the results in my local instance).
This should also work in Redshift but I haven't validated it.
-- Target table
CREATE TABLE target_table
(id integer PRIMARY KEY, firstname varchar(100), lastname varchar(100));
INSERT INTO target_table (id, firstname, lastname) VALUES (14, 'albert', 'einstein');
INSERT INTO target_table (id, firstname, lastname) VALUES (4, 'isaac', 'newton');
-- COPY FROM with protection against duplicates in the target table as well as in the source file
BEGIN;
CREATE TEMP TABLE source_file_table ON COMMIT DROP AS (
SELECT * FROM target_table
)
WITH NO DATA;
-- Simulating COPY FROM
INSERT INTO source_file_table (id, firstname, lastname) VALUES (14, 'albert', 'einstein');
INSERT INTO source_file_table (id, firstname, lastname) VALUES (7, 'marie', 'curie');
INSERT INTO source_file_table (id, firstname, lastname) VALUES (7, 'marie', 'curie');
INSERT INTO source_file_table (id, firstname, lastname) VALUES (7, 'marie', 'curie');
INSERT INTO source_file_table (id, firstname, lastname) VALUES (5, 'Neil deGrasse', 'Tyson');
-- for protection agains duplicate in target_table
UPDATE source_file_table SET id=NULL
FROM target_table WHERE source_file_table.id=target_table.id;
INSERT INTO target_table
SELECT * FROM source_file_table
-- for protection agains duplicate in target_table
WHERE source_file_table.id IS NOT NULL
-- for protection agains duplicate in source file
UNION
(SELECT * FROM source_file_table
WHERE source_file_table.id IS NOT NULL
LIMIT 1);
COMMIT;