Metadata-Version: 2.4
Name: netherframe.backends.postgresql
Version: 0.5.0
Summary: PostgreSQL backend implementation for Netherframe
Author: Sourabh Cheedella
Author-email: Sourabh Cheedella <cheedella.sourabh@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Systems Administration
Requires-Dist: anyio>=4.16.0
Requires-Dist: netherframe-core>=0.14.0,<1.0.0
Requires-Dist: asyncpg>=0.32.0
Requires-Dist: pytest>=9.2.0 ; extra == 'dev'
Requires-Dist: testcontainers>=4.15.0 ; extra == 'dev'
Requires-Python: >=3.15
Project-URL: Homepage, https://netherframe.arsenal.sh
Project-URL: Repository, https://github.com/arsenal-sh/netherframe
Provides-Extra: dev
Description-Content-Type: text/markdown

# Netherframe PostgreSQL Backend

PostgreSQL backend implementation for Netherframe: state in PostgreSQL, with a state lock and conditional writes for hybrid execution.

## Features

- **PostgreSQL State Storage**: Store infrastructure state in PostgreSQL with JSONB columns for optimal querying
- **State Locking**: one lock per state (`state_id`), held by a hybrid run from planning through approval to the end of apply (see [Locking](#locking))
- **ACID Transactions**: Leverage PostgreSQL's ACID guarantees for state consistency
- **Connection Pooling**: Efficient connection management for high-performance operations
- **Hybrid Mode Support**: Works with both synthesis mode (Terraform) and hybrid execution mode
- **Conditional Writes**: a write replaces only the state version its run read (the state row is locked `FOR UPDATE` while compared and written)
- **Force-Unlock and Checkpoints**: a crashed run's lock is released by its ID (`nf state force-unlock`); pre-apply checkpoints are rows of their own

## Installation

```bash
pip install netherframe.backends.postgresql
```

## Configuration

```python
from nf.backends.postgresql import PostgreSQLBackend

# Basic configuration
backend = PostgreSQLBackend(
    nf_scope=stack,
    host="localhost",
    port=5432,
    database="netherframe_state",
    username="nf_user",
    password="your_password",
)

# Advanced configuration with SSL and custom schema
backend = PostgreSQLBackend(
    nf_scope=stack,
    host="db.example.com",
    port=5432,
    database="infrastructure",
    username="nf_user",
    password="your_password",
    schema="netherframe",
    ssl_mode="require",
    table_prefix="nf_",
    pool_size=10,
    max_overflow=20,
)
```

## Schema

The backend creates the following tables:

- `nf_states`: Main state storage with JSONB data
- `nf_locks`: State locks, one per `state_id`
- `nf_resource_locks`: Locks taken through the resource-lock API (see below); hybrid execution does not use them

## Locking

Hybrid execution locks the whole state, as Terraform does; it does not lock
individual resources:

- `apply` takes the lock on its `state_id` before planning and holds it through
  the approval prompt to the end of the apply; `plan` holds it while planning.
- A second run on the same state does not wait: it fails to take the lock
  (`Failed to acquire state lock`) and logs who holds it. Concurrent applies on
  one state are therefore serialized, never interleaved.
- Every state write is conditional: it replaces only the state version
  (`serial` and `lineage`) the run read, with the row locked `FOR UPDATE` while
  it is compared and written. A saved plan is rejected if state changed since it
  was created.
- The lock expires `lock_timeout` seconds (default 300) after its holder last
  renewed it; a running holder renews it in the background. `nf state
  force-unlock` releases the lock of a crashed run at once.

Changes that must be applied independently, at the same time, need separate
states: give each independently deployed app its own `state_id`.

### Resource-Lock API

The backend also has `acquire_resource_lock`, `release_resource_lock`,
`release_all_resource_locks` and `resource_lock_context`: shared and exclusive
locks on a resource id, with timeouts, stored in `nf_resource_locks` (see
`example_usage.py`). Netherframe's hybrid execution never calls them, and they
do not let two runs apply to one state at once. They are only useful to
coordinate your own tooling.

## Settings

Each connection setting is a flag of `nf`, else a key of `[tool.nf.config]` in
`pyproject.toml`, else the backend's argument, else the default. Nothing is read from the
environment.

- `--postgresql-host` / `postgresql_host`: Database host (default: localhost)
- `--postgresql-port` / `postgresql_port`: Database port (default: 5432)
- `--postgresql-database` / `postgresql_database`: Database name
- `--postgresql-username` / `postgresql_username`: Database username
- `--postgresql-ssl-mode` / `postgresql_ssl_mode`: SSL mode (disable, allow, prefer, require, verify-ca, verify-full)
- `--postgresql-password-file` / `postgresql_password_file`: File holding the database password
  (a mounted secret). The password is never a flag. In code, `PostgreSQLBackend` also takes
  `password=`, and `PostgreSQLGraphBackend` takes `password_file=` or `password_source=` (a
  callable).

```toml
[tool.nf.config]
postgresql_host = "db.internal"
postgresql_database = "netherframe"
postgresql_username = "nf_apply"
postgresql_password_file = "/run/secrets/postgresql-password"
```

See the [Settings reference](../../docs/reference/settings.md).
