-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Batch errors associated with wrong statement #872
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.