This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-failover-slots-replication-buffers

PostgreSQL 17 Logical Replication Failover Slots & Memory Buffer Invariants

PostgreSQL 17 introduces native logical replication slot failover synchronization across physical standbys, requiring explicit failover flag enablement and coordinated synchronized_standby_slots configuration. Applications managing logical streaming consumers must guard against unconstrained WAL retention by enforcing max_slot_wal_keep_size alongside bounded logical_decoding_work_mem to prevent primary disk exhaustion and shared_buffers pollution. Failure to enforce these invariants leads to broken consumer streams post-failover or catastrophic WAL accumulation.

Prohibited Anti-Pattern: Unsynchronized Logical Slots without WAL Keep Limits Inducing Failover Loss and WAL Disk Saturation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Logical Replication Failover Slots & Memory Buffer Invariants - PostgreSQL 17 introduces native logical replication slot failover synchronization across physical standbys, requiring explicit failover flag enablement and coordinated synchronized_standby_slots configuration. Applications managing logical streaming consumers must guard against unconstrained WAL retention by enforcing max_slot_wal_keep_size alongside bounded logical_decoding_work_mem to prevent primary disk exhaustion and shared_buffers pollution. Failure to enforce these invariants leads to broken consumer streams post-failover or catastrophic WAL accumulation.
globs: src/db/**/*.ts, src/services/replication/**/*.ts, src/config/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-failover-slots-replication-buffers"
---

# PostgreSQL 17 Logical Replication Failover Slots & Memory Buffer Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unsynchronized Logical Slots without WAL Keep Limits Inducing Failover Loss and WAL Disk Saturation

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing PostgreSQL 17 logical replication and buffer pool invariants. You mandate the use of native Postgres 17 failover-enabled logical slots, physical standby synchronization tracking, bounded WAL slot retention, and memory-safe streaming decoding limits.

## Strict Guidelines
- Engineers MUST ALWAYS provision logical replication slots with `failover => true` in PostgreSQL 17 when interacting with high-availability standby clusters.
- Engineers MUST NOT allow logical replication slots to operate without an explicit, non-negative `max_slot_wal_keep_size` defined at the instance or system configuration level.
- Code orchestrating streaming replication MUST verify slot synchronization state in `pg_replication_slots` (`sync_state = 'ready'`) before initiating switchover or failover consumer reconnections.
- Services MUST configure `logical_decoding_work_mem` to prevent decoding processes from consuming unmonitored host memory or triggering unbuffered spill-to-disk thrashing during large transactions.
- Components NEVER execute raw unbounded batch streaming reads that bypass `shared_buffers` eviction thresholds or trigger connection pool saturation during replication backlog drains.

## Bad vs Good Implementations
### Postgres 17 Logical Slot Provisioning and Synchronization Validation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creates a logical replication slot using obsolete pre-PG17 semantics without failover enabled. If the primary fails over to a physical standby, the slot will not exist on the new primary, breaking streaming consumers. Additionally, it fails to verify WAL retention safety or set decoding memory parameters.
```typescript
import { Client } from 'pg';

export async function initConsumerReplicationSlot(client: Client, slotName: string): Promise<void> {
  // Flaw 1: Legacy slot creation without PG17 failover parameter
  // Flaw 2: No check for max_slot_wal_keep_size; risk of WAL disk exhaustion
  // Flaw 3: Does not bind logical_decoding_work_mem, risking unbounded backend process memory
  await client.query(
    `SELECT pg_create_logical_replication_slot($1, 'pgoutput')`,
    [slotName]
  );
}
```

