I'm trying to turn a query idempotent by marking rows as updated. However, part of the query spec is to return the IDs of rows that matched the filter. I was thinking of doing something like the following:
WITH
prev as (
SELECT id
FROM books
WHERE id = any($1::uuid[])
AND updated
),
updated as (
UPDATE books
SET author = $2 || author, updated = true
WHERE id = any($1::uuid[])
AND not updated
RETURNING id
)
SELECT id FROM prev
UNION ALL
SELECT id FROM updated
I'm hoping to avoid de de-dupe step from using UNION
instead of UNION ALL
so was wondering if the semantics of the operator guarantee that the 1st query does not see the results of the 2nd.
Related Qs:
Using CTEs for update + select:
Execution order for functions with side-effects: Does postgres union guarantee order of execution when invoking functions with side effects?
Manual de-duping: Update a table from a union select statement