<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shivam Gupta</title>
    <description>The latest articles on DEV Community by Shivam Gupta (@shivam-gupta27).</description>
    <link>https://dev.to/shivam-gupta27</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4274719%2Fe72f37ef-336a-4de9-a262-24c4f7f8400e.jpg</url>
      <title>DEV Community: Shivam Gupta</title>
      <link>https://dev.to/shivam-gupta27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shivam-gupta27"/>
    <language>en</language>
    <item>
      <title>How I Built Hybrid Search Across 6,000 Open-Source Repos with PostgreSQL 16 + pgvector (19.7s 1.8s)</title>
      <dc:creator>Shivam Gupta</dc:creator>
      <pubDate>Mon, 30 Nov 2026 16:30:33 +0000</pubDate>
      <link>https://dev.to/shivam-gupta27/how-i-built-hybrid-search-across-6000-open-source-repos-with-postgresql-16-pgvector-197s--36ka</link>
      <guid>https://dev.to/shivam-gupta27/how-i-built-hybrid-search-across-6000-open-source-repos-with-postgresql-16-pgvector-197s--36ka</guid>
      <description>&lt;p&gt;Most of us still evaluate open-source tools by scrolling GitHub stars and skimming marketing READMEs.&lt;/p&gt;

&lt;p&gt;I wanted a faster, evidence-backed way to find and compare production-ready repositories — so I built &lt;strong&gt;&lt;a href="https://www.getnexus.in/" rel="noopener noreferrer"&gt;NEXUS&lt;/a&gt;&lt;/strong&gt;, a hybrid search engine indexing ~6,000 AI &amp;amp; software engineering repositories and 252,000+ line-anchored documentation passages.&lt;/p&gt;

&lt;p&gt;Here’s why I kept the entire search stack inside &lt;strong&gt;PostgreSQL 16 + &lt;code&gt;pgvector&lt;/code&gt;&lt;/strong&gt;, how hybrid ranking doubled retrieval quality, and the &lt;code&gt;pgvector&lt;/code&gt; HNSW trap that cost me 18 seconds per query until I caught it with &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why PostgreSQL 16 + &lt;code&gt;pgvector&lt;/code&gt; Instead of a Separate Vector DB?
&lt;/h2&gt;

&lt;p&gt;When building semantic search, the default instinct is to spin up a separate vector database alongside your relational database. For NEXUS, that would have meant syncing stars, licenses, release tags, and archived status across two datastores.&lt;/p&gt;

&lt;p&gt;Instead, keeping &lt;strong&gt;relational metadata, full-text search (&lt;code&gt;tsvector&lt;/code&gt; GIN), and 384-d embeddings (&lt;code&gt;pgvector&lt;/code&gt; HNSW)&lt;/strong&gt; inside a single PostgreSQL 16 database gave me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One ACID query&lt;/strong&gt; that combines vector similarity, keyword rank, star popularity, push freshness, and hard filters (license safety, language, category).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero dual-write sync bugs&lt;/strong&gt; and &lt;strong&gt;$0 extra vector-DB infrastructure&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For embeddings, I run &lt;code&gt;sentence-transformers/all-MiniLM-L6-v2&lt;/code&gt; (384-d) locally via &lt;strong&gt;ONNX Runtime (&lt;code&gt;fastembed&lt;/code&gt;)&lt;/strong&gt; on CPU — avoiding external embedding API latency/costs and keeping the container lightweight compared to PyTorch.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Hybrid Search Doubled Retrieval Quality
&lt;/h2&gt;

&lt;p&gt;On an internal 30-query developer benchmark:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pure Lexical (&lt;code&gt;tsvector&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;0.456&lt;/code&gt; MRR — nails exact library/API names (&lt;code&gt;PagedAttention&lt;/code&gt;, &lt;code&gt;AGPL-3.0&lt;/code&gt;), misses natural-language intent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure Semantic (&lt;code&gt;pgvector&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;0.465&lt;/code&gt; MRR — understands intent (&lt;em&gt;"fast local LLM runner"&lt;/em&gt;), drifts on exact technical terms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NEXUS Hybrid:&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;0.936&lt;/code&gt; MRR (&lt;code&gt;90.0%&lt;/code&gt; Hit@1, &lt;code&gt;100%&lt;/code&gt; Hit@5)&lt;/strong&gt; — fuses semantic cosine similarity, weighted &lt;code&gt;tsvector&lt;/code&gt; rank, log-scaled star popularity, 180-day half-life push freshness, and curation-list damping (so &lt;code&gt;"awesome-*"&lt;/code&gt; lists don't crowd out real tools).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every search result also surfaces &lt;strong&gt;commit-pinned line excerpts&lt;/strong&gt; directly from READMEs and &lt;code&gt;/docs&lt;/code&gt;, plus 1-click similar alternatives, 4-repo side-by-side comparison, commercial license risk tiers, and a 2D Relevance vs. Health map.&lt;/p&gt;




&lt;h2&gt;
  
  
  The &lt;code&gt;pgvector&lt;/code&gt; HNSW Trap: &lt;code&gt;19.7s&lt;/code&gt; → &lt;code&gt;1.8s&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Right before launching, searching a broad term like &lt;code&gt;"vector"&lt;/code&gt; was taking &lt;strong&gt;19.7 seconds&lt;/strong&gt; on production.&lt;/p&gt;

&lt;p&gt;The culprit? My vector CTE filtered by cosine distance &lt;strong&gt;without&lt;/strong&gt; an &lt;code&gt;ORDER BY ... LIMIT&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Slow: triggers a sequential scan over all 252k vectors!&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;chunk_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;query_vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;similarity&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;chunk_embeddings&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;query_vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;pgvector&lt;/code&gt;, the HNSW index is &lt;strong&gt;only&lt;/strong&gt; used when Postgres sees &lt;code&gt;ORDER BY embedding &amp;lt;=&amp;gt; :query_vec LIMIT K&lt;/code&gt;. Extracting a materialized &lt;code&gt;top_vec_chunks&lt;/code&gt; CTE fixed it immediately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Fast: uses the HNSW cosine index (~20-50ms)&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;top_vec_chunks&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;chunk_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;query_vec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;chunk_embeddings&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;embedding&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;query_vec&lt;/span&gt;
    &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;top_vec_chunks&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;dist&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That single change dropped live hybrid search from &lt;strong&gt;19.7s → 1.87s&lt;/strong&gt; and similar-repository lookups from timing out to &lt;strong&gt;~1.08s&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try It Live
&lt;/h2&gt;

&lt;p&gt;NEXUS is live and free to use at &lt;strong&gt;&lt;a href="https://www.getnexus.in/" rel="noopener noreferrer"&gt;https://www.getnexus.in/&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Try searching for a tool you’re evaluating this week — I’d love your honest feedback on search relevance, speed, or what you’d want to see next!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>postgres</category>
      <category>opensource</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
