Skip to content

Waiting for notify requires Obj.magic #69

@JanBeh

Description

@JanBeh

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions