
postgres-semantic-search
Builds and tunes PostgreSQL retrieval with pgvector, full-text search, pgtrgm, hybrid fusion, ParadeDB BM25 and reranking. Use for search/RAG implementation, weak results, filtered ANN scans, non-English corpora or retrieval evaluation. For general schema, RLS and query tuning unrelated to retrieval, use supabase-postgres-best-practices.
- Installs
- 218
- Last 24h
- —
- Trending rank
- —
- Security
- Not audited
Install
Works with every agent the skills CLI supports.
npx skills add https://github.com/laguagu/claude-code-nextjs-skills --skill postgres-semantic-search- 01
Install
Run the command in your project. The CLI asks which agents to install for.
- 02
Describe the task
Ask your agent for something this skill covers. It loads postgres-semantic-search automatically when relevant.
- 03
Review the result
The agent follows the SKILL.md steps, scripts and references bundled with the skill.
SKILL.md
The instructions your agent reads when the skill is triggered.
PostgreSQL Semantic Search
Decisions, measured findings and silent failure modes for search built on Postgres, plus tested SQL building blocks in scripts/. Syntax the official docs cover well is left to them; what is here goes wrong without an error.
Build order
- Look at what users actually type: identifiers and codes, one-to-three-word terms, full questions, which languages. That decides which arms you need.
- Start with the simplest retrieval suited to those queries: exact/keyword lookup or vector search. For approximate vector search, keep exact vector search as the recall baseline.
- Build two eval sets, long questions and short terms, before tuning (evaluation.md).
- Add another arm and fuse with RRF only where it improves the baseline on the queries that need it (hybrid-search.md).
- Add a reranker last, over the head of a good shortlist (reranking.md).
Change one thing at a time. Predefine the acceptance rule; by default require a gain beyond measured run-to-run spread on the affected query population, without a material loss on the other eval set. Inspect per-query wins/losses and require the agreed latency and cost budget to hold. The numerical findings below are corpus-specific observations, not universal performance guarantees.
Choosing
- Column type by dimensions, not provider:
vector(N)indexes up to 2,000; up to 4,000 index ahalfveccast (or use ahalfveccolumn); above that, binary quantization or Matryoshka truncation. - HNSW by default, IVFFlat when memory or build time rules HNSW out. No row count decides it: measure recall and latency against exact search.
- Models change every few months. Pick embedding and reranker models from the provider's current docs, prefer multilingual models for non-English text, and evaluate on the target language before committing.
- Hybrid when users type both questions and terms. On a Finnish transcript corpus keyword alone was weak on long questions (24 % right first), vector alone on short terms (53 %); hybrid tied vector on long questions and led on short terms (53 % and 93 %; hybrid-search.md).
- BM25 is not available everywhere: managed hosts differ (Neon removed
pg_search). Plain FTS with the fixes below is often enough.
Silent failures
These return fewer rows, zero rows or plausible results, never an error.
pgvector (pgvector.md):
- Without iterative scanning, HNSW results are limited by
hnsw.ef_search(default 40). A largerLIMIT, or a selectiveWHERE, silently returns fewer. Enablehnsw.iterative_scan(off by default), or use partial indexes or partitions. - The default
ef_searchcan cost recall with no warning. Raise it until recall against exact search stops moving. IfEXPLAINshows a seq scan, tuning does nothing, and the seq scan may be the faster plan. - A distance threshold goes outside a
MATERIALIZEDCTE, other filters inside it. SETis per connection. Behind a transaction pooler useSET LOCALin the same transaction, or a function-levelSET.- Similarity cutoffs are model-specific: a cutoff tuned for one model
returns nothing for another.
1 - (a <#> b)is not cosine similarity. - Clients mangle JS arrays: node-postgres sends
{0.1,0.2}and Drizzle'ssqltemplate expands an array into($1, $2, ...); vector input rejects both. SendJSON.stringify(embedding)and cast with::vector.
Keyword (keyword-search.md):
plainto_tsqueryand unquoted plain input towebsearch_to_tsqueryAND bare terms, so long questions can match nothing. For plain-input retrieval, consider OR andts_rank_cd; preserve explicit web-search quotes, OR and exclusions.unaccentmerges distinct words in Finnish, Swedish, German or Turkish. Fold only decorative accents, inside a text search configuration.- Unwanted zero-width export artifacts can glue onto tokens and block stemming. Normalize them consistently without deleting meaningful ZWNJ/ZWJ.
- A generated tsvector column can silently become a plain NULL column after an ORM migration. If hybrid and vector-only return identical lists, the keyword arm may be absent or ineffective: check NULL/population counts and keyword-only results before diagnosing it.
- Prefix matching in inflected languages must OR-join terms and expand hyphenated tokens (prefix_tsquery.sql).
%compares whole strings; use<%for prefixes and short queries against long text.
Non-English and chunking
- Cap chunks with the selected embedding model's tokenizer and input limit.
Ratios vary by model and text: with OpenAI's
cl100k_base(text-embedding-3), Finnish runs about 2.5 characters per token against 4 or more for English, so an English-tuned character cap is about twice too generous. The endpoint rejects the chunk and often fails the whole batch. - Off-language queries can make the lexical arm ineffective and cost about 12 points even with a multilingual embedding model. Translate into a sentence, not a keyword list (a keyword list scored 14 points below no translation on one corpus), and consider two-pass fusion.
- A multi-word synonym expansion enters the tsquery as independent words, and its generic word takes over the ranking. Trim parts an order of magnitude commoner than the rest of their own phrase.
- Inflection: on one Finnish corpus embeddings absorbed it; a stemmed
arm (Postgres
finnish) lifted keyword-only search but not hybrid; a dictionary lemmatizer did worse than the stemmer on spoken and domain text; compound splitting flooded the arm. Re-measure on the target text. Prefix matching never reaches the base form of an inflected query (keyword-search.md). - Transcripts: chunk length barely changed which video was found. A second, fine-grained search over the subtitle cues inside the matched segment finds the moment.
Details and measurements: hybrid-search.md.
Reranking
- Start by reranking only the head (top 5) of a ~30-candidate shortlist, and deepen only if the short-term set holds. Cross-encoders score "mentions X" rather than "is about X" and promote passing mentions of short terms: on a Finnish transcript corpus, reordering all 30 cut short-term Hit@1 from 0.93 to 0.60–0.80, the top 10 still cost some models a term or two, and the top 5 cost none for six rerankers. Re-measure depth on your own sets.
- The relevance question's wording can matter more than the model (about thirty points on one corpus). Every criterion must be checkable from the text sent, and the source title belongs in the reranker input.
- Choose by where it can run and whether text may leave the network. Measured on a 2-core CPU: a ~120M multilingual cross-encoder added about 0.3 s, while 568M–600M models took 10–25 s. On a data-centre GPU, Qwen3-Reranker-8B was the only model clearly more accurate than that small CPU model (reranking.md). Hosted rerankers need no infrastructure but receive the candidate text. A GPU that comes and goes can serve first, with the CPU model as the timeout fallback. Measure p95 on the target hardware.
- Measure the shortlist ceiling first: a reranker cannot recover what the first stage missed.
Numbers, model trade-offs and adoption rules: reranking.md.
Scripts
For a new sample database, use setup.sql, then indexes.sql and selected
function files. For an existing database, review/adapt them as migrations: they
assume documents(id, title, content, metadata, embedding vector(1536)) and
chunks(id, document_id, chunk_index, content, embedding): change the names
and dimension in every file together. Some function files drop older signatures;
review dependent objects before applying them. These are building blocks, not a
complete production migration/authorization policy. None sets hnsw.ef_search;
the caller does.
| File | Functions (real parameter names) |
|---|---|
| semantic_search.sql | match_documents(query_embedding, match_threshold, match_count), match_documents_filtered(query_embedding, filter_metadata, match_threshold, match_count), match_chunks(query_embedding, match_threshold, match_count); match_threshold NULL = plain top-k |
| hybrid_search_fts.sql | hybrid_search_fts(query_embedding, query_text, match_count, rrf_k, fts_language, vector_weight, keyword_weight); either input may be NULL |
| hybrid_search_bm25.sql | hybrid_search_bm25(...), hybrid_search_chunks_bm25(...): same parameters without fts_language; needs pg_search |
| fuzzy_search.sql | fuzzy_search_trigram(query_text, similarity_threshold, max_results), autocomplete_search(search_prefix, max_results), hybrid_search_fuzzy_semantic(query_text, query_embedding, max_results, rrf_k) |
| prefix_tsquery.sql | prefix_tsquery(config, text [, join]) |
| setup.sql, indexes.sql | Extensions, example tables, indexes |
Supabase .rpc() binds arguments by name, so a misspelled key fails at call
time:
const { data, error } = await supabase.rpc('hybrid_search_fts', {
query_embedding: embedding, // number[]
query_text: userQuery,
match_count: 10,
fts_language: 'simple',
});
// Drizzle or node-postgres: send the vector as text and cast it
await db.execute(sql`SELECT * FROM match_documents(${JSON.stringify(embedding)}::vector, NULL, 10)`);
References
- pgvector.md: types and dimension limits, HNSW tuning, planner choice, filters and thresholds, poolers, builds, operators.
- keyword-search.md: tsquery parsing, accents and language configs, dead keyword arms, prefix matching, trigrams, ParadeDB.
- hybrid-search.md: fusion, hybrid versus vector, chunking, transcripts, off-language queries, synonym expansion.
- reranking.md: choosing, depth, hardware, the relevance question, headroom, adoption.
- evaluation.md: two eval sets, bias, noise, and four ways a measurement misleads.
Versions
- pgvector: inspect the installed version with
SELECT extversion FROM pg_extension WHERE extname = 'vector'. 0.8.0+ for iterative scans. Check current stable releases: 0.8.2 fixed a buffer overflow in parallel HNSW builds, 0.8.3 and 0.8.4 HNSW vacuum corruption and errors, 0.8.7 a buffer overflow in IVFFlat builds. Read the current index-fix and upgrade notes before upgrading. Releases ship as git tags only, so read the CHANGELOG, not the empty Releases tab. - pg_search: 0.25+ depends on pgvector; install pgvector first. ParadeDB's API moves quickly; see keyword-search.md.
Related skills
| Need | Skill |
|---|---|
| General Postgres schema, indexes, RLS, pooling | supabase-postgres-best-practices |
| Chatbot orchestration, sessions, tool calls | nextjs-chatbot |
| Embedding calls through the AI SDK | ai-sdk |
FAQ
What does the postgres-semantic-search skill do?
Builds and tunes PostgreSQL retrieval with pgvector, full-text search, pgtrgm, hybrid fusion, ParadeDB BM25 and reranking. Use for search/RAG implementation, weak results, filtered ANN scans, non-English corpora or retrieval evaluation. For general schema, RLS and query tuning unrelated to retrieval, use supabase-postgres-best-practices.
How do I install postgres-semantic-search?
Run npx skills add https://github.com/laguagu/claude-code-nextjs-skills --skill postgres-semantic-search in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/postgres-semantic-search/ (personal) or .claude/skills/postgres-semantic-search/ (project).
Which AI agents can use postgres-semantic-search?
Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.
Is postgres-semantic-search safe to install?
No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.
Who maintains postgres-semantic-search?
It is published by laguagu in laguagu/claude-code-nextjs-skills. It has 218 recorded installs.
More from laguagu/claude-code-nextjs-skills
nextjs-seo
laguagu/claude-code-nextjs-skills
Next.js App Router SEO implementation and audits. Use for metadata and social images, sitemap/robots, canonicals and localization, structured data, Core Web Vitals evidence, AI crawler policy or search-indexing diagnosis. Not for general Next.js feature work unrelated to SEO.
openai-agents-sdk
laguagu/claude-code-nextjs-skills
OpenAI Agents SDK (Python) development. Use when building AI agents, multi-agent handoffs, function tools, guardrails, sessions, streaming, or tracing with the openai-agents / agents Python package — including Azure OpenAI through its native client or LiteLLM. Triggers on imports from agents, uses of Runner.runsync/Runner.runstreamed, @functiontool, AgentOutputSchema, SQLiteSession, or questions about the openai-agents-python SDK. Python only — not the TypeScript @openai/agents SDK.
nextjs-shadcn
laguagu/claude-code-nextjs-skills
Builds distinctive Next.js interfaces with shadcn/ui, choosing typography, styling and purposeful visual effects for the product. Use when creating or polishing a Next.js + shadcn app, page or component, including themes, presets, backgrounds and design-system lint. Uses frontend-design for substantial visual direction and icons for asset sourcing.
next-best-practices
laguagu/claude-code-nextjs-skills
Next.js App Router best practices covering file conventions, RSC boundaries, async APIs, data patterns, hydration errors, metadata, route handlers, image/font optimization, and bundling. Use when writing or reviewing Next.js code to prevent hydration errors, RSC violations, data waterfalls, and configuration mistakes.
react-best-practices
laguagu/claude-code-nextjs-skills
React and Next.js performance optimization guide from Vercel Engineering (70 rules in 8 categories). Use when writing, reviewing, or refactoring React/Next.js code to eliminate waterfalls, reduce bundle size, prevent re-renders, optimize Server Components, or fix performance bottlenecks. Triggers on data fetching, Promise.all, Suspense, useMemo, dynamic imports, bundle analysis, code-splitting, and rendering performance tasks.
cache-components
laguagu/claude-code-nextjs-skills
Expert guidance for Next.js Cache Components and Partial Prerendering (PPR). Use when implementing 'use cache' directive, configuring cache lifetimes with cacheLife(), tagging cached data with cacheTag(), invalidating caches with updateTag()/revalidateTag(), optimizing static vs dynamic content boundaries, instant navigation validation, 'use cache: private', pass-through/interleaving patterns, GET Route Handler caching, debugging cache issues, and reviewing Cache Component implementations.
Popular data & databases skills
supabase-postgres-best-practices
supabase/agent-skills
Postgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill BEFORE writing or changing anything that lives in a Postgres database: creating or altering tables and columns (including choosing column types), schema design, migrations and declarative schema files, RLS policies and the tests that verify them, indexes, triggers, database functions, queues and scheduled jobs (pgcron, pgmq), vector/semantic search (pgvector), and restoring dumps (pgrestore) or importing data. Also load it when diagnosing slow queries, high CPU, timeouts, EXPLAIN plans, connection exhaustion, locking, bloat, or rows visible to the wrong user or tenant. This is not just a performance guide — schema, migration, security, and SQL authoring tasks need these rules too, even for a one-column change or a single query.
prisma-client-api
prisma/skills
Prisma Client API reference covering model queries, filters, operators, and client methods. Use when writing database queries, using CRUD operations, filtering data, or configuring Prisma Client. Triggers on "prisma query", "findMany", "create", "update", "delete", "$transaction".
prisma-database-setup
prisma/skills
Deprecated compatibility name for prisma-orm-setup. Use only when a user or existing integration explicitly invokes prisma-database-setup; otherwise select prisma-orm-setup.
prisma-cli
prisma/skills
Prisma ORM 7 CLI reference for existing Prisma 7 applications or explicitly selected Prisma 7 workflows. Covers prisma init, generate, migrate, db, dev, studio, and mcp. For setup, connection repair, or Prisma 8, use prisma-orm-setup; not the Prisma Platform CLI.
prisma-postgres
prisma/skills
Deprecated compatibility name for prisma-postgres-setup. Use only when a user or existing integration explicitly invokes prisma-postgres; otherwise select prisma-postgres-setup.
prisma-driver-adapter-implementation
prisma/skills
Required reference for Prisma ORM 7 SQL driver adapter work. Use when implementing or modifying adapters, adding database drivers, or touching SqlDriverAdapter, Transaction, savepoint, result mapping, or DriverAdapterError behavior. Covers current transaction lifecycle, optional savepoint hooks, original database-error preservation, and verification.