Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 251AA1EB83E6 for ; Thu, 23 Feb 2012 05:23:58 -0400 (AST) Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0Uu4-0001X2-6D for pgsql-bugs@postgresql.org; Thu, 23 Feb 2012 09:23:58 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1S0Utn-0001bT-GU for pgsql-bugs@postgresql.org; Thu, 23 Feb 2012 10:23:39 +0100 Received: from 89.190.196.121 ([89.190.196.121]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 23 Feb 2012 10:23:39 +0100 Received: from postgres by 89.190.196.121 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 23 Feb 2012 10:23:39 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-bugs@postgresql.org From: Kouber Saparev Subject: Re: BUG #6483: Rows being evaluated, although being outside the LIMIT / OFFSET boundaries Date: Thu, 23 Feb 2012 11:23:30 +0200 Lines: 39 Message-ID: References: <25657.1329946832@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 89.190.196.121 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: X-Pg-Spam-Score: -0.7 (/) X-Archive-Number: 201202/178 X-Sequence-Number: 32521 On 02/23/2012 12:05 AM, Marti Raudsepp wrote: > On Wed, Feb 22, 2012 at 23:40, Tom Lane wrote: >> Marti Raudsepp 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