Postgresql取消查询客户端与PHP断开连接

时间:2011-08-29 15:19:07

标签: php postgresql

我有一个非常昂贵的查询,它从php执行,可能需要一段时间才能执行。有没有办法,在PHP中,检测用户是否在查询完成之前断开连接并取消它?

2 个答案:

答案 0 :(得分:1)

您可能希望使用connection_aborted检测用户是否已断开连接,如果客户端已断开连接则返回1,否则返回0.有一些文档here,但其用法是自我记录,你应该没有问题使用它。

答案 1 :(得分:1)

一种可能的解决方案是使用 pg_send_query(),该函数向数据库发送查询并立即返回而不会阻塞。然后,您可以轮询以查看用户在查询完成之前是否已断开连接。见:

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

这种方法有效但存在很大问题:您确实需要向浏览器发送一些内容来检测浏览器是否断开连接。如果不这样做, connection_status() connection_aborted()将无效。这似乎是一个旧的PHP错误,请参见此处:https://bugs.php.net/bug.php?id=30301

因此,例如,当您在PDF生成例程中查询Postgres时,此方法不起作用。在这种情况下,所需的 chr(0)将破坏生成的二进制文件。