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

  • SEARCH
  • Home
  • 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 7687613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:32+00:00 2026-05-31T19:44:32+00:00

I have a very expensive query which gets executed from php and it can

  • 0

I have a very expensive query which gets executed from php and it can take a while to execute. Is there a way, in php, to detect if a user disconnects prior to the query being done and cancel it?

  • 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-31T19:44:33+00:00Added an answer on May 31, 2026 at 7:44 pm

    A possible solution is to use pg_send_query(), that function sends a query to the database and returns immediatly without blocking. Then you can poll to see if the user disconnected before the query finished. See this:

    ignore_user_abort(false); 
    $db = pg_connect(DATABASE_DSN);
    pg_send_query($db, "SELECT pg_sleep(10000)"); // long running query
    while(pg_connection_busy($db)) {
      /* You need to output something to the browser so PHP can know when the
         browser disconnected. The 0 character will be ignored. 
      */
      echo chr(0); 
    
      /* Need to do both flushes to make sure the chr is sent to the browser */
      ob_flush();
      flush(); 
    
      usleep(100000); // 0.1s to avoid starving the CPU
    
      if(connection_status() !=  CONNECTION_NORMAL || connection_aborted()) {
        // Browser disconnected, cleanup an die
        pg_cancel_query($db);
        pg_query($db, "ROLLBACK"); 
        pg_close($db); 
        die(); 
      }
    }
    
    //  At this point the query finished and you can continue fetching the rows
    

    This approach works but has a big problem: you really need to send something to the browser to detect the browser disconnection. If you don’t, connection_status() and connection_aborted() will not work. This seems to be an old PHP bug, see here: https://bugs.php.net/bug.php?id=30301

    So this method doesn’t work when, for example, you query Postgres in the middle of a PDF generation routine. In that case the needed chr(0) will break the generated binary file.

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

Sidebar

Related Questions

I have heard that DateTime.Now is very expensive call (from here ) Is GETDATE()
I have a very basic question in saving objects which I get from a
I have a several very expensive queries which seem to hog resources that seems
So I have this expensive db query whose result won't change very often. The
In postgis, is the ST_GeomFromText call very expensive? I ask mostly because I have
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I need to read from a dataset which is very large, highly interlinked, the
I have a query like the following: SELECT t1.v3, t2.v2 FROM t1 INNER JOIN
I want to get number of images from Server? It is very expensive to
I have a dataset in MySQL where using limit is already an expensive query,

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.