|
| 1 | +# PostgreSQL Integration Guide |
| 2 | + |
| 3 | +PostgreSQL is the recommended backend for **production, multi-instance, and |
| 4 | +high-concurrency** deployments — a real server with connection pooling and strong |
| 5 | +durability. For time-series-heavy workloads, see the |
| 6 | +[TimescaleDB guide](timescaledb.md), which reuses this same config. |
| 7 | + |
| 8 | +## Build |
| 9 | + |
| 10 | +PostgreSQL is behind a cargo feature (adds the sqlx Postgres driver): |
| 11 | + |
| 12 | +```bash |
| 13 | +cargo build -p reflow_tracing --features postgres |
| 14 | +# or every durable backend at once: |
| 15 | +cargo build -p reflow_tracing --features all-backends |
| 16 | +``` |
| 17 | + |
| 18 | +## Run a database |
| 19 | + |
| 20 | +```bash |
| 21 | +docker run -d --name reflow-pg \ |
| 22 | + -e POSTGRES_DB=traces -e POSTGRES_USER=reflow -e POSTGRES_PASSWORD=secret \ |
| 23 | + -p 5432:5432 postgres:16 |
| 24 | +``` |
| 25 | + |
| 26 | +## Configure |
| 27 | + |
| 28 | +Set `storage.backend` to `postgres` (or `postgresql`) and a `[storage.postgres]` |
| 29 | +block. All fields: |
| 30 | + |
| 31 | +```toml |
| 32 | +[storage] |
| 33 | +backend = "postgres" |
| 34 | + |
| 35 | +[storage.postgres] |
| 36 | +connection_url = "postgresql://reflow:secret@localhost:5432/traces" |
| 37 | +max_connections = 20 |
| 38 | +min_connections = 5 |
| 39 | +acquire_timeout_secs = 5 |
| 40 | +``` |
| 41 | + |
| 42 | +## Schema |
| 43 | + |
| 44 | +Created automatically on startup (the `traces` table + indexes) — no manual DDL. |
| 45 | +Each `FlowTrace` is one row: the full trace as a (zstd-compressed when large) |
| 46 | +JSON `BYTEA` blob, plus denormalized columns: |
| 47 | + |
| 48 | +| column | type | purpose | |
| 49 | +|---|---|---| |
| 50 | +| `trace_id` (PK) | `TEXT` | identity | |
| 51 | +| `flow_id`, `execution_id`, `status` | `TEXT` | query filters | |
| 52 | +| `start_time`, `end_time` | `BIGINT` | time-range queries | |
| 53 | +| `event_count` | `BIGINT` | stats without loading the blob | |
| 54 | +| `data`, `compressed`, `size_bytes` | `BYTEA`/`BOOL`/`BIGINT` | payload | |
| 55 | + |
| 56 | +Stores are **synchronous** with an `INSERT … ON CONFLICT (trace_id) DO UPDATE` |
| 57 | +upsert (idempotent re-finalize), and indexes cover `flow_id`, `execution_id`, |
| 58 | +`status`, `start_time`. |
| 59 | + |
| 60 | +## Verify |
| 61 | + |
| 62 | +```bash |
| 63 | +reflow_tracing # logs: "Initialized storage backend: postgres" |
| 64 | + |
| 65 | +psql "$CONN" -c '\dt' # traces |
| 66 | +psql "$CONN" -c 'SELECT count(*) FROM traces;' |
| 67 | +``` |
| 68 | + |
| 69 | +Run the bundled integration test against a live instance: |
| 70 | + |
| 71 | +```bash |
| 72 | +REFLOW_TEST_POSTGRES_URL="postgresql://reflow:secret@localhost:5432/traces" \ |
| 73 | + cargo test -p reflow_tracing --features postgres --test storage_backends \ |
| 74 | + postgres_store_query_delete_roundtrip |
| 75 | +``` |
| 76 | + |
| 77 | +## Operations |
| 78 | + |
| 79 | +- **Pooling**: size `max_connections` to your collector concurrency; `sqlx` |
| 80 | + manages the pool with `acquire_timeout_secs`. |
| 81 | +- **Retention**: schedule |
| 82 | + `DELETE FROM traces WHERE start_time < EXTRACT(EPOCH FROM now() - INTERVAL '30 days')`, |
| 83 | + or use [TimescaleDB](timescaledb.md) for native retention. |
| 84 | +- **Backups**: standard `pg_dump` / streaming replication / PITR. |
| 85 | +- **Scale**: read replicas, partitioning. If your queries are dominated by |
| 86 | + time ranges, TimescaleDB's hypertable is a better fit. |
| 87 | + |
| 88 | +## Troubleshooting |
| 89 | + |
| 90 | +- **`PostgreSQL backend not compiled in`** — rebuild with `--features postgres`. |
| 91 | +- **`PostgreSQL storage config missing`** — `backend = "postgres"` but no |
| 92 | + `[storage.postgres]` block. |
| 93 | +- **Connection refused / auth failed** — check `connection_url`, that the server |
| 94 | + is reachable, and credentials. |
0 commit comments