Skip to content

bug(performance-memory): Unnecessary memory allocations from reading profile events #1685

@erezrokah

Description

@erezrokah

Observed

ClickHouse Go driver still scans profile events even when a profileEvents listener is not set, causing unnecessary memory allocations

Expected behaviour

Ideally profile events could be disabled via a session setting, but log_profile_events: 0 doesn't seem to help here. Even if log_profile_events is set to 1, the Go driver shouldn't scan events if there's no listener.

Code example

Example test

func benchmarkProfileEvents(ctx context.Context, conn clickhouse.Conn) error {
	for i := 0; i < 10_000; i++ {
		err := conn.Exec(ctx, fmt.Sprintf(`INSERT INTO test_profile_events VALUES (
			%d, '%s', [1, 2, 3, 4, 5, 6, 7, 8, 9], now()
		)`, i, "Golang SQL database driver"), false)
		if err != nil {
			return err
		}
	}
	return nil
}

func BenchmarkProfileEvents(b *testing.B) {
	conn, err := tests.GetConnectionTCP("issues", nil, nil, nil)
	ctx := context.Background()
	require.NoError(b, err)

	const ddl = `CREATE TABLE test_profile_events (Col1 UInt64, Col2 String, Col3 Array(UInt8), Col4 DateTime) Engine ReplacingMergeTree() ORDER BY Col1`
	err = conn.Exec(ctx, ddl)
	require.NoError(b, err)
	defer func() {
		conn.Exec(ctx, "DROP TABLE IF EXISTS test_profile_events")
	}()

	for k := 0; k < b.N; k++ {
		require.NoError(b, benchmarkProfileEvents(ctx, conn))
	}
}

Details

Environment

  • clickhouse-go version: v2.40.3
  • Interface: ClickHouse API
  • Go version: 1.25.1
  • Operating system: MacOS 26.0.1
  • ClickHouse version: 25.6
  • Is it a ClickHouse Cloud? No
  • ClickHouse Server non-default settings, if any: settings are based on the tests suite in this repo
  • CREATE TABLE statements for tables involved: See test above

Memory profile showing allocations

Benchmark results

BenchmarkProfileEvents-12                  1        11126073667 ns/op       276147480 B/op   2349943 allocs/op
Image

Memory profile when skipping scanning profile events when a listener is not set

Benchmark results

BenchmarkProfileEvents-12                  1        11343169583 ns/op       85091152 B/op     860664 allocs/op
Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions