ini sql 215 lines · 2 tabs

Connection pooling and configuration

Maria Garcia Feb 2026
2 tabs
; PgBouncer configuration file

[databases]
; Database connection strings
mydb = host=localhost port=5432 dbname=mydb
analytics = host=replica.example.com port=5432 dbname=mydb

; Fallback database
* = host=localhost port=5432

[pgbouncer]
; Listen on all interfaces
listen_addr = *
listen_port = 6432

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

; Pool mode
; session: Client stays connected to same server (most compatible)
; transaction: Server assigned per transaction (more efficient)
; statement: Server assigned per statement (most efficient, breaks some features)
pool_mode = transaction

; Connection limits
max_client_conn = 1000
default_pool_size = 25
min_pool_size = 10
reserve_pool_size = 5
reserve_pool_timeout = 3

; Server connection limits
server_lifetime = 3600
server_idle_timeout = 600
server_connect_timeout = 15

; Client connection limits
client_idle_timeout = 0
client_login_timeout = 60

; DNS
dns_max_ttl = 15
dns_zone_check_period = 0

; Logging
log_connections = 1
log_disconnections = 1
log_pooler_errors = 1

; Admin console
admin_users = admin
stats_users = stats

; Performance
max_packet_size = 2147483647
pkt_buf = 4096
listen_backlog = 128

; TLS/SSL
; server_tls_sslmode = prefer
; client_tls_sslmode = disable
2 files · ini, sql Explain with highlit

Connection pooling reuses database connections across requests. Creating connections is expensive—pooling amortizes overhead. I use PgBouncer for PostgreSQL, ProxySQL for MySQL. Session pooling maintains session state. Transaction pooling is more efficient but stateless. Statement pooling shares single connection. Pool sizing depends on workload—too few causes queuing, too many overwhelms database. Understanding max_connections limits prevents failures. Connection timeout configuration prevents hung connections. Idle connection cleanup frees resources. Health checks remove broken connections. Proper pooling enables thousands of app connections with hundreds of database connections. Load balancing distributes across read replicas. Connection pooling is essential for web applications at scale.


Related snips

sql
-- Simple function
CREATE OR REPLACE FUNCTION get_full_name(
  first_name VARCHAR,
  last_name VARCHAR
)
RETURNS VARCHAR AS $$

Stored procedures and functions in PostgreSQL

postgresql stored-procedures functions
by Maria Garcia 2 tabs
ruby
require "csv"

class PeopleCsvStream
  include Enumerable

  HEADERS = %w[id full_name email signed_up_at plan].freeze

Resilient CSV Export as a Streamed Response

rails performance streaming
by codesnips 3 tabs
ruby
Rails.application.configure do
  config.after_initialize do
    Bullet.enable = true
    Bullet.alert = false
    Bullet.bullet_logger = true
    Bullet.console = true

N+1 query detection with Bullet gem

rails performance activerecord
by Alex Kumar 2 tabs
ruby
# Vulnerable: user input is concatenated directly into SQL.
email = params[:email]
password = params[:password]

sql = "SELECT * FROM users WHERE email = '#{email}' AND password_hash = '#{password}'"
user = ActiveRecord::Base.connection.execute(sql).first

SQL injection prevention with unsafe and safe query patterns

sql-injection owasp database
by Kai Nakamura 3 tabs
ruby
json.array! @posts do |post|
  json.cache! ['v1', post], expires_in: 1.hour do
    json.id post.id
    json.title post.title
    json.excerpt post.excerpt
    json.published_at post.published_at

Fragment caching for expensive JSON serialization

rails caching performance
by Alex Kumar 1 tab
sql
-- EXPLAIN ANALYZE (actual execution statistics)
EXPLAIN ANALYZE
SELECT u.username, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'

Advanced query optimization techniques

database optimization query-performance
by Maria Garcia 2 tabs

Share this code

Here's the card — post it anywhere.

Connection pooling and configuration — share card
Link copied