From: Kouber Saparev <postgres@saparev.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #6483: Rows being evaluated, although being outside the LIMIT / OFFSET boundaries
Date: Thu, 23 Feb 2012 11:23:30 +0200
Message-ID: <ji50ii$1e8$1@dough.gmane.org> (raw)
In-Reply-To: <CABRT9RCzOKPCbnFwoaw_nfVgz17q+GkW=eUMVa01mEBPw_yO5Q@mail.gmail.com>
References: <E1S0HJX-0004pP-9Y@wrigleys.postgresql.org>
<CABRT9RA6BWuTV1PESSQ-x_5+3qUJMgz0NnVZyzbFyjwY1CQgmQ@mail.gmail.com>
<25657.1329946832@sss.pgh.pa.us>
<CABRT9RCzOKPCbnFwoaw_nfVgz17q+GkW=eUMVa01mEBPw_yO5Q@mail.gmail.com>
On 02/23/2012 12:05 AM, Marti Raudsepp wrote:
> On Wed, Feb 22, 2012 at 23:40, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Marti Raudsepp <marti@juffo.org> writes:
>>> According to this model, evaluating SELECT clause fields for *all*
>>> found rows is done in step 5, whereas LIMIT/OFFSET are only applied
>>> later at step 9. So we're already bending the rules here (in general
>>> we don't do such optimizations around volatile functions). The worst
>>> thing is that it's inconsistent -- the LIMIT gets applied when
>>> computing the SELECT list, but OFFSET doesn't.
>>
>> On what grounds do you say that? LIMIT and OFFSET are practically the
>> same thing internally, and are certainly applied in the same way.
>
> The difference is that the SELECT fields for the first OFFSET rows are
> *evaluated*, but aren't simply returned to the client. But beyond
> LIMIT, query evaluation terminates entirely -- the rest of the SELECT
> clause rows aren't evaluated.
>
> AFAICT, the model in the documentation suggests that the SELECT fields
> are evaluated for all matching rows in indeterminate order, before
> ORDER BY is applied and before the result set is sliced by
> OFFSET/LIMIT.
Indeed, that's probably the main issue - it is not behaving
symmetrically, i.e. fetching the first two rows has one effect (and
performance impact), while fetching the last two - completely different.
In my case, I am making something like an "ON SELECT" rule, triggering
some actions once the rows are read (and sent to the client) from a
SELECT statement. The thing is that "read" and "sent to the client"
appear to be two different things in that case. While I will certainly
use a subquery for it, as proposed by Marti (since real cursors are not
an option in my stateless web environment), I do believe that at least
the documentation should be more clear concerning cases like that (if
the behaviour stays that way).
Regards,
--
Kouber Saparev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: postgres@saparev.com
Subject: Re: BUG #6483: Rows being evaluated, although being outside the LIMIT / OFFSET boundaries
In-Reply-To: <ji50ii$1e8$1@dough.gmane.org>
References: <E1S0HJX-0004pP-9Y@wrigleys.postgresql.org> <CABRT9RA6BWuTV1PESSQ-x_5+3qUJMgz0NnVZyzbFyjwY1CQgmQ@mail.gmail.com> <25657.1329946832@sss.pgh.pa.us> <CABRT9RCzOKPCbnFwoaw_nfVgz17q+GkW=eUMVa01mEBPw_yO5Q@mail.gmail.com> <ji50ii$1e8$1@dough.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox