开发者

Postgresql cancel query on client disconnect with php

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 discon开发者_开发百科nects prior to the query being done and cancel it?


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.


You would want to use connection_aborted to detect if the user has disconnected it returns 1 if the client has disconnected otherwise it returns 0. There is some documentation here, however its usage is self documenting and you should have no problem using it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