pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: grau.fran@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #14518: FTS index not triggered when using function to provide the tsquery
Date: Fri, 27 Jan 2017 10:39:39 +0000
Message-ID: <20170127103939.1431.54363@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      14518
Logged by:          Francisco Grau
Email address:      grau.fran@gmail.com
PostgreSQL version: 9.5.5
Operating system:   Fedora Linux 25
Description:        

I have a table with a tsvector column called 'fts'. Then a GIN index on that
table.

If I run:
SELECT pub.publication_id
FROM publication pub
WHERE
  pub.fts @@ to_tsquery('Omnis:*')

That uses the index:
"Bitmap Heap Scan on publication pub  (cost=180.18..4881.63 rows=4636
width=8)"
"  Recheck Cond: (fts @@ to_tsquery('Omnis:*'::text))"
"  ->  Bitmap Index Scan on idx_publication_fts  (cost=0.00..179.02
rows=4636 width=0)"
"        Index Cond: (fts @@ to_tsquery('Omnis:*'::text))"


But I need to run the query through a function to generate a custom tsquery
sometimes. So if I run:
SELECT pub.publication_id
FROM publication pub
WHERE
  pub.fts @@ x_get_tsquery('Omnis')

"Seq Scan on publication pub  (cost=0.00..5939.89 rows=50 width=8)"
"  Filter: (fts @@ x_get_tsquery('Omnis'::text))"


As you can see it uses a sequential scan... not good.

If I run:
SELECT to_tsquery('Omnis:*') = x_get_tsquery('Omnis')
That returns TRUE

On the other hand, if I use the function inside a CTE it works as
expected:
WITH query AS (SELECT x_get_tsquery('Omnis') AS tsquery)
SELECT pub.publication_id
FROM publication pub, query
WHERE
  pub.fts @@ query.tsquery

"Nested Loop  (cost=8.65..191.36 rows=50 width=8)"
"  CTE query"
"    ->  Result  (cost=0.00..0.26 rows=1 width=0)"
"  ->  CTE Scan on query  (cost=0.00..0.02 rows=1 width=32)"
"  ->  Bitmap Heap Scan on publication pub  (cost=8.39..190.58 rows=50
width=698)"
"        Recheck Cond: (fts @@ query.tsquery)"
"        ->  Bitmap Index Scan on idx_publication_fts  (cost=0.00..8.38
rows=50 width=0)"
"              Index Cond: (fts @@ query.tsquery)"



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


view thread (4+ messages)  latest in thread

Message-ID: <20170127103939.1431.54363@wrigleys.postgresql.org>
Permalink:  ../20170127103939.1431.54363@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170127103939.1431.54363@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: grau.fran@gmail.com
  Subject: Re: BUG #14518: FTS index not triggered when using function to provide the tsquery
  In-Reply-To: <20170127103939.1431.54363@wrigleys.postgresql.org>
  References: <20170127103939.1431.54363@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox