
A reader types backups into your help center. Two articles mention backup once. One is titled “Backup.” The other mentions it in the body of a retention policy. The reader should see the first article first. A search for KB-1042, on the other hand, should open that exact record. PostgreSQL can handle both requests in the database that already holds your articles.
We built a support catalog on PostgreSQL 18.6 to test those two search paths. The example below is runnable in a fresh psql session. Its five editorial records let you check relevance by eye. Another 20,000 synthetic records give the planner a reason to choose an index for a rare word.
A tsvector records searchable word forms and their positions. With the English configuration, “backups” and “backup” produce the same search term. Give words in the title weight A and words in the body weight D. When a document has no body, coalesce supplies an empty string so the title remains searchable.
CREATE TEMP TABLE support_doc (
ref text PRIMARY KEY,
title text NOT NULL,
body text,
search_doc tsvector GENERATED ALWAYS AS (
setweight(to_tsvector('english', title), 'A') ||
setweight(to_tsvector('english', coalesce(body, '')), 'D')
) STORED
);
The temporary table lasts for one session. In an application, the same generated column can live on a regular table. PostgreSQL computes the stored value on insert and when the source text changes. Naming the language in both the column and the query keeps their rules consistent. For a multilingual catalog, apply the configuration that corresponds to each document and search language. PostgreSQL’s guide to searchable tables describes the stored-column pattern.
INSERT INTO support_doc (ref, title, body) VALUES
('KB-1042', 'Reset password', 'Recover access with a verified email.'),
('KB-1043', 'Legacy portal', 'Reset password in the retired console.'),
('KB-1044', 'Backup', NULL),
('KB-1045', 'Retention schedule', 'Backup'),
('KB-1046', 'Quasar connector', 'Rotate the access token.');
INSERT INTO support_doc (ref, title, body)
SELECT 'DEMO-' || n, 'Routine maintenance', 'Inspect the service log.'
FROM generate_series(1, 20000) AS series(n);
ANALYZE support_doc;
websearch_to_tsquery accepts everyday search syntax. Quotation marks request a phrase, a leading minus excludes a term, and unquoted words normally need to match together. The prepared statement takes the search text as a parameter. In an application, pass it as a bound value through your database driver.
PREPARE support_search(text) AS
WITH input AS (
SELECT websearch_to_tsquery('english', $1) AS q
)
SELECT ref, title, ts_rank(search_doc, q) AS score
FROM support_doc CROSS JOIN input
WHERE numnode(q) > 0 AND querytree(q) <> 'T'
AND search_doc @@ q
ORDER BY score DESC, ref
LIMIT 10;
The @@ operator tests for a match. Only then does ts_rank assign the score used to sort the results. The reference breaks ties so equally ranked articles appear in a stable order. The two guards give this particular search box a predictable empty state. Blank input and English stop words alone, such as “the,” have no searchable terms. An exclusion alone, such as -legacy, has no positive term to look up. All three returned zero rows. The interface can ask the reader to enter a word or an article reference.
EXECUTE support_search('backups');
EXECUTE support_search('"reset password" -legacy');
The first query returned KB-1044 (“Backup”) with a score of 0.608, followed by KB-1045 (“Retention schedule”) at 0.061. Each contained the word once. The second query returned only KB-1042. The retired portal article also contained “reset password,” but “legacy” in its title excluded it. These scores order results for one query. They do not express a probability that the answer is correct.
There is one detail to decide for phrase searches. Concatenating the title and body vectors places their positions next to each other, so a phrase can cross the field boundary. If a quoted phrase must occur entirely within one field, match the query against separate title and body vectors. The manual covers query parsing and relevance ranking.
When the input has the shape of one of your catalog references, normalize it to the format you store and use equality. The primary key already has a unique B-tree index. This path preserves the meaning of the hyphen and digits, and a missing reference gives you an unambiguous “article not found.”
SELECT ref, title FROM support_doc WHERE ref = 'KB-1042';
The lookup returned “Reset password.” A search box can route kb-1042 to the same lookup after uppercasing it if all references follow that convention. Keep ordinary support questions on the full-text path.
Run the following query in the same session before creating the index. It looks for “quasar,” which occurs in exactly one title. Then build the index and run the identical query again.
EXPLAIN (ANALYZE, BUFFERS)
SELECT ref FROM support_doc
WHERE search_doc @@ websearch_to_tsquery('english', 'quasar');
CREATE INDEX support_doc_search_gin
ON support_doc USING GIN (search_doc);
Before the index, the plan showed a Seq Scan with 20,004 rows removed by the filter. Afterward, it showed a Bitmap Index Scan on support_doc_search_gin, followed by a Bitmap Heap Scan that fetched one matching row. Both runs used the same 20,005 records and the planner chose the access path itself.
GIN maps terms to candidate rows. It does not return them in relevance order. A frequent term can still leave many rows to score, and a small table can be cheaper to scan. Run the check on common terms from your own search logs as well as on rare terms. Our local temporary-table run demonstrates the change in plan. Estimating latency on a VM requires a representative catalog, query mix and concurrent traffic. See PostgreSQL’s text-search index guidance.
UPDATE support_doc SET title = 'Pulsar connector'
WHERE ref = 'KB-1046';
EXECUTE support_search('quasar');
EXECUTE support_search('pulsar');
After renaming the connector, “quasar” returned no rows and “pulsar” found KB-1046 in the same transaction. Other sessions can see the edit after commit, subject to their transaction snapshot. There is no separate indexing job for an editor to wait for.
Before using this approach in your help center, take a handful of real searches with known answers. Include a product code, a quoted error message and a question phrased differently from its answer. If missing vocabulary explains the failures, the hybrid search guide shows how lexical and semantic results can work together. If you plan to store embeddings, our vector and relational database comparison covers that next decision.
For a self-hosted evaluation, you can run PostgreSQL on a CPU instance with Hivenet. Load a representative sample and check the top results, time under your expected traffic, and index size before choosing an instance.
Pick one AI, compute, or storage workload and see the difference for yourself. Spin it up in minutes, or let our team map your fastest path to production.