#sql #postgres #futures #high-availability

sys pg_async

Asynchronous, HA (master-master) PostgreSQL driver on top of libpq

27 releases

Uses old Rust 2015

0.8.6 Dec 16, 2018
0.8.5 Jun 22, 2017
0.8.3 May 1, 2017
0.5.1 Mar 30, 2017
0.3.1 Dec 31, 2016

#1039 in Asynchronous

Download history 1/week @ 2023-11-06 4/week @ 2023-11-13 56/week @ 2023-11-20 58/week @ 2023-11-27 28/week @ 2023-12-04 31/week @ 2023-12-11 4/week @ 2023-12-18 58/week @ 2023-12-25 28/week @ 2024-01-01 7/week @ 2024-01-08 28/week @ 2024-01-15 39/week @ 2024-01-22 55/week @ 2024-01-29 2/week @ 2024-02-05 113/week @ 2024-02-12 533/week @ 2024-02-19

703 downloads per month

MIT license

70KB
866 lines

pg_async.rs

Asynchronous, HA (master-master) PostgreSQL driver on top of libpq.

crate docs patreon

Vision

  • Designed to work with a master-master replicated database, such as BDR (but standalone databases are supported too).
  • Uses libpq, leveraging it's asynchronous support.
  • Maintains an asynchronous connection to every node of the replicated cluster.
  • Pings the nodes (with SELECT 1) to see who's closer/faster.
  • Every operation is a separate transaction.
  • If a node fails, the operation is transparently retried on another node.
  • Operations are exposed as futures.
  • futures are backed by a thread or two and can be used without a tokio reactor (because KISS).
  • Fast mode: send the operation to every node and return the first answer.
  • Pin mode: send the operation to one of the nodes only (useful to avoid some of the master-master conflicts).
  • There is a JSON helper converting table rows to serde_json objects.
  • If the operation wasn't pipelined already, dropping a Future allows the driver to cancel it.
  • Database-level timeouts can be used to terminate slow operations early.

Notes

The combination of libpq and OpenSSL doesn't work very well during PostgreSQL server restarts. I've seen libpq SEGV-crash reliably when the OpenSSL layer is enabled. For the driver to properly work around PostgreSQL server unavailability and restarts I recommend adding the "sslmode=disable" disabler to the connection strings.

Dependencies

~5MB
~100K SLoC