Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How to avoid unnecessary updates when using on conflict with Postgres?

My use case involves syncing a table with an upstream source on a recurring schedule.

Each row has a unique identifier and other columns, and I want to make sure I'm inserting any new upstream rows, and updating any changed upstream rows. And there could be thousands of rows to sync.

But I'd like to avoid unnecessary updates where the row in the database doesn't differ from what's upstream.

Currently I'm using ON CONFLICT UPDATE like so:

INSERT INTO symbols (id, name, status) 
VALUES 
  (1, 'one', 'online'),
  (2, 'two', 'offline'),
  ...
ON CONFLICT (id) 
UPDATE SET (id, name, status) = (excluded.id, excluded.name, excluded.status)
RETURNING *

But this will write the updates even when nothing is changing. How should I tweak the UPDATE to performantly check and apply to rows that need it?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can add a where clause to only update those rows that are different.

INSERT INTO symbols (id, name, status) 
VALUES 
  (1, 'one', 'online'),
  (2, 'two', 'offline'),
  ...
ON CONFLICT (id) DO
UPDATE SET (id, name, status) = (excluded.id, excluded.name, excluded.status)
WHERE (symbols.id, symbols.name, symbols.status) IS DISTINCT FROM (excluded.id, excluded.name, excluded.status)
RETURNING *

However, this will only return the rows that are actually updated, which may impact how you use the returning clause.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!