C.W.K.
Stream
Lesson 08 of 10 · published

Connection Pooling 과 pgBouncer

~12 min · apps, pooling

Level 0스키마 새싹
0 XP0/86 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Connection 이 공짜 아님

각 Postgres 백엔드 connection 이 프로세스 fork + ~10MB RAM 소비. 프로덕션 Postgres 가 보통 connection 100-200 으로 cap — 더 높이면 도움보다 해. 모던 웹 스택 (serverless function, Lambda, Vercel) 이 수천 동시 프로세스 spawn 가능, 각자 자체 connection 원함. 수학 안 맞음.

Connection pooler

Connection pooler 가 앱과 Postgres 사이에 위치. 앱 프로세스가 pooler 에 연결 (싸); pooler 가 작은 진짜 Postgres connection 풀 유지 + 트래픽 multiplex. pgBouncer 가 정전 도구. 대부분 managed 제공자가 pooler ship.

Pool 모드

  • Session pooling: connection 이 전체 client 세션 동안 잡힘. 제한된 multiplexing; 가장 안전한 semantics.
  • Transaction pooling: connection 이 트랜잭션 동안만 잡힘. 훨씬 좋은 multiplexing; session-level 기능 잃음 (SET, prepared statement, LISTEN). Serverless 의 default.
  • Statement pooling: 각 statement 후 connection 해제. 가장 공격적; 트랜잭션 잃음. 거의 안 씀.

Code

Pool 사이즈 수학·text
목표: max_connections=100 인 Postgres 에 1000 동시 앱 프로세스 지원.

직접 connection: 1000 / 100 = 10× 용량 초과 → connection storm, 에러.

pgBouncer (transaction 모드) 로:
  - 1000 앱 프로세스가 pgBouncer 에 연결 (싸).
  - pgBouncer 가 진짜 Postgres connection 100 유지.
  - 평균 트랜잭션 10ms → 효과적 처리량 100*1000/10 = 초당 10,000 tx.
앱측 pool config·python
# psycopg pool — 앱 레벨, pgBouncer 보완
from psycopg_pool import ConnectionPool

pool = ConnectionPool(
    "postgresql://app:****@pgbouncer:6432/mydb",
    min_size=4,
    max_size=20,
    timeout=10.0,
)

with pool.connection() as conn:
    conn.execute("SELECT 1")
Supabase pgBouncer URL·bash
# 직접 connection (port 5432) — 진짜 Postgres connection 사용
postgresql://postgres.PROJECT:****@aws-0-region.pooler.supabase.com:5432/postgres

# Transaction pooler (port 6543) — 연결 싸, transaction-mode
postgresql://postgres.PROJECT:****@aws-0-region.pooler.supabase.com:6543/postgres
# Serverless / Lambda / Vercel 에 사용.

External links

Exercise

프로젝트에서 pooler 통하는지 직접 연결인지 식별. 직접이면 동시 실행 가능한 앱 프로세스 카운트 — max_connections 안 fit? 안 되면 pgBouncer (또는 managed 동등) 로 마이그레이션 계획.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.