Context manager to upload explain plans to https://explain.dalibo.com/
Project description
Django Postgres Explain Visualizer (Django-PEV)
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
-
pip install django-pev
-
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')),
]
- 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()
# View the stack trace of the slowest query
print(e.slowest.stacktrace)
# Delete the plan hosted on https://explain.dalibo.com
pev_response.delete()
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}"))
TODO
- Add migration to ensure pg_stats_statement_info is correct
- Do not crash when its not available
- Add explain tab = [ ] Add index suggester
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_pev-0.1.8.tar.gz
.
File metadata
- Download URL: django_pev-0.1.8.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ead5ed195d8f8ea6162f71fbd36eab1f3325133a95ec65b6ed434c2f01be5299 |
|
MD5 | 88b78549a4a4a3ebfeaa50f2ffade92c |
|
BLAKE2b-256 | d987a20b6ab96632d74d9c7e7ce19671cf1bfd9ec37f73db6fb49e3b8f2bf659 |
File details
Details for the file django_pev-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: django_pev-0.1.8-py3-none-any.whl
- Upload date:
- Size: 26.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | db1ac1d9ace74b162d565c5371e62b6e90ebb22647ac21f77728b46d0e324474 |
|
MD5 | 496175f7ff92afd40ae61b4d48e22beb |
|
BLAKE2b-256 | b41aa8ca946ccc3ea25aeb41b3c28c07dde8121c700d0905ffe36df26301aa8f |