Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVdXc-0002DG-9r for pgsql-admin@arkaria.postgresql.org; Wed, 20 Jun 2018 13:52:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fVdXa-0000T0-P8 for pgsql-admin@arkaria.postgresql.org; Wed, 20 Jun 2018 13:52:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVdXa-0000Sr-9k for pgsql-admin@lists.postgresql.org; Wed, 20 Jun 2018 13:52:54 +0000 Received: from mail-wr0-x231.google.com ([2a00:1450:400c:c0c::231]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fVdXW-0001Ic-Fw for pgsql-admin@lists.postgresql.org; Wed, 20 Jun 2018 13:52:52 +0000 Received: by mail-wr0-x231.google.com with SMTP id d2-v6so3385526wrm.10 for ; Wed, 20 Jun 2018 06:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=open-lab-org.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:mime-version; bh=Bi1prUv2ZPi3bne9AoElbDQKkLZpOVuEZOHq2dTDo0g=; b=YcDNkxfWJEPLNB7Y3J+TJpnSNSXaxKiAV4CMgYv7IxHuptgOENaspwYUooMZqrNgfS lovSf3/Tw0VYTlEwxJ0FIehAZAyEFIvjswHlxfTg5hr+C7VRl92909LaPn1TwlVTtd17 daYy3wdVasgpgyMeyzGifVYB+SUAa9zVFBNiUZx4YE3bwkar1NUg7gM8L5qthWHjuH+N OpCRJ5cdazhEZ8itvigeGj+nof56aLyjBsGe77JugQ6XLC3nVS1CNTWXMmQFM+5dfqI7 iIijk7J5U93K8mVP2D89lJkrtoBMvH5nkG40dBlyvAnEF95kWzENWIltVUDr/xFeijSB Jy6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=Bi1prUv2ZPi3bne9AoElbDQKkLZpOVuEZOHq2dTDo0g=; b=FeTRkwvQwlx3SAsuOC+B9JbLJhqzPQGqlE/MQA1XHL8yoaZU7bA2qeJtDKpKjh2UHn y/Q6/fgacyQo2dilzYsWBNdQGu3yPVVzdeL6pTna5n8LMJFBqHcxVOjwZjf6i/pVRYCC OmAUrG5HnRUcChqNIMagP5UGOErsd6Pyhm2/ukEjhrya+Oay0Y0M3PaWd1JNVIgK38NQ e6UKThjcIuhlytfGlYvisXKFbd+3ULbIu+68fJ7A3gy/DGsAhtRbgH+mXdnel6Y/Aq6X vXqL3O4MGsNUvgWLzxbb+xjJ04bLgh7yMa+utWKJKInYAJcd6k5P3OAodtYkDmYI7Fy7 x8qQ== X-Gm-Message-State: APt69E2wqbWSOoM1oJswy1ZmIK7Ok2iUv+ugXpUNf/dbcBxFTkBDa2ln 4jmhLTqooTmZDRDe1u9RQ4zCffy8IpA= X-Google-Smtp-Source: ADUXVKIzq+HWPhrnALLazPahC9dg0VIMOO2kDfHGG7FQsuV7Emb1T5q418yoTWl0xc9hyqhh9U2oFQ== X-Received: by 2002:adf:c892:: with SMTP id k18-v6mr2422403wrh.6.1529502768225; Wed, 20 Jun 2018 06:52:48 -0700 (PDT) Received: from [192.168.28.110] (p4FC1212B.dip0.t-ipconnect.de. [79.193.33.43]) by smtp.gmail.com with ESMTPSA id o16-v6sm3254475wri.67.2018.06.20.06.52.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Jun 2018 06:52:46 -0700 (PDT) From: "Enrico Thierbach" To: pgsql-admin@lists.postgresql.org Subject: Trouble matching a nested value in JSONB entries Date: Wed, 20 Jun 2018 15:52:46 +0200 X-Mailer: MailMate (1.11.2r5479) Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_MailMate_733CC38C-19FB-4590-906A-615424007571_=" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=_MailMate_733CC38C-19FB-4590-906A-615424007571_= Content-Type: text/plain; charset=utf-8; format=flowed; markup=markdown Content-Transfer-Encoding: quoted-printable Hi list, I have some trouble matching a value in a JSONB object against multiple = potential matches. Lets say, I have a table with an id, and a metadata JSONB column, which = holds data like the following 1 | {"group_id": 1} 2 | {=E2=80=9Cgroup_id": 1} 3 | {=E2=80=9Cgroup_id": 2} 4 | {=E2=80=9Cgroup_id": 3} I would like to run a query which gives me the result of `SELECT id FROM = mytable WHERE metadata->>=E2=80=99group_id=E2=80=99 IN (1,2)`. Now, obvio= usly I = could use this query, but I would like to get away without an explicit = index on `metadata->>=E2=80=99group_id=E2=80=99`, and I was hoping to fin= d something = using the JSONB containment operators, with support of a gist or gin = index. The following seems to work select * from mytable where (metadata @> '{"group_id":1}') but only with a single value to match. I could, of course, also =E2=80=9Cdenormalize=E2=80=9D the query a la select * from mytable where (metadata @> '{"group_id":1}') OR = (metadata @> '{"group_id=E2=80=9D:2}=E2=80=99) but this seems to call for long execution times; also, depending on the = number of different tag names and values to match this could really = explode into quite a large query. Stackoverflow suggests the use of ANY select * from mytable where (tags->'group_id' @> ANY( ARRAY = ['1','3']::jsonb[] ) ); https://dba.stackexchange.com/questions/130699/postgresql-json-query-arra= y-against-multiple-values This seems to work - but doesn=E2=80=99t that require a group_id specific= = index again? Anything I overlooked? Best, /eno PS: Please note that I am currently at postgres 9.5. An update, if = necessary, would be possible though. -- me at github: https://github.com/radiospiel me at linked.in: https://www.linkedin.com/in/radiospiel --=_MailMate_733CC38C-19FB-4590-906A-615424007571_= Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

Hi list,

I have some trouble matching a value in a JSONB object ag= ainst multiple potential matches.

Lets say, I have a table with an id, and a metadata JSONB= column, which holds data like the following

1 | {"group_id": =
1}
2 | {=E2=80=9Cgroup_id": 1}
3 | {=E2=80=9Cgroup_id": 2}
4 | {=E2=80=9Cgroup_id": 3}

I would like to run a query which gives me the result of = SELECT id FROM mytable WHERE metadata-&= gt;>=E2=80=99group_id=E2=80=99 IN (1,2). Now, obviously I could= use this query, but I would like to get away without an explicit index o= n metadata->>=E2=80=99group_id=E2= =80=99, and I was hoping to find something using the JSONB contain= ment operators, with support of a gist or gin index.

The following seems to work

select * from myt=
able where (metadata @> '{"group_id":1}')

but only with a single value to match.

I could, of course, also =E2=80=9Cdenormalize=E2=80=9D th= e query a la

select * from myt=
able where (metadata @> '{"group_id":1}') OR (metadata @> '{"group_=
id=E2=80=9D:2}=E2=80=99)

but this seems to call for long execution times; also, de= pending on the number of different tag names and values to match this cou= ld really explode into quite a large query.

Stackoverflow suggests the use of ANY

select * from myt=
able where (tags->'group_id' @> ANY( ARRAY ['1','3']::jsonb[] ) );

https://dba.stackexchange.com/questions/130699/postgresql-json-query= -array-against-multiple-values

This seems to work - but doesn=E2=80=99t that require a g= roup_id specific index again?

Anything I overlooked?

Best,
/eno

PS: Please note that I am currently at postgres 9.5. An u= pdate, if necessary, would be possible though.

--
me at github: https://github.com/radiospiel
me at linked.in: https://www.linkedin.com/in/radiospiel

--=_MailMate_733CC38C-19FB-4590-906A-615424007571_=--