def cursor(self, *args, **kwargs):
    kwargs.setdefault('cursor_factory', LoggingCursor2)
return super(LoggingConnection, self).cursor(*args, **kwargs)


class LoggingCursor2(psycopg2.extras.LoggingCursor):

    def execute(self, query, vars=None):
        self.timestamp = time.time()
return LoggingCursor.execute(self, query, vars)

def callproc(self, procname, vars=None):
    self.timestamp = time.time()
return LoggingCursor.execute(self, procname, vars)
Python psycopg2 — Регистрация событий