Skip to content

Commit 3ad16cc

Browse files
authored
Merge pull request #1686 from cloudquery/fix/profile_events_memory
fix: Don't scan profile events if listener is not set
2 parents 643fec3 + d80b272 commit 3ad16cc

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

conn_process.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package clickhouse
32

43
import (
@@ -185,11 +184,14 @@ func (c *connect) handle(ctx context.Context, packet byte, on *onProcess) error
185184
}
186185
c.debugf("[table columns]")
187186
case proto.ServerProfileEvents:
188-
events, err := c.profileEvents(ctx)
187+
scanEvents := on.profileEvents != nil
188+
events, err := c.profileEvents(ctx, scanEvents)
189189
if err != nil {
190190
return err
191191
}
192-
on.profileEvents(events)
192+
if scanEvents {
193+
on.profileEvents(events)
194+
}
193195
case proto.ServerLog:
194196
logs, err := c.logs(ctx)
195197
if err != nil {

conn_profile_events.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package clickhouse
32

43
import (
@@ -18,12 +17,16 @@ type ProfileEvent struct {
1817
Value int64
1918
}
2019

21-
func (c *connect) profileEvents(ctx context.Context) ([]ProfileEvent, error) {
20+
func (c *connect) profileEvents(ctx context.Context, scanEvents bool) ([]ProfileEvent, error) {
2221
block, err := c.readData(ctx, proto.ServerProfileEvents, false)
2322
if err != nil {
2423
return nil, err
2524
}
2625
c.debugf("[profile events] rows=%d", block.Rows())
26+
if !scanEvents {
27+
c.debugf("[profile events] skipping scan")
28+
return nil, nil
29+
}
2730
var (
2831
events []ProfileEvent
2932
names = block.ColumnsNames()

context.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func queryOptionsUserLocation(ctx context.Context) *time.Location {
260260
}
261261

262262
func (q *QueryOptions) onProcess() *onProcess {
263-
return &onProcess{
263+
onProcess := &onProcess{
264264
logs: func(logs []Log) {
265265
if q.events.logs != nil {
266266
for _, l := range logs {
@@ -278,12 +278,16 @@ func (q *QueryOptions) onProcess() *onProcess {
278278
q.events.profileInfo(p)
279279
}
280280
},
281-
profileEvents: func(events []ProfileEvent) {
282-
if q.events.profileEvents != nil {
283-
q.events.profileEvents(events)
284-
}
285-
},
286281
}
282+
283+
profileEventsHandler := q.events.profileEvents
284+
if profileEventsHandler != nil {
285+
onProcess.profileEvents = func(events []ProfileEvent) {
286+
profileEventsHandler(events)
287+
}
288+
}
289+
290+
return onProcess
287291
}
288292

289293
// clone returns a copy of QueryOptions where Settings and Parameters are safely mutable.

tests/issues/1685_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package issues
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/ClickHouse/clickhouse-go/v2"
8+
"github.com/ClickHouse/clickhouse-go/v2/tests"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func benchmark1685(ctx context.Context, conn clickhouse.Conn) error {
13+
for i := 0; i < 10_000; i++ {
14+
err := conn.Exec(ctx,
15+
"INSERT INTO test_xxxx VALUES (?, ?, [1, 2, 3, 4, 5, 6, 7, 8, 9], now())",
16+
i, "Golang SQL database driver", false)
17+
if err != nil {
18+
return err
19+
}
20+
}
21+
return nil
22+
}
23+
24+
func BenchmarkIssue1685(b *testing.B) {
25+
conn, err := tests.GetConnectionTCP("issues", nil, nil, nil)
26+
ctx := context.Background()
27+
require.NoError(b, err)
28+
29+
const ddl = `CREATE TABLE test_xxxx (Col1 UInt64, Col2 String, Col3 Array(UInt8), Col4 DateTime) Engine ReplacingMergeTree() ORDER BY Col1`
30+
err = conn.Exec(ctx, ddl)
31+
require.NoError(b, err)
32+
defer func() {
33+
conn.Exec(ctx, "DROP TABLE IF EXISTS test_xxxx")
34+
}()
35+
36+
for k := 0; k < b.N; k++ {
37+
require.NoError(b, benchmark1685(ctx, conn))
38+
}
39+
}

0 commit comments

Comments
 (0)