Skip to content

Commit 82fbe49

Browse files
authored
Merge pull request #2372 from WGH-/improve-batch-doc
Add note about QueuedQuery.Exec and BatchResults.Close
2 parents 594d9d6 + 248afe6 commit 82fbe49

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

‎batch.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (qq *QueuedQuery) QueryRow(fn func(row Row) error) {
4343
}
4444

4545
// Exec sets fn to be called when the response to qq is received.
46+
//
47+
// Note: for simple batch insert uses where it is not required to handle
48+
// each potential error individually, it's sufficient to not set any callbacks,
49+
// and just handle the return value of BatchResults.Close.
4650
func (qq *QueuedQuery) Exec(fn func(ct pgconn.CommandTag) error) {
4751
qq.Fn = func(br BatchResults) error {
4852
ct, err := br.Exec()
@@ -83,7 +87,7 @@ func (b *Batch) Len() int {
8387

8488
type BatchResults interface {
8589
// Exec reads the results from the next query in the batch as if the query has been sent with Conn.Exec. Prefer
86-
// calling Exec on the QueuedQuery.
90+
// calling Exec on the QueuedQuery, or just calling Close.
8791
Exec() (pgconn.CommandTag, error)
8892

8993
// Query reads the results from the next query in the batch as if the query has been sent with Conn.Query. Prefer
@@ -98,6 +102,9 @@ type BatchResults interface {
98102
// QueuedQuery.Query, QueuedQuery.QueryRow, or QueuedQuery.Exec will be called. If a callback function returns an
99103
// error or the batch encounters an error subsequent callback functions will not be called.
100104
//
105+
// For simple batch inserts inside a transaction or similar queries, it's sufficient to not set any callbacks,
106+
// and just handle the return value of Close.
107+
//
101108
// Close must be called before the underlying connection can be used again. Any error that occurred during a batch
102109
// operation may have made it impossible to resyncronize the connection with the server. In this case the underlying
103110
// connection will have been closed.

0 commit comments

Comments
 (0)