Architecture and technical rescue ·Build

Do you need a search engine? What your database already does, and what a second store costs

PostgreSQL search against a separate engine, on the same data

Search needs to be better, and the usual response is to add a search engine beside the database: a second system to run, keep in sync, secure and back up. Before adding one, I measured PostgreSQL's own search against OpenSearch on the same data and the same labelled queries. At this size they were level on relevance and on speed. What separated them was running them: no second store to keep consistent, and a record searchable the moment it is saved instead of a median of 713 ms later.

Problem
search needs to be better, and the usual response is to add a search engine beside the database: a second store to run, keep in sync, secure and back up.
Deciding constraint
what is searched must stay consistent with what is stored, and every extra system is a standing cost for a small team.
Choice
measure the database's own search against a separate engine on the same data and queries before deciding, and add the engine only for what the database cannot do.
Result
on 504 products and 19,820 customers, PostgreSQL and OpenSearch were equal on relevance and on speed. The difference was operational: no second store, and a write searchable at once instead of a median of 713 ms later. The engine's first query also returned nothing for half the questions until it was tuned.

The lab is public, with its tests and the command behind each of its figures: slow-endpoint-diagnosis.

If this is your situation

If you are about to add a search engine next to the database you already run, first write down what must stay consistent, then run your own labelled queries on both. If the database performs as well, the second store is cost without gain.

Architecture assessment and rescue → discuss a search problem

For your technical team How it was diagnosed, the options rejected and why, what went wrong, and the decision record. About 8 min read

Context and scope

A product catalogue and a customer list need search: products by words in their name, colour and category ("yellow road frames"), customers by a name typed with a mistake in it. The team already runs PostgreSQL. The current proposal is to add a search engine and feed it from the database.

The lab runs both on the same data: Microsoft's AdventureWorks sample, 504 products and 19,820 customers. PostgreSQL does word search with a generated tsvector column and websearch_to_tsquery, and name search with trigrams (pg_trgm). OpenSearch indexes the same fields. 22 product queries and 30 misspelt names were labelled from structured columns, not from text either engine indexes, so neither is graded on its own output.

In scope: relevance, speed under load, how soon a write is searchable, and what each option costs to run. Out of scope: web-scale search, millions of documents, ranking by business rules, facets. Assumption: a modest catalogue or archive, and a team that already runs the database.

Drivers

Constraints

  • A small team: every extra system is something to run, upgrade, secure and back up.
  • What is searched must match what is stored, including right after a write.
  • Access rules decided in the database must hold on every search path.

Scenarios

  • When the same labelled queries run on both, relevance is compared on equal terms. Measured by precision, recall at 10 and MRR.
  • When 50 users search at once, the tail stays acceptable. Measured by p50 and p95 under load.
  • When a record is written, it is searchable within a stated time. Measured from the write to the first search that finds it.
  • When a record is deleted, no search path returns it afterwards.

Options against those drivers

  • Search inside the database you already run · chosen, at this size

    Fits when
    the data is there, the corpus is modest, and the ranking needs are ordinary.
    Breaks when
    ranking needs go beyond what the database offers, or the corpus outgrows one database.
  • Add a search engine beside it (OpenSearch, Elasticsearch)

    Fits when
    volume, facets, relevance tuning or analytics go beyond the database.
    Breaks when
    one small team has to run it, keep it in sync, and repeat the access rules inside it.
  • A hosted search service

    Fits when
    nobody should run the engine, and the data may leave your systems.
    Breaks when
    data location rules apply, or the sync and the access rules are still yours to build.
PostgreSQL OpenSearch
Products: precision / recall@10 / MRR 0.884 / 0.97 / 1.0 0.893 / 1.0 / 0.898
Misspelt names: hit rate / MRR 1.0 / 0.983 1.0 / 0.921
50 users, products p50 / p95 3.5 / 9.4 ms 4.6 / 9.4 ms
50 users, names p50 / p95 10.9 / 79.2 ms 13.7 / 29.5 ms
Index build 0.24 s 1.65 s
Visible after a write in the same transaction (median 0.95 ms) median 713 ms after the index call
Extra services to run 0 1

