Skip to content
Hypha Stack

postgres-eio

A native Postgres driver for OCaml 5 and Eio.

Source on GitHubDocumentationLatest: 0.5.1

About

Feature-complete: TLS with SCRAM and client certificates, a connection pool, failover and load balancing across hosts, streamed COPY, and LISTEN/NOTIFY that reconnects by itself. Errors and logs never contain your parameters or passwords.

Data and persistence

Install

From Hypha's opam repository, on OCaml 5.5 or later:

opam repo add hypha https://github.com/hyphatech/opam-repository.git
opam install postgres-eio

Then in your dune file:

(libraries postgres-eio eio_main)

In code

  1. A query, its rows folded as they arrive and never buffered.

    let names =
      Postgres_eio.query conn
        "select name from customers where country = $1"
        ~params:[ Some "GB" ] ~init:[]
        ~row:(fun names row ->
          match row with [| Some name |] -> name :: names | _ -> names)
  2. Both libpq forms of connection string; with none given, a connection that crosses a network is encrypted.

    postgres://user:pass@host:5432/db?sslmode=require
    host=db user=app sslmode=require

Under load

Queries

libpq
4,090
postgres-eio
3,510
postgres_async
2,040

Rows read

postgres-eio
7.2M
postgres_async
6.9M
libpq
5.6M

Inserts

postgres-eio (pipelined)
396,900
libpq
3,970
postgres_async
1,840

Operations per second on one connection, Postgres 18.6 in Docker on an Apple M4 Pro, postgres-eio 0.5.1, median of three runs. A query is one round trip, rows are read 100,000 a query, and postgres-eio pipelines its 1,000 inserts into one round trip, which libpq and postgres_async cannot. libpq, in C, spends about a third of the CPU a query.

Ontology

Build with Hypha.