django/docs/topics/db/connection-pooling.txt
Pravin Kamble a892459def Add connection pooling references and index for DB topics
- Added .. seealso:: links in ref/databases.txt and topics/async.txt pointing to
topics/db/connection-pooling for general and backend-specific guidance.
- Updated topics/db/index.txt to include 'connection-pooing' in the table of contents.
2025-09-05 17:49:53 +05:30

101 lines
2.9 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

=============================
Database connection pooling
=============================
.. versionadded:: 5.1
PostgreSQL connection pooling was introduced in Django 5.1.
.. versionadded:: 5.2
Oracle connection pooling was introduced in Django 5.2.
Introduction
============
Database connection pooling allows Django to reuse database connections
rather than opening and closing a connection for each request. This can
improve performance and reduce overhead in applications with many
short-lived database operations.
Django supports two main approaches:
- **Persistent connections** via :setting:`CONN_MAX_AGE` (all backends).
- **True connection pooling** (currently PostgreSQL and Oracle only).
Supported backends
==================
PostgreSQL
-----------
Since Django 5.1, the PostgreSQL backend supports connection pooling
when using the :pypi:`psycopg` driver with ``psycopg[pool]`` installed.
Enable pooling by setting the ``"pool"`` option in your database
``OPTIONS``::
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "mydb",
"USER": "myuser",
"PASSWORD": "secret",
"OPTIONS": {
"pool": True, # or a dict of parameters for psycopg_pool.ConnectionPool
},
}
}
See :ref:`postgresql-pool` for details.
Oracle
------
Since Django 5.2, the Oracle backend supports pooling when using the
`python-oracledb <https://python-oracledb.readthedocs.io/>`_ driver.
Enable pooling by setting the ``"pool"`` option in your database
``OPTIONS``. See :ref:`oracle-pool` for details.
Other backends
--------------
- **MySQL / MariaDB / SQLite** do not currently support native connection
pooling in Django.
- These backends can only use persistent connections
(:setting:`CONN_MAX_AGE`) or an **external pooler** (for example,
ProxySQL, MariaDB MaxScale).
Persistent connections vs pooling
=================================
- :setting:`CONN_MAX_AGE` keeps connections open for reuse, but each worker
maintains its own connection(s).
- True pooling manages a **shared pool** of connections that can be borrowed
and returned, which is more efficient for high-concurrency workloads.
External poolers
================
For production deployments, its common to use an external pooler:
- PostgreSQL: `PgBouncer <https://www.pgbouncer.org/>`_
- MySQL/MariaDB: `ProxySQL <https://proxysql.com/>`_
When using external poolers, ensure your Django settings are compatible with
the poolers mode (for example, transaction pooling in PgBouncer has special
considerations, see :ref:`transaction-pooling-server-side-cursors`).
Async considerations
====================
Djangos ORM is still synchronous, but connection pooling plays a role in
improving async support in the future. For now:
- Use the backends built-in pooling if available.
- Or use an external pooler in async workloads.
See :ref:`async` for details.