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

PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard

Mandates deterministic foreign key dependency sequencing, deferrable constraint enforcement, and automated LSN conflict quarantining when utilizing PostgreSQL 17 parallel apply workers. Prevents replication worker crash loops, cross-worker deadlocks, and ingestion pipeline stalls during high-throughput out-of-order transactional streaming.

Prohibited Anti-Pattern: Configuring parallel apply logical replication over non-deferrable relational hierarchies without automated conflict quarantine

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard - Mandates deterministic foreign key dependency sequencing, deferrable constraint enforcement, and automated LSN conflict quarantining when utilizing PostgreSQL 17 parallel apply workers. Prevents replication worker crash loops, cross-worker deadlocks, and ingestion pipeline stalls during high-throughput out-of-order transactional streaming.
globs: src/database/**/*.ts, src/replication/**/*.ts, src/services/cdc/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-logical-replication-parallel-apply-quarantine-standard"
---

# PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Configuring parallel apply logical replication over non-deferrable relational hierarchies without automated conflict quarantine

## System Constraints
You are a Principal Software Architect enforcing the PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard. Mandate that all logical replication target schemas, migration pipelines, and subscription management services account for concurrent out-of-order transaction apply. Enforce deferrable foreign key constraints, explicit subscription streaming parameters, automated conflict logging via pg_stat_subscription_stats, and deterministic dead-letter quarantine skipping using ALTER SUBSCRIPTION ... SKIP to eliminate subscription worker deadlocks and halt states.

## Strict Guidelines
- Engineers MUST ALWAYS define foreign key constraints as DEFERRABLE INITIALLY DEFERRED on target subscriber tables when streaming = 'parallel' is configured.
- Engineers MUST NOT configure subscriptions with streaming = 'parallel' without an active monitoring and quarantine handler connected to pg_stat_subscription_stats.
- Code SHOULD enforce max_parallel_apply_workers_per_subscription based on strict partition boundaries to reduce concurrent apply worker interleaving on coupled tables.
- Services MUST capture replication apply errors (such as confl_insert_exists or confl_update_missing) and route failing transactional LSNs into an audited dead-letter table via ALTER SUBSCRIPTION ... SKIP (lsn = ...).
- Components NEVER execute concurrent cross-table mutations without explicit transactional grouping when published to parallel apply subscriber topologies.

