Skip to content

Instantly share code, notes, and snippets.

Last active May 9, 2023 14:45
Show Gist options
  • Save StevenACoffman/a0dd93025d892b44aea992f2e01eaa00 to your computer and use it in GitHub Desktop.
Save StevenACoffman/a0dd93025d892b44aea992f2e01eaa00 to your computer and use it in GitHub Desktop.

sqlc isn’t an ORM, but it implements one of the most useful features of one – mapping a query back into a struct without the need for boilerplate. If you have query with a SELECT * or RETURNING *, it knows which fields a table is supposed to have, and emits the result to a standard struct representing its records. All queries for a particular table that return its complete set of fields get to share the same output struct.

Rather than implement its own partially-complete SQL parser, sqlc uses PGAnalyze’s excellent pg_query_go, which bakes in the same query parser that Postgres really uses. It’s never given me trouble so far – even complex queries with unusual Postgres embellishments work.

This query parsing also gives you some additional pre-runtime code verification. It won’t protect you against logical bugs, but it won’t compile invalid SQL queries, which is a far shot better than the guarantees you get with SQL-in-Go-strings. And thanks to SQL’s declarative nature, it tends to produce fewer bugs than comparable procedural code. You’ll still want to write tests, but you don’t need to test every query and corner case as exhaustively.

database/sql is database agnostic, which is kind of nice, but by extension that means it conforms to the lowest common denominator, and has no support for Postgres-specific features.

The library pgx is a very well-written and very thorough package for full-featured, performant connections to Postgres. However, it’s opinionated about not offering any ORM-like features, and gets you very little beyond a basic query interface. Like with database/sql, hydrating database results into structs is painful – not only do you have to list target fields off ad nauseam in a SELECT statement, but you also have to Scan them into a struct manually.

When we started using sql in Go, a major reason not to use pgx is that sqlc didn’t support pgx. A 2021 pull request has addressed this problem by giving sqlc support for multiple drivers, and the feature’s now available and mature in the sqlc’s latest release.

When we started using SQL in Go, we used sqlmock as a widely available mocking library, but it doesn't support pgx. Since then, has emerged and matured to provide mature and full-featured pgx mocking.

On top of all that, pgx/v5 was a very major change, which provides a dramatic number of new features and performance improvements. Experimental support for :code:pgx/v5 was added in sqlc v1.17.2. Full support will be included in sqlc v1.18.0. Until then, you'll need to pass the :code:--experimental

I would like us to start to plan to move to using pgx (at least v4) before we move progress reports to use kacontext for database connections.




database/sql provides a concurrency safe connection pool in *sql.DB

// stdlib -> pgx
pgxConn, err := stdlib.AcquireConn(sqlDB)
if err != nil {
  return errors.New("failed to acquire pgx connection")
defer func() {
  if err := stdlib.ReleaseConn(sqlDB, pgxConn); err != nil {
    log.Printf("failed to release connection: %v\n", err) 



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment