Skip to content

Commit 66136eb

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 66136eb

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
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: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,15 +1540,31 @@ func TestConnLogger_read_tx(t *testing.T) {
15401540
}
15411541
}
15421542

1543-
func TestExpandedSQL(t *testing.T) {
1543+
func getTestConn(t testing.TB) *conn {
15441544
ctx := context.Background()
15451545
connector := Connector("file:"+t.TempDir()+"/test.db", nil, nil)
15461546
sqlConn, err := connector.Connect(ctx)
15471547
if err != nil {
15481548
t.Fatalf("Connect: %v", err)
15491549
}
1550-
conn := sqlConn.(*conn)
1550+
t.Cleanup(func() { sqlConn.Close() })
1551+
return sqlConn.(*conn)
1552+
}
1553+
1554+
// Verify we didn't build with -DSQLITE_DEFAULT_MEMSTATUS=0.
1555+
// We want memory stats.
1556+
func TestDBMemoryUsed(t *testing.T) {
1557+
conn := getTestConn(t)
1558+
mem0 := conn.db.MemoryUsed()
1559+
if mem0 == 0 {
1560+
t.Error("MemoryUsed=0; want non-zero; did you build with -DSQLITE_DEFAULT_MEMSTATUS=0?")
1561+
}
1562+
}
15511563

1564+
func TestExpandedSQL(t *testing.T) {
1565+
conn := getTestConn(t)
1566+
1567+
ctx := t.Context()
15521568
sqlStmt, err := conn.PrepareContext(ctx, "SELECT ? + ?")
15531569
if err != nil {
15541570
t.Fatalf("PrepareContext: %v", err)
@@ -1567,4 +1583,14 @@ func TestExpandedSQL(t *testing.T) {
15671583
if got, want := stmt.stmt.ExpandedSQL(), "SELECT 6 + 7"; got != want {
15681584
t.Errorf("wrong sql: got %q, want %q", got, want)
15691585
}
1586+
mem0 := conn.db.MemoryUsed()
1587+
for range 100 {
1588+
if got, want := stmt.stmt.ExpandedSQL(), "SELECT 6 + 7"; got != want {
1589+
t.Errorf("wrong sql: got %q, want %q", got, want)
1590+
}
1591+
}
1592+
mem1 := conn.db.MemoryUsed()
1593+
if mem1 > mem0 {
1594+
t.Errorf("memory leak detected: before=%v after=%v", mem0, mem1)
1595+
}
15701596
}

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)