INSERT ON CONFLICT handles duplicate key violations gracefully. Use DO UPDATE SET to change existing data or DO NOTHING to skip duplicates. This atomic upsert is safe for concurrent use without race conditions.
UPSERT Pattern
-- Insert or update on conflict
INSERT INTO users (email, name)
VALUES ('alice@example.com', 'Alice')
ON CONFLICT (email)
DO UPDATE SET name = EXCLUDED.name;
-- Insert or ignore
INSERT INTO users (email, name)
VALUES ('alice@example.com', 'Alice')
ON CONFLICT (email)
DO NOTHING;