/*------------------------------------------------------------------------- * * autoconfig.c * Generate 1C-oriented initdb postgresql.conf defaults. * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * src/bin/initdb/autoconfig.c * *------------------------------------------------------------------------- */ #include "postgres_fe.h" #include #include #ifdef HAVE_SYS_PARAM_H #include #endif #ifdef HAVE_SYS_SYSCTL_H #include #endif #ifdef WIN32 #include #endif #include "autoconfig.h" #include "pg_config_manual.h" static uint64_t get_system_memory_size(void); static int get_core_count(void); static uint64_t memory_round(uint64_t value, uint64_t minimum); static char *memory_pretty_value(uint64_t value); static void append_new_line(char ***lines, int *nextidx, int *maxlines, char *line); static uint64_t get_system_memory_size(void) { #ifdef WIN32 MEMORYSTATUSEX status; status.dwLength = sizeof(status); if (GlobalMemoryStatusEx(&status)) return (uint64_t) status.ullTotalPhys; #endif #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_MEMSIZE) { int mib[2] = {CTL_HW, HW_MEMSIZE}; uint64_t size = 0; size_t len = sizeof(size); if (sysctl(mib, lengthof(mib), &size, &len, NULL, 0) == 0) return size; } #endif #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_PHYSMEM64) { int mib[2] = {CTL_HW, HW_PHYSMEM64}; uint64_t size = 0; size_t len = sizeof(size); if (sysctl(mib, lengthof(mib), &size, &len, NULL, 0) == 0) return size; } #endif #ifdef _SC_AIX_REALMEM { long realmem = sysconf(_SC_AIX_REALMEM); if (realmem > 0) return (uint64_t) realmem * UINT64_C(1024); } #endif #if defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) { long pages = sysconf(_SC_PHYS_PAGES); long pagesize = sysconf(_SC_PAGESIZE); if (pages > 0 && pagesize > 0) return (uint64_t) pages * (uint64_t) pagesize; } #elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE) { long pages = sysconf(_SC_PHYS_PAGES); long pagesize = sysconf(_SC_PAGE_SIZE); if (pages > 0 && pagesize > 0) return (uint64_t) pages * (uint64_t) pagesize; } #endif #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_REALMEM) { int mib[2] = {CTL_HW, HW_REALMEM}; unsigned int size = 0; size_t len = sizeof(size); if (sysctl(mib, lengthof(mib), &size, &len, NULL, 0) == 0) return (uint64_t) size; } #endif #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_PHYSMEM) { int mib[2] = {CTL_HW, HW_PHYSMEM}; unsigned int size = 0; size_t len = sizeof(size); if (sysctl(mib, lengthof(mib), &size, &len, NULL, 0) == 0) return (uint64_t) size; } #endif return 0; } static int get_core_count(void) { #ifdef WIN32 SYSTEM_INFO info; GetSystemInfo(&info); if (info.dwNumberOfProcessors > 0) return (int) info.dwNumberOfProcessors; #endif #ifdef _SC_NPROCESSORS_ONLN { long nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs > 0) return (int) nprocs; } #endif #if defined(HAVE_SYS_SYSCTL_H) && defined(CTL_HW) && defined(HW_NCPU) { int mib[2] = {CTL_HW, HW_NCPU}; int count = 0; size_t len = sizeof(count); if (sysctl(mib, lengthof(mib), &count, &len, NULL, 0) == 0 && count > 0) return count; } #endif return 1; } static uint64_t memory_round(uint64_t value, uint64_t minimum) { uint64_t inflated; uint64_t msb = 1; uint64_t mask; if (value > UINT64_MAX - value / 8) inflated = UINT64_MAX; else inflated = value + value / 8; while (true) { uint64_t next = msb << 1; if (next <= msb || next > inflated) break; msb = next; } mask = msb | (msb >> 1); value = inflated & mask; return value >= minimum ? value : minimum; } static char * memory_pretty_value(uint64_t value) { static const char *const units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB"}; int unit = 0; while (value > 1024 && unit < lengthof(units) - 1) { value = (value + 512) >> 10; unit++; } return psprintf(UINT64_FORMAT "%s", value, units[unit]); } static void append_new_line(char ***lines, int *nextidx, int *maxlines, char *line) { if (*nextidx >= *maxlines - 1) { *maxlines *= 2; *lines = pg_realloc_array(*lines, char *, *maxlines); } (*lines)[(*nextidx)++] = line; } char ** initdb_autoconfig_update(char **lines) { char **newlines; int nlines = 0; int maxlines; int nextidx = 0; int i; uint64_t ram = get_system_memory_size(); int cpu = get_core_count(); int max_connections = 50 * cpu; int autovacuum_max_workers = Max(cpu * 12 / 5, 4); uint64_t effective_cache_size = memory_round((ram / 4) * 3, BLCKSZ); uint64_t shared_buffers = memory_round(ram / 4, 16 * (uint64_t) BLCKSZ); uint64_t work_mem = memory_round((max_connections > 0) ? (ram * 2 / max_connections) : 0, 64 * (uint64_t) BLCKSZ); uint64_t temp_buffers = memory_round(ram / 8192, 100 * (uint64_t) BLCKSZ); uint64_t maintenance_work_mem = memory_round(ram / 32, UINT64_C(1) << 30); bool highload = ram > (UINT64_C(126) << 30); char *pretty_ram; char *pretty_effective_cache_size; char *pretty_shared_buffers; char *pretty_work_mem; char *pretty_temp_buffers; char *pretty_maintenance_work_mem; while (lines[nlines] != NULL) nlines++; maxlines = nlines + 64; newlines = pg_malloc_array(char *, maxlines); pretty_ram = memory_pretty_value(ram); pretty_effective_cache_size = memory_pretty_value(effective_cache_size); pretty_shared_buffers = memory_pretty_value(shared_buffers); pretty_work_mem = memory_pretty_value(work_mem); pretty_temp_buffers = memory_pretty_value(temp_buffers); pretty_maintenance_work_mem = memory_pretty_value(maintenance_work_mem); for (i = 0; i < nlines; i++) { if (strcmp(lines[i], "# CUSTOMIZED OPTIONS\n") == 0) { append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("# Auto-generated configuration for 1C:Enterprise.\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("# Parameters are tuned for %s RAM and %d CPU cores.\n", pretty_ram, cpu)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("# Values can be used as a good starting point for server configuration.\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("#------------------------------------------------------------------------------\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("autovacuum = on\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("autovacuum_max_workers = %d\n", autovacuum_max_workers)); append_new_line(&newlines, &nextidx, &maxlines, psprintf("autovacuum_worker_slots = %d\n", autovacuum_max_workers)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("autovacuum_naptime = 20s\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("autovacuum_vacuum_cost_delay = 10ms\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("autovacuum_vacuum_cost_limit = -1\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("autovacuum_vacuum_scale_factor = 0.01\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("bgwriter_delay = 20ms\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("bgwriter_lru_maxpages = 500\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("bgwriter_lru_multiplier = 4.0\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("checkpoint_completion_target = 0.9\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("checkpoint_timeout = 10min\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("commit_delay = 1000\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("commit_siblings = 5\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("effective_cache_size = %s\n", pretty_effective_cache_size)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("effective_io_concurrency = 2\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("enable_temp_memory_catalog = off # enable to store temporary tables metadata in RAM\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("enable_temp_rd_buffers = off # enable to store small temporary tables data in RAM\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("from_collapse_limit = 20\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("fsync = on\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("geqo = on\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("geqo_threshold = 12\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("jit = off\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("join_collapse_limit = 20\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("maintenance_work_mem = %s\n", pretty_maintenance_work_mem)); append_new_line(&newlines, &nextidx, &maxlines, psprintf("max_connections = %d\n", max_connections)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_files_per_process = 8000\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_locks_per_transaction = 2000\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_logical_replication_workers = 0\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_parallel_workers = 1\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_parallel_workers_per_gather = 0\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_prepared_transactions = 0\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_wal_senders = 0\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("max_wal_size = %s\n", highload ? "10GB" : "2GB")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("max_worker_processes = 10\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("min_wal_size = 1GB\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.enable = off\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.local_tracking = on\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.min_interval = 10000\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.scale_factor = 0.1\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.table_type = 'temporary'\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.threshold = 50\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("online_analyze.verbose = off\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("plantuner.fix_empty_table = on\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("random_page_cost = 1.1\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("row_security = off\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("shared_buffers = %s\n", pretty_shared_buffers)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("shared_preload_libraries = 'mchar, fasttrun, fulleq'\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("ssl = off\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("synchronous_commit = off\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("temp_buffers = %s\n", pretty_temp_buffers)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("track_activity_query_size = 128\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("wal_level = minimal\n")); append_new_line(&newlines, &nextidx, &maxlines, psprintf("work_mem = %s\n", pretty_work_mem)); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("# pg_stat_temp = '' <- it's recommended to place on a separate disk\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("# temp_tablespaces = '' <- it's recommended to place on a separate disk\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("\n")); append_new_line(&newlines, &nextidx, &maxlines, pg_strdup("#------------------------------------------------------------------------------\n")); } append_new_line(&newlines, &nextidx, &maxlines, lines[i]); } newlines[nextidx] = NULL; free(pretty_ram); free(pretty_effective_cache_size); free(pretty_shared_buffers); free(pretty_work_mem); free(pretty_temp_buffers); free(pretty_maintenance_work_mem); free(lines); return newlines; }