Skip to main content

Django Postgres Explain Visualizer (Django-PEV)

PyPI version versions Lint

This tool captures sql queries and uploads the query plan to postgresql explain visualizer (PEV) by dalibo. This is especially helpful for debugging slow queries.

This tool also exports a graphical UI similar to pghero but is embedded within your django app.

Installation

  1. pip install django-pev

  2. Add to your urls

# urls.py
from django.urls import include, path

urlpatterns = [
    # ....

    path('django-pev/', include(('django_pev.urls', 'django_pev'), namespace='django_pev')),
]
  1. Add to your installed apps
# settings.py

INSTALLED_APPS = [
    # ...
    "django_pev"
]

Usage

Wrap some code with the explain context manager. All sql queries are captured alongside a stacktrace (to locate where it was called). The slowest query is accessible via .slowest.

import django_pev
from django.contrib.auth.models import User

with django_pev.explain() as e:
    # Every SQL query is captured
    list(User.objects.filter(email='test@test.com').all())

# Rerun the slowest query with `EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)`
pev_response = e.slowest.visualize(
    # By default the text of the query is not uploaded for security reasons
    upload_query=True,
    # Set to false if the query is slow and you want only an explain
    analyze=True,
    # Give a helpful title for the uploaded query plan
    title="Measuring email filter",
)
print(pev_response.url)

# View the postgres explain visualization
e.slowest.visualize_in_browser()


# Print the optimization prompt
e.slowest.optimization_prompt()

# Find N+1 queries
for query, count in e.nplusones.items():
    print(f"Found N+1 query executed {count} times:")
    print(query.sql)
    print(f"Stack trace:\n{query.stack_trace}")

# View the stack trace of the slowest query
print(e.slowest.stacktrace)

# Delete the plan hosted on https://explain.dalibo.com
pev_response.delete()

Optionally configure additional settings:

# Replace the default test client used during explain with a custom class
DJANGO_PEV_EXPLAIN_TEST_CLIENT = 'django.test.Client'

How to debug a slow endpoint in production

If you have access to python manage.py shell on the production server; you can run the following code snippet to get an explain plan uploaded. In general this technique is all types of profiling.

import django_pev

from django.contrib.auth.models import User
from django.test import Client as TestClient

client = TestClient()
# Authentication
client.force_login(User.objects.get(id=1))
url = "/some_slow_url"

with django_pev.explain() as e:
    response = client.get(url)

print(e.slowest.visualize(title=f"Fetching {url}"))

How do I

Create a release

Just merge to main and let Release Please bot do its job. Merge the release branch to publish to pypi.

TODO

  • Add migration to ensure pg_stats_statement_info is correct

Disclaimer

Credit goes to Pierre Giraud (@pgiraud) for PEV2 and Alex Tatiyants (@AlexTatiyants) for the original pev tool.

IN NO EVENT SHALL DALIBO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DALIBO HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

DALIBO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND DALIBO HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pev-2.1.0.tar.gz (662.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pev-2.1.0-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file django_pev-2.1.0.tar.gz.

File metadata

  • Download URL: django_pev-2.1.0.tar.gz
  • Upload date:
  • Size: 662.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_pev-2.1.0.tar.gz
Algorithm Hash digest
SHA256 d10ba02eff855030385c4c84104e476ed67cc9402dcc01dbbf34d7d08b49469b
MD5 0bb7ecc04a472f2d15f915197bbb5264
BLAKE2b-256 ae5f8552359ff8e2889dc897d262011599da9e6d01ccb4207515e1781352fba9

See more details on using hashes here.

File details

Details for the file django_pev-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_pev-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_pev-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9ba9f18f9629dc706b24b9c9cea7a90a37e990650baec619db4be1387d35515
MD5 bd3f92bbc43bd5492bd24e960f0e35b6
BLAKE2b-256 1fad22595d7fee6263d8fc6917153cdc73cb09bd11d6142160b9771a3b0b0332

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 files

2.0.0

2 files

1.1.1

2 files

1.1.0

2 files

1.0.2

2 files

1.0.1

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page