pythonpostgresqlsqlalchemyprofiling

How to determine a number of queries SQLAlchemy does during user request?


I searched the internet and didn't find an answer to a very simple question.

I have a simple web application (powered by web.py) which uses SQLAlchemy 0.7.8 + psycopg2, so sqltap doesn't work for me. So, I can enable echo=True in engine or do anything with current session which is saved in threadlocal.

What is the right approach to calculating the number of queries and their execution time? Using some kind of event and increment some global variable? I am almost sure SQLAlchemy can do it by itself, but not a single line about it found in docs.


Solution

  • Pyramid's debugtoolbar does exactly what you describe. They use SQLAlchemy's events to hook into the base Engine class:

    from sqlalchemy import event
    from sqlalchemy.engine.base import Engine
    
    @event.listens_for(Engine, "before_cursor_execute")
    def before_cursor_execute(conn, cursor, stmt, params, context, execmany):
        # do something
    
    @event.listens_for(Engine, "after_cursor_execute")
    def after_cursor_execute(conn, cursor, stmt, params, context, execmany):
        # do something
    

    Maybe you can modify that code to work with web.py.