MCPcopy Create free account
hub / github.com/PostHog/duckgres / AutoMemoryLimit

Function AutoMemoryLimit

server/sysinfo/sysinfo.go:64–90  ·  view source on GitHub ↗

AutoMemoryLimit computes a DuckDB memory_limit based on system memory. Formula: totalMem * 0.75, with a floor of 256MB. Every session gets the full budget — DuckDB will spill to disk/swap if aggregate usage exceeds physical RAM. Returns "4GB" as a safe default if system memory cannot be detected. Th

()

Source from the content-addressed store, hash-verified

62// Returns "4GB" as a safe default if system memory cannot be detected.
63// The result is computed once and cached since system memory doesn't change.
64func AutoMemoryLimit() string {
65 autoMemoryLimitOnce.Do(func() {
66 totalBytes := SystemMemoryBytes()
67 if totalBytes == 0 {
68 autoMemoryLimitValue = "4GB"
69 return
70 }
71
72 const mb = 1024 * 1024
73 const gb = 1024 * mb
74
75 limitBytes := totalBytes * 3 / 4 // 75% of system RAM
76 if limitBytes < 256*mb {
77 limitBytes = 256 * mb
78 }
79
80 // Format as human-readable: use GB if >= 1GB, else MB.
81 if limitBytes >= gb {
82 limitGB := limitBytes / gb
83 autoMemoryLimitValue = fmt.Sprintf("%dGB", limitGB)
84 } else {
85 limitMB := limitBytes / mb
86 autoMemoryLimitValue = fmt.Sprintf("%dMB", limitMB)
87 }
88 })
89 return autoMemoryLimitValue
90}
91
92// validMemoryLimit matches DuckDB memory limit values like "4GB", "512MB", "1.5GB", "1TB".
93var validMemoryLimit = regexp.MustCompile(`(?i)^\d+(\.\d+)?\s*(KB|MB|GB|TB)$`)

Callers 4

ConfigureMainDBFunction · 0.92
TestAutoMemoryLimitFunction · 0.85

Calls 2

SystemMemoryBytesFunction · 0.85
DoMethod · 0.80

Tested by 3

TestAutoMemoryLimitFunction · 0.68