Calling functions with exec instead of select

Diego picture Diego · Aug 19, 2013 · Viewed 76.3k times · Source

Is the default way of calling a function select * from my_function()?

I ask because I have built a function that doesn't return anything, just inserts data into a table and (coming from a SQL Server background) it "feels" strange to call it with select * from...

I was expecting something like exec my_function()

Answer

Roman Pekar picture Roman Pekar · Aug 19, 2013

use PERFORM statement - http://www.postgresql.org/docs/current/static/plpgsql-statements.html

Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement

so it's just

DO $$ BEGIN
    PERFORM my_function();
END $$;