## Bad vs Good Implementations
### Subscriber Setup & Conflict Quarantine Orchestrator

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating standard IMMEDIATE foreign key constraints combined with PostgreSQL parallel apply causes concurrent apply workers to process child records before parent records commit. This halts logical replication with constraint violation errors, requiring manual intervention without any automated quarantine path.
```typescript
import { Client } from 'pg';

export async function setupSubscription(client: Client): Promise<void> {
  // Anti-Pattern: Non-deferrable FKs with unquarantined parallel apply
  await client.query(`
    CREATE TABLE orders (
      id UUID PRIMARY KEY,
      customer_id UUID NOT NULL REFERENCES customers(id)
    );
  `);

  // When streaming = parallel is set, worker 1 might apply 'orders' before worker 2 applies 'customers'
  // This causes immediate worker crash loops and replica stagnation
  await client.query(`
    CREATE SUBSCRIPTION order_sub
    CONNECTION 'host=provider dbname=app user=rep_user password=secret'
    PUBLICATION order_pub
    WITH (streaming = 'parallel', copy_data = false);
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Defines foreign keys as DEFERRABLE INITIALLY DEFERRED to tolerate interleaved commits from parallel workers. Inspects PostgreSQL 17 pg_stat_subscription_stats to detect conflict anomalies and leverages ALTER SUBSCRIPTION ... SKIP alongside an atomic dead-letter table write to prevent worker crash loops.
```typescript
import { Client } from 'pg';

interface ReplicationConflictRecord {
  subname: string;
  lastErrorLsn: string;
  lastErrorMessage: string;
}

export async function setupResilientSubscription(client: Client): Promise<void> {
  // Invariant 1: Ensure FK constraints are deferrable to end of transaction
  await client.query(`
    CREATE TABLE IF NOT EXISTS orders (
      id UUID PRIMARY KEY,
      customer_id UUID NOT NULL,
      CONSTRAINT fk_orders_customer 
        FOREIGN KEY (customer_id) 
        REFERENCES customers(id) 
        DEFERRABLE INITIALLY DEFERRED
    );
  `);

  // Invariant 2: Configure bounded parallel apply with explicit error logging
  await client.query(`
    CREATE SUBSCRIPTION order_sub
    CONNECTION 'host=provider dbname=app user=rep_user password=secret'
    PUBLICATION order_pub
    WITH (
      streaming = 'parallel',
      apply_error_callback = true,
      copy_data = false
    );
  `);
}

export async function evaluateAndQuarantineConflict(
  client: Client,
  subName: string
): Promise<boolean> {
  // Invariant 3: Inspect PG17 subscription stats for apply conflict flags
  const statsQuery = `
    SELECT 
      subname,
      last_error_lsn::text AS "lastErrorLsn",
      last_error_message AS "lastErrorMessage"
    FROM pg_stat_subscription_stats
    WHERE subname = $1 AND (confl_insert_exists > 0 OR confl_update_missing > 0);
  `;
  const { rows } = await client.query<ReplicationConflictRecord>(statsQuery, [subName]);

  if (rows.length === 0 || !rows[0].lastErrorLsn) {
    return false;
  }

  const conflict = rows[0];
  await client.query('BEGIN');
  try {
    // Invariant 4: Quarantine the offending transaction before advancing the stream
    await client.query(`
      INSERT INTO replication_dead_letter_queue (subscription_name, failed_lsn, error_detail, quarantined_at)
      VALUES ($1, $2::pg_lsn, $3, NOW());
    `, [conflict.subname, conflict.lastErrorLsn, conflict.lastErrorMessage]);

    // Invariant 5: Safely skip the deadlocked LSN to resume parallel apply ingestion
    await client.query(`ALTER SUBSCRIPTION ${subName} SKIP (lsn = '${conflict.lastErrorLsn}');`);
    await client.query('COMMIT');
    return true;
  } catch (err) {
    await client.query('ROLLBACK');
    throw err;
  }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard

PostgreSQL 17 Logical Replication Parallel Apply Conflict Quarantine Standard defines an industrial-grade behavioral standard for backend engineering. Mandates deterministic foreign key dependency sequencing, deferrable constraint enforcement, and automated LSN conflict quarantining when utilizing PostgreSQL 17 parallel apply workers. Prevents replication worker crash loops, cross-worker deadlocks, and ingestion pipeline stalls during high-throughput out-of-order transactional streaming. 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: Configuring parallel apply logical replication over non-deferrable relational hierarchies without automated conflict quarantine 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 DriftConfiguring parallel apply logical replication over non-deferrable relational hierarchies without automated conflict quarantineCritical
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 define foreign key constraints as DEFERRABLE INITIALLY DEFERRED on target subscriber tables when streaming = 'parallel' is configured.

Engineers MUST ALWAYS define foreign key constraints as DEFERRABLE INITIALLY DEFERRED on target subscriber tables when streaming = 'parallel' is configured.

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 subscriptions with streaming = 'parallel' without an active monitoring and quarantine handler connected to pg_stat_subscription_stats.

Engineers MUST NOT configure subscriptions with streaming = 'parallel' without an active monitoring and quarantine handler connected to pg_stat_subscription_stats.

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 max_parallel_apply_workers_per_subscription based on strict partition boundaries to reduce concurrent apply worker interleaving on coupled tables.

Code SHOULD enforce max_parallel_apply_workers_per_subscription based on strict partition boundaries to reduce concurrent apply worker interleaving on coupled tables.

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 capture replication apply errors (such as confl_insert_exists or confl_update_missing) and route failing transactional LSNs into an audited dead-letter table via ALTER SUBSCRIPTION ... SKIP (lsn = ...).

Services MUST capture replication apply errors (such as confl_insert_exists or confl_update_missing) and route failing transactional LSNs into an audited dead-letter table via ALTER SUBSCRIPTION ... SKIP (lsn = ...).

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 concurrent cross-table mutations without explicit transactional grouping when published to parallel apply subscriber topologies.

Components NEVER execute concurrent cross-table mutations without explicit transactional grouping when published to parallel apply subscriber topologies.

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 Parallel Apply Conflict Quarantine Standard critical for AI coding assistants?

Mandates deterministic foreign key dependency sequencing, deferrable constraint enforcement, and automated LSN conflict quarantining when utilizing PostgreSQL 17 parallel apply workers. Prevents replication worker crash loops, cross-worker deadlocks, and ingestion pipeline stalls during high-throughput out-of-order transactional streaming. 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 (Configuring parallel apply logical replication over non-deferrable relational hierarchies without automated conflict quarantine) 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