Maxim Boguk <maxim.boguk@xxxxxxxxx> writes: > Is here any reason why Postgresql calculates subqueries/storable procedures > in select list before applying ORDER BY / LIMIT? Well, that's the definition of ORDER BY --- it happens after computing the select list, according to the SQL standard. We try to optimize this in some cases but you can't really complain when we don't. Consider putting the expensive function outside the ORDER BY/LIMIT, ie select ..., expensive_fn() from (select ... order by ... limit ...) ss; regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance