[{"data":1,"prerenderedAt":28},["ShallowReactive",2],{"$fIyie1gIcNSHMoLEVZYS9OPLmwPlMEr-Nmq7UK5y58sU":3},{"slug":4,"title":5,"description":6,"date":7,"tags":8,"draft":12,"readingMinutes":13,"html":14,"related":15},"postgres-full-text-search","Postgres Full Text Search: Enough for Most Apps","Postgres full text search checked on PostgreSQL 18: tsvector columns, GIN indexes, websearch_to_tsquery, ranking, ts_headline, typos with pg_trgm, and the limits.","2026-11-10",[9,10,11],"postgres","search","database",false,4,"\u003Cp>Postgres full text search is the search box most apps should ship first. It&#39;s built into PostgreSQL, it needs no extra service, it stays in the same transaction as your data, and with the right index it answers in milliseconds on a few million rows. People skip it and reach for Elasticsearch or Algolia on day one, then pay for a second system to keep in sync. I&#39;d rather start here and move only when a real limit shows up.\u003C/p>\n\u003Cp>Everything below is checked against the \u003Ca href=\"https://www.postgresql.org/docs/current/textsearch.html\">PostgreSQL 18 text search docs\u003C/a> (18 is the current release; 19 is at release candidate as I write this, November 10, 2026).\u003C/p>\n\u003Ch2>The two types\u003C/h2>\n\u003Cp>Search in Postgres works on two types. A \u003Ccode>tsvector\u003C/code> is a document turned into normalized words, called lexemes, with their positions. A \u003Ccode>tsquery\u003C/code> is what you&#39;re looking for, with operators between the words.\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT to_tsvector(&#39;english&#39;, &#39;The cats were sitting on the mats&#39;);\n-- &#39;cat&#39;:2 &#39;mat&#39;:7 &#39;sit&#39;:4\n\u003C/code>\u003C/pre>\n\u003Cp>&quot;The&quot;, &quot;were&quot; and &quot;on&quot; are gone because they&#39;re stop words. &quot;cats&quot; became \u003Ccode>cat\u003C/code> and &quot;sitting&quot; became \u003Ccode>sit\u003C/code>, because the \u003Ccode>english\u003C/code> configuration stems words. That&#39;s why a search for &quot;sit&quot; finds &quot;sitting&quot;. It&#39;s also why a search for a product code like \u003Ccode>SKU-SIT-01\u003C/code> behaves oddly with \u003Ccode>english\u003C/code>. For names, codes and anything that isn&#39;t prose, use the \u003Ccode>simple\u003C/code> configuration, which lowercases and does nothing else.\u003C/p>\n\u003Cimg src=\"/blog/postgres-full-text-search/tsvector-anatomy.webp\" alt=\"How one sentence becomes a tsvector and how a websearch query matches it: stop words dropped, words stemmed, positions kept, then the highlighted snippet\" width=\"1600\" height=\"900\" loading=\"lazy\">\u003Ch2>Store the tsvector, index it\u003C/h2>\n\u003Cp>Don&#39;t call \u003Ccode>to_tsvector\u003C/code> on every row at query time. Store it in a generated column and put a GIN index on it:\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">ALTER TABLE articles\n  ADD COLUMN search tsvector\n  GENERATED ALWAYS AS (\n    setweight(to_tsvector(&#39;english&#39;, coalesce(title, &#39;&#39;)), &#39;A&#39;) ||\n    setweight(to_tsvector(&#39;english&#39;, coalesce(body, &#39;&#39;)), &#39;B&#39;)\n  ) STORED;\n\nCREATE INDEX articles_search_idx ON articles USING GIN (search);\n\u003C/code>\u003C/pre>\n\u003Cp>Two details that cost people an afternoon. Pass the configuration name (\u003Ccode>&#39;english&#39;\u003C/code>) explicitly. The one-argument form of \u003Ccode>to_tsvector\u003C/code> depends on the \u003Ccode>default_text_search_config\u003C/code> setting, so it isn&#39;t immutable, and Postgres refuses it in a generated column with \u003Ccode>generation expression is not immutable\u003C/code>. And wrap nullable columns in \u003Ccode>coalesce\u003C/code>, because concatenating a NULL tsvector gives you NULL and the row silently becomes unsearchable.\u003C/p>\n\u003Cp>The \u003Ccode>setweight\u003C/code> calls label title words A and body words B, so ranking can prefer a hit in the title. That&#39;s most of what &quot;relevance tuning&quot; means here.\u003C/p>\n\u003Ch2>Query it the way users type\u003C/h2>\n\u003Cp>Users don&#39;t type \u003Ccode>&amp;\u003C/code> and \u003Ccode>|\u003C/code>. Use \u003Ccode>websearch_to_tsquery\u003C/code>, which understands the syntax people already know from search engines and never throws a syntax error:\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT id, title,\n       ts_rank_cd(search, q) AS rank\nFROM articles,\n     websearch_to_tsquery(&#39;english&#39;, &#39;&quot;connection pool&quot; -mysql&#39;) AS q\nWHERE search @@ q\nORDER BY rank DESC\nLIMIT 20;\n\u003C/code>\u003C/pre>\n\u003Cp>Quoted words become a phrase, \u003Ccode>or\u003C/code> becomes OR, a leading \u003Ccode>-\u003C/code> excludes a word, and everything else is AND. The older \u003Ccode>to_tsquery\u003C/code> raises an error on plain input like \u003Ccode>connection pool\u003C/code>, so never feed it user text directly. For search-as-you-type, build a prefix query: \u003Ccode>to_tsquery(&#39;simple&#39;, &#39;conn:*&#39;)\u003C/code> matches anything starting with &quot;conn&quot;.\u003C/p>\n\u003Cp>On ranking: \u003Ccode>ts_rank\u003C/code> counts how often the words appear, and \u003Ccode>ts_rank_cd\u003C/code> also rewards them for being close together, which usually feels better for multi-word searches. Both have a normalization option. \u003Ccode>32\u003C/code> scales the score into 0 to 1, and \u003Ccode>1\u003C/code> divides by the log of the document length so long documents don&#39;t win just by being long. The docs are honest that ranking &quot;can be expensive&quot;: it reads the tsvector of every matching row. Filter hard first, then rank.\u003C/p>\n\u003Ch2>Highlighting results\u003C/h2>\n\u003Cp>\u003Ccode>ts_headline\u003C/code> gives you the snippet with the matched words wrapped in \u003Ccode>&lt;b&gt;\u003C/code> tags (change them with \u003Ccode>StartSel\u003C/code> and \u003Ccode>StopSel\u003C/code>, and shorten with \u003Ccode>MaxWords\u003C/code>, default 35). Two warnings straight from the docs. It works on the original text, not the tsvector, &quot;so it can be slow&quot;, so run it only on the 20 rows you display, in an outer query, never on the whole match set. And its output &quot;is not guaranteed to be safe for direct inclusion in web pages&quot;. If the text came from users, sanitize it before you render it. A search snippet is a very easy XSS to ship by accident.\u003C/p>\n\u003Ch2>Typos are the real gap\u003C/h2>\n\u003Cp>Full text search matches lexemes. &quot;postgress&quot; doesn&#39;t stem to &quot;postgr&quot;, so a typo finds nothing. The usual fix is the \u003Ccode>pg_trgm\u003C/code> extension next to it: a trigram GIN index on the title (\u003Ccode>gin_trgm_ops\u003C/code>) and a fallback query with the \u003Ccode>%\u003C/code> similarity operator when the full text query returns zero rows. For accents (&quot;café&quot; vs &quot;cafe&quot;) add the \u003Ccode>unaccent\u003C/code> extension to a custom configuration.\u003C/p>\n\u003Cp>That combination covers most product, docs and admin search I see in client apps.\u003C/p>\n\u003Ch2>When to move to a search engine\u003C/h2>\n\u003Cp>There are real limits. A tsvector has to be under 1 MB, and positions stop counting at 16,383, so words deep in a very long document can&#39;t be used for phrase or proximity matching (they still match as plain words). There is no built-in typo tolerance, no BM25, no facet counts, no synonyms without maintaining a thesaurus yourself. If search is the product (a marketplace with filters and facet counts, an ecommerce catalog where &quot;iphne&quot; must find iPhones, multiple languages ranked well), use Meilisearch, Typesense or OpenSearch, and treat Postgres as the source of truth you sync from.\u003C/p>\n\u003Cp>If search is a feature inside your app, Postgres is enough, and one less moving part in production is worth a lot.\u003C/p>\n\u003Cp>If you&#39;re deciding which way to go, or your current search is slow and you&#39;re not sure why, \u003Ca href=\"https://calendly.com/director-norsky/meeting\">book a free call\u003C/a>. All consultation is free.\u003C/p>\n",[16,20,24],{"slug":17,"title":18,"description":19},"postgres-jsonb","Postgres JSONB: When I Use It and Which Index I Pick","PostgreSQL jsonb in practice on Postgres 18: json vs jsonb, containment queries, jsonb_ops vs jsonb_path_ops GIN indexes, and the update traps.",{"slug":21,"title":22,"description":23},"postgres-partitioning","Postgres Partitioning: When It Pays Off, and the Traps","Postgres partitioning checked against the PostgreSQL 18 docs: range, list and hash, the primary key rule, pruning, attaching without locks, and when not to bother.",{"slug":25,"title":26,"description":27},"postgres-vs-mongodb","Postgres vs MongoDB: Pick by the Questions You'll Ask Later","Postgres vs MongoDB in November 2026: Postgres 19 just shipped, MongoDB 8.3, JSONB vs documents, joins, transactions, licensing, and how I choose for a new app.",1795910047897]