Abstract
SQL-compiler or sqlc generates type-safe Go code from SQL migrations and queries. Most developers use it as a black box, but understanding how it works matters because great engineers don’t just use tools — they understand them, question them, and get better by knowing what’s happening under the hood.
We are going trace the compilation pipeline inside sqlc: how it delegates parsing to PostgreSQL’s own parser via pg_query_go, builds an in-memory schema catalog, performs query analysis and type inference across joins, CTEs, and subqueries, and finally generates Go code for PostgreSQL and pgx driver.
Instead of magic, we are going see a real sqlc generator architecture: ASTs, catalogs, scope resolution, nullability inference, and type mapping.