;; =============================================================================
;; PgBouncer Configuration for GoodGo Platform
;; Docs: https://www.pgbouncer.org/config.html
;; =============================================================================

[databases]
;; Route all connections to the upstream PostgreSQL container.
;; AUTH_USER is handled via userlist.txt; DB credentials are injected at runtime
;; via environment variable substitution in the entrypoint.
* = host=postgres port=5432

[pgbouncer]
;; ── Listening ────────────────────────────────────────────────────────────────
listen_addr = 0.0.0.0
listen_port = 6432
unix_socket_dir =

;; ── Authentication ───────────────────────────────────────────────────────────
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt

;; ── Pool Mode ────────────────────────────────────────────────────────────────
;; "transaction" is recommended for short-lived, stateless web/API workloads.
;; Each server connection is returned to the pool after every transaction.
;; NOTE: session-level features (LISTEN/NOTIFY, prepared statements in older PG,
;; advisory locks held across transactions) will NOT work in this mode.
pool_mode = transaction

;; ── Pool Sizing ──────────────────────────────────────────────────────────────
;; max_client_conn  — total client connections PgBouncer will accept.
;; default_pool_size — server connections per user/database pair.
;; min_pool_size    — pre-warmed connections kept open even when idle.
;; reserve_pool_size — extra connections allowed when the pool is exhausted.
;; reserve_pool_timeout — seconds to wait before using reserve connections.
max_client_conn = 200
default_pool_size = 20
min_pool_size = 5
reserve_pool_size = 5
reserve_pool_timeout = 3

;; ── Timeouts ─────────────────────────────────────────────────────────────────
;; server_connect_timeout — abort if backend doesn't accept within N seconds.
;; server_idle_timeout    — close idle backend connections after N seconds.
;; server_lifetime        — recycle backend connections after N seconds.
;; client_idle_timeout    — disconnect idle clients after N seconds (0 = off).
;; query_timeout          — cancel queries running longer than N seconds (0 = off).
;; query_wait_timeout     — error if a client waits this long for a server.
server_connect_timeout = 15
server_idle_timeout = 600
server_lifetime = 3600
client_idle_timeout = 0
query_timeout = 0
query_wait_timeout = 120

;; ── Logging ──────────────────────────────────────────────────────────────────
log_connections = 1
log_disconnections = 1
log_pooler_errors = 1
stats_period = 60

;; ── Admin Console ────────────────────────────────────────────────────────────
admin_users = pgbouncer_admin
stats_users = pgbouncer_stats

;; ── TLS (disabled — traffic stays within Docker network) ─────────────────────
;; Uncomment and configure if PgBouncer is exposed outside the Docker network.
;; client_tls_sslmode = prefer
;; client_tls_key_file = /etc/pgbouncer/tls/server.key
;; client_tls_cert_file = /etc/pgbouncer/tls/server.crt
