This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres17-logical-replication-buffers-standard

PostgreSQL 17 Failover Slots, Logical Streaming, and Memory Buffer Invariants

Enforces strict failover synchronization for logical replication slots introduced in PostgreSQL 17 alongside bounded WAL retention and explicit streaming modes. Guarantees memory buffer isolation by constraining logical_decoding_work_mem and shared_buffers to prevent disk spillover and out-of-memory cascades during large transactional streams.

Prohibited Anti-Pattern: Unsynchronized Logical Slots Without Failover Control and Unbounded WAL Retention

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Failover Slots, Logical Streaming, and Memory Buffer Invariants - Enforces strict failover synchronization for logical replication slots introduced in PostgreSQL 17 alongside bounded WAL retention and explicit streaming modes. Guarantees memory buffer isolation by constraining logical_decoding_work_mem and shared_buffers to prevent disk spillover and out-of-memory cascades during large transactional streams.
globs: src/database/**/*.ts, migrations/**/*.sql, infra/postgres/**/*.conf, src/infra/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres17-logical-replication-buffers-standard"
---

# PostgreSQL 17 Failover Slots, Logical Streaming, and Memory Buffer Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Logical Slots Without Failover Control and Unbounded WAL Retention

## System Constraints
You are a Principal Database Architect and Infrastructure Reliability Engineer enforcing PostgreSQL 17 replication and memory invariants. Mandate that all logical replication slots utilize PG17 failover slot management with synchronized standby tracking, enforce non-zero max_slot_wal_keep_size boundaries to prevent disk exhaustion, mandate streaming=parallel or streaming=on subscriptions, and maintain rigid memory budget boundaries across shared_buffers, work_mem, and logical_decoding_work_mem.

## Strict Guidelines
- Engineers MUST ALWAYS create logical replication slots with 'failover = true' in PostgreSQL 17 to permit slot synchronization to physical standbys via pg_sync_replication_slots().
- Engineers MUST ALWAYS set a deterministic non-zero 'max_slot_wal_keep_size' (e.g., 64GB) to guarantee that lagging or partitioned logical subscribers cannot trigger primary node disk full emergencies.
- Engineers MUST set subscription streaming parameter to 'on' or 'parallel' ('streaming = parallel') on PostgreSQL 17 consumers to stream large transactions in-flight and avoid disk spill-over in the decoding cache.
- Services MUST configure 'logical_decoding_work_mem' explicitly (minimum 64MB, maximum 1GB) to bound private process memory during decoding without starving PostgreSQL's shared_buffers pool.
- Configurations MUST NEVER allocate 'shared_buffers' above 40% of total system RAM; standard baseline is 25% to reserve memory for the OS page cache, work_mem allocations, and logical decoding processes.

## Bad vs Good Implementations
### PostgreSQL 17 Logical Slot and Subscription Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fails to configure PostgreSQL 17 failover slot preservation, meaning a physical standby failover corrupts or destroys logical subscriber positions. Fails to configure streaming=parallel, forcing massive transaction buffers to disk, and lacks WAL retention bounds.
```typescript
import { Client } from 'pg';

export async function setupLogicalReplication(client: Client): Promise<void> {
  // ANTI-PATTERN: Pre-PG17 pattern without failover flag, unbounded WAL retention,
  // and default buffered streaming (which buffers entire transactions to disk on publisher)
  await client.query(`
    SELECT pg_create_logical_replication_slot(
      'customer_events_slot',
      'pgoutput'
    );
  `);

  await client.query(`
    CREATE SUBSCRIPTION customer_events_sub
    CONNECTION 'host=db-primary port=5432 user=replicator dbname=app'
    PUBLICATION customer_events_pub
    WITH (copy_data = true, create_slot = false, slot_name = 'customer_events_slot');
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Leverages PostgreSQL 17's native failover parameter to keep logical replication slots synchronized across physical standbys, applies parallel streaming mode for sub-transaction processing, and maintains replica stability under high-write loads.
```typescript
import { Client } from 'pg';

export async function setupLogicalReplication(client: Client): Promise<void> {
  // PRODUCTION INVARIANT: PG17 native failover slot preservation
  // and streaming replication with bounded buffer protections
  await client.query(`
    SELECT pg_create_logical_replication_slot(
      slot_name => 'customer_events_slot',
      plugin => 'pgoutput',
      temporary => false,
      two_phase => true,
      failover => true
    );
  `);

  // Configure subscription to use parallel streaming to prevent transaction spill to disk
  await client.query(`
    CREATE SUBSCRIPTION customer_events_sub
    CONNECTION 'host=db-primary port=5432 user=replicator dbname=app'
    PUBLICATION customer_events_pub
    WITH (
      copy_data = true,
      create_slot = false,
      slot_name = 'customer_events_slot',
      streaming = 'parallel',
      synchronous_commit = 'off'
    );
  `);
}
```

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 Failover Slots, Logical Streaming, and Memory Buffer Invariants

