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

PostgreSQL 17 Async Streaming I/O Direct Buffer Page Alignment

Mandates strict kernel-level page and sector memory alignment (minimum 4096-byte boundaries) for all memory buffers used in PostgreSQL 17 asynchronous streaming read pipelines and direct I/O operations. Unaligned direct I/O buffers force the Linux kernel to fall back to synchronous bounce buffers and buffered copy paths, causing catastrophic cache thrashing and serialization under high-throughput NVMe workloads.

Prohibited Anti-Pattern: Unaligned Heap Allocations in Direct Async I/O Causing Kernel Bounce-Buffer Fallbacks

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Async Streaming I/O Direct Buffer Page Alignment - Mandates strict kernel-level page and sector memory alignment (minimum 4096-byte boundaries) for all memory buffers used in PostgreSQL 17 asynchronous streaming read pipelines and direct I/O operations. Unaligned direct I/O buffers force the Linux kernel to fall back to synchronous bounce buffers and buffered copy paths, causing catastrophic cache thrashing and serialization under high-throughput NVMe workloads.
globs: src/**/*.{c,h,cpp,hpp}, extensions/**/*.{c,h}, src/storage/**/*.{c,h}, contrib/**/*.{c,h}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-async-io-direct-buffer-alignment"
---

# PostgreSQL 17 Async Streaming I/O Direct Buffer Page Alignment

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unaligned Heap Allocations in Direct Async I/O Causing Kernel Bounce-Buffer Fallbacks

## System Constraints
You are a Principal Software Architect enforcing memory alignment and asynchronous I/O invariants in PostgreSQL 17 subsystems and custom extensions. You must reject any direct streaming buffer allocations that use standard palloc/malloc without explicit kernel page alignment (PG_IO_ALIGN / 4096 bytes).

## Strict Guidelines
- Engineers MUST ALWAYS allocate asynchronous streaming I/O buffers using posix_memalign, pg_aligned_alloc, or palloc_aligned adhering to PG_IO_ALIGN (4096-byte boundary).
- Engineers MUST NOT pass arbitrary heap pointers allocated via standard palloc() or malloc() to read_stream or io_uring asynchronous direct I/O interfaces.
- Code SHOULD assert alignment invariants at compile-time and runtime using Assert(((uintptr_t) buffer & (PG_IO_ALIGN - 1)) == 0) before queueing requests.
- Services MUST ensure asynchronous read stream vector lengths are exact integer multiples of the NVMe physical sector size (4096 bytes).
- Components NEVER fall back to standard synchronous buffered reads without explicitly unregistering pending async streaming read queue contexts.

## Bad vs Good Implementations
### PostgreSQL 17 Extension Async Read Stream Allocation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Allocating buffers with standard palloc() provides at best MAXALIGN (typically 8 or 16 bytes). In PostgreSQL 17, when io_method is set to io_uring with direct streaming I/O, the kernel requires direct DMA alignment (PG_IO_ALIGN / 4096 bytes). Passing unaligned buffers forces the kernel to copy memory through bounce buffers or fail silently with synchronous fallback.
```c
// BAD: Standard palloc does not guarantee 4096-byte alignment required for O_DIRECT/io_uring
void init_custom_async_read_worker(BlockNumber start_block, uint32 nblocks) {
    size_t buffer_size = nblocks * BLCKSZ;
    char *io_buffer = (char *) palloc(buffer_size);

    // Passing non-aligned buffer to async streaming read callback
    // When io_method = io_uring and direct I/O is enabled, this forces kernel bounce buffering
    ReadStream *stream = read_stream_begin_relation(
        READ_STREAM_FULL,
        custom_strategy,
        rel,
        MAIN_FORKNUM,
        custom_read_stream_cb,
        (void *) io_buffer,
        0
    );
}
```

