Skip to content

rows.Close() getting all result rows #2153

@vladimirschuka

Description

@vladimirschuka

I have a part of code like this

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions