Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

sqlite: bound every store's page cache to SQLite's default

rcourtmanc3fcddf94c2b
diff --git a/docs/release-control/v6/internal/subsystems/alerts.md b/docs/release-control/v6/internal/subsystems/alerts.md
index df6228a484..ebea6b5277 100644
--- a/docs/release-control/v6/internal/subsystems/alerts.md
+++ b/docs/release-control/v6/internal/subsystems/alerts.md
@@ -1007,6 +1007,10 @@ transitions. Disk-backed event logs use a bounded read-only WAL pool alongside
the serialized durable writer. An open history snapshot cannot block a durable
append or acquire mutation authority. In-memory test stores retain their single
connection because their private database is connection-local.
+Every event-log connection, the writer and both readers, takes its SQLite page
+cache from `pdb.PageCachePragma` in `pkg/db/pagecache.go` instead of a private
+16 MB `cache_size`, because each connection holds its own cache. The modernc driver holds that cache outside the Go heap at about twice its size, where neither the garbage collector nor `GOMEMLIMIT` reaches it (#1803).
+`TestEventLogConnectionsUseSharedPageCacheBound` pins all three.
Chronological replay selects bounded event IDs in time-index order before
loading snapshots and details. Pages preserve occurrence-time and ID ordering,
diff --git a/docs/release-control/v6/internal/subsystems/notifications.md b/docs/release-control/v6/internal/subsystems/notifications.md
index fc5345fa33..52f3c15eb3 100644
--- a/docs/release-control/v6/internal/subsystems/notifications.md
+++ b/docs/release-control/v6/internal/subsystems/notifications.md
@@ -830,6 +830,9 @@ That same queue ownership also governs persistent queue storage roots. The
notifications queue database must normalize its owned data directory and
resolve the fixed `notification_queue.db` leaf through the shared storage-path
helper instead of joining raw caller-provided directory strings.
+The queue database takes its SQLite page cache from `pdb.PageCachePragma` in
+`pkg/db/pagecache.go` rather than a private `cache_size`. The modernc driver holds that cache outside the Go heap at about twice its size, where neither the garbage collector nor `GOMEMLIMIT` reaches it (#1803).
+`TestNotificationQueueUsesSharedPageCacheBound` pins the effective value.
That same queue owner also governs alert-resolution cancellation policy.
Cancelling queued work by alert identifier must remove outstanding firing
deliveries for that alert, but it must preserve already-queued resolved
diff --git a/docs/release-control/v6/internal/subsystems/security-privacy.md b/docs/release-control/v6/internal/subsystems/security-privacy.md
index 8277a028e6..b866824ecc 100644
--- a/docs/release-control/v6/internal/subsystems/security-privacy.md
+++ b/docs/release-control/v6/internal/subsystems/security-privacy.md
@@ -1363,6 +1363,15 @@ tokens, and path-normalization variants.
`internal/api/rbac_handlers_test.go`, and the rejection cases in
`pkg/auth/sqlite_manager_test.go`.
+The audit (`pkg/audit/sqlite_logger.go`) and RBAC (`pkg/auth/sqlite_manager.go`)
+stores take their SQLite page cache from `pdb.PageCachePragma` in
+`pkg/db/pagecache.go`, 2000 KiB per connection, not a private `cache_size`.
+The modernc driver holds that cache outside the Go heap at about twice its size, where neither the garbage collector nor `GOMEMLIMIT` reaches it (#1803).
+The former 64 MB audit cache held about 120 MB of resident memory once the audit
+database grew past it. `TestSQLiteLoggerUsesSharedPageCacheBound` and
+`TestSQLiteManagerUsesSharedPageCacheBound` pin the effective value, and
+`TestNoStoreSetsItsOwnPageCache` rejects any other `cache_size` in production code.
+
14. Security posture presentation must never be computed from a payload the
caller was not authorized to read. `/api/security/status` truncates its
response by authority and declares which tier it returned through
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index ceb9d09e21..86dcee7881 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -718,6 +718,14 @@ read-only incident memory association. It does not rewrite event resource IDs,
resource operator state, approvals, action request identity or execution
capabilities. An unavailable canonical read remains an error.
+### Bounded SQLite page cache
+
+The resource store takes its SQLite page cache from `pdb.PageCachePragma` in
+`pkg/db/pagecache.go`, 2000 KiB per connection, not a private 64 MB
+`cache_size`. The modernc driver holds that cache outside the Go heap at about twice its size, where neither the garbage collector nor `GOMEMLIMIT` reaches it (#1803). Pages outside the cache are re-read from the
+kernel file cache, which the kernel can reclaim under memory pressure.
+`TestSQLiteResourceStoreUsesSharedPageCacheBound` pins the effective value.
+
### Canonical Patrol and Assistant continuation, 2026-09-07
Persisted action request identity is scoped to the trusted actor and exact
diff --git a/internal/alerts/eventlog/eventlog.go b/internal/alerts/eventlog/eventlog.go
index ce1d6a3934..6d3db31042 100644
--- a/internal/alerts/eventlog/eventlog.go
+++ b/internal/alerts/eventlog/eventlog.go
@@ -22,6 +22,7 @@ import (
"sync/atomic"
"time"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog/log"
_ "modernc.org/sqlite"
)
@@ -147,7 +148,7 @@ func sqliteDSN(path string) string {
// telemetry. FULL keeps committed events and their active-state
// projection durable across host power loss as well as process exit.
"synchronous(FULL)",
- "cache_size(-16000)",
+ pdb.PageCachePragma,
},
}.Encode()
}
diff --git a/internal/alerts/eventlog/eventlog_test.go b/internal/alerts/eventlog/eventlog_test.go
index 7560431965..1584a87dd4 100644
--- a/internal/alerts/eventlog/eventlog_test.go
+++ b/internal/alerts/eventlog/eventlog_test.go
@@ -1,11 +1,14 @@
package eventlog
import (
+ "database/sql"
"encoding/json"
"fmt"
"math"
"testing"
"time"
+
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
)
func newTestStore(t *testing.T) *Store {
@@ -256,3 +259,29 @@ func TestDurableAppendAndFlushReportDatabaseFailure(t *testing.T) {
t.Fatal("Flush reported success after the async batch failed")
}
}
+
+// The writer and both read-only connections each hold their own page cache,
+// so all of them must take the shared bound instead of a private 16 MB cache
+// held outside the Go heap (#1803).
+func TestEventLogConnectionsUseSharedPageCacheBound(t *testing.T) {
+ store, err := Open(t.TempDir())
+ if err != nil {
+ t.Fatalf("Open: %v", err)
+ }
+ defer store.Close()
+ assertSharedPageCacheBound(t, "event log writer", store.db)
+ assertSharedPageCacheBound(t, "event log reader", store.readDB)
+}
+
+func assertSharedPageCacheBound(t *testing.T, name string, conn interface {
+ QueryRow(query string, args ...any) *sql.Row
+}) {
+ t.Helper()
+ var size int
+ if err := conn.QueryRow(`PRAGMA cache_size`).Scan(&size); err != nil {
+ t.Fatalf("%s: read cache_size: %v", name, err)
+ }
+ if size != -pdb.MaxPageCacheKiB {
+ t.Fatalf("%s: cache_size = %d, want %d from pdb.PageCachePragma", name, size, -pdb.MaxPageCacheKiB)
+ }
+}
diff --git a/internal/notifications/queue.go b/internal/notifications/queue.go
index e6fca002b2..00bc9d1a14 100644
--- a/internal/notifications/queue.go
+++ b/internal/notifications/queue.go
@@ -16,6 +16,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/operationaltrust"
"github.com/rcourtman/pulse-go-rewrite/internal/securityutil"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog/log"
_ "modernc.org/sqlite"
)
@@ -212,7 +213,7 @@ func queueSQLiteDSN(path string) string {
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
- "cache_size(-64000)",
+ pdb.PageCachePragma,
},
}.Encode()
}
diff --git a/internal/notifications/queue_observability_test.go b/internal/notifications/queue_observability_test.go
index f920ac8683..4d5b2498af 100644
--- a/internal/notifications/queue_observability_test.go
+++ b/internal/notifications/queue_observability_test.go
@@ -2,10 +2,12 @@ package notifications
import (
"bytes"
+ "database/sql"
"strings"
"testing"
"time"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
@@ -117,3 +119,27 @@ func captureNotificationQueueLogs(t *testing.T) *bytes.Buffer {
return &buf
}
+
+// The notification queue must take the shared page cache bound instead of a
+// private 64 MB cache held outside the Go heap (#1803).
+func TestNotificationQueueUsesSharedPageCacheBound(t *testing.T) {
+ nq, err := NewNotificationQueue(t.TempDir())
+ if err != nil {
+ t.Fatalf("NewNotificationQueue: %v", err)
+ }
+ defer func() { _ = nq.Stop() }()
+ assertSharedPageCacheBound(t, "notification queue", nq.db)
+}
+
+func assertSharedPageCacheBound(t *testing.T, name string, conn interface {
+ QueryRow(query string, args ...any) *sql.Row
+}) {
+ t.Helper()
+ var size int
+ if err := conn.QueryRow(`PRAGMA cache_size`).Scan(&size); err != nil {
+ t.Fatalf("%s: read cache_size: %v", name, err)
+ }
+ if size != -pdb.MaxPageCacheKiB {
+ t.Fatalf("%s: cache_size = %d, want %d from pdb.PageCachePragma", name, size, -pdb.MaxPageCacheKiB)
+ }
+}
diff --git a/internal/unifiedresources/store.go b/internal/unifiedresources/store.go
index 9d1021bf66..d1160754d3 100644
--- a/internal/unifiedresources/store.go
+++ b/internal/unifiedresources/store.go
@@ -18,6 +18,7 @@ import (
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
_ "modernc.org/sqlite"
)
@@ -234,7 +235,7 @@ func NewSQLiteResourceStore(dataDir, orgID string) (*SQLiteResourceStore, error)
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
- "cache_size(-64000)",
+ pdb.PageCachePragma,
"auto_vacuum(INCREMENTAL)",
},
}.Encode()
diff --git a/internal/unifiedresources/store_test.go b/internal/unifiedresources/store_test.go
index aa9e2bcadc..f4e16b35e4 100644
--- a/internal/unifiedresources/store_test.go
+++ b/internal/unifiedresources/store_test.go
@@ -16,6 +16,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/operationreceipt"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/stretchr/testify/require"
)
@@ -4529,3 +4530,27 @@ func TestMonitorStateHoldsBackAnOperatorSplitNodeAgentLink(t *testing.T) {
}
}
}
+
+// The resource store must take the shared page cache bound instead of a
+// private 64 MB cache held outside the Go heap (#1803).
+func TestSQLiteResourceStoreUsesSharedPageCacheBound(t *testing.T) {
+ store, err := NewSQLiteResourceStore(t.TempDir(), "default")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer store.Close()
+ assertSharedPageCacheBound(t, "unified resources", store.db)
+}
+
+func assertSharedPageCacheBound(t *testing.T, name string, conn interface {
+ QueryRow(query string, args ...any) *sql.Row
+}) {
+ t.Helper()
+ var size int
+ if err := conn.QueryRow(`PRAGMA cache_size`).Scan(&size); err != nil {
+ t.Fatalf("%s: read cache_size: %v", name, err)
+ }
+ if size != -pdb.MaxPageCacheKiB {
+ t.Fatalf("%s: cache_size = %d, want %d from pdb.PageCachePragma", name, size, -pdb.MaxPageCacheKiB)
+ }
+}
diff --git a/pkg/audit/sqlite_logger.go b/pkg/audit/sqlite_logger.go
index 5a4e09428d..7c89c01ebc 100644
--- a/pkg/audit/sqlite_logger.go
+++ b/pkg/audit/sqlite_logger.go
@@ -12,6 +12,7 @@ import (
"sync"
"time"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog/log"
sqlite "modernc.org/sqlite"
)
@@ -180,7 +181,7 @@ func NewSQLiteLogger(cfg SQLiteLoggerConfig) (*SQLiteLogger, error) {
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
- "cache_size(-64000)",
+ pdb.PageCachePragma,
},
}.Encode()
db, err := sql.Open("sqlite", dsn)
diff --git a/pkg/audit/sqlite_logger_test.go b/pkg/audit/sqlite_logger_test.go
index e8b6e992bc..a3ddc8107d 100644
--- a/pkg/audit/sqlite_logger_test.go
+++ b/pkg/audit/sqlite_logger_test.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/google/uuid"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
)
func TestNewSQLiteLogger(t *testing.T) {
@@ -804,3 +805,32 @@ func TestSQLiteLoggerCloseIdempotent(t *testing.T) {
t.Fatalf("second Close failed: %v", err)
}
}
+
+// The audit database grows large on long-running installs, so it must take the
+// shared page cache bound. Its former private 64 MB cache held about 120 MB of
+// resident memory outside the Go heap (#1803).
+func TestSQLiteLoggerUsesSharedPageCacheBound(t *testing.T) {
+ logger, err := NewSQLiteLogger(SQLiteLoggerConfig{
+ DataDir: t.TempDir(),
+ CryptoMgr: newMockCryptoManager(),
+ RetentionDays: 30,
+ })
+ if err != nil {
+ t.Fatalf("NewSQLiteLogger failed: %v", err)
+ }
+ defer logger.Close()
+ assertSharedPageCacheBound(t, "audit", logger.db)
+}
+
+func assertSharedPageCacheBound(t *testing.T, name string, conn interface {
+ QueryRow(query string, args ...any) *sql.Row
+}) {
+ t.Helper()
+ var size int
+ if err := conn.QueryRow(`PRAGMA cache_size`).Scan(&size); err != nil {
+ t.Fatalf("%s: read cache_size: %v", name, err)
+ }
+ if size != -pdb.MaxPageCacheKiB {
+ t.Fatalf("%s: cache_size = %d, want %d from pdb.PageCachePragma", name, size, -pdb.MaxPageCacheKiB)
+ }
+}
diff --git a/pkg/auth/sqlite_manager.go b/pkg/auth/sqlite_manager.go
index ba4ddb3ab2..51af2a439d 100644
--- a/pkg/auth/sqlite_manager.go
+++ b/pkg/auth/sqlite_manager.go
@@ -15,6 +15,7 @@ import (
"github.com/google/uuid"
"github.com/rcourtman/pulse-go-rewrite/internal/securityutil"
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
"github.com/rs/zerolog/log"
_ "modernc.org/sqlite"
)
@@ -69,7 +70,7 @@ func NewSQLiteManager(cfg SQLiteManagerConfig) (*SQLiteManager, error) {
"journal_mode(WAL)",
"synchronous(NORMAL)",
"foreign_keys(ON)",
- "cache_size(-32000)",
+ pdb.PageCachePragma,
},
}.Encode()
db, err := sql.Open("sqlite", dsn)
diff --git a/pkg/auth/sqlite_manager_test.go b/pkg/auth/sqlite_manager_test.go
index 53cc051404..a238a975f9 100644
--- a/pkg/auth/sqlite_manager_test.go
+++ b/pkg/auth/sqlite_manager_test.go
@@ -8,6 +8,8 @@ import (
"strings"
"testing"
"time"
+
+ pdb "github.com/rcourtman/pulse-go-rewrite/pkg/db"
)
func TestSQLiteManagerAddsIdentityMetadataColumnsOnUpgrade(t *testing.T) {
@@ -1049,3 +1051,27 @@ func TestSQLiteManagerChangeLogRetention(t *testing.T) {
}
})
}
+
+// The RBAC store must take the shared page cache bound instead of a private
+// 32 MB cache held outside the Go heap (#1803).
+func TestSQLiteManagerUsesSharedPageCacheBound(t *testing.T) {
+ manager, err := NewSQLiteManager(SQLiteManagerConfig{DataDir: t.TempDir()})
+ if err != nil {
+ t.Fatalf("NewSQLiteManager: %v", err)
+ }
+ defer manager.Close()
+ assertSharedPageCacheBound(t, "rbac", manager.db)
+}
+
+func assertSharedPageCacheBound(t *testing.T, name string, conn interface {
+ QueryRow(query string, args ...any) *sql.Row
+}) {
+ t.Helper()
+ var size int
+ if err := conn.QueryRow(`PRAGMA cache_size`).Scan(&size); err != nil {
+ t.Fatalf("%s: read cache_size: %v", name, err)
+ }
+ if size != -pdb.MaxPageCacheKiB {
+ t.Fatalf("%s: cache_size = %d, want %d from pdb.PageCachePragma", name, size, -pdb.MaxPageCacheKiB)
+ }
+}
diff --git a/pkg/db/pagecache.go b/pkg/db/pagecache.go
new file mode 100644
index 0000000000..4496c89255
--- /dev/null
+++ b/pkg/db/pagecache.go
@@ -0,0 +1,20 @@
+package db
+
+// PageCachePragma bounds each SQLite connection's private page cache. Pass it
+// in a store's DSN `_pragma` list instead of writing its own cache_size.
+//
+// The modernc driver allocates this cache with anonymous mmap, outside the Go
+// heap, so neither the garbage collector nor GOMEMLIMIT can see or reclaim it,
+// and each cached page costs about twice its size once the allocator rounds it
+// up. A 64 MB cache_size held about 120 MB of resident memory on a large
+// database (#1803). Pages that miss this cache are re-read from the kernel's
+// file cache, which the kernel can reclaim under memory pressure. This matches
+// SQLite's own default of 2000 KiB.
+//
+// modernc applies a DSN's _pragma values in sorted order, not the order given,
+// so a second cache_size in the same DSN cannot reliably override this one.
+const PageCachePragma = "cache_size(-2000)"
+
+// MaxPageCacheKiB is the largest cache_size, in KiB, that a store may request.
+// The pagecache guard test fails on any larger literal in production code.
+const MaxPageCacheKiB = 2000
diff --git a/pkg/db/pagecache_test.go b/pkg/db/pagecache_test.go
new file mode 100644
index 0000000000..ccb2c6a2e2
--- /dev/null
+++ b/pkg/db/pagecache_test.go
@@ -0,0 +1,165 @@
+package db
+
+import (
+ "database/sql"
+ "fmt"
+ "io/fs"
+ "net/url"
+ "os"
+ "path/filepath"
+ "regexp"
+ "runtime"
+ "strconv"
+ "strings"
+ "testing"
+
+ _ "modernc.org/sqlite"
+)
+
+var cacheSizePattern = regexp.MustCompile(`(?i)cache_size\s*(?:\(\s*|=\s*)(-?\d+)`)
+
+func repoRoot(t *testing.T) string {
+ t.Helper()
+ _, file, _, ok := runtime.Caller(0)
+ if !ok {
+ t.Fatal("cannot locate test file")
+ }
+ return filepath.Clean(filepath.Join(filepath.Dir(file), "..", ".."))
+}
+
+// Every production SQLite store must take its page cache from
+// PageCachePragma. A private larger cache_size costs about twice its size in
+// resident memory outside the Go heap (#1803).
+func TestNoStoreSetsItsOwnPageCache(t *testing.T) {
+ root := repoRoot(t)
+ own := filepath.Join(root, "pkg", "db", "pagecache.go")
+ var offenders []string
+ err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+ if d.IsDir() {
+ switch d.Name() {
+ case ".git", "node_modules", "vendor", "worktrees", "testdata":
+ return filepath.SkipDir
+ }
+ return nil
+ }
+ if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") || path == own {
+ return nil
+ }
+ body, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ for _, m := range cacheSizePattern.FindAllStringSubmatch(string(body), -1) {
+ rel, _ := filepath.Rel(root, path)
+ offenders = append(offenders, fmt.Sprintf("%s: cache_size %s", rel, m[1]))
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(offenders) > 0 {
+ t.Fatalf("SQLite stores must use pdb.PageCachePragma, not their own cache_size:\n%s", strings.Join(offenders, "\n"))
+ }
+}
+
+func TestPageCachePragmaStaysWithinBudget(t *testing.T) {
+ m := cacheSizePattern.FindStringSubmatch(PageCachePragma)
+ if m == nil {
+ t.Fatalf("PageCachePragma %q is not a cache_size pragma", PageCachePragma)
+ }
+ n, err := strconv.Atoi(m[1])
+ if err != nil {
+ t.Fatal(err)
+ }
+ if n >= 0 || -n > MaxPageCacheKiB {
+ t.Fatalf("PageCachePragma requests %d; want a KiB size of at most %d (negative value)", n, MaxPageCacheKiB)
+ }
+}
+
+func residentKiB(t *testing.T) int {
+ t.Helper()
+ body, err := os.ReadFile("/proc/self/status")
+ if err != nil {
+ t.Skip("resident memory is only measurable through /proc")
+ }
+ for _, line := range strings.Split(string(body), "\n") {
+ if rest, ok := strings.CutPrefix(line, "VmRSS:"); ok {
+ kib, err := strconv.Atoi(strings.TrimSuffix(strings.TrimSpace(rest), " kB"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return kib
+ }
+ }
+ t.Skip("VmRSS unavailable")
+ return 0
+}
+
+// Reading a database far larger than the cache must not grow resident memory
+// by more than the bounded cache's real cost. A 64 MB cache_size grew it by
+// about 117 MB in the same scan.
+func TestPageCacheBoundsResidentMemory(t *testing.T) {
+ if testing.Short() {
+ t.Skip("builds a 30 MB database")
+ }
+ path := filepath.Join(t.TempDir(), "pagecache.db")
+ writer, err := sql.Open("sqlite", path+"?"+url.Values{"_pragma": {"journal_mode(WAL)", PageCachePragma}}.Encode())
+ if err != nil {
+ t.Fatal(err)
+ }
+ writer.SetMaxOpenConns(1)
+ if _, err := writer.Exec(`CREATE TABLE t (id INTEGER PRIMARY KEY, a TEXT, b TEXT); CREATE INDEX t_a ON t(a)`); err != nil {
+ t.Fatal(err)
+ }
+ tx, err := writer.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+ pad := strings.Repeat("x", 200)
+ for i := 0; i < 120000; i++ {
+ if _, err := tx.Exec(`INSERT INTO t (a, b) VALUES (?, ?)`, fmt.Sprintf("k%09d", (i*7919)%120000), pad); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := tx.Commit(); err != nil {
+ t.Fatal(err)
+ }
+ if err := writer.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ reader, err := sql.Open("sqlite", path+"?"+url.Values{"_pragma": {PageCachePragma}}.Encode())
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer reader.Close()
+ reader.SetMaxOpenConns(1)
+ var effective int
+ if err := reader.QueryRow(`PRAGMA cache_size`).Scan(&effective); err != nil {
+ t.Fatal(err)
+ }
+ if effective != -MaxPageCacheKiB {
+ t.Fatalf("effective cache_size = %d, want %d", effective, -MaxPageCacheKiB)
+ }
+
+ before := residentKiB(t)
+ var rows int
+ var total int64
+ for pass := 0; pass < 2; pass++ {
+ if err := reader.QueryRow(`SELECT count(*), sum(length(b)) FROM t`).Scan(&rows, &total); err != nil {
+ t.Fatal(err)
+ }
+ if err := reader.QueryRow(`SELECT count(*) FROM t WHERE a > 'k'`).Scan(&rows); err != nil {
+ t.Fatal(err)
+ }
+ }
+ grew := residentKiB(t) - before
+ // About twice the cache size, plus allocator and statement slack.
+ if limit := 4*MaxPageCacheKiB + 8192; grew > limit {
+ t.Fatalf("scanning a 30 MB database grew resident memory by %d KiB; the page cache budget allows %d KiB", grew, limit)
+ }
+}