#### Production Standard (Correct)
> **Standard**: Using pg_aligned_alloc with DIRECT_IO_ALIGNMENT (4096 bytes) guarantees that the allocated buffer adheres to kernel page and NVMe hardware sector boundaries. This enables true zero-copy DMA streaming via io_uring without double-buffering churn or kernel fallback.
```c
// GOOD: Strict 4096-byte page alignment using pg_aligned_alloc with boundary assertions
#define DIRECT_IO_ALIGNMENT 4096

void init_custom_async_read_worker(BlockNumber start_block, uint32 nblocks) {
    size_t buffer_size = nblocks * BLCKSZ;
    // Ensure total allocation is also a multiple of alignment size
    size_t aligned_size = TYPEALIGN(DIRECT_IO_ALIGNMENT, buffer_size);

    // Allocate kernel-aligned memory compatible with O_DIRECT and io_uring
    char *io_buffer = (char *) pg_aligned_alloc(DIRECT_IO_ALIGNMENT, aligned_size);
    if (io_buffer == NULL) {
        ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory for aligned async I/O buffer")));
    }

    // Verify alignment invariant
    Assert(((uintptr_t) io_buffer & (DIRECT_IO_ALIGNMENT - 1)) == 0);

    ReadStream *stream = read_stream_begin_relation(
        READ_STREAM_FULL,
        custom_strategy,
        rel,
        MAIN_FORKNUM,
        custom_read_stream_cb,
        (void *) io_buffer,
        0
    );
}
```

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 Async Streaming I/O Direct Buffer Page Alignment

PostgreSQL 17 Async Streaming I/O Direct Buffer Page Alignment defines an industrial-grade behavioral standard for backend engineering. Mandates strict kernel-level page and sector memory alignment (minimum 4096-byte boundaries) for all memory buffers used in PostgreSQL 17 asynchronous streaming read pipelines and direct I/O operations. Unaligned direct I/O buffers force the Linux kernel to fall back to synchronous bounce buffers and buffered copy paths, causing catastrophic cache thrashing and serialization under high-throughput NVMe workloads. 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: Unaligned Heap Allocations in Direct Async I/O Causing Kernel Bounce-Buffer Fallbacks 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 DriftUnaligned Heap Allocations in Direct Async I/O Causing Kernel Bounce-Buffer FallbacksCritical
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 allocate asynchronous streaming I/O buffers using posix_memalign, pg_aligned_alloc, or palloc_aligned adhering to PG_IO_ALIGN (4096-byte boundary).

Engineers MUST ALWAYS allocate asynchronous streaming I/O buffers using posix_memalign, pg_aligned_alloc, or palloc_aligned adhering to PG_IO_ALIGN (4096-byte 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.
2

Engineers MUST NOT pass arbitrary heap pointers allocated via standard palloc() or malloc() to read_stream or io_uring asynchronous direct I/O interfaces.

Engineers MUST NOT pass arbitrary heap pointers allocated via standard palloc() or malloc() to read_stream or io_uring asynchronous direct I/O interfaces.

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 assert alignment invariants at compile-time and runtime using Assert(((uintptr_t) buffer & (PG_IO_ALIGN - 1)) == 0) before queueing requests.

Code SHOULD assert alignment invariants at compile-time and runtime using Assert(((uintptr_t) buffer & (PG_IO_ALIGN - 1)) == 0) before queueing requests.

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 ensure asynchronous read stream vector lengths are exact integer multiples of the NVMe physical sector size (4096 bytes).

Services MUST ensure asynchronous read stream vector lengths are exact integer multiples of the 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.
5

Components NEVER fall back to standard synchronous buffered reads without explicitly unregistering pending async streaming read queue contexts.

Components NEVER fall back to standard synchronous buffered reads without explicitly unregistering pending async streaming read queue contexts.

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 Async Streaming I/O Direct Buffer Page Alignment critical for AI coding assistants?

Mandates strict kernel-level page and sector memory alignment (minimum 4096-byte boundaries) for all memory buffers used in PostgreSQL 17 asynchronous streaming read pipelines and direct I/O operations. Unaligned direct I/O buffers force the Linux kernel to fall back to synchronous bounce buffers and buffered copy paths, causing catastrophic cache thrashing and serialization under high-throughput NVMe workloads. 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 (Unaligned Heap Allocations in Direct Async I/O Causing Kernel Bounce-Buffer Fallbacks) 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