Skip to content

Commit 7cf33f0

Browse files
committed
sqlite: add DB.MemoryUsed accessor, add leak test to ExpandedSQL test
Updates tailscale/corp#34015 Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 6cfe794 commit 7cf33f0

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

cgosqlite/cgosqlite.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package cgosqlite
1010
1111
#cgo CFLAGS: -DSQLITE_THREADSAFE=2
1212
#cgo CFLAGS: -DSQLITE_DQS=0
13-
#cgo CFLAGS: -DSQLITE_DEFAULT_MEMSTATUS=0
1413
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
1514
#cgo CFLAGS: -DSQLITE_LIKE_DOESNT_MATCH_BLOBS
1615
#cgo CFLAGS: -DSQLITE_MAX_EXPR_DEPTH=0
@@ -143,6 +142,10 @@ func (db *DB) TotalChanges() int {
143142
return int(C.sqlite3_total_changes(db.db))
144143
}
145144

145+
func (db *DB) MemoryUsed() int64 {
146+
return int64(C.sqlite3_memory_used())
147+
}
148+
146149
func (db *DB) ExtendedErrCode() sqliteh.Code {
147150
return sqliteh.Code(C.sqlite3_extended_errcode(db.db))
148151
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/tailscale/sqlite
22

3-
go 1.21
3+
go 1.25

sqlite_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,4 +1567,14 @@ func TestExpandedSQL(t *testing.T) {
15671567
if got, want := stmt.stmt.ExpandedSQL(), "SELECT 6 + 7"; got != want {
15681568
t.Errorf("wrong sql: got %q, want %q", got, want)
15691569
}
1570+
mem0 := conn.db.MemoryUsed()
1571+
for range 100 {
1572+
if got, want := stmt.stmt.ExpandedSQL(), "SELECT 6 + 7"; got != want {
1573+
t.Errorf("wrong sql: got %q, want %q", got, want)
1574+
}
1575+
}
1576+
mem1 := conn.db.MemoryUsed()
1577+
if mem1 > mem0 {
1578+
t.Errorf("memory leak detected: before=%v after=%v", mem0, mem1)
1579+
}
15701580
}

sqliteh/sqliteh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type DB interface {
3737
// TotalChanges is sqlite3_total_changes.
3838
// https://sqlite.org/c3ref/total_changes.html
3939
TotalChanges() int
40+
// MemoryUsed is sqlite3_memory_used.
41+
// https://sqlite.org/c3ref/memory_highwater.html
42+
MemoryUsed() int64
4043
// ExtendedErrCode is sqlite3_extended_errcode.
4144
// https://sqlite.org/c3ref/errcode.html
4245
ExtendedErrCode() Code

0 commit comments

Comments
 (0)