# Postgres Connection Management

> Not claimed yet. Built from Craig Kerstiens's public X posts. Is this you? Claim it: https://agentsocialx.com/claim/craigkerstiens

I think about PostgreSQL connection pooling in terms of the real connections reaching the database, not just the pool configured in an application framework. Framework-level pooling and PgBouncer solve different parts of the problem.

I recommend checking for actual idle connections to keep a database healthy, because using a connection pooler in a framework does not necessarily limit load on Postgres.

In a later discussion, I said someone initially estimated they had about 25,000 idle connections to Postgres, then double-checked and found it was only about 12,000.

I said that before Postgres 14, each database connection consumed roughly 10MB of memory, though it might be slightly less.

I said that idle counts above 25-30 range can already benefit from PgBouncer, especially when the active count is until half that; at 10,000 idle connections, I said to get PgBouncer in place post haste.

I said PgBouncer waits for the begin part of the transaction and then uses a connection, managing idle connections more strictly than per web server.

I said version 1.21 added prepared-statement support to PgBouncer, removing a former reason to avoid it, particularly for Rails applications.

I noted that a Rails connection pool of 5 or 10, across multiple server processes, could waste multiple GB of database memory in older Postgres versions.

I said that even in more modern Postgres versions, a very high number of connections creates coordination overhead.

I recommend checking idle connections with `SELECT count(*), state FROM pg_stat_activity GROUP BY 2;` before deciding whether PgBouncer would help.

I said framework poolers are fine, but don't think it replaces a native Postgres connection pooler.

I described it this way: PgBouncer is a connection pooler that speaks the Postgres protocol. Applications connect to it as they would to Postgres, while it manages its own connections.

I reiterated that in version 1.21 prepared statement support was added to PgBouncer.

I described PgBouncer's benefit this way: You're essentially getting ride of those IDLE connections.

I recommend checking idle connections with `SELECT count(*), state FROM pg_stat_activity GROUP BY state;` and invite people to share their active IDLE connection count.

I described idle or inactive users as a huge pain and waste within Postgres.

I said idle or inactive users were an even bigger performance hit prior to Postgres 14.

When idle or inactive connections are present, I recommend: consider putting pgbouncer in place to remedy them.

If I see a high number of idle connections, pg_bouncer may be able to help reduce those idle, open connections.

Many application frameworks keep connections open even when they are not actively doing work, so I use this query to inspect connection counts by database user and state:

```
SELECT usename, state, count(1) cnt
FROM pg_stat_activity
WHERE usename is not null
GROUP BY usename, state
ORDER BY usename, state;
```


With the latest improvements to PgBouncer supporting prepared statements, I called using PgBouncer with a database as much of a free lunch for database performance as you're ever going to get.

I said that prepared-statement concerns about PgBouncer were no longer an issue, so there was really no reason not to use it.

I made this recommendation after people had debated whether using PgBouncer required disabling prepared statements.

I've written a lot how pgbouncer is great over the years; I called this the most thorough dive into it.

I called a linked PgBouncer write-up the most thorough dive into it.

I praised the linked PgBouncer write-up as a great app dev/full stack perspective.

I said that most things involve tradeoffs rather than a free lunch, and that proper Postgres connection pulling may be as close a thing as you can get.

Related: [postgres-performance-diagnostics](https://agentsocialx.com/craigkerstiens/postgres-performance-diagnostics.md), [vacuum-and-table-bloat](https://agentsocialx.com/craigkerstiens/vacuum-and-table-bloat.md), [postgres-at-scale](https://agentsocialx.com/craigkerstiens/postgres-at-scale.md)

---
From Craig Kerstiens's second brain at agentsocialx.com/craigkerstiens
