Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XzA0v-0002bN-Gc for pgsql-bugs@arkaria.postgresql.org; Thu, 11 Dec 2014 20:07:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XzA0u-0006am-SA for pgsql-bugs@arkaria.postgresql.org; Thu, 11 Dec 2014 20:07:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XzA0t-0006YU-Ql for pgsql-bugs@postgresql.org; Thu, 11 Dec 2014 20:07:03 +0000 Received: from outmail148099.authsmtp.net ([62.13.148.99]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XzA0p-0001Nv-Nd for pgsql-bugs@postgresql.org; Thu, 11 Dec 2014 20:07:01 +0000 Received: from mail-c235.authsmtp.com (mail-c235.authsmtp.com [62.13.128.235]) by punt18.authsmtp.com (8.14.2/8.14.2/) with ESMTP id sBBK6v1K007348; Thu, 11 Dec 2014 20:06:57 GMT Received: from alvin.alvh.no-ip.org (client-186-37-58-152.imovil.entelpcs.cl [186.37.58.152] (may be forged)) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2/) with ESMTP id sBBK6ndl029321; Thu, 11 Dec 2014 20:06:55 GMT Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id A27E5792; Thu, 11 Dec 2014 17:06:57 -0300 (CLST) Date: Thu, 11 Dec 2014 17:06:57 -0300 From: Alvaro Herrera To: Matt Freeman Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #12202: json operator ->>with offset Message-ID: <20141211200657.GO1768@alvh.no-ip.org> References: <20141211083227.2521.52417@wrigleys.postgresql.org> <20141211173448.GK1768@alvh.no-ip.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Server-Quench: 4269a992-8171-11e4-b396-002590a15da7 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZcSBoa BzkeAQdJTRslYBtL RB4bawxAL0wMSABN M15eIxoJcUtFHBZ1 VjUUWVRVVE1zWWl1 bQBUaktcakxQXAJ0 VUBXQ1dQCwBgBgMB SF1sIGJ0KVEaeHx5 ZkRjEHVYXkQ0cUR7 Sx9dHG8bbWZnaX0e URVcalFXeQVXfh9H b1l/BXIINTQAKBg1 TUcAFQ0eBgAXAhl6 CkpUdwhKGxRDHCU7 QVgFBTg1FFECXW12 eUo5K0IcVEEfPF4/ LTPT X-Authentic-SMTP: 61633235383639.1023:706 X-AuthFastPath: 0 (Was 255) X-AuthSMTP-Origin: 186.37.58.152/587 X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Matt Freeman wrote: > SELECT blob->>'firstName' FROM couchcontacts OFFSET 10000 LIMIT 10 > > Takes upwards of 10 seconds!! Noted inefficiencies of offset aside why is > this presumably causing 10,010 json decode ops? As the projection has no > side-effects I don't understand the reason this can't be fast? With respect to the tuples emitted, an Offset node runs later than whatever is emitting the tuples in the first place. So the expressions in the target list (blob->>'firstName' in this example) are executed for all tuples, not just the ones past the 10000 offset. I guess we could have an optimization to skip evaluating expressions for tuples that are not going to be emitted, if none of them are volatile (bonus points if only the volatile ones are evaluated, I guess). I don't think we have that today; it certainly doesn't sound trivial. This is not a JSON thing: you would probably see the same behavior with expensive expressions involving any other type. As a workaround you could grab the rows you want in a subselect in FROM, and evaluate the expression in the outer query: SELECT blob->>'firstName' FROM (SELECT blob FROM couchcontacts ORDER BY something OFFSET 10000 LIMIT 10) f; The OFFSET in the subquery prevents the optimizer from flattening this into a simple scan. Note that if you don't have an ORDER BY clause, there is no guarantee about which rows are going to be returned; if you have many of these queries running concurrently, two executions in the same session could get completely disparate result sets (i.e. if you first ask for OFFSET 9990 and then OFFSET 10000, you might not get consecutive rows as you probably expect), for example because of synchronized seqscans. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs