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

PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail

Enforces strict memory and block boundary alignment (4096-byte hardware sector and 8192-byte relation block multiples) for streaming batch ingestion pipelines targeting PostgreSQL 17 deployments running with direct I/O enabled (io_direct). Prevents page cache contention, synchronous read-modify-write degradation, and unbuffered write stall cascades across NVMe storage subsystems.

Prohibited Anti-Pattern: Arbitrary Chunk Splitting and Unaligned Memory Streaming Causing Direct I/O Read-Modify-Write Amplification

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail - Enforces strict memory and block boundary alignment (4096-byte hardware sector and 8192-byte relation block multiples) for streaming batch ingestion pipelines targeting PostgreSQL 17 deployments running with direct I/O enabled (io_direct). Prevents page cache contention, synchronous read-modify-write degradation, and unbuffered write stall cascades across NVMe storage subsystems.
globs: src/ingestion/**/*.ts, src/pipeline/**/*.ts, src/database/**/*.ts, src/workers/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-direct-io-streaming-ingestion-alignment"
---

# PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Arbitrary Chunk Splitting and Unaligned Memory Streaming Causing Direct I/O Read-Modify-Write Amplification

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing strict memory alignment and Direct I/O invariants for high-throughput PostgreSQL 17 streaming ingestion pipelines. Mandate exact block boundary batch framing and reject arbitrary chunk sizes.

## Strict Guidelines
- Engineers MUST ALWAYS buffer incoming streaming ingestion payloads into memory blocks that are strict integer multiples of the PostgreSQL block size (8192 bytes) and underlying NVMe physical sector size (4096 bytes).
- Engineers MUST NOT stream arbitrary-sized network chunks, line-delimited records, or micro-batches directly to PostgreSQL COPY streams without an intermediate aligned block aggregation buffer.
- Code SHOULD pre-allocate page-aligned buffers using TypedArrays (e.g., Uint8Array over aligned ArrayBuffer) sized to target batch increments (e.g., 64 KiB, 128 KiB, or 1 MiB) to avoid intermediate memory copies.
- Services MUST monitor PostgreSQL 17 'io_direct' stalls and ensure streaming writes do not trigger OS page cache thrashing or synchronous read-modify-write sector penalties on direct-write relation buffers.
- Components NEVER flush unaligned binary records across block boundaries unless the transport boundary is finalized via an explicit terminal EOF sequence.

## Bad vs Good Implementations
### Unaligned Arbitrary Chunk Ingestion vs. Sector-Aligned Block Aggregation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Forwarding variable-length network chunks directly to the PostgreSQL binary COPY stream violates PostgreSQL 17 direct I/O block boundary alignment. When io_direct is configured for relation data, unaligned write buffers force underlying file system fallbacks or read-modify-write penalties at the NVMe block layer, producing catastrophic write stalls.
```typescript
import { Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { from as copyFrom } from 'pg-copy-streams';
import { Client } from 'pg';

export async function ingestStreamToPostgres(client: Client, rawNetworkStream: NodeJS.ReadableStream): Promise<void> {
  const copyStream = client.query(copyFrom('COPY raw_telemetry FROM STDIN WITH (FORMAT binary)'));

  // BAD: Piping arbitrary network chunks directly into the PostgreSQL COPY stream.
  // Emits variable chunks (e.g., 512 bytes, 1537 bytes) causing unaligned Direct I/O block writes
  // in PostgreSQL 17, triggering read-modify-write sector amplification on NVMe.
  await pipeline(
    rawNetworkStream,
    new Transform({
      transform(chunk: Buffer, _encoding, callback) {
        // Unchecked chunk size forwarded directly
        callback(null, chunk);
      }
    }),
    copyStream
  );
}
```

