Re: How to build a btree index with integer values on jsonb data?

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Johann Spies <johann(dot)spies(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to build a btree index with integer values on jsonb data?
Date: 2018-12-06 17:27:45
Message-ID: 750facc26efc79b1e44836ea1c3d985a9577f80a.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Johann Spies wrote:
> How can I transform the following definition to index pubyear as
> integer and not text?
>
> CREATE INDEX pubyear_idx
> ON some_table_where_data_field_is_of_type_jsonb USING btree
> ((((((data -> 'REC'::text) -> 'static_data'::text) ->
> 'summary'::text) -> 'pub_info'::text) ->> '@pubyear'::text) COLLATE
> pg_catalog."default");
>
> While I can cast the value in a SELECT statement to integer I have
> been able to do the same while creating the index.

Replace

COLLATE pg_catalog."default"

with

::integer

> Why btree index? I want to do queries like
>
> select stuff from sometable where pubyear between 2015 and 2018;

Because b-tree indexes are perfect for >= and <=.

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2018-12-06 17:29:36 Re: n_mod_since_analyze
Previous Message Ravi Krishna 2018-12-06 16:44:53 Re: Limitting full join to one match