This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-parallel-apply-replication-invariant

PostgreSQL 17 Parallel Apply Logical Replication and CDC Invariant Guardrail

Enforces strict configuration and migration invariants for PostgreSQL 17 parallel apply logical replication workers across cross-region subscriber topologies. Guarantees transaction-order serialization without cascading serialization-failure deadlocks or subscriber replication slot unbounded bloat during high-concurrency ingestion. Eliminates single-worker apply bottlenecks while preserving foreign key dependency graph order and cross-table consistency.

Prohibited Anti-Pattern: Applying Unpartitioned Parallel Streaming Apply Without Serialization Order Guardrails or Worker Pool Bounds

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Parallel Apply Logical Replication and CDC Invariant Guardrail - Enforces strict configuration and migration invariants for PostgreSQL 17 parallel apply logical replication workers across cross-region subscriber topologies. Guarantees transaction-order serialization without cascading serialization-failure deadlocks or subscriber replication slot unbounded bloat during high-concurrency ingestion. Eliminates single-worker apply bottlenecks while preserving foreign key dependency graph order and cross-table consistency.
globs: src/database/**/*.ts, src/migrations/**/*.ts, src/replication/**/*.ts, scripts/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-parallel-apply-replication-invariant"
---

# PostgreSQL 17 Parallel Apply Logical Replication and CDC Invariant Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Applying Unpartitioned Parallel Streaming Apply Without Serialization Order Guardrails or Worker Pool Bounds

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing PostgreSQL 17 parallel apply logical replication constraints. You must prevent replication lag cascades, deadlock cycles caused by concurrent transactional apply workers, and slot invalidation during high-throughput cross-region change data capture (CDC). Enforce explicit subscriber worker pool dimensioning, dynamic conflict resolution policies, and transactional boundary isolation across all migration scripts and replication provisioning harnesses.

## Strict Guidelines
- Engineers MUST ALWAYS configure `streaming = 'parallel'` in conjunction with explicit `max_parallel_apply_workers_per_subscription` and bounded global `max_logical_replication_workers` settings on PG17 subscriber nodes.
- Engineers MUST NOT configure parallel apply without declaring explicit `origin = any` or `origin = none` settings to prevent split-brain write loops and recursive replication cycles across bi-directional or multi-region meshes.
- Code SHOULD enforce deterministic foreign key evaluation order and avoid distributed cross-table transaction interleaving that triggers serialization failures (`40001`) inside concurrent apply workers.
- Services MUST monitor `pg_stat_subscription_stats` and `pg_replication_slots` to automatically flag apply lag, worker exhaustions, and spill-to-disk write amplification on the subscriber.
- Components NEVER execute raw DDL updates on active logical replication target tables without pausing subscriber worker pools via `ALTER SUBSCRIPTION ... DISABLE` or using controlled maintenance locks.

## Bad vs Good Implementations
### Configuring PostgreSQL 17 Parallel Apply Subscription with Concurrency Control

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Defaulting to single-threaded sequential apply causes unbounded WAL buildup at the publisher and severe lag on the subscriber during write bursts. It omits worker pool constraints, failover slots, and parallel streaming controls.
```typescript
import { Client } from 'pg';

export async function setupSubscription(client: Client): Promise<void> {
  // Anti-pattern: Default single-worker sequential apply with streaming disabled
  // and unmanaged origin fallback causes massive lag during concurrent bulk ingestion
  await client.query(`
    CREATE SUBSCRIPTION regional_cdc_sub
    CONNECTION 'host=provider-db.internal port=5432 dbname=prod user=replicator password=secret'
    PUBLICATION regional_pub;
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Leverages PostgreSQL 17 parallel streaming apply mode (`streaming = 'parallel'`) with explicit worker boundaries, two-phase commit validation, and failover support to sustain linear CDC throughput without single-worker saturation.
```typescript
import { Client } from 'pg';

interface ReplicationSubscriptionConfig {
  subscriptionName: string;
  publicationName: string;
  connectionString: string;
  maxParallelWorkers: number;
}

export async function setupSubscription(
  client: Client,
  config: ReplicationSubscriptionConfig
): Promise<void> {
  // Invariant: PG17 parallel apply requires streaming='parallel' and explicit worker provisioning
  // Failover slot integration and origin scoping prevent cross-region split-brain and lag
  const query = `
    CREATE SUBSCRIPTION ${client.escapeIdentifier(config.subscriptionName)}
    CONNECTION ${client.escapeLiteral(config.connectionString)}
    PUBLICATION ${client.escapeIdentifier(config.publicationName)}
    WITH (
      copy_data = true,
      streaming = 'parallel',
      origin = 'none',
      failover = true,
      two_phase = on,
      max_parallel_apply_workers_per_subscription = ${Math.min(config.maxParallelWorkers, 8)}
    );
  `;
  await client.query(query);
}
```

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 Parallel Apply Logical Replication and CDC Invariant Guardrail

PostgreSQL 17 Parallel Apply Logical Replication and CDC Invariant Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict configuration and migration invariants for PostgreSQL 17 parallel apply logical replication workers across cross-region subscriber topologies. Guarantees transaction-order serialization without cascading serialization-failure deadlocks or subscriber replication slot unbounded bloat during high-concurrency ingestion. Eliminates single-worker apply bottlenecks while preserving foreign key dependency graph order and cross-table consistency. 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: Applying Unpartitioned Parallel Streaming Apply Without Serialization Order Guardrails or Worker Pool Bounds 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 DriftApplying Unpartitioned Parallel Streaming Apply Without Serialization Order Guardrails or Worker Pool BoundsCritical
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 configure `streaming = 'parallel'` in conjunction with explicit `max_parallel_apply_workers_per_subscription` and bounded global `max_logical_replication_workers` settings on PG17 subscriber nodes.

Engineers MUST ALWAYS configure `streaming = 'parallel'` in conjunction with explicit `max_parallel_apply_workers_per_subscription` and bounded global `max_logical_replication_workers` settings on PG17 subscriber nodes.

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 configure parallel apply without declaring explicit `origin = any` or `origin = none` settings to prevent split-brain write loops and recursive replication cycles across bi-directional or multi-region meshes.

Engineers MUST NOT configure parallel apply without declaring explicit `origin = any` or `origin = none` settings to prevent split-brain write loops and recursive replication cycles across bi-directional or multi-region meshes.

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 enforce deterministic foreign key evaluation order and avoid distributed cross-table transaction interleaving that triggers serialization failures (`40001`) inside concurrent apply workers.

Code SHOULD enforce deterministic foreign key evaluation order and avoid distributed cross-table transaction interleaving that triggers serialization failures (`40001`) inside concurrent apply workers.

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 `pg_stat_subscription_stats` and `pg_replication_slots` to automatically flag apply lag, worker exhaustions, and spill-to-disk write amplification on the subscriber.

Services MUST monitor `pg_stat_subscription_stats` and `pg_replication_slots` to automatically flag apply lag, worker exhaustions, and spill-to-disk write amplification on the subscriber.

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 DDL updates on active logical replication target tables without pausing subscriber worker pools via `ALTER SUBSCRIPTION ... DISABLE` or using controlled maintenance locks.

Components NEVER execute raw DDL updates on active logical replication target tables without pausing subscriber worker pools via `ALTER SUBSCRIPTION ... DISABLE` or using controlled maintenance locks.

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 Parallel Apply Logical Replication and CDC Invariant Guardrail critical for AI coding assistants?

Enforces strict configuration and migration invariants for PostgreSQL 17 parallel apply logical replication workers across cross-region subscriber topologies. Guarantees transaction-order serialization without cascading serialization-failure deadlocks or subscriber replication slot unbounded bloat during high-concurrency ingestion. Eliminates single-worker apply bottlenecks while preserving foreign key dependency graph order and cross-table consistency. 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 (Applying Unpartitioned Parallel Streaming Apply Without Serialization Order Guardrails or Worker Pool Bounds) 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