In Postgres, what is the best manner of aggregating the values of a “returning” clause

aggregateinsertpostgresqlupdate

I would like to query an aggregate of values that get updated. For a simple and contrived example, if this were legal syntax I would do :

update mytable set mynumber = mynumber + 1 returning sum(mynumber);

An aggregate function is not allowed in the returning clause. If this is possible, what is the best way to achieve this?

Best Answer

Use a CTE:

WITH numbers AS (
   update mytable set mynumber = mynumber + 1 returning mynumber
)
SELECT sum(mynumber) FROM numbers;