# Using package postgresql and waiting for a notify

**URL:** <https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780>\
**Category:** Learning\
**Tags:** unix, io, postgresql\
**Created:** [February 6, 2026, 3:44pm UTC](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780 "2026-02-06T15:44:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbe](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jbe](https://discuss.ocaml.org/u/jbe)\
**Post date:** [February 6, 2026, 3:44pm UTC](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780/1 "2026-02-06T15:44:08Z")

</div>

I’m using the [`postgresql`](https://ocaml.org/p/postgresql/5.3.2) package and would like to wait for a [`NOTIFY`](https://www.postgresql.org/docs/18/sql-notify.html) without repeatedly polling.

The [`connection`](https://mmottl.github.io/postgresql-ocaml/api/postgresql/Postgresql/class-connection/index.html) class provides the methods [`#consume_input`](https://mmottl.github.io/postgresql-ocaml/api/postgresql/Postgresql/class-connection/index.html#method-consume_input) and [`#socket`](https://mmottl.github.io/postgresql-ocaml/api/postgresql/Postgresql/class-connection/index.html#method-socket) which could be used in a loop with [`Unix.select`](https://ocaml.org/manual/5.4/api/Unix.html#VALselect). This method is the [forseen method](https://www.postgresql.org/docs/18/libpq-notify.html) by PostgreSQL’s [`libpq`](https://www.postgresql.org/docs/18/libpq.html), 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`](https://ocaml.org/manual/5.4/api/Unix.html#VALselect) function, but this function works with abstract [`Unix.file_descr`](https://ocaml.org/manual/5.4/api/Unix.html#TYPEfile_descr) types, and I didn’t find any function to convert an integer into that type.

What can I do?

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [February 6, 2026, 3:55pm UTC](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780/2 "2026-02-06T15:55:01Z")

</div>

```auto
let file_descr_of_int (x : int) : Unix.file_descr = Obj.magic x

```

It’s not nice but will work,

---

<div class="post-metadata">

**Author:** ![jrfondren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jrfondren/32/3871_2.png) [@jrfondren](https://discuss.ocaml.org/u/jrfondren)\
**Post date:** [February 6, 2026, 4:46pm UTC](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780/3 "2026-02-06T16:46:21Z")

</div>

It’ll break on Windows where file\_descr is an opaque type that maps to a HANDLE.

[File descriptors are not integers](https://www.dra27.uk/blog/platform/2025/09/30/file-descriptors-are-not-integers.html) 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](https://github.com/ocaml/ocaml/pull/1990)

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](https://github.com/mmottl/postgresql-ocaml/pull/50)

---

<div class="post-metadata">

**Author:** ![jbe](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jbe](https://discuss.ocaml.org/u/jbe)\
**Post date:** [February 7, 2026, 9:40am UTC](https://discuss.ocaml.org/t/using-package-postgresql-and-waiting-for-a-notify/17780/4 "2026-02-07T09:40:06Z")

</div>

Thank you very much for your responses.

The necessary changes to the `postgresql` package [seem to be small](https://github.com/mmottl/postgresql-ocaml/pull/50/changes), 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:

> <https://github.com/mmottl/postgresql-ocaml/issues/69>
>
> 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).
