feat(codegen/go): PoC for opt-in iter.Seq2 companions on :many queries - #4488
feat(codegen/go): PoC for opt-in iter.Seq2 companions on :many queries#4488Tilzen wants to merge 2 commits into
Conversation
Implement emit_iterators PoC from docs/emit-iterators-prd.md: lazy iter.Seq2[T, error] methods alongside existing slice APIs, with global or explicit_only scope and :stream / :many:stream query annotations. Includes stdlib and pgx templates, config options, end-to-end testdata, and codegen integration tests.
…rix tests Add SQLite runtime tests (parity, lazy start, early break, parameterized queries), codegen matrix for global/explicit/pgx/disabled scenarios, opts validation tests, and golden replay fixtures for all cases.
|
Thanks for working on this. One limitation I think is worth considering is allocation control. The iterator avoids retaining the full result set, but each row is still scanned into a fresh result value. Dynamic fields such as strings, byte slices, or driver-specific values may require additional allocations, which are only reclaimed when the GC runs. The iterator therefore removes the growing It would be valuable for the streaming API to give callers some way to control reuse of the scan destination. A synchronous consumer could reuse a single destination, while a consumer with multiple results in flight could obtain destinations from a If that can be supported cleanly by the iterator API, it may also cover the allocation-control use case discussed in #4108, without requiring separate low-level query and scan helpers. If it cannot be supported safely within the iterator abstraction, then the lower-level approach from #4108 would still be useful alongside the iterator. I also noticed that |
Why
Revives the long-standing request for streaming/lazy
:manyquery results (#720). Today, sqlc always materializes:manyinto[]T, which is fine for small result sets but costly for exports, sync jobs, and backfills.Go 1.23+ shipped native iterators (
iter.Seq2). Kyle noted that this unblocks native iterator generation; PR #3631 explored an implementation but closed without merge after API design remained open.This PR is not a merge-ready feature. It is a proof of concept (PoC) posted for design feedback before investing in a production implementation. See the discussion proposal in #720 and related #4464.
PoC reference (fork): https://github.com/Tilzen/sqlc/tree/feat/emit-iterators-poc
PRD in PoC branch:
docs/emit-iterators-prd.mdWhat Changed (PoC scope)
Opt-in config (zero breaking changes to default
:many→[]T):Generates a lazy companion alongside existing slice methods:
Also supports query annotations for
explicit_onlyscope:-- name: StreamAuthors :stream-- name: ListAuthors :many:streamPoC includes: stdlib + pgx/v5 templates, metadata parsing, config wiring, PRD doc.
Explicitly out of PoC scope:
callback/rowsstyles, eager mode,emit_interfaceiter methods, Python/Kotlin generators, full driver matrix.Open Design Questions (feedback welcome)
ItervsStream?emit_iterators: globaland:streamannotations?:streamremain a separate query kind or only:many:stream?Test Plan
PoC test coverage (all passing on the fork branch):
:stream,:many:stream),emit_iterators: falseregression, parameterized:many, pgx/v5TestReplay/emit_iterators*fixturesrange), earlybreak+ connection reuse, parameterized queriesReferences
Notes
seq2+ lazy +Iterprefix), I can refine the PoC toward a production-ready PR in follow-up commits.