| 1 | { |
| 2 | config, |
| 3 | lib, |
| 4 | pkgs, |
| 5 | ... |
| 6 | }: |
| 7 | |
| 8 | { |
| 9 | services.prometheus.exporters.postgres = { |
| 10 | enable = true; |
| 11 | # CREATE USER "postgres-exporter"; |
| 12 | # GRANT pg_monitor TO "postgres-exporter"; |
| 13 | dataSourceName = "user=postgres-exporter database=hydra host=/run/postgresql sslmode=disable"; |
| 14 | openFirewall = true; |
| 15 | firewallRules = '' |
| 16 | ip6 saddr $prometheus_inet6 tcp dport ${toString config.services.prometheus.exporters.postgres.port} accept |
| 17 | ip saddr $prometheus_inet4 tcp dport ${toString config.services.prometheus.exporters.postgres.port} accept |
| 18 | ''; |
| 19 | }; |
| 20 | |
| 21 | services.postgresql = { |
| 22 | enable = true; |
| 23 | package = pkgs.postgresql_18; |
| 24 | # https://pgtune.leopard.in.ua/#/ |
| 25 | # https://vadosware.io/post/everything-ive-seen-on-optimizing-postgres-on-zfs-on-linux/#zfs-related-tunables-on-the-postgres-side |
| 26 | settings = { |
| 27 | # no page tearing on ZFS |
| 28 | full_page_writes = "off"; |
| 29 | |
| 30 | # avoid zero-filling with ZFS |
| 31 | wal_init_zero = "off"; |
| 32 | wal_recycle = "off"; |
| 33 | |
| 34 | checkpoint_completion_target = "0.9"; |
| 35 | default_statistics_target = 100; |
| 36 | |
| 37 | log_duration = "off"; |
| 38 | log_statement = "none"; |
| 39 | |
| 40 | # pgbadger-compatible logging |
| 41 | log_transaction_sample_rate = 0.01; |
| 42 | log_min_duration_statement = 5000; |
| 43 | log_checkpoints = "on"; |
| 44 | log_connections = "on"; |
| 45 | log_disconnections = "on"; |
| 46 | log_lock_waits = "on"; |
| 47 | log_temp_files = 0; |
| 48 | log_autovacuum_min_duration = 0; |
| 49 | log_line_prefix = "user=%u,db=%d,app=%a,client=%h "; |
| 50 | |
| 51 | max_worker_processes = 48; |
| 52 | max_parallel_workers_per_gather = 4; |
| 53 | max_parallel_workers = 48; |
| 54 | max_parallel_maintenance_workers = 4; |
| 55 | |
| 56 | max_connections = 100; |
| 57 | work_mem = "150MB"; |
| 58 | maintenance_work_mem = "8GB"; |
| 59 | |
| 60 | # 25% of memory |
| 61 | shared_buffers = "32GB"; |
| 62 | |
| 63 | # Reduce WAL file creation churn |
| 64 | min_wal_size = "1GB"; |
| 65 | max_wal_size = "4GB"; |
| 66 | |
| 67 | # Shared memory allocation before writing WAL to disk |
| 68 | wal_buffers = "16MB"; |
| 69 | |
| 70 | # Async I/O over shared ringbuffer with the kernel |
| 71 | io_method = "io_uring"; |
| 72 | |
| 73 | # NVMe related performance tuning |
| 74 | effective_io_concurrency = 1000; |
| 75 | random_page_cost = "1.1"; |
| 76 | |
| 77 | # query planner estimate for memory available for disk caching |
| 78 | effective_cache_size = "96GB"; |
| 79 | |
| 80 | # With ZFS we can risk losing some transactions. |
| 81 | synchronous_commit = "off"; |
| 82 | |
| 83 | # Try to allocate huge pages, if possible |
| 84 | huge_pages = "try"; |
| 85 | |
| 86 | # Only useful for long-running CPU-bound queries |
| 87 | jit = "off"; |
| 88 | |
| 89 | # autovacuum and autoanalyze much more frequently: |
| 90 | # at these values vacuum should run approximately |
| 91 | # every 2 mass rebuilds, or a couple times a day |
| 92 | # on the builds table. Some of those queries really |
| 93 | # benefit from frequent vacuums, so this should |
| 94 | # help. In particular, I'm thinking the jobsets |
| 95 | # pages. |
| 96 | autovacuum_vacuum_scale_factor = 0.02; # down from 0.2 |
| 97 | autovacuum_analyze_scale_factor = 0.01; # down from 0.1 |
| 98 | |
| 99 | shared_preload_libraries = "pg_stat_statements"; |
| 100 | compute_query_id = "on"; |
| 101 | }; |
| 102 | |
| 103 | authentication = lib.mkBefore '' |
| 104 | local all postgres-exporter peer |
| 105 | local hydra zrepl peer map=zrepl |
| 106 | ''; |
| 107 | |
| 108 | identMap = '' |
| 109 | zrepl root zrepl |
| 110 | ''; |
| 111 | }; |
| 112 | } |