jbe
February 6, 2026, 3:44pm
1
I’m using the postgresql package and would like to wait for a NOTIFY without repeatedly polling.
The connection class provides the methods #consume_input and #socket which could be used in a loop with Unix.select . This method is the forseen method by PostgreSQL’s libpq , which the OCaml postgresql package wraps.
The #socket method returns an integer. However, I don’t know how to wait for a file descriptor, given as an integer. OCaml has a Unix.select function, but this function works with abstract Unix.file_descr types, and I didn’t find any function to convert an integer into that type.
What can I do?
2 Likes
lindig
February 6, 2026, 3:55pm
2
let file_descr_of_int (x : int) : Unix.file_descr = Obj.magic x
It’s not nice but will work,
1 Like
It’ll break on Windows where file_descr is an opaque type that maps to a HANDLE.
File descriptors are not integers goes into this with fdopen and io_uring as context. Personally I’m very sympathetic to just treating fds as ints since there are so many more serious caveats to keep in mind anyway with them, but the suggested alternative is doing the conversion in C, and you can have to do that anyway to access newer kernel APIs and constants.
That article also links to a firm refusal in Add Unix.descr_of_fd and Unix.descr_of_os by dra27 · Pull Request #1990 · ocaml/ocaml · GitHub
And IMO this should be resolved by changing #socket to return a Unix.file_descr … which was attempted, but needs an update: use Unix.file_descr for sockect by craff · Pull Request #50 · mmottl/postgresql-ocaml · GitHub
4 Likes
jbe
February 7, 2026, 9:40am
4
Thank you very much for your responses.
The necessary changes to the postgresql package seem to be small , but I don’t think I’m experienced enough yet to (soundly) fix this myself. Maybe someone else would like to help here and fix the issue.
I also opened an issue on GitHub describing the problem and referring to the information you shared here:
opened 09:36AM - 07 Feb 26 UTC
It currently seems to be impossible to wait for a [`NOTIFY`](https://www.postgre… sql.org/docs/18/sql-notify.html) from PostgreSQL. The method [recommended by the PostgreSQL documentation](https://www.postgresql.org/docs/18/libpq-notify.html) is to use [`PQconsumeInput`](https://www.postgresql.org/docs/18/libpq-async.html#LIBPQ-PQCONSUMEINPUT), check for notifies, and then wait for the file descriptor obtained by [`PQsocket`](https://www.postgresql.org/docs/18/libpq-status.html#LIBPQ-PQSOCKET) to be ready for reading.
Unfortunately, the [`#socket`](https://github.com/mmottl/postgresql-ocaml/blob/96f12cb43dcff2425780414d68f79ba047761fbf/lib/postgresql.mli#L948-L952) 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`](https://ocaml.org/manual/5.4/api/Obj.html#VALmagic) (which is not what one really should use). See also [this pull request for OCaml](https://github.com/ocaml/ocaml/pull/1990), where adding such functionality was [rejected](https://github.com/ocaml/ocaml/pull/1990#issuecomment-413464113).
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](https://github.com/mmottl/postgresql-ocaml/pull/50#issuecomment-2455975448) and wasn't resubmitted yet. The [previous changes](https://github.com/mmottl/postgresql-ocaml/pull/50/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"](https://www.dra27.uk/blog/platform/2025/09/30/file-descriptors-are-not-integers.html) 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"](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780).
3 Likes