jackc / jackc/pgx

rows.Close() getting all result rows

Open
#2,153 20 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

I have a part of code like this
```go
rows, err := dbconn.Query(ctx, sql)
defer rows.Close()

for rows.Next() {
msg, err := pgx.RowToAddrOfStructByName[Sometype](rows)
if err != nil {
return fmt.Errorf("error scanning row: %w", err)
}
}

```
but when I get an error, app tries to close rows and it is stuck in a loop where it is getting all result rows, which I don't need anymore. Particularly here:
pgconn.go 1593
```go
func (rr *ResultReader) Close() (CommandTag, error) {
if rr.closed {
return rr.commandTag, rr.err
}
rr.closed = true
//// HERE ->
for !rr.commandConcluded {
_, err := rr.receiveMessage()
if err != nil {
return CommandTag{}, rr.err
}
}

```
and If your query returns huge amount of rows you just wait until all rows are read.
Do I do something wrong?
Shouldn't I close the rows? Or ..?

I expect that `rows.Close() ` just stops the query in the database and cleans the memory.

Version:
v5.7.1

Contributor guide

Open the contributing guide

Research direction

Start with pgconn.go around ResultReader.Close and trace receiveMessage while reviewing the reported rows.Close() usage with Query and RowToAddrOfStructByName. Reproduce the behavior with a large result set and an early scan error; done means the expected close behavior is established and covered by an appropriate regression test or documented as intentional.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.