| 1 | { |
| 2 | config, |
| 3 | pkgs, |
| 4 | ... |
| 5 | }: |
| 6 | |
| 7 | { |
| 8 | services.prometheus.exporters.postgres = { |
| 9 | enable = true; |
| 10 | dataSourceName = "user=root database=hydra host=/run/postgresql sslmode=disable"; |
| 11 | openFirewall = true; |
| 12 | firewallRules = '' |
| 13 | ip6 saddr $prometheus_inet6 tcp dport ${toString config.services.prometheus.exporters.postgres.port} accept |
| 14 | ip saddr $prometheus_inet4 tcp dport ${toString config.services.prometheus.exporters.postgres.port} accept |
| 15 | ''; |
| 16 | }; |
| 17 | |
| 18 | services.postgresql = { |
| 19 | enable = true; |
| 20 | enableJIT = true; |
| 21 | package = pkgs.postgresql_16; |
| 22 | dataDir = "/var/db/postgresql/16"; |
| 23 | # https://pgtune.leopard.in.ua/#/ |
| 24 | settings = { |
| 25 | # https://vadosware.io/post/everything-ive-seen-on-optimizing-postgres-on-zfs-on-linux/#zfs-related-tunables-on-the-postgres-side |
| 26 | full_page_writes = "off"; |
| 27 | |
| 28 | checkpoint_completion_target = "0.9"; |
| 29 | default_statistics_target = 100; |
| 30 | |
| 31 | log_duration = "off"; |
| 32 | log_statement = "none"; |
| 33 | |
| 34 | # pgbadger-compatible logging |
| 35 | log_transaction_sample_rate = 0.01; |
| 36 | log_min_duration_statement = 5000; |
| 37 | log_checkpoints = "on"; |
| 38 | log_connections = "on"; |
| 39 | log_disconnections = "on"; |
| 40 | log_lock_waits = "on"; |
| 41 | log_temp_files = 0; |
| 42 | log_autovacuum_min_duration = 0; |
| 43 | log_line_prefix = "user=%u,db=%d,app=%a,client=%h "; |
| 44 | |
| 45 | max_connections = 500; |
| 46 | work_mem = "20MB"; |
| 47 | maintenance_work_mem = "2GB"; |
| 48 | |
| 49 | # 25% of memory |
| 50 | shared_buffers = "16GB"; |
| 51 | |
| 52 | # Checkpoint every 1GB. (default) |
| 53 | # increased after seeing many warninsg about frequent checkpoints |
| 54 | min_wal_size = "1GB"; |
| 55 | max_wal_size = "2GB"; |
| 56 | wal_buffers = "16MB"; |
| 57 | |
| 58 | max_worker_processes = 16; |
| 59 | max_parallel_workers_per_gather = 8; |
| 60 | max_parallel_workers = 16; |
| 61 | |
| 62 | # NVMe related performance tuning |
| 63 | effective_io_concurrency = 200; |
| 64 | random_page_cost = "1.1"; |
| 65 | |
| 66 | # We can risk losing some transactions. |
| 67 | synchronous_commit = "off"; |
| 68 | |
| 69 | effective_cache_size = "16GB"; |
| 70 | |
| 71 | # Enable JIT compilation if possible. |
| 72 | jit = "on"; |
| 73 | |
| 74 | # autovacuum and autoanalyze much more frequently: |
| 75 | # at these values vacuum should run approximately |
| 76 | # every 2 mass rebuilds, or a couple times a day |
| 77 | # on the builds table. Some of those queries really |
| 78 | # benefit from frequent vacuums, so this should |
| 79 | # help. In particular, I'm thinking the jobsets |
| 80 | # pages. |
| 81 | autovacuum_vacuum_scale_factor = 0.02; |
| 82 | autovacuum_analyze_scale_factor = 0.01; |
| 83 | |
| 84 | shared_preload_libraries = "pg_stat_statements"; |
| 85 | compute_query_id = "on"; |
| 86 | }; |
| 87 | |
| 88 | # FIXME: don't use 'trust'. |
| 89 | authentication = '' |
| 90 | host hydra all 10.0.40.0/32 trust |
| 91 | local all root peer map=prometheus |
| 92 | ''; |
| 93 | |
| 94 | identMap = '' |
| 95 | prometheus root root |
| 96 | prometheus postgres-exporter root |
| 97 | ''; |
| 98 | }; |
| 99 | } |