#4426·sqlx

Postgres: connect over a caller-provided socket, and a wasm-safe `Instant`, for hosts like Cloudflare Workers

Author: iamralchCreated Sep 28, 2026Updated Oct 1, 2026

I have found these related issues/pull requests

Relates to #1145, #2985, #4056

Description

Hosts that give you their own sockets have no way to run the Postgres driver, even though the protocol code itself compiles fine for them. My case is Cloudflare Workers (wasm32-unknown-unknown): the runtime hands out TCP sockets through its connect() API (for example, to reach Postgres through Hyperdrive), and sqlx-postgres 0.9 builds for that target once getrandom's wasm_js feature is on. Two things then panic at runtime:

  1. Connecting. PgStream::connect always goes through sqlx_core::net::connect_tcp (or connect_uds). With no tokio or async-io runtime, that ends in rt::missing_rt, and there is no public way to hand PgConnection a socket opened some other way.
  2. Every query. QueryLogger::new calls std::time::Instant::now(), which panics on wasm32-unknown-unknown with time not implemented on this platform. The migrators call it too.

Prefered solution

Two small, additive changes, both on a branch: https://github.com/transact-rs/sqlx/compare/main...iamralch:sqlx:wasm-custom-socket

  1. PgConnection::connect_with_socket(&options, socket), taking any sqlx_core::net::Socket the caller has already connected. The host, port and socket path in the options are ignored; everything else, including ssl_mode, applies as it does for connect_with. establish now goes through the same path after opening its own socket, so existing connections are unchanged.
  2. sqlx_core::rt::Instant: std::time::Instant everywhere except wasm32-unknown-unknown, where it is web_time::Instant (the host's performance.now()). Used in the query logger and the Postgres, MySQL and SQLite migrators. web-time is a dependency on that target only.

This is deliberately smaller than #4056. It doesn't add a runtime to sqlx; it lets the caller bring the transport, which should also cover WASI sockets or a WebSocket tunnel like #1145, without sqlx knowing about any of them. It leaves Pool and PgListener alone: both need a runtime to spawn tasks, and on hosts like Workers you want one connection per request anyway.

Tested:

  • A Worker under wrangler dev --local, against Postgres 17 with SCRAM auth: connect, bound parameters and scalar types, 2,000 rows spanning many socket reads, unique-violation mapping, rollback and commit, prepared-statement reuse, sqlx::migrate!() migrations, ping and close. All pass. Reverting only the Instant change makes the same Worker panic with time not implemented on this platform.
  • On the host: a new it_connects_with_socket test (a tokio TcpStream opened by the test, then connect_with_socket), plus the existing tests/postgres/postgres.rs (58 passed, 2 ignored) and tests/postgres/migrate.rs (4 passed).

TLS doesn't need a hook: a caller whose host upgrades the socket itself (Workers' startTls()) can send the SSLRequest, upgrade, and pass the result in with ssl_mode = Disable.

If this direction is acceptable, I'll open the PR from that branch. The same shape would work for MySQL (MySqlConnection::connect_with_socket), which Hyperdrive also supports; I've kept this to Postgres to start.

Is this a breaking change? Why or why not?

No. Both changes only add public items (PgConnection::connect_with_socket, sqlx_core::rt::Instant). sqlx_core::rt::Instant is std::time::Instant on every target except wasm32-unknown-unknown, where Instant::now() panicked before.