With Django , When I use connection pool , eoor(PoolTimeout) occurs. why?

When I use connection pool, yhere is no problem when starting it for the first time run, but, After a while, an error will start occurring.

Traceback (most recent call last):
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection
    self.connect()
  File "web/django/venv/lib/python3.11/site-packages/sentry_sdk/utils.py", line 1786, in runner
    return sentry_patched_function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/sentry_sdk/integrations/django/__init__.py", line 692, in connect
    return real_connect(self)
           ^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
    self.connection = self.get_new_connection(conn_params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 348, in get_new_connection
    connection = self.pool.getconn()
                 ^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/psycopg_pool/pool.py", line 202, in getconn
    raise PoolTimeout(
psycopg_pool.PoolTimeout: couldn't get a connection after 30.00 sec

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "repli/web/django/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/sentry_sdk/integrations/django/views.py", line 90, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/.pyenv/versions/3.11.2/lib/python3.11/contextlib.py", line 80, in inner
    with self._recreate_cm():
  File "web/django/venv/lib/python3.11/site-packages/django/db/transaction.py", line 198, in __enter__
    if not connection.get_autocommit():
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 454, in get_autocommit
    self.ensure_connection()
  File "web/django/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 278, in ensure_connection
    with self.wrap_database_errors:
  File "web/django/venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 279, in ensure_connection
    self.connect()
  File "web/django/venv/lib/python3.11/site-packages/sentry_sdk/utils.py", line 1786, in runner
    return sentry_patched_function(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/sentry_sdk/integrations/django/__init__.py", line 692, in connect
    return real_connect(self)
           ^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/base/base.py", line 256, in connect
    self.connection = self.get_new_connection(conn_params)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 348, in get_new_connection
    connection = self.pool.getconn()
                 ^^^^^^^^^^^^^^^^^^^
  File "web/django/venv/lib/python3.11/site-packages/psycopg_pool/pool.py", line 202, in getconn
    raise PoolTimeout(
django.db.utils.OperationalError: couldn't get a connection after 30.00 sec

Are some settings missing?

  • about version

    • Django 5.1.0
    • Postegresql 16.3
  • dfr/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '***',
        'USER': '***',
        'PASSWORD': '***',
        'HOST': 'localhost',
        'PORT': '5432',
        'ATOMIC_REQUESTS': True,
        'OPTIONS': {
            'pool': {
                'min_size': 900,
                'max_size': 900,
            }
            # 'pool': True
        },
    }
}
  • I add max_connections = 1000 in postgresql.conf

As mentioned above, max_size was set smaller than Postgresql's max_connections.

Back to Top