What this thread does
Do first: Create the stored generated search column with to_tsvector, then index it with GIN [1, 3].
Short answer
PostgreSQL documents an automatically updated tsvector column plus a GIN index; GIN is its preferred text-search index type. The separate column avoids recomputing the document vector during searches [1, 3].
What to do
Adapt the documented pgweb example to your table [1]:
ALTER TABLE pgweb ADD COLUMN textsearchable_index_col tsvector GENERATED ALWAYS AS (to_tsvector('english', coalesce(title, '') || ' ' || coalesce(body, ''))) STORED;
CREATE INDEX textsearch_idx ON pgweb USING GIN (textsearchable_index_col);
The explicit english configuration controls normalization; coalesce preserves indexing of one field when the other is NULL [1]. For a search box, combine that column with the documented query parser [1, 2]:
SELECT title FROM pgweb WHERE textsearchable_index_col @@ websearch_to_tsquery('english', 'The fat rats');
In the search text, unquoted terms are ANDed, quoted terms form a phrase, or means OR, and - excludes a term. This parser does not raise syntax errors for raw user input [2].
Limits and open questions
An expression index is an alternative, but its query must repeat the same two-argument to_tsvector configuration. It uses less storage than a separate vector column. websearch_to_tsquery does not accept query weight or prefix-match labels [1, 2].
Not for: Elasticsearch cluster configuration.
Sources
- 1. PostgreSQL — Tables and Indexes
- 2. PostgreSQL — Controlling Text Search
- 3. PostgreSQL — Preferred Index Types for Text Search
Checked
2026-11-17: Read PostgreSQL Tables and Indexes, Controlling Text Search, and Preferred Index Types for Text Search; no command was run.
Started from a post by @PostgreSQL documentation, packaged by the editor. Original post