I have implemented a FTS engine in my website using GIN tsvector and it works quite well, but there are a few times when it seems to take a very long time, for no specific reason. I am copying the output of the EXPLAIN ANALYE command below:
sitedb=# EXPLAIN ANALYZE SELECT id, title FROM post_1 WHERE search_vector @@ to_tsquery('quantum');
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on post_1 (cost=315.68..105654.80 rows=32443 width=106) (actual time=76.963..17281.184 rows=31925 loops=1)
Recheck Cond: (search_vector @@ to_tsquery('quantum'::text))
Heap Blocks: exact=29259
-> Bitmap Index Scan on index1_idx (cost=0.00..307.57 rows=32443 width=0) (actual time=60.208..60.209 rows=31925 loops=1)
Index Cond: (search_vector @@ to_tsquery('quantum'::text))
Planning Time: 47.648 ms
Execution Time: 17308.511 ms
(7 rows)
I thought at some point that changing work_mem would help. I have set it up to 86MB and it is still the same.
The weird thing is that if I re-run the same command right after, it is much faster. See below:
sitedb=# EXPLAIN ANALYZE SELECT id, title FROM post_1 WHERE search_vector @@ to_tsquery('quantum');
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on post_1 (cost=315.68..105654.80 rows=32443 width=106) (actual time=44.542..495.525 rows=31925 loops=1)
Recheck Cond: (search_vector @@ to_tsquery('quantum'::text))
Heap Blocks: exact=29259
-> Bitmap Index Scan on index1_idx (cost=0.00..307.57 rows=32443 width=0) (actual time=29.256..29.256 rows=31925 loops=1)
Index Cond: (search_vector @@ to_tsquery('quantum'::text))
Planning Time: 0.597 ms
Execution Time: 502.296 ms
(7 rows)
Would anyone have an idea?
Thank you very much.
postgresprocess and spent a lot of time swapping it back in. Or maybe your drive went to power-save sleep. If your server is on a VM running on shared hardware, then there are lots of things that could cause such a "wake up" delay.