Tsearch2 has function to_tsquery defined as:
CREATE FUNCTION to_tsquery(oid, text)
RETURNS tsquery
AS '$libdir/tsearch2'
LANGUAGE 'c' with (isstrict,iscachable);
And let we take 2 essential equivalent queries:
# explain select book.id from to_tsquery('foo') as t, book where book.fts @@ t; QUERY PLAN
-------------------------------------------------------------------- Nested Loop (cost=13.19..6550.69 rows=290
width=4) Join Filter: ("inner".fts @@ "outer".t) -> Function Scan on t (cost=0.00..12.50 rows=1000 width=32) ->
Materialize (cost=13.19..16.09 rows=290 width=36) -> Seq Scan on book (cost=0.00..12.90 rows=290 width=36)
# explain select book.id from book where book.fts @@ to_tsquery('foo'); QUERY PLAN
----------------------------------------------------- Seq Scan on book (cost=0.00..13.62 rows=1 width=4) Filter:
(fts@@ '''foo'''::tsquery)
Why planner suppose that t 'table' will return 1000 rows? Obviosly that function
returns only one value because of itsn't marked as 'returns setof'.
--
Teodor Sigaev E-mail: teodor@sigaev.ru
WWW: http://www.sigaev.ru/