GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add the :iter query annotation by sgielen · Pull Request #4646 · sqlc-dev/sqlc

Conversation

@sgielen

@sgielen sgielen commented Oct 16, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

An :iter query generates a method returning an iter.Seq over the query's rows, plus a function returning the query's error once the loop is done. This saves memory, especially for :many queries returning lots of rows; it also reduces latency because it allows the code to start processing earlier.

Usage looks like this:

authors, ferr := q.ListAuthors(ctx)
for author := range authors {
  // ...
}
if err := ferr(); err != nil {
  // ...
}

There is some discussion about the best syntax for iterations (e.g. Close() functions) in #720. I've used this approach in production for a while now, and it feels very comfortable to me. Due to the ferr return, the compiler will warn us when we forget to check the error.

In order to keep this PR reviewable, it contains only the codegen changes, the PostgreSQL example and the documentation update. Further tests are in #4647.


For future reference, some of my considerations while designing this.

I decided for iter.Seq[T], func() error instead of iter.Seq2[T, error] mainly because the second is incompatible with adapters like slices.Collect. Also, this way, the loop doesn't need to check for an error every time, and it's harder to forget to check the error because the argument would be unused in that case.

Initially, I wanted to start the query eagerly when the function was called, so that the yield loop only scans the next row. It felt slightly more elegant to me, and also, it starts the query ever-so-slightly earlier than within the yield closure. However, the main downside that made me change my mind, is that that method causes the query to leak if user never ranges over the iterator. This can happen due to panics, early returns, context cancellations, etc. Smaller downsides is that that method reserves q until the range-over is done (which I don't mind very much), and that I didn't like the generated code much.

Because the query starts within the loop, it would be trivial to allow ranging over the objects twice, where the second range just runs the query again. While that feels natural for a SELECT loop, I decided not to allow this, because an INSERT ... RETURNING, UPDATE ... RETURNING or DELETE ... RETURNING would also be executed twice in that case. And this might not be obvious: chaining the sequence with e.g. slices.Collect(seq) would also trigger this. I lean towards defensive code, and so I decided to make the second range return an explicit error; intentionally repeating the query only costs one extra line of code, after all.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kofalt, LarsStegman, and Jille reacted with hooray emoji
All reactions
  • 🎉 3 reactions
sgielen added 2 commits October 16, 2026 18:57
@sgielen
Add the :iter query annotation
289098b
A :iter query generates a method returning an iter.Seq over the query's
rows, plus a function returning the query's error once the loop is done.
The PostgreSQL authors example shows its use.
@sgielen
Add documentation for the :iter query annotation
138b1c9
@sgielen sgielen mentioned this pull request Oct 16, 2026
Add tests for PR #4646 (:iter query annotation) #4647
Open
@sgielen
sgielen marked this pull request as ready for review October 16, 2026 17:56
@sgielen sgielen mentioned this pull request Oct 16, 2026
Ability to return an iterator on a "many" query #720
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@sgielen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.