Re: index on values stored in a json array

From: "Jonathan S(dot) Katz" <jonathan(dot)katz(at)excoventures(dot)com>
To: Raphael Bauduin <rblists(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: index on values stored in a json array
Date: 2013-10-25 14:21:37
Message-ID: 2A2AB310-E5D1-4CB6-9A2E-30D3A5623F95@excoventures.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi Raph,

On Oct 25, 2013, at 5:17 AM, Raphael Bauduin wrote:

> Hi,
>
> I have a table with a column of type json, in which arrays of products are stored in the form:
>
> {products: [ {id : 123 , name ='product1'}, {id: 214, name: 'product 2}], size: 'XL'}
>
> Is is possible to create an index on the row's products' ids?
> I know I have to use GIN index, but I don't know how to collect the product ids out of the array in the create index command.

So with 9.3 this is unfortunately still nontrivial to create an index on an array nested in a JSON object. GIN indexes are not supported on JSON yet. If the ids were not in a nested array, you would be able to create an expression index using "json_extract_path_text" pointing to the ids you want to extract.

However, another approach would be to pull the array out of the JSON data, extract the integer IDs and put them into an integer array, and then put the integer array into a separate column. From there you could create a GIN index on the integer array column.

Best,

Jonathan

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Raphael Bauduin 2013-10-28 07:30:17 Re: index on values stored in a json array
Previous Message Raphael Bauduin 2013-10-25 09:17:42 index on values stored in a json array