Skip to content

fix(postgres): close per-schema typeRows inside the iteration#45

Open
robert-chiniquy wants to merge 2 commits into
mainfrom
rch/bugfix/defer-in-loop-rows-close
Open

fix(postgres): close per-schema typeRows inside the iteration#45
robert-chiniquy wants to merge 2 commits into
mainfrom
rch/bugfix/defer-in-loop-rows-close

Conversation

@robert-chiniquy
Copy link
Copy Markdown

`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`.

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.
@robert-chiniquy robert-chiniquy requested a review from arreyder May 20, 2026 18:51
Comment thread pkg/postgres/roles.go
revokeError = errors.Join(revokeError, err)
}
}
typeRows.Close()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)
}

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 20, 2026

Connector PR Review: fix(postgres): close per-schema typeRows inside the iteration

Blocking Issues: 0 | Suggestions: 0 | Threads Resolved: 0
Review mode: incremental since c61bdfeb
View review run

Review Summary

The new commit adds a typeRows.Err() check after the for typeRows.Next() loop, directly addressing the suggestion from the previous review. The error is logged and joined into revokeError, consistent with the existing error-handling pattern in the function. No new issues found.

Security Issues

None found.

Correctness Issues

None found.

Suggestions

None.

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues found.

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.
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues found.

@robert-chiniquy robert-chiniquy requested a review from jirwin May 20, 2026 21:00
@robert-chiniquy robert-chiniquy marked this pull request as ready for review May 20, 2026 21:04
@robert-chiniquy robert-chiniquy requested a review from a team May 20, 2026 21:04
@btipling btipling removed their assignment May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants