Batch errors associated with wrong statement
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
When using `SendBatch()`, if one of the statements in the batch returns an error during prepared statement generation, the error is returned on the first result in the `BatchResult`. This is incorrect as the error may have been a result of latter statements.
For example:
```
batch := &pgx.Batch{}
batch.Queue("create temp table batchtest (col int)")
batch.Queue("insert into batchtest values (1)")
result := conn.SendBatch(context.Background(), batch)
_, err := result.Exec()
fmt.Print(err)
```
```
ERROR: relation "batchtest" does not exist (SQLSTATE 42P01)
```
This makes it very confusing as returning the error on the first `result.Exec()` implies the error came from `create temp table ...`.
I'm not sure the right way this should be solved. However I would argue that if a prepared statement doesn't already exist in the cache, it should not be making a round trip to create it, as this defeats the entire point of using batch statements.
Additionally, in my use case it's actually the usage of prepared statements that is causing the problem. The second statement is throwing an error because a table being referenced doesn't exist. However the first statement in the batch is the one creating it.
Contributor guide
Research direction
Start at the SendBatch() entry point and reproduce the example with the two queued statements. Trace how prepared-statement generation errors are assigned to BatchResult entries, then determine the intended result-to-statement association. Done means an error from a later statement is reported by the corresponding result rather than the first one, with regression coverage for this scenario.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100