Using a set-returning function
A set-returning function (also known as a table function) can be used in most places a table, view, or subquery can be used. They are a powerful and flexible way to return your data.
You can call the function in the SELECT clause like you do with a scalar function:
postgres=# SELECT fibonacci_seq(3);
fibonacci_seq
---------------
0
1
1
(3 rows)You can also call the function as part of the FROM clause:
postgres=# SELECT * FROM fibonacci_seq(3);
fibonacci_seq
---------------
0
1
1
(3 rows)You can even call it in the WHERE clause:
postgres=# SELECT * FROM fibonacci_seq(3) WHERE 1 = ANY(SELECT fibonacci_seq(3));
fibonacci_seq
---------------
0
1
1
(3 rows)Using database side functions for all data access is a great way to secure your application, help with performance, and allow for easy maintenance. Table functions allow you to use functions in...