patternsqlCritical
Does updating a row with the same value actually update the row?
Viewed 0 times
sametheupdatewithupdatingvalueactuallydoesrow
Problem
I have a performance-related question. Let's say I have a user with first name Michael. Take the following query:
Will the query actually execute the update, even though it is being updated to the same value? If so, how do I prevent it from happening?
UPDATE users
SET first_name = 'Michael'
WHERE users.id = 123Will the query actually execute the update, even though it is being updated to the same value? If so, how do I prevent it from happening?
Solution
Due to the MVCC model of Postgres, and according to the rules of SQL, an
This does have a more or less substantial impact on performance, directly and indirectly. "Empty updates" have largely the same cost per row as any other update. They fire triggers (if present) like any other update, they have to be WAL-logged and they produce dead rows bloating the table and causing more work for
Index entries and TOASTed columns where none of the involved columns are changed can stay the same, but that is true for any updated row. Related:
It's almost always a good idea to exclude such empty updates (when there is an actual chance it may happen). You did not provide a table definition in your question. We have to assume
If
Related:
UPDATE writes a new row version for every row that is not excluded in the WHERE clause.This does have a more or less substantial impact on performance, directly and indirectly. "Empty updates" have largely the same cost per row as any other update. They fire triggers (if present) like any other update, they have to be WAL-logged and they produce dead rows bloating the table and causing more work for
VACUUM later like any other update.Index entries and TOASTed columns where none of the involved columns are changed can stay the same, but that is true for any updated row. Related:
- PostgreSQL Initial Database Size
- Redundant data in update statements
It's almost always a good idea to exclude such empty updates (when there is an actual chance it may happen). You did not provide a table definition in your question. We have to assume
first_name can be null (unsurprising for a "first name"), hence the query has to use null-safe comparison:UPDATE users
SET first_name = 'Michael'
WHERE id = 123
AND first_name IS DISTINCT FROM 'Michael';If
first_name IS NULL before the update, a test with just first_name <> 'Michael' would evaluate to null and as such exclude the row from the update. Sneaky error. If the column is defined NOT NULL, use a simple equality check, though, that's a bit cheaper.Related:
- How do I (or can I) SELECT DISTINCT on multiple columns?
- Update column with data from another table
Code Snippets
UPDATE users
SET first_name = 'Michael'
WHERE id = 123
AND first_name IS DISTINCT FROM 'Michael';Context
StackExchange Database Administrators Q#118178, answer score: 62
Revisions (0)
No revisions yet.