When writing functions in postgres that involve querying the database you might come into the Why is my function slow? issue. However there are other pitfalls when returning (and using) multiple output values or using RETURNS TABLE syntax. Read why and how to avoid them.
TL;DR;
When calling functions in postgres that return multiple output values avoid calling
1
|
|
and write
1
|
|
instead.
An example
Consider the following test table.
1
|
|
Let’s create 10 million test records:
1 2 |
|
Querying this table without an index would be rather slow as a whole table scan is needed.
1 2 3 4 5 6 7 |
|
To avoid this we can easily add an index
1
|
|
And the same query will run a lot faster.
1 2 3 4 5 6 7 |
|
So far so easy.
Now lets assume that for some reason you want to create a function that takes two parameters and basically does the same query.
1 2 3 4 5 6 7 8 |
|
We can now do the same thing using our function.
1 2 3 4 5 6 7 |
|
But the result now takes almost twice as long.
However we would get the exactly same result querying
1 2 3 4 5 6 7 |
|
So what makes the difference here?
Let’s modify the function to return another constant value. Note: As we change the output format of the function we need to drop the function before we can change it.
1 2 3 4 5 6 7 8 9 |
|
All we did is just adding the text foo
to the output table.
1 2 3 4 5 6 7 |
|
And suddenly the query takes even longer. While the other version is still as fast as before.
1 2 3 4 5 6 7 |
|
What effectively happens is that the query internally get’s modified into
1 2 3 |
|
So the function is called three times and thus taking three times longer. The only way to avoid this behavior is to modify the statement in the above mentioned way.