Skip to content

Batch errors associated with wrong statement #872

@phemmer

Description

@phemmer

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.

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