Skip to main content

Context manager to upload explain plans to https://explain.dalibo.com/

Project description

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.

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}"))

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


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-0.1.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

django_pev-0.1.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_pev-0.1.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.2 Darwin/21.3.0

File hashes

Hashes for django_pev-0.1.4.tar.gz
Algorithm Hash digest
SHA256 e2a9ec9a7339ba32c0ffba8ae7499ebd466b7581584db593e254a7b510697f3d
MD5 40b323a51e51f47ae06b36c4a5f9b0ff
BLAKE2b-256 8943aca39074df281cbd4cfba73a3dc5feb10811d342ad3ca268db8f6018176f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_pev-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.2 Darwin/21.3.0

File hashes

Hashes for django_pev-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b8febe4407706578c3f7df31cf832bd4e38dc751314668438f09debc66f6c26b
MD5 521215f2462d0e709385d3f1512cacfc
BLAKE2b-256 7ca686b36e4b82c0d0b9d3b8e9dce51dcf3c70b8e4b7f5c4a85359e3be6e48b4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page