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

Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention

Enforces publisher transactional boundary discipline and subscriber schema indexing to guarantee causal consistency under Postgres 17's parallel apply engine (streaming = parallel). Prevents cross-worker foreign-key deadlocks, commit-order stalls, and replication slot divergence during sustained high-throughput write bursts.

Prohibited Anti-Pattern: Splitting Interdependent Writes Across Uncoordinated Concurrent Transactions Causing Subscriber Parallel Apply Worker Deadlocks

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention - Enforces publisher transactional boundary discipline and subscriber schema indexing to guarantee causal consistency under Postgres 17's parallel apply engine (streaming = parallel). Prevents cross-worker foreign-key deadlocks, commit-order stalls, and replication slot divergence during sustained high-throughput write bursts.
globs: src/**/*.ts, src/**/*.sql, migrations/**/*.sql, src/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-logical-replication-parallel-apply-invariants"
---

# Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Splitting Interdependent Writes Across Uncoordinated Concurrent Transactions Causing Subscriber Parallel Apply Worker Deadlocks

## System Constraints
You are a Principal Software Architect enforcing zero-deadlock invariants for Postgres 17 logical replication under parallel apply mode (`streaming = parallel`). You must mandate strict publisher transactional cohesion, subscriber-side foreign key indexing, and deterministic write ordering to eliminate cross-apply-worker deadlocks and preserve causal consistency during sustained write bursts.

## Strict Guidelines
- Engineers MUST ALWAYS encapsulate mutually dependent parent-child mutations into the exact same publisher transaction envelope instead of emitting them across detached parallel transactions.
- Engineers MUST ALWAYS create explicit B-tree indexes on all foreign key referencing columns on subscriber databases to prevent parallel apply workers from acquiring sequential table-level share locks during constraint checks.
- Engineers MUST NOT issue cross-transaction circular relational dependencies (e.g., Tx1 mutating Parent A then Child B, while Tx2 mutates Parent B then Child A) that induce circular wait chains between Postgres 17 parallel apply workers.
- Subscriber schemas SHOULD define foreign key constraints as `DEFERRABLE INITIALLY DEFERRED` when high-throughput stream processing pipelines batch inter-entity writes that commit in high-contention worker pools.
- Services MUST sort batch entity mutations deterministically by primary key or natural compound key prior to issuing multi-row `INSERT` or `UPDATE` statements to prevent parallel streaming chunks from interleaving lock orders.

## Bad vs Good Implementations
### Transactional Ingestion Pipeline: Anti-Pattern vs Causal Order Safe Pattern

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Splitting parent 'orders' and child 'order_items' across detached concurrent transactions breaks causal ordering. Postgres 17 parallel apply workers assign transactions to different worker threads, leading to foreign key validation deadlocks and missing parent row errors.
```typescript
import { PoolClient } from 'pg';

interface OrderBatch {
  orderId: string;
  customerId: string;
  items: Array<{ itemId: string; quantity: number }>;
}

// ANTI-PATTERN: Emits parent order and child items in decoupled, concurrent transactions.
// Under Postgres 17 `streaming = parallel`, these commit blocks are processed concurrently
// by distinct parallel apply workers on the subscriber, triggering Foreign Key deadlocks
// or constraint violation race conditions when child items apply before the parent order.
export async function ingestOrdersAntiPattern(client: PoolClient, batch: OrderBatch[]): Promise<void> {
  await Promise.all(batch.map(async (order) => {
    // Separate transaction for parent
    await client.query('BEGIN');
    await client.query(
      'INSERT INTO orders (id, customer_id) VALUES ($1, $2)',
      [order.orderId, order.customerId]
    );
    await client.query('COMMIT');

    // Separate concurrent transaction for items
    await client.query('BEGIN');
    for (const item of order.items) {
      await client.query(
        'INSERT INTO order_items (order_id, item_id, quantity) VALUES ($1, $2, $3)',
        [order.orderId, item.itemId, item.quantity]
      );
    }
    await client.query('COMMIT');
  }));
}
```

#### Production Standard (Correct)
> **Standard**: Bundling the interdependent relational graph inside a single atomic transaction ensures the changes are decoded as an atomic transactional commit unit. Postgres 17 parallel apply workers process the entire hierarchy in the same apply worker, completely removing foreign key wait cycles.
```typescript
import { PoolClient } from 'pg';

interface OrderBatch {
  orderId: string;
  customerId: string;
  items: Array<{ itemId: string; quantity: number }>;
}

// PRODUCTION INVARIANT: Atomic transaction encapsulation with deterministic key sorting
// and unified CTE submission. Guarantees that parent-child relationships remain bound to a single
// logical replication transaction stream, fully eliminating cross-worker apply deadlocks.
export async function ingestOrdersDeterministic(client: PoolClient, batch: OrderBatch[]): Promise<void> {
  // 1. Sort parent orders deterministically by PK to avoid lock inversion
  const sortedBatch = [...batch].sort((a, b) => a.orderId.localeCompare(b.orderId));
  
  const orderIds: string[] = [];
  const customerIds: string[] = [];
  const itemOrderIds: string[] = [];
  const itemIds: string[] = [];
  const quantities: number[] = [];

  for (const order of sortedBatch) {
    orderIds.push(order.orderId);
    customerIds.push(order.customerId);
    
    // Sort child items deterministically per order
    const sortedItems = [...order.items].sort((a, b) => a.itemId.localeCompare(b.itemId));
    for (const item of sortedItems) {
      itemOrderIds.push(order.orderId);
      itemIds.push(item.itemId);
      quantities.push(item.quantity);
    }
  }

  await client.query('BEGIN');
  try {
    // Single atomic statement via CTE ensures unified logical decode record sequence
    const query = `
      WITH inserted_orders AS (
        INSERT INTO orders (id, customer_id)
        SELECT * FROM UNNEST($1::text[], $2::text[])
        ON CONFLICT (id) DO NOTHING
      )
      INSERT INTO order_items (order_id, item_id, quantity)
      SELECT * FROM UNNEST($3::text[], $4::text[], $5::int[])
      ON CONFLICT (order_id, item_id) DO UPDATE
        SET quantity = EXCLUDED.quantity;
    `;

    await client.query(query, [orderIds, customerIds, itemOrderIds, itemIds, quantities]);
    await client.query('COMMIT');
  } catch (error) {
    await client.query('ROLLBACK');
    throw error;
  }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention

Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention defines an industrial-grade behavioral standard for backend engineering. Enforces publisher transactional boundary discipline and subscriber schema indexing to guarantee causal consistency under Postgres 17's parallel apply engine (streaming = parallel). Prevents cross-worker foreign-key deadlocks, commit-order stalls, and replication slot divergence during sustained high-throughput write bursts. 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: Splitting Interdependent Writes Across Uncoordinated Concurrent Transactions Causing Subscriber Parallel Apply Worker Deadlocks 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 DriftSplitting Interdependent Writes Across Uncoordinated Concurrent Transactions Causing Subscriber Parallel Apply Worker DeadlocksCritical
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 encapsulate mutually dependent parent-child mutations into the exact same publisher transaction envelope instead of emitting them across detached parallel transactions.

Engineers MUST ALWAYS encapsulate mutually dependent parent-child mutations into the exact same publisher transaction envelope instead of emitting them across detached parallel 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.
2

Engineers MUST ALWAYS create explicit B-tree indexes on all foreign key referencing columns on subscriber databases to prevent parallel apply workers from acquiring sequential table-level share locks during constraint checks.

Engineers MUST ALWAYS create explicit B-tree indexes on all foreign key referencing columns on subscriber databases to prevent parallel apply workers from acquiring sequential table-level share locks during constraint checks.

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 NOT issue cross-transaction circular relational dependencies (e.g., Tx1 mutating Parent A then Child B, while Tx2 mutates Parent B then Child A) that induce circular wait chains between Postgres 17 parallel apply workers.

Engineers MUST NOT issue cross-transaction circular relational dependencies (e.g., Tx1 mutating Parent A then Child B, while Tx2 mutates Parent B then Child A) that induce circular wait chains between Postgres 17 parallel 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

Subscriber schemas SHOULD define foreign key constraints as `DEFERRABLE INITIALLY DEFERRED` when high-throughput stream processing pipelines batch inter-entity writes that commit in high-contention worker pools.

Subscriber schemas SHOULD define foreign key constraints as `DEFERRABLE INITIALLY DEFERRED` when high-throughput stream processing pipelines batch inter-entity writes that commit in high-contention worker pools.

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

Services MUST sort batch entity mutations deterministically by primary key or natural compound key prior to issuing multi-row `INSERT` or `UPDATE` statements to prevent parallel streaming chunks from interleaving lock orders.

Services MUST sort batch entity mutations deterministically by primary key or natural compound key prior to issuing multi-row `INSERT` or `UPDATE` statements to prevent parallel streaming chunks from interleaving lock orders.

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 Postgres 17 Logical Replication Parallel Apply Transaction Ordering & FK Deadlock Prevention critical for AI coding assistants?

Enforces publisher transactional boundary discipline and subscriber schema indexing to guarantee causal consistency under Postgres 17's parallel apply engine (streaming = parallel). Prevents cross-worker foreign-key deadlocks, commit-order stalls, and replication slot divergence during sustained high-throughput write bursts. 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 (Splitting Interdependent Writes Across Uncoordinated Concurrent Transactions Causing Subscriber Parallel Apply Worker Deadlocks) 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

Postgres