title: How to set statement timeout for query execution
body: <p>In my web app some postgres sql queries take along time for execution.
  I want set statement timeout for only part of them.</p>
  
  <p>One part of queries must canceled by timeout, but other must work without any restriction.</p>
  
  <p>In postgres exist statement_timeout function.</p>
  
  <p>How to wrap SqlAlchemy query with statement_timeout function?</p>
  
  <p>Like this:</p>
  
  <pre><code>SET statement_timeout TO 1000; -- timeout for one second
  &lt;sqlalchemy generated query&gt;;
  RESET statement_timeout; -- reset
  </code></pre>
  
  <p>Perfect way for me set timeout for query like this:</p>
  
  <pre><code>users = session.query(User).timeout(0.5).all()
  </code></pre>
  
  <p>SqlAlchemy must: 1) set statement timeout 2) execute query and return result 3) reset statement timeout for current session</p>
  
  <p>May be other way to set timeout for query execution?</p>
  
  <p><strong>UPDATE 1.</strong> My solution</p>
  
  <p>My solution is a custom connection proxy (tested with psycopg2==2.4 and SQLAlchemy==0.6.6):</p>
  
  <pre><code>from sqlalchemy.interfaces import ConnectionProxy
  
  class TimeOutProxy(ConnectionProxy):
      def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
  
          timeout = context.execution_options.get('timeout', None)
  
          if timeout:
              c = cursor._parent.cursor()
              c.execute('SET statement_timeout TO %d;' % int(timeout * 1000))
              c.close()
  
          return execute(cursor, statement, parameters, context)
  
  
  engine = create_engine(URL, proxy=TimeOutProxy(), pool_size=1, max_overflow=0)
  </code></pre>
  
  <p>This solution without reseting statement_timeout, because each SqlAlchemy query executed in isolated transaction and
  statement_timeout defined inside current transaction.</p>
  
  <p>Usage example (timeout pаram in seconds):</p>
  
  <pre><code>Session.query(Author).execution_options(timeout=0.001).all()
  
  Session.bind.execute(text('select * from author;') \
        .execution_options(timeout=0.001)) \
        .fetchall()
  </code></pre>