Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:20:17+00:00 2026-05-23T11:20:17+00:00

In my web app some postgres sql queries take along time for execution. I

  • 0

In my web app some postgres sql queries take along time for execution.
I want set statement timeout for only part of them.

One part of queries must canceled by timeout, but other must work without any restriction.

In postgres exist statement_timeout function.

How to wrap SqlAlchemy query with statement_timeout function?

Like this:

SET statement_timeout TO 1000; -- timeout for one second
<sqlalchemy generated query>;
RESET statement_timeout; -- reset

Perfect way for me set timeout for query like this:

users = session.query(User).timeout(0.5).all()

SqlAlchemy must: 1) set statement timeout 2) execute query and return result 3) reset statement timeout for current session

May be other way to set timeout for query execution?

UPDATE 1. My solution

My solution is a custom connection proxy (tested with psycopg2==2.4 and SQLAlchemy==0.6.6):

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)

This solution without reseting statement_timeout, because each SqlAlchemy query executed in isolated transaction and
statement_timeout defined inside current transaction.

Usage example (timeout pаram in seconds):

Session.query(Author).execution_options(timeout=0.001).all()

Session.bind.execute(text('select * from author;') \
      .execution_options(timeout=0.001)) \
      .fetchall()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:20:17+00:00Added an answer on May 23, 2026 at 11:20 am

    You should look at the extensions provided with SQLAlchemy <= 0.6:

    http://www.sqlalchemy.org/docs/06/orm/interfaces.html

    There are hooks where you could stick in your code for individual operations.

    SQLAlchemy 0.7+ now has an event system…there might be something similar. See

    http://www.sqlalchemy.org/docs/core/events.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on an ASP.NET/C# web app for some time and its
I have a web app that uses some backend servers (UNC, HTTP and SQL).
I have some usage queries for my web app's database, the results of which
In my web app I'm using some session variables, which are set when I
Im building a web app with some real-time features, chat, auto updating lists. What's
Problem: We have a web app that calls some web services asynchronously (from the
We are experiencing some slowdowns on our web-app deployed on a Tomcat 5.5.17 running
I'm developing a web app in Perl with some C as necessary for some
In my web app, I submit some form fields with jQuery's $.getJSON() method. I
I have some configuration values for an asp.net web app. They will be maintained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.