agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Tim Dudgeon <tdudgeon.ml@gmail.com>
To: pgsql-sql@postgresql.org
Cc: Giuseppe Broccolo <giuseppe.broccolo@2ndquadrant.it>
Subject: Re: querying within json
Date: Thu, 30 Oct 2014 16:24:53 +0000
Message-ID: <54526655.2010306@gmail.com> (raw)
In-Reply-To: <CAFzmHiX5oXGe+kgatVNaq5QqB_BrADtZczHo79x+R4Pecn3vCw@mail.gmail.com>
References: <CAP=hSSfiqaxt7NFi8qUEjKrH=te_cFXDfOfZ9PYEhcJRC+oGMg@mail.gmail.com>
	<CAFzmHiX5oXGe+kgatVNaq5QqB_BrADtZczHo79x+R4Pecn3vCw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

On 30/10/2014 15:22, Giuseppe Broccolo wrote:
> Hi Tim,
>
> 2014-10-30 15:55 GMT+01:00 Tim Dudgeon <tdudgeon.ml@gmail.com 
> <mailto:tdudgeon.ml@gmail.com>>:
>
>     Any advice on how to best query for values within json (using
>     9.4). I have numeric fields within the json and want to include
>     terms for those fields.
>
>     I've found that something like this works:
>
>     select * from atable where (json_col->>'numeric_prop')::numeric < 100;
>
>     But whilst that works:
>     1. seems to have unnecessary casts? The numeric _prop item is of
>     numeric type, but its getting retrieved as text and then cast to
>     numeric and then compared
>
>
> The right operand type of the ->> oeprator is text when ->> is used to 
> get a json object field. So the cast to numeric is needed.

Needed to work, yes. But if my reading of the docs is right then numeric 
types within json as supposed to be treated as Postgres numeric type?
See table 8.23 here: 
http://www.postgresql.org/docs/9.4/static/datatype-json.html
So this would mean a cast from numeric to text and then back to numeric?
There is no way to ask for a json 'field' in its actual data type so 
avoiding the cast?

>     2. its not going to use any index on the json_col jsonb column.
>
>
> The usage of an index is mostly ruled by the 'selectivity' of the 
> query. Anyway, if querying for particular items within the key is 
> common (as 'numeric_prop' in your example), defining an index like 
> this may be worthwhile:
>
> CREATE INDEX idxgin_numeric_prop ON atable USING 
> gin((json_col->'numeric_prop'));

I can add the index, but no evidence of it being used when I run a query 
like this:
select * from atable where (json_col->>'numeric_prop')::numeric < 100;

Tim

>
> Regards,
> -- 
> Giuseppe Broccolo - 2ndQuadrant Italy
> PostgreSQL Training, Services and Support
> giuseppe.broccolo@2ndQuadrant.it 
> <mailto:giuseppe.broccolo@2ndQuadrant.it> | www.2ndQuadrant.it 
> <http://www.2ndQuadrant.it;

view thread (5+ messages)  latest in thread

Message-ID: <54526655.2010306@gmail.com>
Permalink:  ../54526655.2010306@gmail.com/
Also on:    postgresql.org/message-id/54526655.2010306@gmail.com

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tdudgeon.ml@gmail.com, giuseppe.broccolo@2ndquadrant.it
  Subject: Re: querying within json
  In-Reply-To: <54526655.2010306@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox