---
id: w1/m22/t001
title: HA replicated CNPG cluster from the Database CR
worker: worker1
status: todo
estimate: 90m
depends_on: []
---

## Objective

Provision a replicated (HA) CNPG cluster when the `Database` CR requests high availability: a primary + one or more standbys with replication, spread across nodes. Single-instance DBs must stay single-instance (no behavior change).

## Context

- `docs/ADR009-postgresql-management.md`: `Database` CR → CNPG `Cluster`; today `highAvailabilityEnabled: false` (single instance).
- CNPG natively supports `instances: N` with sync/async replication + anti-affinity; m19's multi-node substrate (platform/tenant pools) is what makes spreading standbys real.
- Plans (m8/m17) — decide which plans allow HA (likely Standard+); gate accordingly.

## Steps

1. Add `spec.highAvailability` (+ replica count / sync mode) to the `Database` type in `lego/types/`.
2. Map it to CNPG `instances`, replication mode, and pod anti-affinity across nodes.
3. Keep single-instance the default; regenerate deepcopy/CRD.

## Files

- `lego/types/…` (Database HA fields)
- `lego/operator/internal/controller/…` (Database → CNPG Cluster HA)

## Acceptance criteria

- [ ] An HA `Database` yields a CNPG cluster with ≥2 instances (primary + standby) and replication healthy; a non-HA one is unchanged.
- [ ] `make test` green; deepcopy/CRD regenerated.

## Out of scope

- Failover verb (t002), replica URLs (t003), API/UI (t004/t005).
