Skip to content

Commit

Permalink
fix: work_mem formulas based on...
Browse files Browse the repository at this point in the history
.. memory percentage defined in the profile. Now each profile defines
the maximum of total memory used when computing a per connection related
buffer, like `work_mem`.

Signed-off-by: Sebastian Webber <[email protected]>
  • Loading branch information
sebastianwebber committed Apr 4, 2022
1 parent 79bae6e commit 12156e2
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions pkg/category/memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package category

import "github.com/pgconfig/api/pkg/config"
import (
"github.com/pgconfig/api/pkg/config"
"github.com/pgconfig/api/pkg/profile"
)

// MemoryCfg is the main memory category
type MemoryCfg struct {
Expand All @@ -10,12 +13,36 @@ type MemoryCfg struct {
MaintenanceWorkMem config.Byte `json:"maintenance_work_mem"`
}

// Memory changes inspired by https://www.enterprisedb.com/postgres-tutorials/how-tune-postgresql-memory

// MaxMemoryPercent limits the maximum memory used
// for the profile when computing per connection buffers
var MaxMemoryPercent = map[string]float32{
profile.Web: 0.25,
profile.OLTP: 0.35,
profile.DW: 0.50,
profile.Mixed: 0.2,
profile.Desktop: 0.1,
}

const (
// SharedBufferPerc defines the percentage of ram
// for the shared_buffers setting
SharedBufferPerc = 0.25

// EffectiveCacheSizePerc defines the percentage of ram
// for the effective_cache_size setting - basically the
// total_ram - shared_buffers
EffectiveCacheSizePerc = 1 - SharedBufferPerc
)

// NewMemoryCfg creates a new Memory Configuration
func NewMemoryCfg(in config.Input) *MemoryCfg {

return &MemoryCfg{
SharedBuffers: config.Byte(in.TotalRAM) / 4,
EffectiveCacheSize: (config.Byte(in.TotalRAM) / 4) * 3,
WorkMem: in.TotalRAM / config.Byte(in.MaxConnections),
MaintenanceWorkMem: config.Byte(in.TotalRAM) / 16,
SharedBuffers: config.Byte(float32(in.TotalRAM) * SharedBufferPerc),
EffectiveCacheSize: config.Byte(float32(in.TotalRAM) * EffectiveCacheSizePerc),
WorkMem: config.Byte(float32(in.TotalRAM) * MaxMemoryPercent[in.Profile] / float32(in.MaxConnections)),
MaintenanceWorkMem: config.Byte(float32(in.TotalRAM) * 0.05),
}
}

0 comments on commit 12156e2

Please sign in to comment.