Re: expensive function in select list vs limit clause

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris Mair <chris(at)1006(dot)org>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: expensive function in select list vs limit clause
Date: 2017-04-05 14:29:36
Message-ID: 7205.1491402576@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Chris Mair <chris(at)1006(dot)org> writes:
> From the timings it appears that in the second explain analyze query a function
> call in the select list (expensive()) is evaluated in the sequential scan node
> *for each* row in big, despite the use of limit.

According to the SQL standard, the SELECT list is evaluated before ORDER
BY, so if you need an explicit sort step the function is going to get
calculated first. This is obviously necessary if the function is used
as the sort key, but otherwise it's possible to be smarter. We were not
smarter before 9.6 though. You might find this commit message informative:

https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=9118d03a8

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2017-04-05 14:39:13 Re: browser interface to forums please?
Previous Message vinny 2017-04-05 14:14:04 Re: browser interface to forums please?