Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*-------------------------------------------------------------------------
 *
 * 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 <stdint.h>
#include <unistd.h>
 
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef WIN32
#include <windows.h>
#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;
}