2024-11-02 23:59:38 -04:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
# sudo -u postgres vacuumdb --all --analyze-in-stages
|
|
|
|
# /var/lib/postgresql/16/delete_old_cluster.sh
|
|
|
|
let
|
|
|
|
vars = import ./vars.nix;
|
|
|
|
dataDir = "${vars.primary_db}/postgresql/nix/${config.services.postgresql.package.psqlSchema}";
|
|
|
|
backupLocation = "${vars.primary_db}/postgresql/nix_backups";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
services = {
|
|
|
|
postgresql = {
|
|
|
|
inherit dataDir;
|
|
|
|
enable = true;
|
|
|
|
enableJIT = true;
|
|
|
|
package = pkgs.postgresql_16;
|
2025-04-25 12:52:40 -04:00
|
|
|
configurePgStatStatements = true;
|
|
|
|
enableAllPreloadedLibraries = true;
|
|
|
|
preloadAllExtensions = true;
|
2024-11-02 23:59:38 -04:00
|
|
|
identMap = ''
|
|
|
|
# ArbitraryMapName systemUser DBUser
|
|
|
|
superuser_map root postgres
|
|
|
|
superuser_map alice postgres
|
|
|
|
# Let other names login as themselves
|
|
|
|
superuser_map /^(.*)$ \1
|
|
|
|
'';
|
|
|
|
|
|
|
|
# initialScript = config.sops.secrets."postgres/init".path;
|
2025-04-07 15:44:42 -04:00
|
|
|
ensureDatabases = [
|
|
|
|
"atticd"
|
|
|
|
"alice"
|
|
|
|
];
|
2024-11-02 23:59:38 -04:00
|
|
|
ensureUsers = [
|
|
|
|
{
|
|
|
|
name = "atticd";
|
|
|
|
ensureDBOwnership = true;
|
|
|
|
}
|
2025-04-07 15:44:42 -04:00
|
|
|
{
|
|
|
|
name = "alice";
|
|
|
|
ensureDBOwnership = true;
|
|
|
|
ensureClauses = {
|
|
|
|
superuser = true;
|
|
|
|
login = true;
|
|
|
|
createrole = true;
|
|
|
|
createdb = true;
|
|
|
|
replication = true;
|
|
|
|
};
|
|
|
|
}
|
2024-11-02 23:59:38 -04:00
|
|
|
];
|
2025-04-25 12:24:21 -04:00
|
|
|
# Thank you NotAShelf
|
|
|
|
# https://github.com/NotAShelf/nyx/blob/d407b4d6e5ab7f60350af61a3d73a62a5e9ac660/modules/core/roles/server/system/services/databases/postgresql.nix#L74
|
|
|
|
# commented out statements are likely overriden by pgtune settings
|
|
|
|
# https://pgtune.leopard.in.ua/?dbVersion=17&osType=linux&dbType=web&cpuNum=64&totalMemory=8&totalMemoryUnit=GB&connectionNum=1024&hdType=hdd
|
|
|
|
settings = {
|
|
|
|
# Connectivity;
|
|
|
|
# max_connections = 100;
|
|
|
|
superuser_reserved_connections = 3;
|
|
|
|
|
|
|
|
# Memory Settings;
|
|
|
|
#shared_buffers = "1024 MB";
|
|
|
|
#work_mem = "32 MB";
|
|
|
|
#maintenance_work_mem = "320 MB";
|
|
|
|
#huge_pages = "off";
|
|
|
|
#effective_cache_size = "2 GB";
|
|
|
|
#effective_io_concurrency = 100; # concurrent IO only really activated if OS supports posix_fadvise function;
|
|
|
|
#random_page_cost = 1.25; # speed of random disk access relative to sequential access (1.0);
|
|
|
|
|
|
|
|
# Monitoring;
|
2025-04-25 12:52:40 -04:00
|
|
|
#shared_preload_libraries = "pg_stat_statements,auto_explain"; # per statement resource usage stats & log explain statements for slow queries
|
2025-04-25 12:24:21 -04:00
|
|
|
track_io_timing = "on"; # measure exact block IO times;
|
|
|
|
track_functions = "pl"; # track execution times of pl-language procedures if any;
|
|
|
|
# Replication;
|
|
|
|
wal_level = "replica"; # consider using at least "replica";
|
|
|
|
max_wal_senders = 0;
|
|
|
|
synchronous_commit = "on";
|
|
|
|
|
|
|
|
# Checkpointing: ;
|
|
|
|
checkpoint_timeout = "15 min";
|
|
|
|
#checkpoint_completion_target = 0.9;
|
|
|
|
#max_wal_size = "1024 MB";
|
|
|
|
#min_wal_size = "512 MB";
|
|
|
|
|
|
|
|
# WAL writing;
|
|
|
|
wal_compression = "on";
|
|
|
|
wal_buffers = -1; # auto-tuned by Postgres till maximum of segment size (16MB by default);
|
|
|
|
wal_writer_delay = "200ms";
|
|
|
|
wal_writer_flush_after = "1MB";
|
|
|
|
|
|
|
|
# Background writer;
|
|
|
|
bgwriter_delay = "200ms";
|
|
|
|
bgwriter_lru_maxpages = 100;
|
|
|
|
bgwriter_lru_multiplier = 2.0;
|
|
|
|
bgwriter_flush_after = 0;
|
|
|
|
|
|
|
|
# Parallel queries: ;
|
|
|
|
#max_worker_processes = 6;
|
|
|
|
#max_parallel_workers_per_gather = 3;
|
|
|
|
#max_parallel_maintenance_workers = 3;
|
|
|
|
#max_parallel_workers = 6;
|
|
|
|
parallel_leader_participation = "on";
|
|
|
|
|
|
|
|
# Advanced features ;
|
|
|
|
enable_partitionwise_join = "on";
|
|
|
|
enable_partitionwise_aggregate = "on";
|
|
|
|
jit = "on";
|
|
|
|
|
|
|
|
jit_above_cost = 100000;
|
|
|
|
jit_inline_above_cost = 150000;
|
|
|
|
jit_optimize_above_cost = 500000;
|
|
|
|
|
|
|
|
# log slow queries
|
|
|
|
log_min_duration_statement = 100;
|
|
|
|
"auto_explain.log_min_duration" = 100;
|
|
|
|
|
|
|
|
# logging configuration
|
|
|
|
log_connections = true;
|
|
|
|
log_statement = "all";
|
|
|
|
logging_collector = true;
|
|
|
|
log_disconnections = true;
|
|
|
|
|
|
|
|
# from pgtune
|
|
|
|
# DB Version: 17
|
|
|
|
# OS Type: linux
|
|
|
|
# DB Type: web
|
|
|
|
# Total Memory (RAM): 8 GB
|
|
|
|
# CPUs num: 64
|
|
|
|
# Connections num: 1024
|
|
|
|
# Data Storage: hdd
|
|
|
|
|
|
|
|
max_connections = 1024;
|
|
|
|
shared_buffers = "2GB";
|
|
|
|
effective_cache_size = "6GB";
|
|
|
|
maintenance_work_mem = "512MB";
|
|
|
|
checkpoint_completion_target = 0.9;
|
|
|
|
#wal_buffers = "16MB"; allow auto-tuning as per above
|
|
|
|
default_statistics_target = 100;
|
|
|
|
random_page_cost = 4;
|
|
|
|
effective_io_concurrency = 2;
|
|
|
|
work_mem = "512kB";
|
|
|
|
huge_pages = "off";
|
|
|
|
min_wal_size = "1GB";
|
|
|
|
max_wal_size = "4GB";
|
|
|
|
max_worker_processes = 64;
|
|
|
|
max_parallel_workers_per_gather = 4;
|
|
|
|
max_parallel_workers = 64;
|
|
|
|
max_parallel_maintenance_workers = 4;
|
|
|
|
|
|
|
|
};
|
2024-11-02 23:59:38 -04:00
|
|
|
|
|
|
|
refreshCollation = true;
|
|
|
|
vacuumAnalyzeTimer.enable = true;
|
|
|
|
upgrade = {
|
|
|
|
enable = true;
|
|
|
|
stopServices = [
|
|
|
|
"hydra-evaluator"
|
|
|
|
"hydra-init"
|
|
|
|
"hydra-notify"
|
|
|
|
"hydra-queue-runner"
|
|
|
|
"hydra-send-stats"
|
|
|
|
"hydra-server"
|
|
|
|
"atticd"
|
2025-04-07 15:44:42 -04:00
|
|
|
"gitea"
|
2024-11-02 23:59:38 -04:00
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
postgresqlBackup = {
|
|
|
|
enable = true;
|
|
|
|
compression = "zstd";
|
|
|
|
compressionLevel = 19;
|
|
|
|
pgdumpOptions = "--create --clean";
|
|
|
|
location = backupLocation;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|