Sub topic 11
Storing the `tsvector` in a generated column
This chapter assumes you know basic SELECT, WHERE, and ORDER BY queries, and that you can create PostgreSQL tables and indexes. A generated column is a column whose value PostgreSQL calculates from other columns. A stored generated column saves that calculated value with each row.
Why store the search document?
You can call to_tsvector inside every search query, but PostgreSQL would need to process every document before it could find matches. A stored tsvector keeps the normalized search representation ready for matching.
The following table gives titles weight A and body text weight B:
CREATE TABLE articles (
id bigserial PRIMARY KEY,
title text NOT NULL,
body text NOT NULL,
search tsvector GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'B')
) STORED
);
CREATE INDEX articles_search_idx
ON articles USING gin (search);GENERATED ALWAYS AS prevents application code from assigning a value to search. PostgreSQL recalculates it when a row is inserted or when title or body changes. STORED means the result is physically saved rather than calculated when it is read.
setweight marks lexemes from different fields. Ranking functions can then prefer a title match over a body-only match.
Querying the indexed column
Parse the user's input into a tsquery, then use @@ against the generated column:
SELECT a.id,
a.title,
ts_rank(a.search, q.query) AS rank
FROM articles AS a
CROSS JOIN (
SELECT websearch_to_tsquery('english', 'database indexing') AS query
) AS q
WHERE a.search @@ q.query
ORDER BY rank DESC
LIMIT 10;The WHERE condition is important. A GIN index can support the search @@ query test, allowing PostgreSQL to avoid scanning every article. Run EXPLAIN to check whether the planner chooses that index for your data size and query.
> callout note > A generated column follows the expression, not later changes to the text-search configuration. If you change dictionaries, stop words, or stemming rules, existing stored vectors do not automatically change. Plan a rebuild of the generated column or table.
Generated columns compared with triggers
| Approach | Maintenance | Typical reason to choose it |
|---|---|---|
| Generated column | PostgreSQL maintains it from the expression | Simple, deterministic search fields |
| Trigger-maintained column | A trigger controls the update | Complex logic or older PostgreSQL versions |
Generated expressions must use functions PostgreSQL allows in generated columns. Keep the expression deterministic and based on the row's own values. If you need content from another table, calculate that content before storing it or use a different design.
Exercise
Why does the example use a GIN index on the generated tsvector instead of indexing title directly?
Show answer
The generated column stores normalized lexemes, positions, and weights in the form used by the @@ operator, so the GIN index can accelerate full-text matching.