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 1iZZ9x-0002uh-HO for pgsql-general@arkaria.postgresql.org; Tue, 26 Nov 2019 11:37:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iZZ9w-0007aH-9a for pgsql-general@arkaria.postgresql.org; Tue, 26 Nov 2019 11:37:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZZ9v-0007aA-TD for pgsql-general@lists.postgresql.org; Tue, 26 Nov 2019 11:37:32 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZZ9s-000698-Rw for pgsql-general@lists.postgresql.org; Tue, 26 Nov 2019 11:37:31 +0000 Received: by mail-wm1-x341.google.com with SMTP id z19so2912972wmk.3 for ; Tue, 26 Nov 2019 03:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:mime-version:date:from:subject:thread-topic:to :content-transfer-encoding; bh=Xo5RFiOKi0tzHvt3jlYhavblC35J8f73i5OfZOqHuDY=; b=o5phqBkA7zGddbXlcFQjJJXk3Zpe3fxSw+VaOAkYUEbhpBCqRCxynqW9t3RwJGBCui 0DM0HvsX2k2VhPRB4tRLdfOxrF2olfv40Gq7Gv5YIWtEhG+x3wSbS3knvvLvKy3Hw+uR ROKcS2P+I+YRaVl8h7vW34OV0CQgaudmkN4vD/GPxSBvR9NFVvSjW/7jzYpvMm6SrLPq DAMR8TiqjBdW11qRxCnzdGC/S80R9xdGn1hJOD22KBxsVTdUB3OSVlaVJXjc1ltX+Qke lKjrZXOC2WKe1Ct6+QEL1drW+76eG66hynaTeqBPDhw6jc5eAR3VFXp8akivaVl51oLL mIcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:mime-version:date:from:subject :thread-topic:to:content-transfer-encoding; bh=Xo5RFiOKi0tzHvt3jlYhavblC35J8f73i5OfZOqHuDY=; b=KBjSrCW4qQcfUbdMmP5uXFMwEXRLu9UNlu5XjN2LLuE73O7obGRgs9ir7hMRX3RVbX NIlVXrFL0p7i6koRUcxOVG5Nu+7ehVb6ysIJORAj1KDtmlTqryz8fZ45z+qZMv93TiP/ jtRBfQBBQNSKcjXbPelswDNDhVXLJDs+qnzoFkBL+/5I3+6oR1/VWKnNezS/03GBzOMp mgV1AF2cNI+ZYyTc3t2dJy5bYWeextkgRdyQmYdIw6AxR5YpUzVc1dAMNgZJ4cAqlVel n825zQ6Y+qdlSTQCounOetQetOa6fyTTGsVFYHKywtanqdOSm0GVHfdDCu1BLugorchg Kdaw== X-Gm-Message-State: APjAAAV8caWfkdFP5D6JLEIehDmMVOV8SpihcT6hZjlaPXxznr0TCmoI WoyaeEIpUgUURKStiEQAZL/KTmATtvY= X-Google-Smtp-Source: APXvYqyIKdAfFmrcIMRL8TP04S0fLAofCR5nUhcmf3ZCQrJkNWPjqyc+ZapvI5vnIsjHc7xpwl1qPQ== X-Received: by 2002:a1c:2846:: with SMTP id o67mr3912507wmo.7.1574768246922; Tue, 26 Nov 2019 03:37:26 -0800 (PST) Received: from smtp.gmail.com ([193.105.219.210]) by smtp.gmail.com with ESMTPSA id p1sm2716462wmc.38.2019.11.26.03.37.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Nov 2019 03:37:26 -0800 (PST) Message-ID: <5ddd0e76.1c69fb81.45d66.cc21@mx.google.com> MIME-Version: 1.0 Date: Tue, 26 Nov 2019 13:37:24 +0200 From: Dmytro Zhluktenko Subject: Postgres Full Text Search Jsonb Array column does not search for first row Thread-Topic: Postgres Full Text Search Jsonb Array column does not search for first row To: "pgsql-general@lists.postgresql.org" Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk

Hey,

Pgsql is unable to perform indexed full text search= onto jsonb column containing an array when looking for the first row in th= e table.

Any ideas why this is happening?

Tab= le is nothing more than just Id and Foo which is jsonb<= span style=3D'font-size:11.5pt;color:#242729'> column.

The case is that I have an empty datab= ase with predefined gin index repro_fts_idx on make_tsvector<= /code> function. <= /span> creates tsvector from given jsonb column.

When I add a new item into the table, I expect it t= o appear in make_tsvector function in a form of tsvector. It's there. Also, I expe= ct that if I run full text search query onto it, it would appear in search = results. However, this is not the case because it returns empty specificall= y for the first row. It simply does not take it into account. If I add one = more row which is completely the same, the system is able to find it with t= he same query.

here is a small repro case:<= /span>

 
-- drop table cp."Repro" cascade<=
/span><=
o:p>
<=
o:p> 
CREATE TABLE cp."Repro" (
=C2=A0=C2=A0 =C2=A0"Id" s= erial NOT NULL,
=C2=A0=C2=A0 =C2=A0"Foo" jsonb NULL=
=
);<=
/span>
 <=
/o:p>
CREATE=
=
 OR REPLACE FUNCTION cp.make_tsvector(in_t cp."Repro")=
 RETURNS ts=
vector
 LANGUAGE plpgsql
 IMMUTABLE
AS $function$ begin=C2=A0=C2=A0=C2=A0 <=
o:p>
=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return=
=
 to_tsvector(jsonb_agg(x.prop))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 =C2=A0from (SELECT CONCAT( jsonb_array_elements=
(in_t.<=
/span>&=
quot;Foo") ->> 'Name', ' ', jsonb_array_elements(<=
span class=3Dpln>in_t."Foo&=
quot;) ->> 'Address' ) as prop from<=
span style=3D'font-family:"inherit",serif;color:#303336;border:none windowt=
ext 1.0pt;padding:0cm;background:#EFF0F1'> cp."Repro" f) <=
/span>as x;
=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=
END;
=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0$function$
;
&nbs=
p;
 
CREATE INDEX repro_fts_idx ON cp."Repro" USING=
 gin (<=
/span>cp.make_=
tsvector(cp."Repro".*)) WITH (fastupdate=3Doff, gin_pending_list_=
limit=3D'64');
 
 
INSERT INTO cp."Repro"
("Foo"<=
/span>)=
=
=
VALUES('[{"Name": "Sup", "Address": "Adr=
ess", "IsCurrent": true}]');=
<= pre style=3D'background:#EFF0F1;vertical-align:baseline'>=  <= /pre>
-- just in case it's the =
indexing issue
-- REINDEX INDEX cp.repro_fts_idx;=
 <=
/span>
select * from cp.&q=
uot;Repro"
 
select cp.make_tsvector(x) from cp.<=
span class=3Dstr>"Repro&quo=
t; 
select * from ts_stat('select cp.make_tsvector(x) from cp."=
Repro" x')
 
-- explain analyze
SELECT *
 FROM "cp"."=
;Repro" x where cp.make_tsvector(x) @@=
 'sup:*'::tsquery
<=
span class=3Dpln> 
&nbs=
p;
 IN=
SERT INTO cp."Repro"
("Foo")
VALUES('[{"Name&quo=
t;: "Sup", "Address": "Adress", "IsCurre=
nt": true}]');
 
 
-- explain analyze=
SELECT *
 FROM "cp&qu=
ot;."Repro" x where cp.make_tsvector(x) @@ 'sup:*'::tsquery

 

 

BR, Dmytro= .

 

=