I have a query like
UPDATE my_table
SET my_value =
CASE WHEN random() > 0.5 THEN my_value * 2 ELSE my_value END
RETURNING *;
Now, inside the RETURNING statement I'd like to have a boolean indicating whether my_value has been changed by the current query or not.
Let's assume, I cannot pass the previous value of my_value as a param to the query.
So, is there a way to obtain something like a list of columns which have different values after the UPDATE? Or get the values at the state before UPDATE in RETURNING?
In my example, I could, of course, put the result of random() in a CTE like
WITH random_cte AS (
SELECT random() AS my_random
)
UPDATE my_table
SET my_value =
CASE WHEN my_random > 0.5 THEN my_value * 2 ELSE my_value END
FROM random_cte
RETURNING *, my_random > 0.5 AS value_changed;
But that would bloat the query somewhat up. So I'm wondering if I could do that in a more elegant way?