|
17 | 17 | #include "miscadmin.h"
|
18 | 18 | #include "optimizer/planner.h"
|
19 | 19 | #include "pgstat.h"
|
| 20 | +#include "postmaster/autovacuum.h" |
| 21 | +#if PG_VERSION_NUM >= 120000 |
| 22 | +#include "replication/walsender.h" |
| 23 | +#endif |
20 | 24 | #include "storage/ipc.h"
|
21 | 25 | #include "storage/pg_shmem.h"
|
22 | 26 | #include "storage/procarray.h"
|
@@ -66,20 +70,46 @@ static void pgws_ExecutorEnd(QueryDesc *queryDesc);
|
66 | 70 |
|
67 | 71 | /*
|
68 | 72 | * Calculate max processes count.
|
69 |
| - * Look at InitProcGlobal (proc.c) and TotalProcs variable in it |
70 |
| - * if something wrong here. |
| 73 | + * |
| 74 | + * The value has to be in sync with ProcGlobal->allProcCount, initialized in |
| 75 | + * InitProcGlobal() (proc.c). |
| 76 | + * |
| 77 | + * We calculate the value here as it won't initialized when we need need |
| 78 | + * it during _PG_init(). |
| 79 | + * |
| 80 | + * Note that the value returned during _PG_init() might be different from the |
| 81 | + * value returned later if some third-party modules change one of the |
| 82 | + * underlying GUC. This isn't ideal but can't lead to a crash, as the value |
| 83 | + * returned during _PG_init() is only used to ask for additional shmem with |
| 84 | + * RequestAddinShmemSpace(), and postgres has an extra 100kB of shmem to |
| 85 | + * compensate some small unaccounted usage. So if the value later changes, we |
| 86 | + * will allocate and initialize the new (and correct) memory size, which |
| 87 | + * will either work thanks for the extra 100kB of shmem, of fail (and prevent |
| 88 | + * postgres startup) due to an out of shared memory error. |
71 | 89 | */
|
72 | 90 | static int
|
73 | 91 | get_max_procs_count(void)
|
74 | 92 | {
|
75 | 93 | int count = 0;
|
76 | 94 |
|
77 |
| - /* MyProcs, including autovacuum workers and launcher */ |
78 |
| - count += MaxBackends; |
| 95 | + /* |
| 96 | + * MaxBackends: bgworkers, autovacuum workers and launcher. |
| 97 | + * This has to be in sync with the value computed in |
| 98 | + * InitializeMaxBackends() (postinit.c) |
| 99 | + */ |
| 100 | + count += MaxConnections + autovacuum_max_workers + 1 |
| 101 | + + max_worker_processes; |
| 102 | + |
| 103 | + /* |
| 104 | + * Starting with pg12, wal senders aren't part of MaxConnections anymore |
| 105 | + * and have to be accounted for. |
| 106 | + */ |
| 107 | +#if PG_VERSION_NUM >= 120000 |
| 108 | + count += max_wal_senders; |
| 109 | +#endif |
| 110 | + |
79 | 111 | /* AuxiliaryProcs */
|
80 | 112 | count += NUM_AUXILIARY_PROCS;
|
81 |
| - /* Prepared xacts */ |
82 |
| - count += max_prepared_xacts; |
83 | 113 |
|
84 | 114 | return count;
|
85 | 115 | }
|
|
0 commit comments