At this size the two are level on relevance, each a little ahead on one measure, and level on speed, except the misspelt-name case under 50 users, where OpenSearch has the better tail (29.5 against 79.2 ms). What separates them is the last two rows.

The decision and the rules it rests on

Search columns are stored in the row. The words a product is searched by are a generated column of the product row, so a write and its search entry are one transaction: there is no sync job to fail, lag or be forgotten. In the lab a write was searchable in the same transaction; in OpenSearch, a median of 713 ms after the index call, because the index refreshes every second.

The access rule is written once. Inside the database, the rule that decides what a user may see is part of the query. In a second store it must be copied, and a copy drifts.

An engine is added for a reason the tests show. Facets, relevance tuning at scale, analytics over search, or volumes one database cannot serve: when a test on your own data shows one of these, the engine pays for its upkeep. Until then it is a second system for the same answers.

Meaning-based search, and what a second store does to a delete

The same choice was made in a personal document archive that searches by word, by code and by meaning. Its first version kept meaning-based search in a separate vector database, written during each transaction but not part of it: a delete could commit in the main store, fail in the vector store, and leave the document in results without anything detecting it. Moving everything into one PostgreSQL, with pgvector for the embeddings, made a delete one transaction. The labelled queries' ranking moved by one place on one query (MRR 0.883 to 0.871), and search answered in half the time at 10,000 documents (p95 49 to 24 ms). A replacement still has a short, tested window: the old text stays searchable until the new version's indexing job runs.

How the move is made, and how it is undone

To add search inside the database: add the generated columns and indexes in a migration, run the labelled queries, and compare. Undoing it is dropping the columns. To move from an engine back into the database: backfill, run the same labelled queries against both until they agree, switch reads, and keep the engine read-only until a restore of the database has been tested.

How the decision is kept true

  • A labelled query set, graded from structured data rather than from either engine's own text, run on both before any decision and again after every change to the search.
  • Search under load at 1, 10 and 50 users, read at p95 as well as p50 ([The system is slow. Where, exactly?] has the method).
  • The visibility test: the time from a write to the first search that finds it.
  • A delete test: after a delete, no search path returns the record.

Where it went wrong, and the risks that remain

The engine's first query was wrong, and the figures said so. The first OpenSearch query required every word in one field. "Yellow road frames" has the colour in one field and the rest in another, so 11 of the 22 product queries returned nothing (recall 0.5). Matching across fields fixed it. A separate engine is also something to tune, and a comparison run on its first configuration would have judged it unfairly.

Design notes drift from code. In the archive, an audit of the code found that the notes claimed more consistency than the code gave: a replacement was not one transaction. The article describes what the code does, and the replacement window now has a test. Check a consistency claim against the code, never against the design document.

Risk Likelihood Impact Mitigation
The corpus grows far past the tested size Unknown Latency or index builds grow Test on your own data at the target size before deciding
A ranking need the database cannot meet appears Medium Users miss results The labelled set detects it; that is the moment to add an engine
A second store drifts from the database High once there is one Deleted or stale records in results One store; or a sync with a visibility test and a delete test
The access rule is copied into the engine High once there is one A user sees what they should not One rule, in the query

Operations, cost and scale boundary

  • To run: one database, against a database and an engine, with the sync between them.
  • To back up: one store, restorable in one step, against two that must agree after a restore.
  • To learn: the database's search features, against a second product with its own query language and tuning.
  • Tested range: 504 products and 19,820 customers in the lab; up to 100,000 synthetic documents in the archive. Neither says anything about millions of documents, other languages or facets.

Where the choice flips: when your own labelled queries, at your own size, show a ranking or a volume the database cannot reach, the engine justifies its upkeep.

On a different stack

The reasoning carries to SQL Server: keep search structures in the same transactional store as the data, write the access rule once, and measure before choosing. The features differ by product and version, so check what yours offers before assuming the same result. The lab tested PostgreSQL only.

Architecture assessment and rescue → discuss a search problem

Where to start?

Describe the problem in a few minutes: what it is about, a little context and how to reach you.