Refusing clients and pooling them are different jobs
Practice uses real PostgreSQL clients to exhaust six slots and then demonstrates a two-connection role cap. PgBouncer is absent, so configuring and monitoring a running pool remains reference-only.
Each PostgreSQL connection has a backend process and consumes resources. Capture concurrent client logs: a later successful monitoring query does not erase earlier rejections. CONNECTION LIMIT isolates a non-superuser role but neither queues clients nor reuses sessions.
The reference configuration maps beer_db to port 5432 and accepts clients on 6432. The authentication file needs real credentials outside this lab. max_client_conn bounds clients; default_pool_size applies per user/database pair, so multiple pools can exceed one pool's setting.
Session pooling retains a backend until disconnect. Transaction pooling returns it at transaction end; statement pooling disallows multi-statement transactions. Session state such as SQL PREPARE, LISTEN, or session advisory locks cannot simply follow a client across transaction-pooled backends.
Did you know? Prepared statements are not one compatibility category: modern PgBouncer can track protocol-level prepared plans with nonzero max_prepared_statements, while SQL PREPARE remains incompatible with transaction pooling. PgBouncer feature map.
Key concepts
pool_mode: session vs transaction vs statement
Session pooling keeps a backend until disconnect. Transaction pooling returns it at transaction end; statement pooling disallows multi-statement transactions. Review session features individually: modern PgBouncer supports tracked protocol-level prepared plans with suitable configuration, while SQL PREPARE is still session-bound.
ALTER ROLE ... CONNECTION LIMIT
A per-role cap on concurrent connections, enforced directly by PostgreSQL itself with no external tool required. It does not multiplex connections the way PgBouncer does — it simply refuses a role's connection attempts past the limit — but it is a genuine, immediately available safeguard against one role's runaway connection count starving every other role on the same cluster.