#### Production Standard (Correct)
> **Standard**: Uses PostgreSQL 17's native `failover => true` keyword argument to ensure the logical slot synchronizes across physical standbys (`sync_replication_slots`). Enforces bounded memory allocation via `logical_decoding_work_mem` and validates that `max_slot_wal_keep_size` is non-negative to avoid unbounded disk growth.
```typescript
import { Client } from 'pg';

export interface ReplicationSlotConfig {
  slotName: string;
  plugin?: string;
  decodingWorkMemMb?: number;
}

export async function initConsumerReplicationSlot(
  client: Client,
  config: ReplicationSlotConfig
): Promise<{ slotName: string; consistentPoint: string }> {
  const plugin = config.plugin ?? 'pgoutput';
  const memMb = config.decodingWorkMemMb ?? 64;

  // 1. Enforce local process memory cap for streaming decoding
  await client.query(`SET LOCAL logical_decoding_work_mem = '${memMb}MB'`);

  // 2. Validate max_slot_wal_keep_size is not unbounded (-1) to protect disk
  const walKeepRes = await client.query(
    `SELECT setting FROM pg_settings WHERE name = 'max_slot_wal_keep_size'`
  );
  const walKeepSize = parseInt(walKeepRes.rows[0]?.setting ?? '-1', 10);
  if (walKeepSize === -1) {
    throw new Error('Unsafe replication setup: max_slot_wal_keep_size must be configured to prevent WAL exhaustion');
  }

  // 3. PostgreSQL 17 native failover slot creation with failover => true
  const slotRes = await client.query(
    `SELECT slot_name, lsn AS consistent_point 
     FROM pg_create_logical_replication_slot(
       slot_name => $1,
       plugin => $2,
       temporary => false,
       twophase => true,
       failover => true
     )`,
    [config.slotName, plugin]
  );

  return {
    slotName: slotRes.rows[0].slot_name,
    consistentPoint: slotRes.rows[0].consistent_point,
  };
}
```

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 Logical Replication Failover Slots & Memory Buffer Invariants

PostgreSQL 17 Logical Replication Failover Slots & Memory Buffer Invariants defines an industrial-grade behavioral standard for backend engineering. PostgreSQL 17 introduces native logical replication slot failover synchronization across physical standbys, requiring explicit failover flag enablement and coordinated synchronized_standby_slots configuration. Applications managing logical streaming consumers must guard against unconstrained WAL retention by enforcing max_slot_wal_keep_size alongside bounded logical_decoding_work_mem to prevent primary disk exhaustion and shared_buffers pollution. Failure to enforce these invariants leads to broken consumer streams post-failover or catastrophic WAL accumulation. 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 WAL Keep Limits Inducing Failover Loss and WAL Disk Saturation 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 WAL Keep Limits Inducing Failover Loss and WAL Disk SaturationCritical
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 provision logical replication slots with `failover => true` in PostgreSQL 17 when interacting with high-availability standby clusters.

Engineers MUST ALWAYS provision logical replication slots with `failover => true` in PostgreSQL 17 when interacting with high-availability standby clusters.

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 allow logical replication slots to operate without an explicit, non-negative `max_slot_wal_keep_size` defined at the instance or system configuration level.

Engineers MUST NOT allow logical replication slots to operate without an explicit, non-negative `max_slot_wal_keep_size` defined at the instance or system configuration level.

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 orchestrating streaming replication MUST verify slot synchronization state in `pg_replication_slots` (`sync_state = 'ready'`) before initiating switchover or failover consumer reconnections.

Code orchestrating streaming replication MUST verify slot synchronization state in `pg_replication_slots` (`sync_state = 'ready'`) before initiating switchover or failover consumer reconnections.

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` to prevent decoding processes from consuming unmonitored host memory or triggering unbuffered spill-to-disk thrashing during large transactions.

Services MUST configure `logical_decoding_work_mem` to prevent decoding processes from consuming unmonitored host memory or triggering unbuffered spill-to-disk thrashing during large transactions.

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 execute raw unbounded batch streaming reads that bypass `shared_buffers` eviction thresholds or trigger connection pool saturation during replication backlog drains.

Components NEVER execute raw unbounded batch streaming reads that bypass `shared_buffers` eviction thresholds or trigger connection pool saturation during replication backlog drains.

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

PostgreSQL 17 introduces native logical replication slot failover synchronization across physical standbys, requiring explicit failover flag enablement and coordinated synchronized_standby_slots configuration. Applications managing logical streaming consumers must guard against unconstrained WAL retention by enforcing max_slot_wal_keep_size alongside bounded logical_decoding_work_mem to prevent primary disk exhaustion and shared_buffers pollution. Failure to enforce these invariants leads to broken consumer streams post-failover or catastrophic WAL accumulation. 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 WAL Keep Limits Inducing Failover Loss and WAL Disk Saturation) 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