Jack Douglas
<>https://dbfiddle.uk/?rdbms=postgres_12&fiddle=5bf582e78d63db5efe82d03fd6eb4614
I was expecting both `bar` and `baz` to be `20`, what is going on?
Top Answer
Jack Douglas
It [is documented](https://www.postgresql.org/docs/12/queries-with.html#QUERIES-WITH-MODIFYING) that this is not supported for data-modifying statements in `with` clauses:
> Trying to update the same row twice in a single statement is not supported. Only one of the modifications takes place, but it is not easy (and sometimes not possible) to reliably predict which one.
The docs go on to say:
> This also applies to deleting a row that was already updated in the same statement: only the update is performed. Therefore you should generally avoid trying to modify a single row twice in a single statement.
You'll need to rewrite your updates so each row only gets changed in at most one sub-statement, OR in the main statement:
<>https://dbfiddle.uk/?rdbms=postgres_12&fiddle=0a8aadc390bae57ee1a99fe73dd939d9&hide=3