This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-streaming-io-bulk-write-invariant

Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant

Governs the orchestration of multi-terabyte transactional writes against PostgreSQL 17's streaming I/O subsystem. Mandates aligned, asynchronous buffer pipelining and direct COPY streams to prevent shared buffer cache eviction spirals and synchronous kernel write stalls. Ensures ingestion worker memory pools remain isolated from general OLTP buffer allocations.

Prohibited Anti-Pattern: Unbuffered Micro-Batching with Transactional Thrashing and Kernel Cache Dirtying

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant - Governs the orchestration of multi-terabyte transactional writes against PostgreSQL 17's streaming I/O subsystem. Mandates aligned, asynchronous buffer pipelining and direct COPY streams to prevent shared buffer cache eviction spirals and synchronous kernel write stalls. Ensures ingestion worker memory pools remain isolated from general OLTP buffer allocations.
globs: src/database/**/*.ts, src/ingestion/**/*.ts, src/workers/**/*.ts, src/services/storage/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-streaming-io-bulk-write-invariant"
---

# Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbuffered Micro-Batching with Transactional Thrashing and Kernel Cache Dirtying

## System Constraints
You are a Principal Database Architect enforcing PostgreSQL 17 streaming I/O bulk ingest invariants. Prevent micro-batch transactional thrashing, memory bloat, and buffer cache pollution during massive ingest workloads by mandating vectorized COPY streams with aligned, zero-copy buffer chunks and tuned backend session states.

## Strict Guidelines
- Engineers MUST ALWAYS utilize pg-copy-streams or binary COPY protocols instead of multi-row INSERT statements when persisting batches exceeding 10,000 records.
- Engineers MUST ALWAYS isolate bulk sessions by configuring 'SET LOCAL synchronous_commit = off' and sizing 'maintenance_work_mem' appropriately within the ingest transaction boundary.
- Engineers MUST NOT stream unchunked dynamic strings or allocate arbitrary micro-buffers; writes MUST be vectorized into fixed 64KB-aligned ByteBuffers matching kernel page block multiples.
- Code SHOULD dynamically tune max_parallel_maintenance_workers and disable autovacuum/indexes during initial bulk load phases, deferring index construction until ingest completion.
- Services MUST monitor PostgreSQL 17 pg_stat_io metrics to assert zero unexpected backend fsync stalls and avoid dirtying shared_buffers over predetermined thresholds.

## Bad vs Good Implementations
### Vectorized Bulk Ingest Pipeline with Zero-Copy Binary Buffering

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Micro-batching or individual row INSERTs overfill the WAL prematurely, thrash Postgres shared_buffers, and produce unaligned synchronous write stalls that bypass Postgres 17's vectorized streaming I/O engine optimizations.
```typescript
import { Pool } from 'pg';

export async function ingestMetrics(pool: Pool, records: Array<{ id: string; val: number; ts: number }>) {
  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    // ANTI-PATTERN: Iterative multi-value inserts causing transaction log contention,
    // dirtying shared_buffers repeatedly, and triggering synchronous OS disk stalls.
    for (const record of records) {
      await client.query(
        'INSERT INTO telemetry (id, val, ts) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING',
        [record.id, record.val, record.ts]
      );
    }
    await client.query('COMMIT');
  } catch (err) {
    await client.query('ROLLBACK');
    throw err;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Leverages the PostgreSQL binary/text COPY interface with aligned 64KB chunks and disables synchronous commit within a local transaction, directly feeding PostgreSQL 17's streaming I/O buffers without thrashing shared_buffers.
```typescript
import { Pool } from 'pg';
import { from as copyFrom } from 'pg-copy-streams';
import { pipeline } from 'node:stream/promises';
import { Readable } from 'node:stream';

