Skip to content

Errors in Batch cause infinite loop #1678

Closed
@birdayz

Description

@birdayz

Version

1.14.0

What happened?

If a query in the new :batchexec causes an error, batch.Exec runs in an infinite loop.
I think this is caused by this (generated code):

func (b *MyBatchResults) Exec(f func(int, error)) {
	for {
		_, err := b.br.Exec()
		if err != nil && (err.Error() == "no result" || err.Error() == "batch already closed") {
			break
		}
		if f != nil {
			f(b.ind, err)
		}
		b.ind++
	}
}

it only exits if no result/closed. if there is some error, but a "real error" that is not one of these two, the for loop will keep running forever.
a workaround i found is calling Close() on the batch BEFORE running exec. But i am not convinced this is the right way; pgx' tests do run Close only after the right amount of Exec() calls have been made : https://github.com/jackc/pgx/blob/master/batch_test.go

Possible fix could be: when creating the batch, the generated code saves the number of items in the batch into the batch struct. When running exec, the for loop exits if enough calls have been made.

Any opinions? I could probably contribute a fix, if it can be confirmed that my assumptions here are correct.

Relevant log output

1103466 ERROR: duplicate key value violates unique constraint "<redacted>" (SQLSTATE 23505)


### Database schema

_No response_

### SQL queries

_No response_

### Configuration

_No response_

### Playground URL

_No response_

### What operating system are you using?

_No response_

### What database engines are you using?

_No response_

### What type of code are you generating?

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions