It currently seems to be impossible to wait for a NOTIFY from PostgreSQL. The method recommended by the PostgreSQL documentation is to use PQconsumeInput, check for notifies, and then wait for the file descriptor obtained by PQsocket to be ready for reading.
Unfortunately, the #socket method returns the file descriptor as an integer. AFAIK there is no way in OCaml (without using C or external packages) to convert an integer into a file descriptor other than using Obj.magic (which is not what one really should use). See also this pull request for OCaml, where adding such functionality was rejected.
I would like to see a solution that allows me to use the postgresql OCaml package to wait for a NOTIFY without repeatedly polling or consuming CPU time in a loop. Apparently there has been a pull request (#50) to fix this issue, but it became obsolete due to OCaml runtime changes and wasn't resubmitted yet. The previous changes seem to be minimal, so I don't believe it would be a lot of work to fix this. I still lack expertise to do this (soundly) on my own, so maybe someone could take a look at this issue.
For further information, see the article "File descriptors are not integers" from September 30, 2025. I initially brought up this issue on discuss.ocaml.org with the post "Using package postgresql and waiting for a notify".
It currently seems to be impossible to wait for a
NOTIFYfrom PostgreSQL. The method recommended by the PostgreSQL documentation is to usePQconsumeInput, check for notifies, and then wait for the file descriptor obtained byPQsocketto be ready for reading.Unfortunately, the
#socketmethod returns the file descriptor as an integer. AFAIK there is no way in OCaml (without using C or external packages) to convert an integer into a file descriptor other than usingObj.magic(which is not what one really should use). See also this pull request for OCaml, where adding such functionality was rejected.I would like to see a solution that allows me to use the
postgresqlOCaml package to wait for aNOTIFYwithout repeatedly polling or consuming CPU time in a loop. Apparently there has been a pull request (#50) to fix this issue, but it became obsolete due to OCaml runtime changes and wasn't resubmitted yet. The previous changes seem to be minimal, so I don't believe it would be a lot of work to fix this. I still lack expertise to do this (soundly) on my own, so maybe someone could take a look at this issue.For further information, see the article "File descriptors are not integers" from September 30, 2025. I initially brought up this issue on
discuss.ocaml.orgwith the post "Using package postgresql and waiting for a notify".