Postgres turned 30 this year, and the same Postgres mistakes keep showing up on teams of every size. A veteran who has worked on the database for 22 years walked through five of them at the NYC Postgres Meetup. Each works fine on a laptop, shows up in production metrics, and looks harmless at first.
Connecting anew on every request
Each Postgres connection gets its own server process. Setting one up involves forking, allocating memory, authenticating and warming a catalog cache, all before the first row comes back. For a query that takes 5 ms, roughly 2 ms can go to connecting alone. The rate of new connections hurts more than the count.
A pgbench test on a 10-core laptop (Postgres 17, 20 clients, read-only queries against a table of one million rows) showed the gap. Opening a fresh connection for each transaction capped out near 1,415 per second. Going through PgBouncer lifted that to 31,524. Persistent connections reached 215,538, about 150 times faster than connecting each time.
To spot churn on Postgres 14 or later, sample the session counters in pg_stat_database twice, a minute apart. The fix is a pool. Size an application pool to the cores, not the threads, and put PgBouncer in front, in transaction mode, when a crowd of clients should share a handful of backends. Five thousand clients can funnel into 20.
Appending to a JSONB column
Postgres never overwrites a row. Every UPDATE writes a new version and leaves the old one for VACUUM. If the row grows each time, the new version never fits the gap the old one left, and the table bloats.
The example is a users table with everything in one JSONB column. A profile that starts at 110 bytes reaches 688 bytes after a year of features, including a login_history array that gains an entry at each login. Switching a theme setting changes 17 bytes, yet the database writes out all 688 again. At one login a day, a single user’s row finishes the year with one current version and 364 dead copies spread over 221 pages. Multiply by a million users and 9.3 GB of live data sits inside 1.8 TB of row versions.
Readers wade through dead versions, and each query holds its connection longer. The fix is to put data that accumulates, like login history, in its own table. A smaller step is to replace a single value rather than grow a list, so each version stays the same size.
Holding a transaction open during an API call
With psycopg2 and psycopg 3, autocommit starts switched off, so even a SELECT begins a transaction. Call a payments API next and that transaction keeps its row lock and transaction ID until the call returns. In the default READ COMMITTED mode, the transaction ID also pins the vacuum horizon, so VACUUM can’t remove newer row versions in any table.
If the API goes from 50 ms to 20 seconds, every request holds a connection while it waits. The pool grows, fills the same way, and ends up exhausted while the database sits idle. A 30-second HTTP timeout doesn’t solve it. At 300 commits per second, one stuck request can leave 9,000 transactions queued behind it.
When VACUUM reports dead rows as not yet removable and the cutoff is old, something is holding the horizon back. Query pg_stat_activity for the oldest holders. The cure is to keep only SQL inside a transaction: turn autocommit on, charge with an idempotency key first, then run one guarded UPDATE. Per-role timeouts add a backstop.
Running a nightly bulk DELETE
Replication lag and a WAL surge around 02:00 point to this one. Take an events table of 2 billion rows, 90 days of retention and six indexes: the nightly job removes about 22 million rows. DELETE only marks them dead, so the table gains 22 million dead tuples and writes WAL that every replica replays. The transaction can run for hours while it pins the horizon.
The better design is partitioning. Build a partitioned table beside the old one, with the partition key in the primary key. Retention then becomes a catalog change. Dropping one month removes about 680 million rows with no dead tuples, and it takes milliseconds once open transactions on the table finish. Until partitioning is possible, remove rows in small batches and commit after each.
Keeping unused indexes
Every index adds work to every write. On a 1-million-row table, an UPDATE takes 0.2 ms with no extra indexes or seven, because everything fits in RAM. Scaled to 1 billion rows and 5,000 updates a second, the indexes compete with the table for memory, and replicas and the WAL archive carry an extra 235 GB a day.
The wrong index can also switch off HOT updates, which touch only the heap, and only when the new row version fits on its current page and no indexed column changed. With autovacuum off, the table without the extra index held at 1.9% dead tuples after 64,000 updates. With an index on the constantly updated column, it was 38% dead after 61,000.
To find candidates, list indexes with zero scans over 100 MB, and check every replica, because each counts its own scans. A low ratio of n_tup_hot_upd to n_tup_upd (under one half) means an indexed column is changing or pages are full. Record the definition with pg_get_indexdef() first, and then drop the index concurrently.
Fixing these Postgres mistakes
The speaker’s closing point is that these Postgres mistakes all end up as connection problems. Each one makes transactions hold connections longer, and raising max_connections only hides it. Making each transaction cheaper brings the count down.
Five timeout settings default to 0, meaning no limit: statement_timeout, lock_timeout, idle_in_transaction_session_timeout, transaction_timeout (added in Postgres 17) and idle_session_timeout (added in 14). Apply them role by role, with reports given a longer statement limit than the app. Alert on sessions idle in a transaction past 30 seconds, and on any transaction open for over a minute.
Source: Neon