#### Production Standard (Correct)
> **Standard**: The aligned batcher buffers incoming streaming data into exact 64 KiB frames (integer multiple of both the 8192-byte relation block and 4096-byte NVMe sector). This ensures that PostgreSQL 17 operating with io_direct can commit memory pages straight to disk without kernel page cache contention or NVMe sector read-modify-write overhead.
```typescript
import { Transform, TransformCallback } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { from as copyFrom } from 'pg-copy-streams';
import { Client } from 'pg';

// PostgreSQL 17 Direct I/O optimal alignment: 64 KiB (multiple of 8192 PG block and 4096 NVMe sector)
const DIRECT_IO_ALIGNMENT_BYTES = 65536;

class DirectIoAlignedBatcher extends Transform {
  private currentBuffer: Buffer;
  private writeOffset: number = 0;

  constructor() {
    super();
    // Pre-allocate aligned memory block
    this.currentBuffer = Buffer.allocUnsafe(DIRECT_IO_ALIGNMENT_BYTES);
  }

  override _transform(chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback): void {
    let sourceOffset = 0;
    while (sourceOffset < chunk.length) {
      const available = DIRECT_IO_ALIGNMENT_BYTES - this.writeOffset;
      const bytesToCopy = Math.min(available, chunk.length - sourceOffset);

      chunk.copy(this.currentBuffer, this.writeOffset, sourceOffset, sourceOffset + bytesToCopy);
      this.writeOffset += bytesToCopy;
      sourceOffset += bytesToCopy;

      if (this.writeOffset === DIRECT_IO_ALIGNMENT_BYTES) {
        this.push(this.currentBuffer);
        this.currentBuffer = Buffer.allocUnsafe(DIRECT_IO_ALIGNMENT_BYTES);
        this.writeOffset = 0;
      }
    }
    callback();
  }

  override _flush(callback: TransformCallback): void {
    if (this.writeOffset > 0) {
      // Emit terminal remainder slice without corrupting preceding aligned frames
      this.push(this.currentBuffer.subarray(0, this.writeOffset));
    }
    callback();
  }
}

export async function ingestStreamToPostgres(client: Client, rawNetworkStream: NodeJS.ReadableStream): Promise<void> {
  const copyStream = client.query(copyFrom('COPY raw_telemetry FROM STDIN WITH (FORMAT binary)'));

  // GOOD: Strictly bundles data into 64 KiB blocks aligned to PG relation and NVMe block sizes
  await pipeline(
    rawNetworkStream,
    new DirectIoAlignedBatcher(),
    copyStream
  );
}
```

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: PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail

PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory and block boundary alignment (4096-byte hardware sector and 8192-byte relation block multiples) for streaming batch ingestion pipelines targeting PostgreSQL 17 deployments running with direct I/O enabled (io_direct). Prevents page cache contention, synchronous read-modify-write degradation, and unbuffered write stall cascades across NVMe storage subsystems. 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: Arbitrary Chunk Splitting and Unaligned Memory Streaming Causing Direct I/O Read-Modify-Write Amplification 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 DriftArbitrary Chunk Splitting and Unaligned Memory Streaming Causing Direct I/O Read-Modify-Write AmplificationCritical
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 buffer incoming streaming ingestion payloads into memory blocks that are strict integer multiples of the PostgreSQL block size (8192 bytes) and underlying NVMe physical sector size (4096 bytes).

Engineers MUST ALWAYS buffer incoming streaming ingestion payloads into memory blocks that are strict integer multiples of the PostgreSQL block size (8192 bytes) and underlying NVMe physical sector size (4096 bytes).

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 NOT stream arbitrary-sized network chunks, line-delimited records, or micro-batches directly to PostgreSQL COPY streams without an intermediate aligned block aggregation buffer.

Engineers MUST NOT stream arbitrary-sized network chunks, line-delimited records, or micro-batches directly to PostgreSQL COPY streams without an intermediate aligned block aggregation buffer.

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

Code SHOULD pre-allocate page-aligned buffers using TypedArrays (e.g., Uint8Array over aligned ArrayBuffer) sized to target batch increments (e.g., 64 KiB, 128 KiB, or 1 MiB) to avoid intermediate memory copies.

Code SHOULD pre-allocate page-aligned buffers using TypedArrays (e.g., Uint8Array over aligned ArrayBuffer) sized to target batch increments (e.g., 64 KiB, 128 KiB, or 1 MiB) to avoid intermediate memory copies.

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

Services MUST monitor PostgreSQL 17 'io_direct' stalls and ensure streaming writes do not trigger OS page cache thrashing or synchronous read-modify-write sector penalties on direct-write relation buffers.

Services MUST monitor PostgreSQL 17 'io_direct' stalls and ensure streaming writes do not trigger OS page cache thrashing or synchronous read-modify-write sector penalties on direct-write relation buffers.

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

Components NEVER flush unaligned binary records across block boundaries unless the transport boundary is finalized via an explicit terminal EOF sequence.

Components NEVER flush unaligned binary records across block boundaries unless the transport boundary is finalized via an explicit terminal EOF sequence.

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 PostgreSQL 17 Direct I/O Sector Alignment and Block-Bounded Batch Streaming Guardrail critical for AI coding assistants?

Enforces strict memory and block boundary alignment (4096-byte hardware sector and 8192-byte relation block multiples) for streaming batch ingestion pipelines targeting PostgreSQL 17 deployments running with direct I/O enabled (io_direct). Prevents page cache contention, synchronous read-modify-write degradation, and unbuffered write stall cascades across NVMe storage subsystems. 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 (Arbitrary Chunk Splitting and Unaligned Memory Streaming Causing Direct I/O Read-Modify-Write Amplification) 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

PostgreSQL