Skip to content

Instantly share code, notes, and snippets.

@montanalow
Created January 15, 2021 21:38
Show Gist options
  • Save montanalow/99dc3b5d735e6048bdb4a276738cf98a to your computer and use it in GitHub Desktop.
Save montanalow/99dc3b5d735e6048bdb4a276738cf98a to your computer and use it in GitHub Desktop.
INSERT INTO …
SELECT * FROM temp_staging_table -- first prepare the data by preloading into a temp table
ON CONFLICT … DO UPDATE SET … -- upsert on the primary key
WHERE updated_at <= excluded.updated_at -- ensure idempotent jobs
AND ( -- only write new distinct data
column_1 IS DISTINCT FROM excluded.column_1 OR
column_2 IS DISTINCT FROM excluded.column_2 OR
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment