feat(v2/postgres): add QueryExecMode config with SimpleProtocol default for PgBouncer
Context
Part of the LabKit v2 Artifact Registry follow-up work. Identified as a required change in the Artifact Registry PoC assessment.
Tracking issue: gitlab-org/quality/quality-engineering/team-tasks#4291 (closed) Parent epic: gitlab-org/quality#360
What's in this MR?
- Expose
QueryExecModeas a configuration field onpostgres.Config - Default to
pgx.QueryExecModeSimpleProtocol, which avoids server-side prepared statements that break under PgBouncer transaction pooling (the standard GitLab.com deployment) - Add
QueryExecMode()accessor method onClientfor observability and testing - Update PgBouncer documentation in
doc.goandREADME.mdto explain the default and how to override it
pgx defaults to QueryExecModeCacheStatement which uses PostgreSQL prepared statements. PgBouncer in transaction-pooling mode cannot track prepared statements across connections, causing prepared statement "..." already exists errors. The Container Registry already uses QueryExecModeSimpleProtocol to solve this.
Manual PgBouncer Verification
To verify locally that SimpleProtocol works through PgBouncer:
# 1. Start PostgreSQL + PgBouncer (transaction pooling mode)
docker run -d --name pg-test \
-e POSTGRES_PASSWORD=test -e POSTGRES_USER=postgres -e POSTGRES_DB=testdb \
-p 5432:5432 postgres:16-alpine
docker run -d --name pgbouncer-test --link pg-test \
-e DATABASE_URL="postgres://postgres:test@pg-test:5432/testdb" \
-e POOL_MODE=transaction \
-e AUTH_TYPE=plain \
-p 6432:5432 edoburu/pgbouncer:latest
# 2. Wait for services to be ready
until docker exec pg-test pg_isready -U postgres; do sleep 1; done
# 3. Verify SimpleProtocol works through PgBouncer
docker exec pg-test psql -U postgres -d testdb -c "SELECT 1+1"
docker exec -e PGPASSWORD=test pgbouncer-test psql -U postgres -d testdb -h 127.0.0.1 -p 5432 -c "SELECT 1+1" \
&& echo "PgBouncer connection OK" || echo "PgBouncer connection failed"
# 4. Cleanup
docker rm -f pg-test pgbouncer-test
Automated integration tests (PostgreSQL + PgBouncer CI services) are in the follow-up !364 (merged).
Related
Edited by Doug Barrett