This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: drizzle-pgvector-connection-pinning-invariant

Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes

Mandates strict transactional scoping and connection lifecycle isolation when setting pgvector index hyperparameters (such as hnsw.ef_search or ivfflat.probes) in Drizzle ORM. Enforces that all asynchronous embedding generation occurs entirely outside the database transaction boundary to prevent connection pool exhaustion and transaction interleaving deadlocks under connection poolers like PgBouncer.

Prohibited Anti-Pattern: Setting Global Session Variables or Holding Open Drizzle Transactions Across Vector Embeddings Inference

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes - Mandates strict transactional scoping and connection lifecycle isolation when setting pgvector index hyperparameters (such as hnsw.ef_search or ivfflat.probes) in Drizzle ORM. Enforces that all asynchronous embedding generation occurs entirely outside the database transaction boundary to prevent connection pool exhaustion and transaction interleaving deadlocks under connection poolers like PgBouncer.
globs: src/**/*.{ts,tsx}, src/db/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/drizzle-pgvector-connection-pinning-invariant"
---

# Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Setting Global Session Variables or Holding Open Drizzle Transactions Across Vector Embeddings Inference

## System Constraints
You are a Principal Software Architect enforcing database scalability and connection invariants for Drizzle ORM and PostgreSQL pgvector. Vector search query tuning variables like `hnsw.ef_search` or `ivfflat.probes` modify session-level state. In pooled topologies (e.g., PgBouncer transaction pooling, Supabase, Neon), executing session-level `SET` commands pollutes arbitrary pooled connections, while wrapping non-database asynchronous latency (such as LLM embedding API calls) within active database transactions causes catastrophic connection pool starvation and deadlocks. You must mandate strict adherence to `SET LOCAL` scoped within explicit read-only Drizzle transactions, enforce decoupling of embedding inference from database transactions, and require explicit statement timeouts.

## Strict Guidelines
- Engineers MUST ALWAYS compute vector embeddings before acquiring a database connection or opening a transaction.
- Engineers MUST NEVER execute external HTTP requests, OpenAI/cohere embeddings calls, or long-running CPU computation inside a `db.transaction()` block.
- Engineers MUST ALWAYS use `SET LOCAL` (via `sql` tagged template) rather than `SET` when tuning `hnsw.ef_search` or `ivfflat.probes` to restrict scope to the current transaction and avoid state contamination in pooled proxies.
- Engineers MUST ALWAYS configure the transaction access mode to `{ accessMode: 'read only', isolationLevel: 'read committed' }` for pure vector similarity read queries.
- Code SHOULD enforce a tight `statement_timeout` (e.g., `SET LOCAL statement_timeout = '3000ms'`) inside vector probe transactions to mitigate multi-second graph traversals on high-concurrency workloads.
- Services MUST NOT interleave write-locks or updates with approximate nearest neighbor (ANN) traversals inside the same transactional scope to prevent graph index interleaving deadlocks.

## Bad vs Good Implementations
### Anti-Pattern: Session Contamination & Embedding Inference inside Transaction

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: 1. Acquires a pooled database connection and holds it during external embedding inference, leading to connection starvation under high concurrency. 2. Uses `SET hnsw.ef_search` without `LOCAL`, which permanently alters the state of the pooled connection for future, unrelated workloads.
```typescript
import { sql } from 'drizzle-orm';
import { db } from '../db';
import { documents } from '../db/schema';
import { generateEmbedding } from '../ai/embeddings';

export async function searchSimilarDocuments(userQuery: string, limit: number = 10) {
  // ANTI-PATTERN: Transaction opened before vector generation.
  // Holding pool connection across network I/O to external model API.
  return await db.transaction(async (tx) => {
    // ANTI-PATTERN: Leaks session state across pooled connections in PgBouncer
    await tx.execute(sql`SET hnsw.ef_search = 128;`);

    const embedding = await generateEmbedding(userQuery); // High-latency external call

    return await tx.select({
      id: documents.id,
      content: documents.content,
      similarity: sql<number>`1 - (${documents.embedding} <=> ${JSON.stringify(embedding)}::vector)`,
    })
    .from(documents)
    .orderBy(sql`${documents.embedding} <=> ${JSON.stringify(embedding)}::vector`)
    .limit(limit);
  });
}
```

