阻止SELECT,直到结果可用 [英] Block SELECT until results available
问题描述
我正在尝试编写用于长轮询的PHP脚本,在将新行添加到(Postgres)数据库表中时返回数据。有什么办法让SELECT查询仅在返回结果时才返回,否则阻塞?还是应该在数据库之外使用其他信号机制?
I'm trying to write a PHP script for 'long-polling', returning data when new rows are added to a (Postgres) database table. Is there any way to get a SELECT query to return only when it would return results, blocking otherwise? Or should I use another signaling mechanism, outside of the database?
推荐答案
看看LISTEN / NOTIFY:
Take a look at LISTEN/NOTIFY:
NOTIFY命令向每个客户端
应用程序发送一个
通知事件,该应用程序先前已为指定的
执行LISTEN名称当前
数据库中的
通知名称
The NOTIFY command sends a notification event to each client application that has previously executed LISTEN name for the specified notification name in the current database
http://www.postgresql.org/docs/8.4/static/sql-notify.html
您可以向表添加 ON INSERT触发器以触发NOTIFY事件。但是,您将需要另一种机制来找出需要选择哪些记录,因为具有NOTIFY事件的有效载荷传递功能直到9.0才可用:
You can add an "ON INSERT" trigger to the table to fire off a NOTIFY event. However, you will need another mechanism to figure out which records need to be selected as the ability to deliver a payload with the NOTIFY event won't be available until 9.0:
http://www.postgresql.org /docs/9.0/static/sql-notify.html
这篇关于阻止SELECT,直到结果可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!