Awesome Go
Read post

pgx.CollectRows: Nice APIs Don't Have To Be Slow

A deep dive into the performance characteristics of pgx's CollectRows and AppendRows convenience APIs for Go. The author benchmarks these APIs against a hand-written loop, finds they are 1.8× slower and allocate 10× more memory, then explains why: repeated reflection-based struct field lookups, heap escapes of temporary values, and per-row allocation of scan target slices. The post covers patches submitted to pgx (landed in v5.6) that added caching, and introduces pgx-collect, a drop-in compatible library that redesigns the core abstraction from a per-row RowToFunc to a Scanner interface with separate Initialize and ScanRowInto methods. This allows per-query setup to run once, scan targets to be reused, and data to be written directly into the output slice — reducing overhead from 180% to near-baseline. Key lessons: benchmark convenience APIs, choose abstractions that match the concrete work, cache reflection metadata by type, and minimize heap allocations.

    #performance#golang#postgresql
Aug 05•14m read time•From zolstein.substack.com
Post cover image
Table of contents
The APIsThe benchmarkWhat is AppendRows doing?Changing the abstractionLessons learned
287 Impressions
Awesome Go's image
Awesome Go

Awego's platform is dedicated to providing insights and resources for developers and technology ent...

1.2K Followers

•

8.2K Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard