The script that makes the index, three statements for exec() to run on
each open:
DO block that adds the _tsv column when the content table has none.DO block that creates the GIN index idx_<table>_tsv when the
content table's schema has no relation of that name.UPDATE that fills _tsv on the rows where it is null.ALTER TABLE takes its ACCESS EXCLUSIVE lock, and CREATE INDEX its
SHARE lock, before either reads its IF NOT EXISTS, and a lock request
waits behind every queued request it conflicts with: an open behind a
long read of the table would hold up each later read and write of it
until that read ended. So each block reads the catalog first, through
to_regclass, which finds the content table as the statement does (the
search path, a schema prefix, quoted names) and locks nothing. An open
that finds the column and the index takes no lock on the table beyond the
UPDATE's ROW EXCLUSIVE, which no read or write of the table waits for.
Two first opens can both pass a check. The later ALTER TABLE waits for
the earlier one's lock and then finds the column, which its
IF NOT EXISTS passes. The later CREATE INDEX shares the earlier one's
lock, builds an index of its own, and fails on the catalog's unique name
(unique_violation) once the earlier one commits: its block takes that
failure as the index made.
OptionalcontentOptionaltokenizer?: stringOptionalweights?: Record<string, Weight>A rank value, in each engine's own direction.
SQLite FTS5: memory_traces_fts.rank (lower is better)
SQLite FTS4: length(offsets(memory_traces_fts)) (higher is better)
Postgres: ts_rank(_tsv, websearch_to_tsquery('english', $1)) (higher is better)
Use relevanceOrder to order the same way on every engine.
OptionalqueryPlaceholder: stringAn ORDER BY fragment that puts the best match first on every engine.
SQLite FTS5: memory_traces_fts.rank
SQLite FTS4: length(offsets(memory_traces_fts)) DESC
Postgres: ts_rank(_tsv, websearch_to_tsquery('english', $1)) DESC
OptionalqueryPlaceholder: stringGenerate an INSERT to sync external-content FTS after a row insert.
SQLite: INSERT INTO fts_table (rowid, col1, col2) VALUES (expr, ?, ?)
Postgres: UPDATE content_table SET _tsv = to_tsvector(...) WHERE ...
Generate a SELECT joining the FTS index to the content table. This handles the structural difference between FTS5 (separate virtual table joined via rowid) and Postgres (tsvector column on the content table itself).
The base table (e.g. 'memory_traces').
Alias for the content table (e.g. 't').
FROM/JOIN clause fragment.
Postgres full-text search using tsvector columns and GIN indexes.
Unlike FTS5 (separate virtual table), Postgres stores the tsvector as a column
_tsvon the content table itself. ThecreateIndex()call adds the column and creates the GIN index when they are missing, and backfills existing rows. Queries go throughwebsearch_to_tsquery, which reads raw user text without syntax errors: words are ANDed, andor,-wordand quoted phrases work as in a web search box.