PostgreSQL Full-Text Search for Product Catalogs

Using Postgres tsvector and pg_trgm for ranked, typo-tolerant product search on a 180,000-product catalog instead of adding a separate Elasticsearch cluster.

· 3 min read · Erik Kubica

The Cost of a Separate Search Cluster

A client once came to me with an e-commerce platform catalog of 180,000 products.

Their previous agency had quoted them 180 $ a month on AWS for a managed OpenSearch / Elasticsearch cluster, on the assumption that a relational database would not handle text search well enough.

I looked at the infrastructure they were proposing:

  • A dedicated OpenSearch instance needing 4 GB of RAM minimum to keep the Java Virtual Machine heap healthy.
  • An asynchronous sync pipeline (Debezium or custom webhook workers) syncing updates from PostgreSQL to Elasticsearch.
  • The risk of consistency bugs, such as a product price updated in PostgreSQL while the search index lags behind by 30 seconds.
  • Cluster alerts to monitor.

For their catalog size, we decided to skip the extra instance. We stayed on their existing production database server (4 vCPU, 8 GB RAM) and used PostgreSQL's native text search.

For a catalog of this size, I found the built-in Postgres search was enough, and it kept the setup simpler.

The Built-in Option: Generated tsvector + GIN

PostgreSQL has stem-aware full-text search built into its core.

You do not need external synchronization scripts. You create a generated column that updates whenever a product title or description changes:

-- Step 1: Add a stored tsvector generated column
ALTER TABLE products ADD COLUMN search_vector tsvector 
GENERATED ALWAYS AS (
    setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
    setweight(to_tsvector('english', coalesce(brand, '')), 'B') ||
    setweight(to_tsvector('english', coalesce(description, '')), 'C')
) STORED;

-- Step 2: Build a Generalized Inverted Index (GIN)
CREATE INDEX idx_products_search 
ON products USING gin (search_vector);

By using setweight, title matches are weighted higher ('A') than brand matches ('B') or descriptions ('C').

Because the column is STORED, PostgreSQL computes the search lexemes once, when the row is written. At query time it is a GIN index lookup.

Handling Typos with pg_trgm

A common reason people give for Elasticsearch is typo tolerance: if someone searches for 'iphne', will Postgres find it?

It can. The pg_trgm extension (trigram matching) ships with standard PostgreSQL distributions:

-- Enable trigram extension for fuzzy typo matching
CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE INDEX idx_products_title_trgm 
ON products USING gin (title gin_trgm_ops);

Now you can combine ranked full-text search with fuzzy trigram matching in a single query:

EXPLAIN (ANALYZE, BUFFERS)
SELECT 
    id, 
    title, 
    price_cents,
    ts_rank(search_vector, websearch_to_tsquery('english', 'wireless mechanical')) AS rank,
    similarity(title, 'wireles mechancl') AS sim
FROM products
WHERE 
    search_vector @@ websearch_to_tsquery('english', 'wireless mechanical')
    OR title % 'wireles mechancl'
ORDER BY rank DESC, sim DESC
LIMIT 20;

Here is the query planner output on a 500,000-row test catalog:

Limit (cost=24.12..95.40 rows=20 width=64) (actual time=2.810..3.450 rows=20 loops=1)
  -> Bitmap Heap Scan on products (actual time=2.805..3.435 rows=20 loops=1)
  Buffers: shared hit=84
Planning Time: 0.180 ms
Execution Time: 3.520 ms

About 3.5 milliseconds for typo-tolerant, weighted, stem-aware search across half a million rows, on existing hardware. Your data and hardware will give different numbers.

What We Avoided

For that client, going this way meant:

  • No sync pipeline to write or monitor.
  • No lag between the database and search: when an inventory count drops to 0, search results reflect it right away.
  • No extra servers to patch.
  • Roughly 2,000 $ in AWS costs avoided in the first year.

Elasticsearch or OpenSearch is the better call if you are indexing tens of millions of log events a day, need heavy faceting and analytics, or are building a very large search product. For an e-commerce catalog of this size or a SaaS document search, Postgres was enough for me.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.