Question Details

No question body available.

Tags

sql postgresql sql-update

Answers (2)

March 5, 2026 Score: 2 Rep: 258,614 Quality: Low Completeness: 40%

Yes, there is a performance advantage.

The second statement avoids the update wherever it would not make a difference. Updating a row means writing a new version of the row and modifying all indexes on the row (unless it is a HOT update). All that costs performance. Moreover, each update will eventually produce a “dead” row version that will have to get removed by autovacuum, which puts some additional strain on your system.

March 5, 2026 Score: 1 Rep: 30,607 Quality: Medium Completeness: 100%

It's worth mentioning the built-in suppressredundantupdatestrigger. It's there precisely because this sort of thing does potentially improve performance and the doc explains how:

CREATE TRIGGER trg
BEFORE UPDATE ON person
FOR EACH ROW EXECUTE FUNCTION suppressredundantupdatestrigger();

The suppressredundantupdatestrigger function, when applied as a row-level BEFORE UPDATE trigger, will prevent any update that does not actually change the data in the row from taking place. This overrides the normal behavior which always performs a physical row update regardless of whether or not the data has changed. (This normal behavior makes updates run faster, since no checking is required, and is also useful in certain cases.)

Ideally, you should avoid running updates that don't actually change the data in the record. Redundant updates can cost considerable unnecessary time, especially if there are lots of indexes to alter, and space in dead rows that will eventually have to be vacuumed. However, detecting such situations in client code is not always easy, or even possible, and writing expressions to detect them can be error-prone. An alternative is to use suppressredundantupdatestrigger, which will skip updates that don't change the data. You should use this with care, however. The trigger takes a small but non-trivial time for each record, so if most of the records affected by updates do actually change, use of this trigger will make updates run slower on average.

While suppressing the update right in the source DML is typically cheaper than firing the trigger, it may not always be desired:

  1. There could be triggers that are supposed to intercept and alter the incoming value. In that case the addition of where old :new is hardcoding an assumption that's not valid or may be invalidated in the future.
  2. Even if the incoming :new isn't supposed to change on its own, suppressing the update might stop triggers from doing some other maintenance work. Relying on side-effects like this is a clear anti-pattern, but that doesn't mean it's not happening on production systems.

It's possible to add transparent DML-level update suppression without resorting to triggers, with an RLS but it's a bit ugly as it requires looking up the current row version.