PostgreSQL 17 Failover Slots, Logical Streaming, and Memory Buffer Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces strict failover synchronization for logical replication slots introduced in PostgreSQL 17 alongside bounded WAL retention and explicit streaming modes. Guarantees memory buffer isolation by constraining logical_decoding_work_mem and shared_buffers to prevent disk spillover and out-of-memory cascades during large transactional streams. 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: Unsynchronized Logical Slots Without Failover Control and Unbounded WAL Retention 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 DriftUnsynchronized Logical Slots Without Failover Control and Unbounded WAL RetentionCritical
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 create logical replication slots with 'failover = true' in PostgreSQL 17 to permit slot synchronization to physical standbys via pg_sync_replication_slots().

Engineers MUST ALWAYS create logical replication slots with 'failover = true' in PostgreSQL 17 to permit slot synchronization to physical standbys via pg_sync_replication_slots().

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 set a deterministic non-zero 'max_slot_wal_keep_size' (e.g., 64GB) to guarantee that lagging or partitioned logical subscribers cannot trigger primary node disk full emergencies.

Engineers MUST ALWAYS set a deterministic non-zero 'max_slot_wal_keep_size' (e.g., 64GB) to guarantee that lagging or partitioned logical subscribers cannot trigger primary node disk full emergencies.

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 set subscription streaming parameter to 'on' or 'parallel' ('streaming = parallel') on PostgreSQL 17 consumers to stream large transactions in-flight and avoid disk spill-over in the decoding cache.

Engineers MUST set subscription streaming parameter to 'on' or 'parallel' ('streaming = parallel') on PostgreSQL 17 consumers to stream large transactions in-flight and avoid disk spill-over in the decoding cache.

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 configure 'logical_decoding_work_mem' explicitly (minimum 64MB, maximum 1GB) to bound private process memory during decoding without starving PostgreSQL's shared_buffers pool.

Services MUST configure 'logical_decoding_work_mem' explicitly (minimum 64MB, maximum 1GB) to bound private process memory during decoding without starving PostgreSQL's shared_buffers pool.

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

Configurations MUST NEVER allocate 'shared_buffers' above 40% of total system RAM

Configurations MUST NEVER allocate 'shared_buffers' above 40% of total system RAM; standard baseline is 25% to reserve memory for the OS page cache, work_mem allocations, and logical decoding processes.

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 Failover Slots, Logical Streaming, and Memory Buffer Invariants critical for AI coding assistants?

Enforces strict failover synchronization for logical replication slots introduced in PostgreSQL 17 alongside bounded WAL retention and explicit streaming modes. Guarantees memory buffer isolation by constraining logical_decoding_work_mem and shared_buffers to prevent disk spillover and out-of-memory cascades during large transactional streams. 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 (Unsynchronized Logical Slots Without Failover Control and Unbounded WAL Retention) 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