My function will return -1 if the user exist or will run the query and return the Id of the new created user. The app_users table has a column named entity_id which is a sequence number. Here is my sql which will probably be in a function :

In short: RETURNING is part of INSERT statement, so it has to be in the dynamic string too. On the opposite, EXECUTE IMMEDIATE knows only RETURNING INTO <variable> clause (nothing between these two words). So you shall use something like