#### Production Standard (Correct)
> **Standard**: 1. External network latency is isolated from database resources. 2. `SET LOCAL` guarantees that probe hyperparameters and timeouts only exist for the duration of the explicit read-only transaction and revert automatically, preserving pooled connection hygiene in PgBouncer/Neon/Supabase.
```typescript
import { sql } from 'drizzle-orm';
import { db } from '../db';
import { documents } from '../db/schema';
import { generateEmbedding } from '../ai/embeddings';

export async function searchSimilarDocuments(userQuery: string, limit: number = 10) {
  // Invariant 1: Perform embedding generation outside the database transaction
  const embedding = await generateEmbedding(userQuery);
  const vectorLiteral = `[${embedding.join(',')}]`;

  // Invariant 2: Explicit read-only transaction with scoped SET LOCAL
  return await db.transaction(
    async (tx) => {
      await tx.execute(sql`
        SET LOCAL hnsw.ef_search = 128;
        SET LOCAL statement_timeout = '3000ms';
      `);

      const results = await tx
        .select({
          id: documents.id,
          content: documents.content,
          distance: sql<number>`${documents.embedding} <=> ${vectorLiteral}::vector`,
        })
        .from(documents)
        .orderBy(sql`${documents.embedding} <=> ${vectorLiteral}::vector`)
        .limit(limit);

      return results;
    },
    {
      accessMode: 'read only',
      isolationLevel: 'read committed',
    }
  );
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes

Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes defines an industrial-grade behavioral standard for backend engineering. Mandates strict transactional scoping and connection lifecycle isolation when setting pgvector index hyperparameters (such as hnsw.ef_search or ivfflat.probes) in Drizzle ORM. Enforces that all asynchronous embedding generation occurs entirely outside the database transaction boundary to prevent connection pool exhaustion and transaction interleaving deadlocks under connection poolers like PgBouncer. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Setting Global Session Variables or Holding Open Drizzle Transactions Across Vector Embeddings Inference Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftSetting Global Session Variables or Holding Open Drizzle Transactions Across Vector Embeddings InferenceCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS compute vector embeddings before acquiring a database connection or opening a transaction.

Engineers MUST ALWAYS compute vector embeddings before acquiring a database connection or opening a transaction.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NEVER execute external HTTP requests, OpenAI/cohere embeddings calls, or long-running CPU computation inside a `db.transaction()` block.

Engineers MUST NEVER execute external HTTP requests, OpenAI/cohere embeddings calls, or long-running CPU computation inside a `db.transaction()` block.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST ALWAYS use `SET LOCAL` (via `sql` tagged template) rather than `SET` when tuning `hnsw.ef_search` or `ivfflat.probes` to restrict scope to the current transaction and avoid state contamination in pooled proxies.

Engineers MUST ALWAYS use `SET LOCAL` (via `sql` tagged template) rather than `SET` when tuning `hnsw.ef_search` or `ivfflat.probes` to restrict scope to the current transaction and avoid state contamination in pooled proxies.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST ALWAYS configure the transaction access mode to `{ accessMode

Engineers MUST ALWAYS configure the transaction access mode to `{ accessMode: 'read only', isolationLevel: 'read committed' }` for pure vector similarity read queries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Code SHOULD enforce a tight `statement_timeout` (e.g., `SET LOCAL statement_timeout = '3000ms'`) inside vector probe transactions to mitigate multi-second graph traversals on high-concurrency workloads.

Code SHOULD enforce a tight `statement_timeout` (e.g., `SET LOCAL statement_timeout = '3000ms'`) inside vector probe transactions to mitigate multi-second graph traversals on high-concurrency workloads.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Services MUST NOT interleave write-locks or updates with approximate nearest neighbor (ANN) traversals inside the same transactional scope to prevent graph index interleaving deadlocks.

Services MUST NOT interleave write-locks or updates with approximate nearest neighbor (ANN) traversals inside the same transactional scope to prevent graph index interleaving deadlocks.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Drizzle ORM Connection-Pinning & Session Hygiene for pgvector Index Probes critical for AI coding assistants?

Mandates strict transactional scoping and connection lifecycle isolation when setting pgvector index hyperparameters (such as hnsw.ef_search or ivfflat.probes) in Drizzle ORM. Enforces that all asynchronous embedding generation occurs entirely outside the database transaction boundary to prevent connection pool exhaustion and transaction interleaving deadlocks under connection poolers like PgBouncer. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Setting Global Session Variables or Holding Open Drizzle Transactions Across Vector Embeddings Inference) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Drizzle