Skip to content

Commit b59e468

Browse files
committed
Add AI guidelines to CONTRIBUTING.md
Also add CLAUDE.md with guidance for Claude Code.
1 parent 9823ad4 commit b59e468

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

CLAUDE.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.

CONTRIBUTING.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ proposal. This will help to ensure your proposed change has a reasonable chance
1010
Adding a dependency is a big deal. While on occasion a new dependency may be accepted, the default answer to any change
1111
that adds a dependency is no.
1212

13+
## AI
14+
15+
Using AI is acceptable (not that it can really be stopped) under one the following conditions.
16+
17+
* AI was used, but you deeply understand the code and you can answer questions regarding your change. You are not going
18+
to answer questions with "I don't know", AI did it. You are not going to "answer" questions by relaying them to your
19+
agent. This is wasteful of the code reviewer's time.
20+
* AI was used to solve a problem without your deep understanding. This can still be a good starting point for a fix or
21+
feature. But you need to clearly state that this is an AI proposal. You should include additional information such as
22+
the AI used and what prompts were used. You should also be aware that large, complicated, or subtle changes may be
23+
rejected simply because the reviewer is not confident in a change that no human understands.
24+
1325
## Development Environment Setup
1426

1527
pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the `PGX_TEST_DATABASE`

0 commit comments

Comments
 (0)