Files
goodgo-platform/infra/pgbouncer/entrypoint.sh
Ho Ngoc Hai 05abbc5250 feat(infra): add PgBouncer connection pooling for production PostgreSQL
Introduces PgBouncer as a connection pooler between the API service and
PostgreSQL in docker-compose.prod.yml, reducing connection overhead and
improving concurrency under production load.

- Add PgBouncer service (edoburu/pgbouncer:1.23.1-p2) with transaction
  pool mode, max_client_conn=200, default_pool_size=20
- Route API DATABASE_URL through PgBouncer (port 6432), keep direct
  connection (DATABASE_URL_DIRECT) for Prisma migrations/introspection
- Create infra/pgbouncer/ config: pgbouncer.ini, userlist template,
  and entrypoint script with runtime env-var substitution
- Update prisma.config.ts to prefer DATABASE_URL_DIRECT for migrations
- Add K6 load test (e2e/load/pgbouncer-pool-test.js) with ramp-up to
  200 VUs, pool exhaustion detection, and p95 < 2s threshold
- Add PgBouncer env vars to .env.example

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-10 20:15:21 +07:00

32 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
# =============================================================================
# PgBouncer entrypoint — render userlist from environment variables and start.
# =============================================================================
set -eu
USERLIST_TEMPLATE="/etc/pgbouncer/userlist.txt.template"
USERLIST="/etc/pgbouncer/userlist.txt"
if [ -z "${DB_USER:-}" ] || [ -z "${DB_PASSWORD:-}" ]; then
echo "ERROR: DB_USER and DB_PASSWORD must be set" >&2
exit 1
fi
# Render userlist from template, substituting env vars
envsubst < "$USERLIST_TEMPLATE" > "$USERLIST"
chmod 600 "$USERLIST"
echo "PgBouncer userlist rendered for user: ${DB_USER}"
echo "Starting PgBouncer on port 6432 (pool_mode=transaction, pool_size=${PGBOUNCER_POOL_SIZE:-20})..."
# Override pool settings via env vars if provided
if [ -n "${PGBOUNCER_POOL_SIZE:-}" ]; then
sed -i "s/^default_pool_size = .*/default_pool_size = ${PGBOUNCER_POOL_SIZE}/" /etc/pgbouncer/pgbouncer.ini
fi
if [ -n "${PGBOUNCER_MAX_CLIENT_CONN:-}" ]; then
sed -i "s/^max_client_conn = .*/max_client_conn = ${PGBOUNCER_MAX_CLIENT_CONN}/" /etc/pgbouncer/pgbouncer.ini
fi
exec pgbouncer /etc/pgbouncer/pgbouncer.ini