pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Matt Freeman <matt@nonuby.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12202: json operator ->>with offset
Date: Thu, 11 Dec 2014 17:06:57 -0300
Message-ID: <20141211200657.GO1768@alvh.no-ip.org> (raw)
In-Reply-To: <CAHEc+CDpuQKWcr-EiXGdbzGqDZ2zckHKzS8wYpca4huRvqnk0w@mail.gmail.com>
References: <20141211083227.2521.52417@wrigleys.postgresql.org>
	<20141211173448.GK1768@alvh.no-ip.org>
	<CAHEc+CDpuQKWcr-EiXGdbzGqDZ2zckHKzS8wYpca4huRvqnk0w@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (4+ messages)

Message-ID: <20141211200657.GO1768@alvh.no-ip.org>
Permalink:  ../20141211200657.GO1768@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20141211200657.GO1768@alvh.no-ip.org

 · 

reply

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: alvherre@2ndquadrant.com, matt@nonuby.com
  Subject: Re: BUG #12202: json operator ->>with offset
  In-Reply-To: <20141211200657.GO1768@alvh.no-ip.org>
  References: <20141211083227.2521.52417@wrigleys.postgresql.org> <20141211173448.GK1768@alvh.no-ip.org> <CAHEc+CDpuQKWcr-EiXGdbzGqDZ2zckHKzS8wYpca4huRvqnk0w@mail.gmail.com> <20141211200657.GO1768@alvh.no-ip.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