Prompt-to-PR: Add PostgreSQL Full-Text Search
SOP for adding native PostgreSQL full-text search with tsvector, GIN index, ts_rank, and a Next.js search API — no third-party search service needed.
CursorClaude CodeCodexWindsurf Next.jsPostgreSQLTypeScript
PostgreSQL’s built-in full-text search handles most product search needs without Elasticsearch or Algolia. This playbook adds a tsvector column, a GIN index, and a ranked search query behind a Next.js API route.
1. Requirement
Add full-text search over a posts table (columns: title, body, tags). Search should return results ranked by relevance with snippet highlights. The implementation must use a generated tsvector column (not computed at query time) so the GIN index is used.
2. First Prompt
Add native PostgreSQL full-text search to the posts table in this project.
Database: PostgreSQL. ORM/query builder: [Drizzle / postgres.js — use whicheveris already in src/db/].
Step 1 — migration: Create a migration file (or Drizzle schema change) that: a. Adds a generated column: search_vector tsvector GENERATED ALWAYS AS ( setweight(to_tsvector('english', coalesce(title, '')), 'A') || setweight(to_tsvector('english', coalesce(body, '')), 'B') || setweight(to_tsvector('english', coalesce(tags, '')), 'C') ) STORED; b. Creates a GIN index on search_vector: CREATE INDEX posts_search_idx ON posts USING gin(search_vector);
Step 2 — query function: Create `src/lib/queries/search.ts` exporting `searchPosts(q: string, limit = 10)`. The query must: - Convert the user query to a tsquery: `websearch_to_tsquery('english', $1)`. - Filter: `search_vector @@ query`. - Rank: `ts_rank(search_vector, query) DESC`. - Return: id, title, ts_headline('english', body, query, 'MaxWords=30, MinWords=15, ShortWord=3, HighlightAll=false') AS snippet. - Use parameterized query (no string interpolation of the user input).
Step 3 — API route: Create `src/app/api/search/route.ts` (GET). Read `q` from URL search params. Return 400 if q is empty or shorter than 2 chars. Return JSON array of results. Add cache-control: public, max-age=60.
Step 4 — do not touch any UI components.3. Expected File Changes
src/db/migrations/<timestamp>_add_search_vector.sql (new — or Drizzle migration)src/db/schema.ts (search_vector column if Drizzle)src/lib/queries/search.ts (new — searchPosts function)src/app/api/search/route.ts (new — GET endpoint)4. Review Checklist
GENERATED ALWAYS AS ... STORED— column is stored (not virtual), so the GIN index can be used. ConfirmSTOREDkeyword is present.websearch_to_tsqueryis used (notto_tsquery) — handles multi-word and phrase queries from untrusted input without syntax errors.- The user-supplied query is passed as a SQL parameter, never interpolated.
ts_rankorders results — not alphabetical or insertion order.ts_headlinelength is capped (MaxWords=30) to avoid returning huge snippets.- GIN index name is explicit — easier to drop/recreate if needed.
- API route validates minimum query length (2 chars) to avoid full-table-scan tsqueries like
'a':*. Cache-Control: public, max-age=60is set on the response — search results can be short-lived cached.
5. Test Commands
# Run the migrationnpx drizzle-kit migrate# or: psql $DATABASE_URL < migration.sql
# Verify the generated column and index existpsql $DATABASE_URL -c "\d posts" | grep search_vectorpsql $DATABASE_URL -c "\di posts_search_idx"
# Query performance — confirm index scan, not seq scanpsql $DATABASE_URL -c " EXPLAIN ANALYZE SELECT id FROM posts WHERE search_vector @@ websearch_to_tsquery('english', 'your test query') LIMIT 10;" | grep "Index Scan"
# Test the API endpointbun dev &curl "http://localhost:3000/api/search?q=your+test+query" | jq .
# Confirm empty query returns 400curl -o /dev/null -w "%{http_code}" "http://localhost:3000/api/search?q="# Expect: 4006. Common Failures
- Seq scan instead of index scan —
VIRTUALcolumn (notSTORED). OnlySTOREDgenerated columns can be indexed in PostgreSQL. Confirm the migration usesSTORED. to_tsquerythrows on multi-word input —to_tsquery('english', 'quick brown')is a syntax error. Always usewebsearch_to_tsqueryfor user-supplied strings.ts_headlinereturns entire body —HighlightAll=trueaccidentally set, orMaxWordsnot set. Check the options string.- GIN index not used for
LIKEqueries — ensure the WHERE clause uses@@operator, notLIKEorILIKE. Full-text search andLIKEare separate. - Migration fails on existing data — the
GENERATEDcolumn populates on creation; on large tables this can be slow. Run in a transaction with a progress check.
7. Fix Prompt
EXPLAIN ANALYZE shows a sequential scan instead of an index scan on theposts table. The search_vector column was added as VIRTUAL (or withoutthe STORED keyword) so PostgreSQL cannot create a GIN index on it.
Fix the migration: ALTER TABLE posts DROP COLUMN search_vector; ALTER TABLE posts ADD COLUMN search_vector tsvector GENERATED ALWAYS AS ( setweight(to_tsvector('english', coalesce(title, '')), 'A') || setweight(to_tsvector('english', coalesce(body, '')), 'B') ) STORED; CREATE INDEX posts_search_idx ON posts USING gin(search_vector);
Confirm the word STORED appears in the column definition.8. PR Description
## Feature: PostgreSQL native full-text search on posts
- Generated `tsvector` column (`STORED`) with weighted fields: title (A), body (B), tags (C)- GIN index `posts_search_idx` — index scans confirmed via `EXPLAIN ANALYZE`- `searchPosts(q, limit)` uses `websearch_to_tsquery` (safe for user input) and `ts_rank` for relevance ordering- `ts_headline` snippets (max 30 words) with matched terms highlighted- GET `/api/search?q=` — validates query length, returns ranked JSON, `Cache-Control: public, max-age=60`
No third-party search dependency added.