Thursday, February 9, 2017

Function slower than query direct Postgres

For queries on tables with even data distribution, this will generally be no problem and PL/pgSQL functions will perform somewhat faster than raw SQL queries or SQL functions. But if your query can use certain indexes depending on the actual values in the WHERE clause or, more generally, chose a better query plan for the particular values, you may end up with a sub-optimal query plan.
(source: http://stackoverflow.com/questions/9305133/why-does-postgresql-treat-my-query-differently-in-a-function)

RETURN QUERY : cache plan
RETURN QUERY EXECUTE: replan for each call

RETURN QUERY EXECUTE $$
//query body
$$ USING param1, param2

Saturday, February 4, 2017