{
  "id": "add-postgres-full-text-search",
  "type": "playbooks",
  "category": "playbooks",
  "locale": "en",
  "url": "/playbooks/add-postgres-full-text-search",
  "title": "Prompt-to-PR: Add PostgreSQL Full-Text Search",
  "description": "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.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "PostgreSQL",
    "TypeScript"
  ],
  "tags": [
    "postgres",
    "nextjs",
    "typescript",
    "search",
    "sql"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "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.\n\n## 1. Requirement\n\nAdd 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.\n\n## 2. First Prompt\n\n```txt title=\"First Prompt\"\nAdd native PostgreSQL full-text search to the posts table in this project.\n\nDatabase: PostgreSQL. ORM/query builder: [Drizzle / postgres.js — use whichever\nis already in src/db/].\n\nStep 1 — migration:\n  Create a migration file (or Drizzle schema change) that:\n  a. Adds a generated column:\n       search_vector tsvector GENERATED ALWAYS AS (\n         setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n         setweight(to_tsvector('english', coalesce(body, '')), 'B') ||\n         setweight(to_tsvector('english', coalesce(tags, '')), 'C')\n       ) STORED;\n  b. Creates a GIN index on search_vector:\n       CREATE INDEX posts_search_idx ON posts USING gin(search_vector);\n\nStep 2 — query function:\n  Create `src/lib/queries/search.ts` exporting `searchPosts(q: string, limit = 10)`.\n  The query must:\n  - Convert the user query to a tsquery: `websearch_to_tsquery('english', $1)`.\n  - Filter: `search_vector @@ query`.\n  - Rank: `ts_rank(search_vector, query) DESC`.\n  - Return: id, title, ts_headline('english', body, query,\n      'MaxWords=30, MinWords=15, ShortWord=3, HighlightAll=false') AS snippet.\n  - Use parameterized query (no string interpolation of the user input).\n\nStep 3 — API route:\n  Create `src/app/api/search/route.ts` (GET). Read `q` from URL search params.\n  Return 400 if q is empty or shorter than 2 chars. Return JSON array of results.\n  Add cache-control: public, max-age=60.\n\nStep 4 — do not touch any UI components.\n```\n\n## 3. Expected File Changes\n\n```txt\nsrc/db/migrations/<timestamp>_add_search_vector.sql   (new — or Drizzle migration)\nsrc/db/schema.ts                                       (search_vector column if Drizzle)\nsrc/lib/queries/search.ts                              (new — searchPosts function)\nsrc/app/api/search/route.ts                            (new — GET endpoint)\n```\n\n## 4. Review Checklist\n\n- `GENERATED ALWAYS AS ... STORED` — column is stored (not virtual), so the GIN index can be used. Confirm `STORED` keyword is present.\n- `websearch_to_tsquery` is used (not `to_tsquery`) — handles multi-word and phrase queries from untrusted input without syntax errors.\n- The user-supplied query is passed as a SQL parameter, never interpolated.\n- `ts_rank` orders results — not alphabetical or insertion order.\n- `ts_headline` length is capped (MaxWords=30) to avoid returning huge snippets.\n- GIN index name is explicit — easier to drop/recreate if needed.\n- API route validates minimum query length (2 chars) to avoid full-table-scan tsqueries like `'a':*`.\n- `Cache-Control: public, max-age=60` is set on the response — search results can be short-lived cached.\n\n## 5. Test Commands\n\n```bash\n# Run the migration\nnpx drizzle-kit migrate\n# or: psql $DATABASE_URL < migration.sql\n\n# Verify the generated column and index exist\npsql $DATABASE_URL -c \"\\d posts\" | grep search_vector\npsql $DATABASE_URL -c \"\\di posts_search_idx\"\n\n# Query performance — confirm index scan, not seq scan\npsql $DATABASE_URL -c \"\n  EXPLAIN ANALYZE\n  SELECT id FROM posts\n  WHERE search_vector @@ websearch_to_tsquery('english', 'your test query')\n  LIMIT 10;\n\" | grep \"Index Scan\"\n\n# Test the API endpoint\nbun dev &\ncurl \"http://localhost:3000/api/search?q=your+test+query\" | jq .\n\n# Confirm empty query returns 400\ncurl -o /dev/null -w \"%{http_code}\" \"http://localhost:3000/api/search?q=\"\n# Expect: 400\n```\n\n## 6. Common Failures\n\n- **Seq scan instead of index scan** — `VIRTUAL` column (not `STORED`). Only `STORED` generated columns can be indexed in PostgreSQL. Confirm the migration uses `STORED`.\n- **`to_tsquery` throws on multi-word input** — `to_tsquery('english', 'quick brown')` is a syntax error. Always use `websearch_to_tsquery` for user-supplied strings.\n- **`ts_headline` returns entire body** — `HighlightAll=true` accidentally set, or `MaxWords` not set. Check the options string.\n- **GIN index not used for `LIKE` queries** — ensure the WHERE clause uses `@@` operator, not `LIKE` or `ILIKE`. Full-text search and `LIKE` are separate.\n- **Migration fails on existing data** — the `GENERATED` column populates on creation; on large tables this can be slow. Run in a transaction with a progress check.\n\n## 7. Fix Prompt\n\n```txt title=\"Fix Prompt\"\nEXPLAIN ANALYZE shows a sequential scan instead of an index scan on the\nposts table. The search_vector column was added as VIRTUAL (or without\nthe STORED keyword) so PostgreSQL cannot create a GIN index on it.\n\nFix the migration:\n  ALTER TABLE posts DROP COLUMN search_vector;\n  ALTER TABLE posts ADD COLUMN search_vector tsvector\n    GENERATED ALWAYS AS (\n      setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n      setweight(to_tsvector('english', coalesce(body, '')), 'B')\n    ) STORED;\n  CREATE INDEX posts_search_idx ON posts USING gin(search_vector);\n\nConfirm the word STORED appears in the column definition.\n```\n\n## 8. PR Description\n\n```md title=\"PR description\"\n## Feature: PostgreSQL native full-text search on posts\n\n- Generated `tsvector` column (`STORED`) with weighted fields:\n  title (A), body (B), tags (C)\n- GIN index `posts_search_idx` — index scans confirmed via `EXPLAIN ANALYZE`\n- `searchPosts(q, limit)` uses `websearch_to_tsquery` (safe for user input)\n  and `ts_rank` for relevance ordering\n- `ts_headline` snippets (max 30 words) with matched terms highlighted\n- GET `/api/search?q=` — validates query length, returns ranked JSON,\n  `Cache-Control: public, max-age=60`\n\nNo third-party search dependency added.\n```"
}