PostgreSQL function definition "syntax error at or near +"
A LANGUAGE sql function must be a complete SQL statement (or more than one).

Yours is just an expression.

Just prepend SELECT, like you would when running it stand-alone.

$BODY$
SELECT (SELECT ...


The reason it complains at the + is that, surprisingly, it's legal to parenthesise a top-level query. This, for example, is a valid query:

regress=&gt; (SELECT 1);
 ?column? 
----------
        1
(1 row)


(I had no idea about that until your question, either, so thanks. I'm not even sure it's intentional...)

Note that you'd have got the same error if you ran your original query as-written, so I assume you'd prepended SELECT to it.

regress=&gt; (SELECT 1) + (SELECT 2);
ERROR:  syntax error at or near "+"
LINE 1: (SELECT 1) + (SELECT 2);