Opened 11 years ago

Closed 11 years ago

Last modified 9 years ago

#5236 closed (wontfix)

Provide stack trace information in sql debug entries

Reported by: calvin@… Owned by: Philippe Raoult
Component: Database layer (models, ORM) Version: master
Severity: Keywords: feature debug
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



when profiling django views I often use the list of stored SQL queries to review. What I am missing there is where each of the SQL statements comes from. So I added a stack frame text to each of them.
I have not measured the performance impact, but since it only affects installations with DEBUG=True, I consider the tradeoff acceptable.

The patch also simplifies the debug entry by splitting off the storing to a separate method save_entry().

Attachments (2)

django_sql_debug_stack.patch (1.6 KB) - added by Bastian Kleineidam <calvin@…> 11 years ago.
5236.diff (3.2 KB) - added by Philippe Raoult 11 years ago.
patch slightly cleaned up, but mostly documented in the FAQ

Download all attachments as: .zip

Change History (9)

Changed 11 years ago by Bastian Kleineidam <calvin@…>

comment:1 Changed 11 years ago by Simon G. <dev@…>

Has patch: set
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 11 years ago by Philippe Raoult

Keywords: feature debug added

comment:3 Changed 11 years ago by Philippe Raoult

Owner: changed from nobody to Philippe Raoult

Changed 11 years ago by Philippe Raoult

Attachment: 5236.diff added

patch slightly cleaned up, but mostly documented in the FAQ

comment:4 Changed 11 years ago by Philippe Raoult

Status: newassigned

comment:5 Changed 11 years ago by Philippe Raoult

Triage Stage: Design decision neededReady for checkin

comment:6 Changed 11 years ago by Malcolm Tredinnick

Resolution: wontfix
Status: assignedclosed

Thanks for taking the time to make the patch, but no thanks. Running with DEBUG=True is useful in a lot of situations and we don't need this type of overhead all the time there.

If you want to do this, you can replace the CursorDebugWrapper.execute with your own function in, say,, so no changes to core are necessary (the advantage of functions as first class objects).

comment:7 Changed 9 years ago by CBWhiz

Just in case anybody is still looking for this, create a new app and throw this into the _ _init_

from django.conf import settings
DEBUG_SQL_STACK = getattr(settings, 'DEBUG_SQL_STACK', False)
    from time import time
    import traceback
    import django.db.backends.util
    class TracingCursorDebugWrapper(django.db.backends.util.CursorDebugWrapper):
        def trace_entry (self, sql, start): 
            stop = time()
            entry = { 
                'sql': sql, 
                'time': "%.3f" % (stop - start), 
                'stack': traceback.format_stack()[:-2],
        def execute(self, sql, params=()):
            start = time()
                return self.cursor.execute(sql, params)
                sql = self.db.ops.last_executed_query(self.cursor, sql, params)
                self.trace_entry(sql, start)
        def executemany(self, sql, param_list):
            start = time()
                return self.cursor.executemany(sql, param_list)
                sql = '%s times: %s' % (len(param_list), sql)
                self.trace_entry(sql, start)
    django.db.backends.util.CursorDebugWrapper = TracingCursorDebugWrapper

then add the app to your INSTALLED_APPS settings, and set DEBUG_SQL_STACK = True in your file. Works with 1.2.

Note: See TracTickets for help on using tickets.
Back to Top