Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
An
:iterquery 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:manyqueries returning lots of rows; it also reduces latency because it allows the code to start processing earlier.Usage looks like this:
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
ferrreturn, 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() errorinstead ofiter.Seq2[T, error]mainly because the second is incompatible with adapters likeslices.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
quntil 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 ... RETURNINGorDELETE ... RETURNINGwould 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.