export async function ingestMetrics(pool: Pool, records: Array<{ id: string; val: number; ts: number }>) {
  const client = await pool.connect();
  const CHUNK_SIZE = 64 * 1024; // 64KB aligned buffer
  try {
    await client.query('BEGIN');
    // Optimize for Postgres 17 Streaming I/O Engine and WAL throughput
    await client.query('SET LOCAL synchronous_commit = off');
    await client.query('SET LOCAL work_mem = "128MB"');

    const ingestStream = client.query(
      copyFrom('COPY telemetry (id, val, ts) FROM STDIN WITH (FORMAT text, DELIMITER "\t")')
    );

    async function* generateVectorizedChunks() {
      let buffer = '';
      for (const record of records) {
        buffer += `${record.id}\t${record.val}\t${record.ts}\n`;
        if (buffer.length >= CHUNK_SIZE) {
          yield Buffer.from(buffer, 'utf-8');
          buffer = '';
        }
      }
      if (buffer.length > 0) {
        yield Buffer.from(buffer, 'utf-8');
      }
    }

    await pipeline(Readable.from(generateVectorizedChunks()), ingestStream);
    await client.query('COMMIT');
  } catch (err) {
    await client.query('ROLLBACK');
    throw err;
  } finally {
    client.release();
  }
}
```

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: Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant

Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant defines an industrial-grade behavioral standard for backend engineering. Governs the orchestration of multi-terabyte transactional writes against PostgreSQL 17's streaming I/O subsystem. Mandates aligned, asynchronous buffer pipelining and direct COPY streams to prevent shared buffer cache eviction spirals and synchronous kernel write stalls. Ensures ingestion worker memory pools remain isolated from general OLTP buffer allocations. 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: Unbuffered Micro-Batching with Transactional Thrashing and Kernel Cache Dirtying 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 DriftUnbuffered Micro-Batching with Transactional Thrashing and Kernel Cache DirtyingCritical
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 utilize pg-copy-streams or binary COPY protocols instead of multi-row INSERT statements when persisting batches exceeding 10,000 records.

Engineers MUST ALWAYS utilize pg-copy-streams or binary COPY protocols instead of multi-row INSERT statements when persisting batches exceeding 10,000 records.

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 ALWAYS isolate bulk sessions by configuring 'SET LOCAL synchronous_commit = off' and sizing 'maintenance_work_mem' appropriately within the ingest transaction boundary.

Engineers MUST ALWAYS isolate bulk sessions by configuring 'SET LOCAL synchronous_commit = off' and sizing 'maintenance_work_mem' appropriately within the ingest transaction boundary.

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 NOT stream unchunked dynamic strings or allocate arbitrary micro-buffers

Engineers MUST NOT stream unchunked dynamic strings or allocate arbitrary micro-buffers; writes MUST be vectorized into fixed 64KB-aligned ByteBuffers matching kernel page block multiples.

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

Code SHOULD dynamically tune max_parallel_maintenance_workers and disable autovacuum/indexes during initial bulk load phases, deferring index construction until ingest completion.

Code SHOULD dynamically tune max_parallel_maintenance_workers and disable autovacuum/indexes during initial bulk load phases, deferring index construction until ingest completion.

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

Services MUST monitor PostgreSQL 17 pg_stat_io metrics to assert zero unexpected backend fsync stalls and avoid dirtying shared_buffers over predetermined thresholds.

Services MUST monitor PostgreSQL 17 pg_stat_io metrics to assert zero unexpected backend fsync stalls and avoid dirtying shared_buffers over predetermined thresholds.

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 Postgres 17 Streaming I/O Engine Vectorized Bulk Ingestion Invariant critical for AI coding assistants?

Governs the orchestration of multi-terabyte transactional writes against PostgreSQL 17's streaming I/O subsystem. Mandates aligned, asynchronous buffer pipelining and direct COPY streams to prevent shared buffer cache eviction spirals and synchronous kernel write stalls. Ensures ingestion worker memory pools remain isolated from general OLTP buffer allocations. 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 (Unbuffered Micro-Batching with Transactional Thrashing and Kernel Cache Dirtying) 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

Postgres