fix(postgres): close per-schema typeRows inside the iteration#45
Open
robert-chiniquy wants to merge 2 commits into
Open
fix(postgres): close per-schema typeRows inside the iteration#45robert-chiniquy wants to merge 2 commits into
robert-chiniquy wants to merge 2 commits into
Conversation
RevokeAllGrantsFromRole iterates schemas and for each schema queries the database type list via typeRows. The Close was registered with defer, but defer fires only on FUNCTION return, not iteration return -- so each schema iteration accumulated a pending typeRows.Close, and the underlying sql connections were held until the outer function returned. On a database with many schemas this exhausts the connection pool. Replaced the deferred Close with an explicit Close after the inner for typeRows.Next() loop completes. Iteration semantics are unchanged: typeRows.Close on a fully-consumed Rows is safe; the Close on the error path is intentionally not reached because the err branch already records the error and falls through without opening typeRows. How found: occult-go-analysis baton pool scan (2026-05-20), detector defer_in_loop_close.
| revokeError = errors.Join(revokeError, err) | ||
| } | ||
| } | ||
| typeRows.Close() |
Contributor
There was a problem hiding this comment.
🟡 Suggestion: Pre-existing: typeRows.Err() is not checked after the for typeRows.Next() loop. If the iterator stops due to a network or driver error (rather than exhausting rows), the error is silently lost. Consider adding a typeRows.Err() check before Close().
Suggested change
| typeRows.Close() | |
| typeRows.Close() | |
| if err := typeRows.Err(); err != nil { | |
| l.Warn("error iterating types", zap.String("schema", schema), zap.Error(err)) | |
| revokeError = errors.Join(revokeError, err) | |
| } |
Contributor
Connector PR Review: fix(postgres): close per-schema typeRows inside the iterationBlocking Issues: 0 | Suggestions: 0 | Threads Resolved: 0 Review SummaryThe new commit adds a Security IssuesNone found. Correctness IssuesNone found. SuggestionsNone. |
Bot review suggestion on #45: the for typeRows.Next() loop didn't check typeRows.Err() afterward, so a driver/network error that ends iteration before exhaustion was silently lost. Surface it into revokeError alongside the existing Warn-and-join pattern used elsewhere in this function.
btipling
approved these changes
May 26, 2026
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.
`RevokeAllGrantsFromRole` iterates schemas and, for each schema, queries the database type list via `typeRows`. The `Close` was registered with `defer`, but `defer` fires only on FUNCTION return, not iteration return -- so each schema iteration accumulated a pending `typeRows.Close`, and the underlying sql connections were held until the outer function returned. On a database with many schemas this exhausts the connection pool.
Replaced the deferred `Close` with an explicit `Close` after the inner `for typeRows.Next()` loop completes. Iteration semantics are unchanged: `typeRows.Close` on a fully-consumed `Rows` is safe, and the error path didn't reach the Close anyway (the `err != nil` branch records the error and falls through without opening `typeRows`).
How found: occult-go-analysis baton pool scan (2026-05-20), detector `defer_in_loop_close`.