Quantcast
Viewing all articles
Browse latest Browse all 207

How to make a Data Modifying CTE abort if an update only partially succeeded?

I am wondering if it is possible to add some kind of check to make a data modifying CTE abort if an update is partially successful. Attached is a minimal query that represents the problem. I would like the query to fail if updated_rows has not updated the 3 rows specified in the WHERE IN clause.

This is easy to achieve if this query is rewritten as an interactive transaction, but I'm wondering if it's possible to achieve this while keeping things as a single data modifying CTE.

with updated_rows as (    update some_table    set status = 'SUCCEEDED'    where id in ('a', 'b', 'c') and status = 'PENDING'    returning *), more_updated_rows as (    update another_table    set total = total + 3    where completed = false     returning *) select * from more_updated_rows

Viewing all articles
Browse latest Browse all 207

Trending Articles