I'm using PostgreSQL with SKIP LOCKED
to implement a queue as described in What is SKIP LOCKED for in PostgreSQL 9.5?. Basically, this works fine, but sometimes when the workers are fast and there is not that much work to do, the queue runs empty. IOW: The SELECT
does not find anything, and so the statement runs, does nothing in the end, and we're done.
Now what I would like to do is to re-run the statement as soon as a new row comes in, and try again (although all the other workers may try to do this as well). This in the end comes down to polling the database, which is – let's say – not optimal.
Of course I could use PostgreSQL's NOTIFY
feature to inform the workers once a new row has been written, but I was wondering if this is possible in the SELECT
itself, such as leaving it open until it succeeds and returns a record? Something such as a long-running SELECT
which stays open as long as it takes to return a result.
Is this possible?