question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support tsvector columns for Postgres fulltext search

See original GitHub issue

Problem

See https://github.com/prisma/prisma/issues/8950#issuecomment-1067395682 a search runs 1,000x faster with a tsvector column with an index, when compared to the “TEXT” field with an GIN index approach you currently recommend (which does not use the index due to the bug linked)

Suggested solution

Support tsvector columns

Alternatives

None

Additional context

n/a

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

3reactions
joshribakoff-smcommented, Mar 15, 2022

Here’s the workaround I used for now:

model products {
    indexed_name Unsupported("tsvector")? @default(dbgenerated("''::tsvector")) /// Pre-computed tsvector search index, updated via trigger
}
-- AlterTable
ALTER TABLE "products" ADD COLUMN     "indexed_name" tsvector DEFAULT ''::tsvector;
CREATE INDEX products_indexed_name ON products USING GIN (indexed_name);
const myQuery = `'foo':*`
const query = `SELECT id from products where indexed_name @@ to_tsquery(${myQuery})`; // important, do not select indexed_name!
const res = await prisma.$queryRawUnsafe(query);
// use Prisma to hydrate the IDs
const hydratedMatches = await prisma.products.findMany({
        where: {
          id: {
            in: res.map((row) => row.id),
          },
        },
})

Performance is 🔥 :

josh=# explain analyze SELECT id, name from products where indexed_name @@ to_tsquery('slack:*');
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on products  (cost=16.30..27.27 rows=6 width=64) (actual time=0.050..0.051 rows=0 loops=1)
   Recheck Cond: (indexed_name @@ to_tsquery('slack:*'::text))
   ->  Bitmap Index Scan on products_indexed_name  (cost=0.00..16.30 rows=6 width=0) (actual time=0.048..0.048 rows=0 loops=1)
         Index Cond: (indexed_name @@ to_tsquery('slack:*'::text))
 Planning Time: 1.211 ms
 Execution Time: 0.148 ms
(6 rows)


Compared to what Prisma does, with it’s “preview fulltext search” module. 214x slower.

josh=# explain analyze SELECT id, name from products where name @@ to_tsquery('slack:*');
                                               QUERY PLAN                                               
--------------------------------------------------------------------------------------------------------
 Seq Scan on products  (cost=0.00..363.72 rows=7 width=64) (actual time=21.444..30.549 rows=1 loops=1)
   Filter: (name @@ to_tsquery('slack:*'::text))
   Rows Removed by Filter: 912
 Planning Time: 0.129 ms
 Execution Time: 30.567 ms
(5 rows)

1reaction
AzSiAzcommented, Mar 14, 2022

That’s actually one of my proposed solution, using a tsvector stored generated column from one or more column + index

https://github.com/prisma/prisma/issues/8950#issuecomment-982915628

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 12.2. Tables and Indexes - PostgreSQL
It is possible to do a full text search without an index. ... ALTER TABLE pgweb ADD COLUMN textsearchable_index_col tsvector GENERATED ALWAYS AS ......
Read more >
Full Text Searching with Postgres | Forestry.io
Postgres has a data type called tsvector that is used for full text search. A tsvector value merges different variants of the same...
Read more >
Postgres Full-Text Search: A Search Engine in a Database
A document is a set of data on which you want to carry out your full-text search. In Postgres, this could be built...
Read more >
Mastering PostgreSQL Tools: Full-Text Search and Phrase ...
full-text search refers to techniques for searching a single ... all of this (notice the tsvector data type for the document_tokens column):
Read more >
Probing Text Data Using PostgreSQL Full-Text Search - Arctype
Learn how to leverage the power of full-text search in PostgreSQL to search through ... By now, we know that the tsvector is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found