Comparison of Go libraries for working with PostgreSQL
Runs benchmarks on the same "typical" database workflow implemented in various ways.
results.md and its chart are regenerated by the Benchmarks workflow whenever the code
or its dependencies change on main. It runs on a shared GitHub-hosted runner, so compare implementations within one
run rather than absolute numbers across runs.
Each implementation aims to be idiomatic for its library and to use the features that make it fastest for the
workload: single-statement (or COPY) bulk inserts, the library's statement caching, and the fastest driver it
supports.
# Run tests
go test ./...
# Run benchmarks and write the report
go test -run '^$' -bench . -benchmem -count 6 . | tee bench.txt
go run ./internal/benchreport -svg results.svg bench.txt > results.md
# Reset if cleanup was not performed by previous run
psql postgres -c 'DROP DATABASE dbeval_db'
Don't copy the check function to your own code! More often than not you want to return errors to your caller, with
optional context from your error site. When you get ready to show the error to your user, either print it out or
display it, optionally translating into an understandable error message. I'm using panics here to simplify the test
harness. The DB manipulation implementations then conform to that. This is not a typical use-case so it's not likely
to be the best approach for your problem.
- github.com/lib/pq
- github.com/jackc/v5/pgx
- github.com/jackc/pgx/v5/stdlib
- github.com/upper/db/v4
- github.com/jmoiron/sqlx
- gorm.io/gorm
- github.com/gocraft/dbr/v2
- xorm.io/xorm
- github.com/go-pg/pg/v10
- github.com/go-ozzo/ozzo-dbx
- https://github.com/uptrace/bun
- https://github.com/uptrace/bun on the pgx stdlib driver
- https://sqlc.dev/