Fastest PostgreSQL Client Library for Go

Rizal Widyarta Gowandy
Level Up Coding
Published in
2 min readJul 19, 2021

--

Benchmarking some popular PostgreSQL client libraries for Go to find the fastest one.

Motivation

Increasing RPS (request per second) is not an easy task. Changing your code to increase performance is even more challenging. There are several popular libraries out there, and almost every single one claims to be the fastest. The problem arises when the benchmarking result is out of date. Even worse when the one who did the benchmark tunes the library in a non-general way. Hence, the achieve the same performance is pretty much impossible for the general user.

The purpose of the benchmark is to find the fastest PostgreSQL client library for Go with a controlled environment and without any magic hack to tune any of the libraries.

Environment

Go client libraries used:

Specification:

  • Using PostgreSQL v13.3.0
  • Instance size is 4 core 8 GB
  • With connection pooling where the max connection pool size is 16

Benchmarking Result

The result below is the average result after running the benchmark 25 times.

Understanding the Result

There are four columns on the right side of each SQL operation like CREATE included in the result above. The meaning for each one of them is:

  • iteration => the number of iterations of the operation run, a higher value is better.
  • ns/op => this is the average time each function call takes to complete, a smaller value is better.
  • B/op => this is the average memory used for each function call, a smaller value is better.
  • allocs/o => this is the average memory allocation for each function call, a smaller value is better.

We can see jackc/pgx outperformed other libraries almost across all the operation benchmarks. Excluding the UPDATE operation where uptrace/bun actually outperformed it.

Conclusion

By looking at the result above, we can conclude that jackc/pgx is the overall fastest PostgreSQL client library for Go. It’s recommended to use it with a dedicated max connection pool size based on your own environment. Some testing is required to find the balance max connection pool size depending on your most heavy operations.

--

--

Software Engineer | INTJ | Choleric | The Questioner (CD) | Creator & Advisor