Skip to content

Commit 03f32c0

Browse files
authored
Merge branch 'master' into zeronull-int-valuer
2 parents 5cb495f + 82fbe49 commit 03f32c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+645
-292
lines changed

.golangci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See for configurations: https://golangci-lint.run/usage/configuration/
2+
version: 2
3+
4+
# See: https://golangci-lint.run/usage/formatters/
5+
formatters:
6+
default: none
7+
enable:
8+
- gofmt # https://pkg.go.dev/cmd/gofmt
9+
- gofumpt # https://github.com/mvdan/gofumpt
10+
11+
settings:
12+
gofmt:
13+
simplify: true # Simplify code: gofmt with `-s` option.
14+
15+
gofumpt:
16+
# Module path which contains the source code being formatted.
17+
# Default: ""
18+
module-path: github.com/jackc/pgx/v5 # Should match with module in go.mod
19+
# Choose whether to use the extra rules.
20+
# Default: false
21+
extra-rules: true

batch.go

Lines changed: 29 additions & 3 deletions
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.
@@ -207,7 +214,6 @@ func (br *batchResults) Query() (Rows, error) {
207214
func (br *batchResults) QueryRow() Row {
208215
rows, _ := br.Query()
209216
return (*connRow)(rows.(*baseRows))
210-
211217
}
212218

213219
// Close closes the batch operation. Any error that occurred during a batch operation may have made it impossible to
@@ -220,6 +226,8 @@ func (br *batchResults) Close() error {
220226
}
221227
br.endTraced = true
222228
}
229+
230+
invalidateCachesOnBatchResultsError(br.conn, br.b, br.err)
223231
}()
224232

225233
if br.err != nil {
@@ -378,7 +386,6 @@ func (br *pipelineBatchResults) Query() (Rows, error) {
378386
func (br *pipelineBatchResults) QueryRow() Row {
379387
rows, _ := br.Query()
380388
return (*connRow)(rows.(*baseRows))
381-
382389
}
383390

384391
// Close closes the batch operation. Any error that occurred during a batch operation may have made it impossible to
@@ -391,6 +398,8 @@ func (br *pipelineBatchResults) Close() error {
391398
}
392399
br.endTraced = true
393400
}
401+
402+
invalidateCachesOnBatchResultsError(br.conn, br.b, br.err)
394403
}()
395404

396405
if br.err == nil && br.lastRows != nil && br.lastRows.err != nil {
@@ -441,3 +450,20 @@ func (br *pipelineBatchResults) nextQueryAndArgs() (query string, args []any, er
441450
br.qqIdx++
442451
return bi.SQL, bi.Arguments, nil
443452
}
453+
454+
// invalidates statement and description caches on batch results error
455+
func invalidateCachesOnBatchResultsError(conn *Conn, b *Batch, err error) {
456+
if err != nil && conn != nil && b != nil {
457+
if sc := conn.statementCache; sc != nil {
458+
for _, bi := range b.QueuedQueries {
459+
sc.Invalidate(bi.SQL)
460+
}
461+
}
462+
463+
if sc := conn.descriptionCache; sc != nil {
464+
for _, bi := range b.QueuedQueries {
465+
sc.Invalidate(bi.SQL)
466+
}
467+
}
468+
}
469+
}

batch_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ func TestConnSendBatchCloseRowsPartiallyRead(t *testing.T) {
488488
defer cancel()
489489

490490
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
491-
492491
batch := &pgx.Batch{}
493492
batch.Queue("select n from generate_series(0,5) n")
494493
batch.Queue("select n from generate_series(0,5) n")
@@ -539,7 +538,6 @@ func TestConnSendBatchCloseRowsPartiallyRead(t *testing.T) {
539538
if err != nil {
540539
t.Fatal(err)
541540
}
542-
543541
})
544542
}
545543

@@ -550,7 +548,6 @@ func TestConnSendBatchQueryError(t *testing.T) {
550548
defer cancel()
551549

552550
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
553-
554551
batch := &pgx.Batch{}
555552
batch.Queue("select n from generate_series(0,5) n where 100/(5-n) > 0")
556553
batch.Queue("select n from generate_series(0,5) n")
@@ -580,7 +577,6 @@ func TestConnSendBatchQueryError(t *testing.T) {
580577
if pgErr, ok := err.(*pgconn.PgError); !(ok && pgErr.Code == "22012") {
581578
t.Errorf("br.Close() => %v, want error code %v", err, 22012)
582579
}
583-
584580
})
585581
}
586582

@@ -591,7 +587,6 @@ func TestConnSendBatchQuerySyntaxError(t *testing.T) {
591587
defer cancel()
592588

593589
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
594-
595590
batch := &pgx.Batch{}
596591
batch.Queue("select 1 1")
597592

@@ -607,7 +602,6 @@ func TestConnSendBatchQuerySyntaxError(t *testing.T) {
607602
if err == nil {
608603
t.Error("Expected error")
609604
}
610-
611605
})
612606
}
613607

@@ -618,7 +612,6 @@ func TestConnSendBatchQueryRowInsert(t *testing.T) {
618612
defer cancel()
619613

620614
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
621-
622615
sql := `create temporary table ledger(
623616
id serial primary key,
624617
description varchar not null,
@@ -647,7 +640,6 @@ func TestConnSendBatchQueryRowInsert(t *testing.T) {
647640
}
648641

649642
br.Close()
650-
651643
})
652644
}
653645

@@ -658,7 +650,6 @@ func TestConnSendBatchQueryPartialReadInsert(t *testing.T) {
658650
defer cancel()
659651

660652
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
661-
662653
sql := `create temporary table ledger(
663654
id serial primary key,
664655
description varchar not null,
@@ -687,7 +678,6 @@ func TestConnSendBatchQueryPartialReadInsert(t *testing.T) {
687678
}
688679

689680
br.Close()
690-
691681
})
692682
}
693683

