Skip to content

About

A small PostgreSQL pooling demo with a concurrent load test and measured results

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Postgres connection pooling demo

A small Python demo to see how much time gets spent opening database connections.

There are two functions in demo.py. no_pool() opens a connection, runs one query and closes it. with_pool() borrows a connection, runs that same query and gives it back.

Run it

You need Python 3.10 or newer and Docker with Docker Compose.

From this folder, start the database:

docker compose up -d --wait

On Windows PowerShell:

python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
.\.venv\Scripts\python.exe load_test.py --requests 400 --concurrency 20 --pool-size 20 --rounds 3

On Linux or macOS:

python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python load_test.py --requests 400 --concurrency 20 --pool-size 20 --rounds 3

The script prints each result and saves all the timings and connection samples in results.json. A failed query or failed connection makes the test stop instead of counting it as a fast result.

Stop the demo database when finished:

docker compose down

If you already have PostgreSQL running, create a database called pooling_demo and set DATABASE_URL to its connection string. For example, on PowerShell:

$env:DATABASE_URL = "postgresql://postgres:postgres@localhost:5432/pooling_demo"

The password in the Docker file is just for this local demo. Its port is only exposed on localhost.

What gets tested

  • 400 operations per version, with up to 20 running at once.
  • A fixed pool of 20 connections.
  • Three rounds, with the order reversed in the second round.
  • The same database, query and connection settings for both versions.
  • Ten queries before the tests to warm up PostgreSQL, excluded from the results.
  • Autocommit on and automatic prepared statements off in both versions.

The query is:

SELECT sum(n) FROM generate_series(1, 100) AS n

Every operation checks that the answer is 5050. There is no added sleep or artificial delay. The load test calls the Python functions directly using a thread pool, so these are database operation timings rather than HTTP response timings.

The average time starts when a worker begins an operation. It includes opening a connection in the no-pool version and waiting for a borrowed connection in the pooled version. It does not include time waiting in the executor queue. Total time covers the entire batch, including worker startup and scheduling.

The pool is opened before the timed batch. Its setup time is recorded separately, so it is also possible to compare a single batch including setup. Each round creates a fresh pool and closes it afterwards.

Results

These measurements were recorded on 3 October 2026 using Python 3.14.7 and PostgreSQL 17.6 on Windows 11, with an Intel Core i5-1240P. PostgreSQL ran locally on port 55432 with password authentication and max_connections = 100. The Docker setup above uses the same PostgreSQL version, but runs it in a Linux container, so its numbers can differ.

Round Version Average (ms) Batch time (ms) Opened connections Peak demo connections Peak pg_stat_activity
1 No pool 212.54 4329.74 400 3 9
1 Pool 7.57 191.74 20 20 26
2 No pool 202.06 4130.57 400 2 8
2 Pool 7.20 173.65 20 20 26
3 No pool 189.53 3877.92 400 2 8
3 Pool 6.66 160.35 20 20 26

Across the three rounds, average operation time was 201.38 ms without a pool and 7.15 ms with a pool. Average batch time was 4.113 seconds and 0.175 seconds, respectively. There were zero errors in all six runs.

The pooled batch was about 23.5 times faster once the pool was ready. Pool setup took 533.45 ms on average. Including that setup, a pooled batch averaged 0.709 seconds, still about 5.8 times faster in this test.

The pool keeps all 20 connections open, including idle ones. The no-pool connections exist briefly, so the sampler saw only 2 or 3 at a time, even though 400 were opened during each batch. A higher resident connection count in the pooled run does not mean more connections were created overall. These sampled peaks are observations, not exact maximums.

These numbers apply to this small query and this computer. Pooling does not make the query itself faster, and results will change with the query, machine, network and pool size.

Counting connections

A separate monitoring connection runs this while each test is active:

SELECT count(*) FROM pg_stat_activity;

It also counts connections with the test's application_name, to separate them from background processes and the monitor. It waits 20 ms between samples. Sampling can miss a short peak, and the monitoring queries add a little overhead to both versions.

pg_stat_activity shows connections present at that moment, not every connection ever opened. The no-pool total is counted from successful operations, since each operation opens exactly one connection. The pool total comes from pool.get_stats()["connections_num"]. Both totals exclude the monitor and the warmup. The raw samples are in results.json.

Why the numbers differ

Opening a connection makes PostgreSQL authenticate the client and start a backend process.
The no-pool version repeats that work for every operation.
The pooled version pays that cost when the pool starts, then reuses those connections.
This query is small, so connection setup takes a large share of the time; a slow query could show a smaller benefit.

Files

  • demo.py: the two database versions.
  • load_test.py: the concurrent test and connection sampling.
  • results.json: recorded measurements, including every operation time.
  • compose.yaml: a local PostgreSQL instance for repeating the test.

The connection handling follows the Psycopg connection documentation and pool documentation.

About

A small PostgreSQL pooling demo with a concurrent load test and measured results

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages