Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 851E8CE4E74 for ; Sun, 27 Nov 2011 18:51:18 -0400 (AST) Received: from mail-iy0-f174.google.com ([209.85.210.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RUnZ7-00089i-Dv for pgsql-performance@postgresql.org; Sun, 27 Nov 2011 22:51:19 +0000 Received: by iage36 with SMTP id e36so7947194iag.19 for ; Sun, 27 Nov 2011 14:51:04 -0800 (PST) Received: by 10.231.61.210 with SMTP id u18mr1673475ibh.86.1322434264287; Sun, 27 Nov 2011 14:51:04 -0800 (PST) Received: from ayaki.localdomain (124-169-45-65.dyn.iinet.net.au. [124.169.45.65]) by mx.google.com with ESMTPS id g16sm37395151ibs.8.2011.11.27.14.51.01 (version=SSLv3 cipher=OTHER); Sun, 27 Nov 2011 14:51:03 -0800 (PST) Message-ID: <4ED2BED3.1070303@ringerc.id.au> Date: Mon, 28 Nov 2011 06:50:59 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1 MIME-Version: 1.0 To: Maxim Boguk CC: Tom Lane , pgsql-performance@postgresql.org Subject: Re: Some question about lazy subquery/procedures execution in SELECT ... ORDER BY... LIMIT N queries References: <24774.1322154302@sss.pgh.pa.us> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201111/270 X-Sequence-Number: 45781 On 11/25/2011 06:53 AM, Maxim Boguk wrote: > I understand that position. > However if assumption: " the definition of ORDER BY --- it happens after > computing the select list, according to the SQL standard" > is correct, > then plans like: > > postgres=# EXPLAIN ANALYZE SELECT * from test order by _data limit 10 > offset 1000; > QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------------- > Limit (cost=2884.19..2913.03 rows=10 width=8) (actual > time=3.584..3.620 rows=10 loops=1) > -> Index Scan using random_key on test (cost=0.00..2884190.16 > rows=1000000 width=8) (actual time=0.103..3.354 rows=1010 loops=1) > Total runtime: 3.663 ms > (3 rows) > should not be used at all. `LIMIT' and `OFFSET' are explicitly defined to compute only that part of the SELECT list that is required. If they weren't specifically defined with that exception then you'd be right. LIMIT and OFFSET aren't standard anyway, so Pg can define them to mean whatever is most appropriate. The SQL standard is adding new and (as usual) painfully clumsily worded features that work like LIMIT and OFFSET, but I don't know whether they have the same rules about whether execution of functions can be skipped or not. > And it is not possible to predict in advance where and when you get hit > by that problem. That's the biggest problem with statistics- and heuristics-based query planners in general, but this does seem to be a particularly difficult case. Setting a cost on the function call that more accurately reflects how expensive it is so PostgreSQL will work harder to avoid calling it might help. See http://www.postgresql.org/docs/current/static/sql-createfunction.html . -- Craig Ringer