@@ -698,7 +688,6 @@ func TestTxSendBatch(t *testing.T) {
698688
defer cancel()
699689

700690
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
701-
702691
sql := `create temporary table ledger1(
703692
id serial primary key,
704693
description varchar not null
@@ -757,7 +746,6 @@ func TestTxSendBatch(t *testing.T) {
757746
if err != nil {
758747
t.Fatal(err)
759748
}
760-
761749
})
762750
}
763751

@@ -768,7 +756,6 @@ func TestTxSendBatchRollback(t *testing.T) {
768756
defer cancel()
769757

770758
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
771-
772759
sql := `create temporary table ledger1(
773760
id serial primary key,
774761
description varchar not null
@@ -795,7 +782,6 @@ func TestTxSendBatchRollback(t *testing.T) {
795782
if count != 0 {
796783
t.Errorf("count => %v, want %v", count, 0)
797784
}
798-
799785
})
800786
}
801787

@@ -855,7 +841,6 @@ func TestConnBeginBatchDeferredError(t *testing.T) {
855841
defer cancel()
856842

857843
pgxtest.RunWithQueryExecModes(ctx, t, defaultConnTestRunner, nil, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
858-
859844
pgxtest.SkipCockroachDB(t, conn, "Server does not support deferred constraint (https://github.com/cockroachdb/cockroach/issues/31632)")
860845

861846
mustExec(t, conn, `create temporary table t (
@@ -894,7 +879,6 @@ func TestConnBeginBatchDeferredError(t *testing.T) {
894879
if err, ok := err.(*pgconn.PgError); !ok || err.Code != "23505" {
895880
t.Fatalf("expected error 23505, got %v", err)
896881
}
897-
898882
})
899883
}
900884

bench_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc
516516
}
517517

518518
return rowCount, nil
519-
520519
}
521520

522521
func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) {
@@ -535,7 +534,8 @@ func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) {
535534
src := newBenchmarkWriteTableCopyFromSrc(n)
536535

537536
_, err := multiInsert(conn, "t",
538-
[]string{"varchar_1",
537+
[]string{
538+
"varchar_1",
539539
"varchar_2",
540540
"varchar_null_1",
541541
"date_1",
@@ -547,7 +547,8 @@ func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) {
547547
"tstz_2",
548548
"bool_1",
549549
"bool_2",
550-
"bool_3"},
550+
"bool_3",
551+
},
551552
src)
552553
if err != nil {
553554
b.Fatal(err)
@@ -568,7 +569,8 @@ func benchmarkWriteNRowsViaCopy(b *testing.B, n int) {
568569

569570
_, err := conn.CopyFrom(context.Background(),
570571
pgx.Identifier{"t"},
571-
[]string{"varchar_1",
572+
[]string{
573+
"varchar_1",
572574
"varchar_2",
573575
"varchar_null_1",
574576
"date_1",
@@ -580,7 +582,8 @@ func benchmarkWriteNRowsViaCopy(b *testing.B, n int) {
580582
"tstz_2",
581583
"bool_1",
582584
"bool_2",
583-
"bool_3"},
585+
"bool_3",
586+
},
584587
src)
585588
if err != nil {
586589
b.Fatal(err)
@@ -611,6 +614,7 @@ func BenchmarkWrite5RowsViaInsert(b *testing.B) {
611614
func BenchmarkWrite5RowsViaMultiInsert(b *testing.B) {
612615
benchmarkWriteNRowsViaMultiInsert(b, 5)
613616
}
617+
614618
func BenchmarkWrite5RowsViaBatchInsert(b *testing.B) {
615619
benchmarkWriteNRowsViaBatchInsert(b, 5)
616620
}
@@ -626,6 +630,7 @@ func BenchmarkWrite10RowsViaInsert(b *testing.B) {
626630
func BenchmarkWrite10RowsViaMultiInsert(b *testing.B) {
627631
benchmarkWriteNRowsViaMultiInsert(b, 10)
628632
}
633+
629634
func BenchmarkWrite10RowsViaBatchInsert(b *testing.B) {
630635
benchmarkWriteNRowsViaBatchInsert(b, 10)
631636
}
@@ -641,6 +646,7 @@ func BenchmarkWrite100RowsViaInsert(b *testing.B) {
641646
func BenchmarkWrite100RowsViaMultiInsert(b *testing.B) {
642647
benchmarkWriteNRowsViaMultiInsert(b, 100)
643648
}
649+
644650
func BenchmarkWrite100RowsViaBatchInsert(b *testing.B) {
645651
benchmarkWriteNRowsViaBatchInsert(b, 100)
646652
}
@@ -672,6 +678,7 @@ func BenchmarkWrite10000RowsViaInsert(b *testing.B) {
672678
func BenchmarkWrite10000RowsViaMultiInsert(b *testing.B) {
673679
benchmarkWriteNRowsViaMultiInsert(b, 10000)
674680
}
681+
675682
func BenchmarkWrite10000RowsViaBatchInsert(b *testing.B) {
676683
benchmarkWriteNRowsViaBatchInsert(b, 10000)
677684
}
@@ -1043,7 +1050,6 @@ func BenchmarkSelectRowsScanDecoder(b *testing.B) {
10431050
}
10441051
for _, format := range formats {
10451052
b.Run(format.name, func(b *testing.B) {
1046-
10471053
br := &BenchRowDecoder{}
10481054
for i := 0; i < b.N; i++ {
10491055
rows, err := conn.Query(

0 commit comments

Comments
 (0)