|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +pgx is a PostgreSQL driver and toolkit for Go (`github.com/jackc/pgx/v5`). It provides both a native PostgreSQL interface and a `database/sql` compatible driver. Requires Go 1.24+ and supports PostgreSQL 14+ and CockroachDB. |
| 8 | + |
| 9 | +## Build & Test Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Run all tests (requires PGX_TEST_DATABASE to be set) |
| 13 | +go test ./... |
| 14 | + |
| 15 | +# Run a specific test |
| 16 | +go test -run TestFunctionName ./... |
| 17 | + |
| 18 | +# Run tests for a specific package |
| 19 | +go test ./pgconn/... |
| 20 | + |
| 21 | +# Run tests with race detector |
| 22 | +go test -race ./... |
| 23 | + |
| 24 | +# DevContainer: run tests against specific PostgreSQL versions |
| 25 | +./test.sh pg18 # Default: PostgreSQL 18 |
| 26 | +./test.sh pg16 -run TestConnect # Specific test against PG16 |
| 27 | +./test.sh crdb # CockroachDB |
| 28 | +./test.sh all # All targets (pg14-18 + crdb) |
| 29 | + |
| 30 | +# Format |
| 31 | +goimports -w . |
| 32 | + |
| 33 | +# Lint |
| 34 | +golangci-lint run ./... |
| 35 | +``` |
| 36 | + |
| 37 | +## Test Database Setup |
| 38 | + |
| 39 | +Tests require `PGX_TEST_DATABASE` environment variable. In the devcontainer, `test.sh` handles this. For local development: |
| 40 | + |
| 41 | +```bash |
| 42 | +export PGX_TEST_DATABASE="host=localhost user=postgres password=postgres dbname=pgx_test" |
| 43 | +``` |
| 44 | + |
| 45 | +The test database needs extensions: `hstore`, `ltree`, and a `uint64` domain. See `testsetup/postgresql_setup.sql` for full setup. Many tests are skipped unless additional `PGX_TEST_*` env vars are set (for TLS, SCRAM, MD5, unix socket, PgBouncer testing). |
| 46 | + |
| 47 | +## Architecture |
| 48 | + |
| 49 | +The codebase is a layered architecture, bottom-up: |
| 50 | + |
| 51 | +- **pgproto3/** — PostgreSQL wire protocol v3 encoder/decoder. Defines `FrontendMessage` and `BackendMessage` types for every protocol message. |
| 52 | +- **pgconn/** — Low-level connection layer (roughly libpq-equivalent). Handles authentication, TLS, query execution, COPY protocol, and notifications. `PgConn` is the core type. |
| 53 | +- **pgx** (root package) — High-level query interface built on `pgconn`. Provides `Conn`, `Rows`, `Tx`, `Batch`, `CopyFrom`, and generic helpers like `CollectRows`/`ForEachRow`. Includes automatic statement caching (LRU). |
| 54 | +- **pgtype/** — Type system mapping between Go and PostgreSQL types (70+ types). Key interfaces: `Codec`, `Type`, `TypeMap`. Custom types (enums, composites, domains) are registered through `TypeMap`. |
| 55 | +- **pgxpool/** — Concurrency-safe connection pool built on `puddle/v2`. `Pool` is the main type; wraps `pgx.Conn`. |
| 56 | +- **stdlib/** — `database/sql` compatibility adapter. |
| 57 | + |
| 58 | +Supporting packages: |
| 59 | +- **internal/stmtcache/** — Prepared statement cache with LRU eviction |
| 60 | +- **internal/sanitize/** — SQL query sanitization |
| 61 | +- **tracelog/** — Logging adapter that implements tracer interfaces |
| 62 | +- **multitracer/** — Composes multiple tracers into one |
| 63 | +- **pgxtest/** — Test helpers for running tests across connection types |
| 64 | + |
| 65 | +## Key Design Conventions |
| 66 | + |
| 67 | +- **Semantic versioning** — strictly followed. Do not break the public API (no removing or renaming exported types, functions, methods, or fields; no changing function signatures). |
| 68 | +- **Minimal dependencies** — adding new dependencies is strongly discouraged (see CONTRIBUTING.md). |
| 69 | +- **Context-based** — all blocking operations take `context.Context`. |
| 70 | +- **Tracer interfaces** — observability via `QueryTracer`, `BatchTracer`, `CopyFromTracer`, `PrepareTracer` on `ConnConfig.Tracer`. |
| 71 | +- **Formatting** — use `goimports -w .` to format code. CI checks formatting via `gofmt -l -s -w . && git diff --exit-code`. `gofumpt` with extra rules is also enforced via `golangci-lint`. |
| 72 | +- **Linters** — `govet` and `ineffassign` only (configured in `.golangci.yml`). |
| 73 | +- **CI matrix** — tests run against Go 1.24/1.25 × PostgreSQL 14-18 + CockroachDB, on Linux and Windows. Race detector enabled on Linux only. |
0 commit comments