Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUxRR-00014o-H0 for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 17:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUxRQ-0006xj-3E for pgsql-general@arkaria.postgresql.org; Thu, 06 Dec 2018 17:28:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUxRP-0006xb-Nz for pgsql-general@lists.postgresql.org; Thu, 06 Dec 2018 17:27:59 +0000 Received: from mx1.mailbox.org ([2001:67c:2050:104:0:1:25:1]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUxRL-0008IW-QI for pgsql-general@postgresql.org; Thu, 06 Dec 2018 17:27:58 +0000 Received: from smtp2.mailbox.org (unknown [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 3793349672; Thu, 6 Dec 2018 18:27:49 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id bX2lfXoGGdmN; Thu, 6 Dec 2018 18:27:47 +0100 (CET) Message-ID: <750facc26efc79b1e44836ea1c3d985a9577f80a.camel@cybertec.at> Subject: Re: How to build a btree index with integer values on jsonb data? From: Laurenz Albe To: Johann Spies , "pgsql-general@postgresql.org" Date: Thu, 06 Dec 2018 18:27:45 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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