# Build from source

> Toolchain, build targets and test targets.

---

LLMS index: [llms.txt](/llms.txt)

---

Building `pgredis` from source compiles both the `pg_redis` PostgreSQL C extension and the `pgredis` Go gateway executable.

## Toolchain {#toolchain}

{.fields}
- `PostgreSQL 18`
  Server binaries (`initdb`, `pg_ctl`, `postgres`, `psql`), `pg_config`, and server development headers (`postgresql-server-dev-18` on Debian/Ubuntu).
- `Go 1.27.4`
  Required to build the gateway and test harnesses. `CGO_ENABLED=1` is mandatory because the gateway uses libc host resolution for `MIGRATE`.
- `C11 compiler`
  `clang` or `gcc` compatible with the PostgreSQL PGXS build flags reported by `pg_config`.
- `Python 3`
  Used by `scripts/prepare_pgx.py` (which applies the local read-budget patch to the vendored `pgx` cache) and by the reference/application test runners.

The gateway and both Go test modules require Go 1.27.4. Current source uses `pgx` v5.12.0 with the project's read-budget and password-preparation patch; the `v0.1.0-rc1` packages use v5.11.0. The Go client tests use `go-redis` v9.23.0.

## Build the gateway and extension {#build}

Run the gateway build from the repository root, then compile and install the C extension with PGXS:

```bash
make build
make -C extension PG_CONFIG=/path/to/pg_config
sudo make -C extension PG_CONFIG=/path/to/pg_config install
```

What each step produces:

- `make build` runs `python3 scripts/prepare_pgx.py`, verifies `CGO_ENABLED=1`, executes `go generate ./internal/gateway` (which generates the command dispatch table from `spec/commands.json`), and writes the gateway binary to `build/pgredis`.
- `make -C extension` compiles `extension/pg_redis.c` into `extension/pg_redis.so` against the target PostgreSQL 18 headers.
- `make -C extension install` copies `pg_redis.so`, `pg_redis.control`, and `pg_redis--*.sql` (`0.1` through `0.54`) into the PostgreSQL installation directories reported by `PG_CONFIG`.

## Test targets {#tests}

{.wide}
| Target | Command | Scope |
| --- | --- | --- |
| `make test` | `go test ./cmd/... ./internal/... ./tests/integration/... ./tests/load/... ./tests/probes/...` | Fast Go unit tests (RESP parser/writer, command table generation, SQL error probes) without a running PostgreSQL cluster. |
| `make test-integration` | `PGREDIS_INTEGRATION=1 go test -count=1 -v ...` | Initializes a temporary PostgreSQL 18 cluster, installs `pg_redis`, and runs end-to-end gateway, SQL rollback, Pub/Sub, Streams, Blocking, and Tracking tests. |
| `make test-reference` | `python3 tests/reference/prepare.py && python3 tests/reference/run.py --mode external --variant prd` | Builds the pinned Redis 7.2.4 reference binary (`5221b82623c`) and runs the upstream Tcl test harness against `pgredis` (requires `tclsh`). |
{.wide .expand}

## Repository layout {#layout}

```filetree
pgredis/
├── cmd/pgredis/              # Gateway CLI entry point and flags
├── internal/
│   ├── gateway/              # Session router, tx/watch state, blocking, Pub/Sub, MIGRATE
│   └── resp/                 # Binary-safe RESP2 and RESP3 parser and serializer
├── extension/
│   ├── pg_redis.c            # C dispatcher, SPI plans, shared ring, background worker
│   ├── pg_redis.control      # Extension metadata (default_version = '0.54')
│   └── pg_redis--*.sql       # Base schema (0.1) and upgrade scripts through 0.54
├── patches/                  # Local pgx v5.12.0 read-budget patch and earlier versions
├── spec/commands.json        # Redis 7.2.4 command specification (392 commands)
├── examples/outbox.sql       # Transactional outbox SQL example
└── tests/                    # Integration, reference, client, application, and load suites
```

---

Backlinks:

- [Operations](/docs/redis/operations/)
- [Install](/docs/redis/operations/install/)
