Question: I have a performance-related question. Let's say I have a user with first name Michael. Take the following query:

```
UPDATE users
SET first_name = 'Michael'
WHERE users.id = 123

```

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?

Answer: 
Due to the [MVCC model](https://www.postgresql.org/docs/current/mvcc-intro.html) of Postgres, and according to the rules of SQL, an `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 the same cost per row as any other update. They fire triggers (if present) like any other update, they have to be [WAL-logged](https://www.postgresql.org/docs/current/wal-intro.html) and they produce dead rows bloating the table and causing more work for `VACUUM` later like any other update.

Index entries and [TOASTed](https://www.postgresql.org/docs/current/storage-toast.html#STORAGE-TOAST-ONDISK) 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](https://dba.stackexchange.com/questions/21014/postgresql-initial-database-size/21507#21507)
* [Redundant data in update statements](https://stackoverflow.com/a/7806610/939860)

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 (which wouldn't be surprising for a "first name"), hence the query has to use [**NULL-safe** comparison](https://www.postgresql.org/docs/current/functions-comparison.html):

```
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`](https://www.postgresql.org/docs/current/ddl-constraints.html#AEN2541), use the simple equality check, though, that's a bit cheaper.

Related:

* [How do I (or can I) SELECT DISTINCT on multiple columns?](https://stackoverflow.com/a/12632129/939860)
* [Update column with data from another table](https://dba.stackexchange.com/questions/58371/sql-update-column-with-data-from-another-table/58383#58383)
**From a database point of view**

The answer to your question is YES. The update will take place. The database does not check the previous value, it only sets the new value.

As this happen in memory (and will only be written to the datafiles after a commit is issued) the performance would not be an issue.

**From an ORM perspective**

Normally you will have an Object representing a single row of the database (it can be a lot more complex than that, but let's keep it simple). This object is managed in memory (at the app server level) and only the latest commited version of that object will actually make it to the database at a certain point.

That may explain the different behaviour.

Now, let's not compare a cargo ship with a 3D printer. The fact that you can send 3D printers using cargo ships doesn't mean that there may be any kind of comparison between them.

Enjoy!

I hope this clarified some concepts.