Postgresql取消客户端与php的连接



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

一个可能的解决方案是使用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)将破坏生成的二进制文件。

您希望使用connection_aborted来检测用户是否已断开连接,如果客户端已断开连接,则返回1,否则返回0。这里有一些文档,但是它的用法是自我文档化的,使用它应该没有问题。

最新更新