Search code examples
cpostgresqlpostgresql-12postgresql-extensions

How to make a Postgres background workers sleep and wake up on a signal?


PostgreSQL makes use of background workers to allow processes working in a concurrent fashion and they have an API for backend/extension developers to control them. So far, I have managed to successfully work with this feature in a demo extension, successfully spawning a number of workers.

I am in a situation where one of my workers has to wait for another to finish. What I am doing so far is an infinite loop on an idle worker until the worker being waited for is finished, which can be quite inefficient. So I was wondering how would I go about making the idle process sleep until some signal is sent? What would I be looking for? Is there an extension which does something similar so that I could use for guidance?


Solution

  • I think the official way to do something like this is with condition variables, implemented in the file src/backend/storage/lmgr/condition_variable.c

    I don't see it being used in any "contrib" extensions, however, just the core code.