My function fun() returned a long crosstab table generation string (varchar) that I can view with select fun(). How to execute it not inside a function but from a console?

1 Answer 1


Use \gexec in psql to execute what the query returns:

SELECT fun()\gexec

The manual:

Sends the current query buffer to the server, then treats each column of each row of the query's output (if any) as an SQL statement to be executed.

Read the manual for more details.


  • even if fun() returned something like SELECT * from...?
    – Blender
    Oct 6, 2021 at 6:15
  • @Blender: Yes. Follow the link to read details in the manual. Oct 6, 2021 at 14:14
  • Yeah it does not work in datagrip console sadly for me…
    – Blender
    Oct 6, 2021 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.