gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

639 line
45KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>18.4. Managing Kernel Resources</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="server-start.html" title="18.3. Starting the Database Server" /><link rel="next" href="server-shutdown.html" title="18.5. Shutting Down the Server" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">18.4. Managing Kernel Resources</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="server-start.html" title="18.3. Starting the Database Server">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="runtime.html" title="Chapter 18. Server Setup and Operation">Up</a></td><th width="60%" align="center">Chapter 18. Server Setup and Operation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="server-shutdown.html" title="18.5. Shutting Down the Server">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="KERNEL-RESOURCES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">18.4. Managing Kernel Resources</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="kernel-resources.html#SYSVIPC">18.4.1. Shared Memory and Semaphores</a></span></dt><dt><span class="sect2"><a href="kernel-resources.html#SYSTEMD-REMOVEIPC">18.4.2. systemd RemoveIPC</a></span></dt><dt><span class="sect2"><a href="kernel-resources.html#id-1.6.5.6.5">18.4.3. Resource Limits</a></span></dt><dt><span class="sect2"><a href="kernel-resources.html#LINUX-MEMORY-OVERCOMMIT">18.4.4. Linux Memory Overcommit</a></span></dt><dt><span class="sect2"><a href="kernel-resources.html#LINUX-HUGE-PAGES">18.4.5. Linux Huge Pages</a></span></dt></dl></div><p>
  3. <span class="productname">PostgreSQL</span> can sometimes exhaust various operating system
  4. resource limits, especially when multiple copies of the server are running
  5. on the same system, or in very large installations. This section explains
  6. the kernel resources used by <span class="productname">PostgreSQL</span> and the steps you
  7. can take to resolve problems related to kernel resource consumption.
  8. </p><div class="sect2" id="SYSVIPC"><div class="titlepage"><div><div><h3 class="title">18.4.1. Shared Memory and Semaphores</h3></div></div></div><a id="id-1.6.5.6.3.2" class="indexterm"></a><a id="id-1.6.5.6.3.3" class="indexterm"></a><p>
  9. <span class="productname">PostgreSQL</span> requires the operating system to provide
  10. inter-process communication (<acronym class="acronym">IPC</acronym>) features, specifically
  11. shared memory and semaphores. Unix-derived systems typically provide
  12. <span class="quote">“<span class="quote"><span class="systemitem">System V</span></span>”</span> <acronym class="acronym">IPC</acronym>,
  13. <span class="quote">“<span class="quote"><span class="systemitem">POSIX</span></span>”</span> <acronym class="acronym">IPC</acronym>, or both.
  14. <span class="systemitem">Windows</span> has its own implementation of
  15. these features and is not discussed here.
  16. </p><p>
  17. The complete lack of these facilities is usually manifested by an
  18. <span class="quote">“<span class="quote"><span class="errorname">Illegal system call</span></span>”</span> error upon server
  19. start. In that case there is no alternative but to reconfigure your
  20. kernel. <span class="productname">PostgreSQL</span> won't work without them.
  21. This situation is rare, however, among modern operating systems.
  22. </p><p>
  23. By default, <span class="productname">PostgreSQL</span> allocates
  24. a very small amount of System V shared memory, as well as a much larger
  25. amount of anonymous <code class="function">mmap</code> shared memory.
  26. Alternatively, a single large System V shared memory region can be used
  27. (see <a class="xref" href="runtime-config-resource.html#GUC-SHARED-MEMORY-TYPE">shared_memory_type</a>).
  28. In addition a significant number of semaphores, which can be either
  29. System V or POSIX style, are created at server startup. Currently,
  30. POSIX semaphores are used on Linux and FreeBSD systems while other
  31. platforms use System V semaphores.
  32. </p><div class="note"><h3 class="title">Note</h3><p>
  33. Prior to <span class="productname">PostgreSQL</span> 9.3, only System V shared memory
  34. was used, so the amount of System V shared memory required to start the
  35. server was much larger. If you are running an older version of the
  36. server, please consult the documentation for your server version.
  37. </p></div><p>
  38. System V <acronym class="acronym">IPC</acronym> features are typically constrained by
  39. system-wide allocation limits.
  40. When <span class="productname">PostgreSQL</span> exceeds one of these limits,
  41. the server will refuse to start and
  42. should leave an instructive error message describing the problem
  43. and what to do about it. (See also <a class="xref" href="server-start.html#SERVER-START-FAILURES" title="18.3.1. Server Start-up Failures">Section 18.3.1</a>.) The relevant kernel
  44. parameters are named consistently across different systems; <a class="xref" href="kernel-resources.html#SYSVIPC-PARAMETERS" title="Table 18.1. System V IPC Parameters">Table 18.1</a> gives an overview. The methods to set
  45. them, however, vary. Suggestions for some platforms are given below.
  46. </p><div class="table" id="SYSVIPC-PARAMETERS"><p class="title"><strong>Table 18.1. <span class="systemitem">System V</span> <acronym class="acronym">IPC</acronym> Parameters</strong></p><div class="table-contents"><table class="table" summary="System V IPC Parameters" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Description</th><th>Values needed to run one <span class="productname">PostgreSQL</span> instance</th></tr></thead><tbody><tr><td><code class="varname">SHMMAX</code></td><td>Maximum size of shared memory segment (bytes)</td><td>at least 1kB, but the default is usually much higher</td></tr><tr><td><code class="varname">SHMMIN</code></td><td>Minimum size of shared memory segment (bytes)</td><td>1</td></tr><tr><td><code class="varname">SHMALL</code></td><td>Total amount of shared memory available (bytes or pages)</td><td>same as <code class="varname">SHMMAX</code> if bytes,
  47. or <code class="literal">ceil(SHMMAX/PAGE_SIZE)</code> if pages,
  48. plus room for other applications</td></tr><tr><td><code class="varname">SHMSEG</code></td><td>Maximum number of shared memory segments per process</td><td>only 1 segment is needed, but the default is much higher</td></tr><tr><td><code class="varname">SHMMNI</code></td><td>Maximum number of shared memory segments system-wide</td><td>like <code class="varname">SHMSEG</code> plus room for other applications</td></tr><tr><td><code class="varname">SEMMNI</code></td><td>Maximum number of semaphore identifiers (i.e., sets)</td><td>at least <code class="literal">ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 5) / 16)</code> plus room for other applications</td></tr><tr><td><code class="varname">SEMMNS</code></td><td>Maximum number of semaphores system-wide</td><td><code class="literal">ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 5) / 16) * 17</code> plus room for other applications</td></tr><tr><td><code class="varname">SEMMSL</code></td><td>Maximum number of semaphores per set</td><td>at least 17</td></tr><tr><td><code class="varname">SEMMAP</code></td><td>Number of entries in semaphore map</td><td>see text</td></tr><tr><td><code class="varname">SEMVMX</code></td><td>Maximum value of semaphore</td><td>at least 1000 (The default is often 32767; do not change unless necessary)</td></tr></tbody></table></div></div><br class="table-break" /><p>
  49. <span class="productname">PostgreSQL</span> requires a few bytes of System V shared memory
  50. (typically 48 bytes, on 64-bit platforms) for each copy of the server.
  51. On most modern operating systems, this amount can easily be allocated.
  52. However, if you are running many copies of the server or you explicitly
  53. configure the server to use large amounts of System V shared memory (see
  54. <a class="xref" href="runtime-config-resource.html#GUC-SHARED-MEMORY-TYPE">shared_memory_type</a> and <a class="xref" href="runtime-config-resource.html#GUC-DYNAMIC-SHARED-MEMORY-TYPE">dynamic_shared_memory_type</a>), it may be necessary to
  55. increase <code class="varname">SHMALL</code>, which is the total amount of System V shared
  56. memory system-wide. Note that <code class="varname">SHMALL</code> is measured in pages
  57. rather than bytes on many systems.
  58. </p><p>
  59. Less likely to cause problems is the minimum size for shared
  60. memory segments (<code class="varname">SHMMIN</code>), which should be at most
  61. approximately 32 bytes for <span class="productname">PostgreSQL</span> (it is
  62. usually just 1). The maximum number of segments system-wide
  63. (<code class="varname">SHMMNI</code>) or per-process (<code class="varname">SHMSEG</code>) are unlikely
  64. to cause a problem unless your system has them set to zero.
  65. </p><p>
  66. When using System V semaphores,
  67. <span class="productname">PostgreSQL</span> uses one semaphore per allowed connection
  68. (<a class="xref" href="runtime-config-connection.html#GUC-MAX-CONNECTIONS">max_connections</a>), allowed autovacuum worker process
  69. (<a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MAX-WORKERS">autovacuum_max_workers</a>) and allowed background
  70. process (<a class="xref" href="runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES">max_worker_processes</a>), in sets of 16.
  71. Each such set will
  72. also contain a 17th semaphore which contains a <span class="quote">“<span class="quote">magic
  73. number</span>”</span>, to detect collision with semaphore sets used by
  74. other applications. The maximum number of semaphores in the system
  75. is set by <code class="varname">SEMMNS</code>, which consequently must be at least
  76. as high as <code class="varname">max_connections</code> plus
  77. <code class="varname">autovacuum_max_workers</code> plus <code class="varname">max_wal_senders</code>,
  78. plus <code class="varname">max_worker_processes</code>, plus one extra for each 16
  79. allowed connections plus workers (see the formula in <a class="xref" href="kernel-resources.html#SYSVIPC-PARAMETERS" title="Table 18.1. System V IPC Parameters">Table 18.1</a>). The parameter <code class="varname">SEMMNI</code>
  80. determines the limit on the number of semaphore sets that can
  81. exist on the system at one time. Hence this parameter must be at
  82. least <code class="literal">ceil((max_connections + autovacuum_max_workers + max_wal_senders + max_worker_processes + 5) / 16)</code>.
  83. Lowering the number
  84. of allowed connections is a temporary workaround for failures,
  85. which are usually confusingly worded <span class="quote">“<span class="quote">No space
  86. left on device</span>”</span>, from the function <code class="function">semget</code>.
  87. </p><p>
  88. In some cases it might also be necessary to increase
  89. <code class="varname">SEMMAP</code> to be at least on the order of
  90. <code class="varname">SEMMNS</code>. If the system has this parameter
  91. (many do not), it defines the size of the semaphore
  92. resource map, in which each contiguous block of available semaphores
  93. needs an entry. When a semaphore set is freed it is either added to
  94. an existing entry that is adjacent to the freed block or it is
  95. registered under a new map entry. If the map is full, the freed
  96. semaphores get lost (until reboot). Fragmentation of the semaphore
  97. space could over time lead to fewer available semaphores than there
  98. should be.
  99. </p><p>
  100. Various other settings related to <span class="quote">“<span class="quote">semaphore undo</span>”</span>, such as
  101. <code class="varname">SEMMNU</code> and <code class="varname">SEMUME</code>, do not affect
  102. <span class="productname">PostgreSQL</span>.
  103. </p><p>
  104. When using POSIX semaphores, the number of semaphores needed is the
  105. same as for System V, that is one semaphore per allowed connection
  106. (<a class="xref" href="runtime-config-connection.html#GUC-MAX-CONNECTIONS">max_connections</a>), allowed autovacuum worker process
  107. (<a class="xref" href="runtime-config-autovacuum.html#GUC-AUTOVACUUM-MAX-WORKERS">autovacuum_max_workers</a>) and allowed background
  108. process (<a class="xref" href="runtime-config-resource.html#GUC-MAX-WORKER-PROCESSES">max_worker_processes</a>).
  109. On the platforms where this option is preferred, there is no specific
  110. kernel limit on the number of POSIX semaphores.
  111. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><span class="systemitem">AIX</span>
  112. <a id="id-1.6.5.6.3.16.1.1.2" class="indexterm"></a>
  113. </span></dt><dd><p>
  114. At least as of version 5.1, it should not be necessary to do
  115. any special configuration for such parameters as
  116. <code class="varname">SHMMAX</code>, as it appears this is configured to
  117. allow all memory to be used as shared memory. That is the
  118. sort of configuration commonly used for other databases such
  119. as <span class="application">DB/2</span>.</p><p> It might, however, be necessary to modify the global
  120. <code class="command">ulimit</code> information in
  121. <code class="filename">/etc/security/limits</code>, as the default hard
  122. limits for file sizes (<code class="varname">fsize</code>) and numbers of
  123. files (<code class="varname">nofiles</code>) might be too low.
  124. </p></dd><dt><span class="term"><span class="systemitem">FreeBSD</span>
  125. <a id="id-1.6.5.6.3.16.2.1.2" class="indexterm"></a>
  126. </span></dt><dd><p>
  127. The default IPC settings can be changed using
  128. the <code class="command">sysctl</code> or
  129. <code class="command">loader</code> interfaces. The following
  130. parameters can be set using <code class="command">sysctl</code>:
  131. </p><pre class="screen">
  132. <code class="prompt">#</code> <strong class="userinput"><code>sysctl kern.ipc.shmall=32768</code></strong>
  133. <code class="prompt">#</code> <strong class="userinput"><code>sysctl kern.ipc.shmmax=134217728</code></strong>
  134. </pre><p>
  135. To make these settings persist over reboots, modify
  136. <code class="filename">/etc/sysctl.conf</code>.
  137. </p><p>
  138. These semaphore-related settings are read-only as far as
  139. <code class="command">sysctl</code> is concerned, but can be set in
  140. <code class="filename">/boot/loader.conf</code>:
  141. </p><pre class="programlisting">
  142. kern.ipc.semmni=256
  143. kern.ipc.semmns=512
  144. </pre><p>
  145. After modifying that file, a reboot is required for the new
  146. settings to take effect.
  147. </p><p>
  148. You might also want to configure your kernel to lock System V shared
  149. memory into RAM and prevent it from being paged out to swap.
  150. This can be accomplished using the <code class="command">sysctl</code>
  151. setting <code class="literal">kern.ipc.shm_use_phys</code>.
  152. </p><p>
  153. If running in FreeBSD jails by enabling <span class="application">sysctl</span>'s
  154. <code class="literal">security.jail.sysvipc_allowed</code>, <span class="application">postmaster</span>s
  155. running in different jails should be run by different operating system
  156. users. This improves security because it prevents non-root users
  157. from interfering with shared memory or semaphores in different jails,
  158. and it allows the PostgreSQL IPC cleanup code to function properly.
  159. (In FreeBSD 6.0 and later the IPC cleanup code does not properly detect
  160. processes in other jails, preventing the running of postmasters on the
  161. same port in different jails.)
  162. </p><p>
  163. <span class="systemitem">FreeBSD</span> versions before 4.0 work like
  164. old <span class="systemitem">OpenBSD</span> (see below).
  165. </p></dd><dt><span class="term"><span class="systemitem">NetBSD</span>
  166. <a id="id-1.6.5.6.3.16.3.1.2" class="indexterm"></a>
  167. </span></dt><dd><p>
  168. In <span class="systemitem">NetBSD</span> 5.0 and later,
  169. IPC parameters can be adjusted using <code class="command">sysctl</code>,
  170. for example:
  171. </p><pre class="screen">
  172. <code class="prompt">#</code> <strong class="userinput"><code>sysctl -w kern.ipc.semmni=100</code></strong>
  173. </pre><p>
  174. To make these settings persist over reboots, modify
  175. <code class="filename">/etc/sysctl.conf</code>.
  176. </p><p>
  177. You will usually want to increase <code class="literal">kern.ipc.semmni</code>
  178. and <code class="literal">kern.ipc.semmns</code>,
  179. as <span class="systemitem">NetBSD</span>'s default settings
  180. for these are uncomfortably small.
  181. </p><p>
  182. You might also want to configure your kernel to lock System V shared
  183. memory into RAM and prevent it from being paged out to swap.
  184. This can be accomplished using the <code class="command">sysctl</code>
  185. setting <code class="literal">kern.ipc.shm_use_phys</code>.
  186. </p><p>
  187. <span class="systemitem">NetBSD</span> versions before 5.0
  188. work like old <span class="systemitem">OpenBSD</span>
  189. (see below), except that kernel parameters should be set with the
  190. keyword <code class="literal">options</code> not <code class="literal">option</code>.
  191. </p></dd><dt><span class="term"><span class="systemitem">OpenBSD</span>
  192. <a id="id-1.6.5.6.3.16.4.1.2" class="indexterm"></a>
  193. </span></dt><dd><p>
  194. In <span class="systemitem">OpenBSD</span> 3.3 and later,
  195. IPC parameters can be adjusted using <code class="command">sysctl</code>,
  196. for example:
  197. </p><pre class="screen">
  198. <code class="prompt">#</code> <strong class="userinput"><code>sysctl kern.seminfo.semmni=100</code></strong>
  199. </pre><p>
  200. To make these settings persist over reboots, modify
  201. <code class="filename">/etc/sysctl.conf</code>.
  202. </p><p>
  203. You will usually want to
  204. increase <code class="literal">kern.seminfo.semmni</code>
  205. and <code class="literal">kern.seminfo.semmns</code>,
  206. as <span class="systemitem">OpenBSD</span>'s default settings
  207. for these are uncomfortably small.
  208. </p><p>
  209. In older <span class="systemitem">OpenBSD</span> versions,
  210. you will need to build a custom kernel to change the IPC parameters.
  211. Make sure that the options <code class="varname">SYSVSHM</code>
  212. and <code class="varname">SYSVSEM</code> are enabled, too. (They are by
  213. default.) The following shows an example of how to set the various
  214. parameters in the kernel configuration file:
  215. </p><pre class="programlisting">
  216. option SYSVSHM
  217. option SHMMAXPGS=4096
  218. option SHMSEG=256
  219. option SYSVSEM
  220. option SEMMNI=256
  221. option SEMMNS=512
  222. option SEMMNU=256
  223. </pre><p>
  224. </p></dd><dt><span class="term"><span class="systemitem">HP-UX</span>
  225. <a id="id-1.6.5.6.3.16.5.1.2" class="indexterm"></a>
  226. </span></dt><dd><p>
  227. The default settings tend to suffice for normal installations.
  228. On <span class="productname">HP-UX</span> 10, the factory default for
  229. <code class="varname">SEMMNS</code> is 128, which might be too low for larger
  230. database sites.
  231. </p><p>
  232. <acronym class="acronym">IPC</acronym> parameters can be set in the <span class="application">System
  233. Administration Manager</span> (<acronym class="acronym">SAM</acronym>) under
  234. <span class="guimenu">Kernel
  235. Configuration</span> → <span class="guimenuitem">Configurable Parameters</span>. Choose
  236. <span class="guibutton">Create A New Kernel</span> when you're done.
  237. </p></dd><dt><span class="term"><span class="systemitem">Linux</span>
  238. <a id="id-1.6.5.6.3.16.6.1.2" class="indexterm"></a>
  239. </span></dt><dd><p>
  240. The default maximum segment size is 32 MB, and the
  241. default maximum total size is 2097152
  242. pages. A page is almost always 4096 bytes except in unusual
  243. kernel configurations with <span class="quote">“<span class="quote">huge pages</span>”</span>
  244. (use <code class="literal">getconf PAGE_SIZE</code> to verify).
  245. </p><p>
  246. The shared memory size settings can be changed via the
  247. <code class="command">sysctl</code> interface. For example, to allow 16 GB:
  248. </p><pre class="screen">
  249. <code class="prompt">$</code> <strong class="userinput"><code>sysctl -w kernel.shmmax=17179869184</code></strong>
  250. <code class="prompt">$</code> <strong class="userinput"><code>sysctl -w kernel.shmall=4194304</code></strong>
  251. </pre><p>
  252. In addition these settings can be preserved between reboots in
  253. the file <code class="filename">/etc/sysctl.conf</code>. Doing that is
  254. highly recommended.
  255. </p><p>
  256. Ancient distributions might not have the <code class="command">sysctl</code> program,
  257. but equivalent changes can be made by manipulating the
  258. <code class="filename">/proc</code> file system:
  259. </p><pre class="screen">
  260. <code class="prompt">$</code> <strong class="userinput"><code>echo 17179869184 &gt;/proc/sys/kernel/shmmax</code></strong>
  261. <code class="prompt">$</code> <strong class="userinput"><code>echo 4194304 &gt;/proc/sys/kernel/shmall</code></strong>
  262. </pre><p>
  263. </p><p>
  264. The remaining defaults are quite generously sized, and usually
  265. do not require changes.
  266. </p></dd><dt><span class="term"><span class="systemitem">macOS</span>
  267. <a id="id-1.6.5.6.3.16.7.1.2" class="indexterm"></a>
  268. </span></dt><dd><p>
  269. The recommended method for configuring shared memory in macOS
  270. is to create a file named <code class="filename">/etc/sysctl.conf</code>,
  271. containing variable assignments such as:
  272. </p><pre class="programlisting">
  273. kern.sysv.shmmax=4194304
  274. kern.sysv.shmmin=1
  275. kern.sysv.shmmni=32
  276. kern.sysv.shmseg=8
  277. kern.sysv.shmall=1024
  278. </pre><p>
  279. Note that in some macOS versions,
  280. <span class="emphasis"><em>all five</em></span> shared-memory parameters must be set in
  281. <code class="filename">/etc/sysctl.conf</code>, else the values will be ignored.
  282. </p><p>
  283. Beware that recent releases of macOS ignore attempts to set
  284. <code class="varname">SHMMAX</code> to a value that isn't an exact multiple of 4096.
  285. </p><p>
  286. <code class="varname">SHMALL</code> is measured in 4 kB pages on this platform.
  287. </p><p>
  288. In older macOS versions, you will need to reboot to have changes in the
  289. shared memory parameters take effect. As of 10.5 it is possible to
  290. change all but <code class="varname">SHMMNI</code> on the fly, using
  291. <span class="application">sysctl</span>. But it's still best to set up your preferred
  292. values via <code class="filename">/etc/sysctl.conf</code>, so that the values will be
  293. kept across reboots.
  294. </p><p>
  295. The file <code class="filename">/etc/sysctl.conf</code> is only honored in macOS
  296. 10.3.9 and later. If you are running a previous 10.3.x release,
  297. you must edit the file <code class="filename">/etc/rc</code>
  298. and change the values in the following commands:
  299. </p><pre class="programlisting">
  300. sysctl -w kern.sysv.shmmax
  301. sysctl -w kern.sysv.shmmin
  302. sysctl -w kern.sysv.shmmni
  303. sysctl -w kern.sysv.shmseg
  304. sysctl -w kern.sysv.shmall
  305. </pre><p>
  306. Note that
  307. <code class="filename">/etc/rc</code> is usually overwritten by macOS system updates,
  308. so you should expect to have to redo these edits after each update.
  309. </p><p>
  310. In macOS 10.2 and earlier, instead edit these commands in the file
  311. <code class="filename">/System/Library/StartupItems/SystemTuning/SystemTuning</code>.
  312. </p></dd><dt><span class="term"><span class="systemitem">Solaris</span> 2.6 to 2.9 (Solaris
  313. 6 to Solaris 9)
  314. <a id="id-1.6.5.6.3.16.8.1.2" class="indexterm"></a>
  315. </span></dt><dd><p>
  316. The relevant settings can be changed in
  317. <code class="filename">/etc/system</code>, for example:
  318. </p><pre class="programlisting">
  319. set shmsys:shminfo_shmmax=0x2000000
  320. set shmsys:shminfo_shmmin=1
  321. set shmsys:shminfo_shmmni=256
  322. set shmsys:shminfo_shmseg=256
  323. set semsys:seminfo_semmap=256
  324. set semsys:seminfo_semmni=512
  325. set semsys:seminfo_semmns=512
  326. set semsys:seminfo_semmsl=32
  327. </pre><p>
  328. You need to reboot for the changes to take effect. See also
  329. <a class="ulink" href="http://sunsite.uakom.sk/sunworldonline/swol-09-1997/swol-09-insidesolaris.html" target="_top">http://sunsite.uakom.sk/sunworldonline/swol-09-1997/swol-09-insidesolaris.html</a>
  330. for information on shared memory under older versions of Solaris.
  331. </p></dd><dt><span class="term"><span class="systemitem">Solaris</span> 2.10 (Solaris
  332. 10) and later<br /></span><span class="term"><span class="systemitem">OpenSolaris</span></span></dt><dd><p>
  333. In Solaris 10 and later, and OpenSolaris, the default shared memory and
  334. semaphore settings are good enough for most
  335. <span class="productname">PostgreSQL</span> applications. Solaris now defaults
  336. to a <code class="varname">SHMMAX</code> of one-quarter of system <acronym class="acronym">RAM</acronym>.
  337. To further adjust this setting, use a project setting associated
  338. with the <code class="literal">postgres</code> user. For example, run the
  339. following as <code class="literal">root</code>:
  340. </p><pre class="programlisting">
  341. projadd -c "PostgreSQL DB User" -K "project.max-shm-memory=(privileged,8GB,deny)" -U postgres -G postgres user.postgres
  342. </pre><p>
  343. </p><p>
  344. This command adds the <code class="literal">user.postgres</code> project and
  345. sets the shared memory maximum for the <code class="literal">postgres</code>
  346. user to 8GB, and takes effect the next time that user logs
  347. in, or when you restart <span class="productname">PostgreSQL</span> (not reload).
  348. The above assumes that <span class="productname">PostgreSQL</span> is run by
  349. the <code class="literal">postgres</code> user in the <code class="literal">postgres</code>
  350. group. No server reboot is required.
  351. </p><p>
  352. Other recommended kernel setting changes for database servers which will
  353. have a large number of connections are:
  354. </p><pre class="programlisting">
  355. project.max-shm-ids=(priv,32768,deny)
  356. project.max-sem-ids=(priv,4096,deny)
  357. project.max-msg-ids=(priv,4096,deny)
  358. </pre><p>
  359. </p><p>
  360. Additionally, if you are running <span class="productname">PostgreSQL</span>
  361. inside a zone, you may need to raise the zone resource usage
  362. limits as well. See "Chapter2: Projects and Tasks" in the
  363. <em class="citetitle">System Administrator's Guide</em> for more
  364. information on <code class="literal">projects</code> and <code class="command">prctl</code>.
  365. </p></dd></dl></div></div><div class="sect2" id="SYSTEMD-REMOVEIPC"><div class="titlepage"><div><div><h3 class="title">18.4.2. systemd RemoveIPC</h3></div></div></div><a id="id-1.6.5.6.4.2" class="indexterm"></a><p>
  366. If <span class="productname">systemd</span> is in use, some care must be taken
  367. that IPC resources (including shared memory) are not prematurely
  368. removed by the operating system. This is especially of concern when
  369. installing PostgreSQL from source. Users of distribution packages of
  370. PostgreSQL are less likely to be affected, as
  371. the <code class="literal">postgres</code> user is then normally created as a system
  372. user.
  373. </p><p>
  374. The setting <code class="literal">RemoveIPC</code>
  375. in <code class="filename">logind.conf</code> controls whether IPC objects are
  376. removed when a user fully logs out. System users are exempt. This
  377. setting defaults to on in stock <span class="productname">systemd</span>, but
  378. some operating system distributions default it to off.
  379. </p><p>
  380. A typical observed effect when this setting is on is that shared memory
  381. objects used for parallel query execution are removed at apparently random
  382. times, leading to errors and warnings while attempting to open and remove
  383. them, like
  384. </p><pre class="screen">
  385. WARNING: could not remove shared memory segment "/PostgreSQL.1450751626": No such file or directory
  386. </pre><p>
  387. Different types of IPC objects (shared memory vs. semaphores, System V
  388. vs. POSIX) are treated slightly differently
  389. by <span class="productname">systemd</span>, so one might observe that some IPC
  390. resources are not removed in the same way as others. But it is not
  391. advisable to rely on these subtle differences.
  392. </p><p>
  393. A <span class="quote">“<span class="quote">user logging out</span>”</span> might happen as part of a maintenance
  394. job or manually when an administrator logs in as
  395. the <code class="literal">postgres</code> user or something similar, so it is hard
  396. to prevent in general.
  397. </p><p>
  398. What is a <span class="quote">“<span class="quote">system user</span>”</span> is determined
  399. at <span class="productname">systemd</span> compile time from
  400. the <code class="symbol">SYS_UID_MAX</code> setting
  401. in <code class="filename">/etc/login.defs</code>.
  402. </p><p>
  403. Packaging and deployment scripts should be careful to create
  404. the <code class="literal">postgres</code> user as a system user by
  405. using <code class="literal">useradd -r</code>, <code class="literal">adduser --system</code>,
  406. or equivalent.
  407. </p><p>
  408. Alternatively, if the user account was created incorrectly or cannot be
  409. changed, it is recommended to set
  410. </p><pre class="programlisting">
  411. RemoveIPC=no
  412. </pre><p>
  413. in <code class="filename">/etc/systemd/logind.conf</code> or another appropriate
  414. configuration file.
  415. </p><div class="caution"><h3 class="title">Caution</h3><p>
  416. At least one of these two things has to be ensured, or the PostgreSQL
  417. server will be very unreliable.
  418. </p></div></div><div class="sect2" id="id-1.6.5.6.5"><div class="titlepage"><div><div><h3 class="title">18.4.3. Resource Limits</h3></div></div></div><p>
  419. Unix-like operating systems enforce various kinds of resource limits
  420. that might interfere with the operation of your
  421. <span class="productname">PostgreSQL</span> server. Of particular
  422. importance are limits on the number of processes per user, the
  423. number of open files per process, and the amount of memory available
  424. to each process. Each of these have a <span class="quote">“<span class="quote">hard</span>”</span> and a
  425. <span class="quote">“<span class="quote">soft</span>”</span> limit. The soft limit is what actually counts
  426. but it can be changed by the user up to the hard limit. The hard
  427. limit can only be changed by the root user. The system call
  428. <code class="function">setrlimit</code> is responsible for setting these
  429. parameters. The shell's built-in command <code class="command">ulimit</code>
  430. (Bourne shells) or <code class="command">limit</code> (<span class="application">csh</span>) is
  431. used to control the resource limits from the command line. On
  432. BSD-derived systems the file <code class="filename">/etc/login.conf</code>
  433. controls the various resource limits set during login. See the
  434. operating system documentation for details. The relevant
  435. parameters are <code class="varname">maxproc</code>,
  436. <code class="varname">openfiles</code>, and <code class="varname">datasize</code>. For
  437. example:
  438. </p><pre class="programlisting">
  439. default:\
  440. ...
  441. :datasize-cur=256M:\
  442. :maxproc-cur=256:\
  443. :openfiles-cur=256:\
  444. ...
  445. </pre><p>
  446. (<code class="literal">-cur</code> is the soft limit. Append
  447. <code class="literal">-max</code> to set the hard limit.)
  448. </p><p>
  449. Kernels can also have system-wide limits on some resources.
  450. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  451. On <span class="productname">Linux</span>
  452. <code class="filename">/proc/sys/fs/file-max</code> determines the
  453. maximum number of open files that the kernel will support. It can
  454. be changed by writing a different number into the file or by
  455. adding an assignment in <code class="filename">/etc/sysctl.conf</code>.
  456. The maximum limit of files per process is fixed at the time the
  457. kernel is compiled; see
  458. <code class="filename">/usr/src/linux/Documentation/proc.txt</code> for
  459. more information.
  460. </p></li></ul></div><p>
  461. </p><p>
  462. The <span class="productname">PostgreSQL</span> server uses one process
  463. per connection so you should provide for at least as many processes
  464. as allowed connections, in addition to what you need for the rest
  465. of your system. This is usually not a problem but if you run
  466. several servers on one machine things might get tight.
  467. </p><p>
  468. The factory default limit on open files is often set to
  469. <span class="quote">“<span class="quote">socially friendly</span>”</span> values that allow many users to
  470. coexist on a machine without using an inappropriate fraction of
  471. the system resources. If you run many servers on a machine this
  472. is perhaps what you want, but on dedicated servers you might want to
  473. raise this limit.
  474. </p><p>
  475. On the other side of the coin, some systems allow individual
  476. processes to open large numbers of files; if more than a few
  477. processes do so then the system-wide limit can easily be exceeded.
  478. If you find this happening, and you do not want to alter the
  479. system-wide limit, you can set <span class="productname">PostgreSQL</span>'s <a class="xref" href="runtime-config-resource.html#GUC-MAX-FILES-PER-PROCESS">max_files_per_process</a> configuration parameter to
  480. limit the consumption of open files.
  481. </p></div><div class="sect2" id="LINUX-MEMORY-OVERCOMMIT"><div class="titlepage"><div><div><h3 class="title">18.4.4. Linux Memory Overcommit</h3></div></div></div><a id="id-1.6.5.6.6.2" class="indexterm"></a><a id="id-1.6.5.6.6.3" class="indexterm"></a><a id="id-1.6.5.6.6.4" class="indexterm"></a><p>
  482. In Linux 2.4 and later, the default virtual memory behavior is not
  483. optimal for <span class="productname">PostgreSQL</span>. Because of the
  484. way that the kernel implements memory overcommit, the kernel might
  485. terminate the <span class="productname">PostgreSQL</span> postmaster (the
  486. master server process) if the memory demands of either
  487. <span class="productname">PostgreSQL</span> or another process cause the
  488. system to run out of virtual memory.
  489. </p><p>
  490. If this happens, you will see a kernel message that looks like
  491. this (consult your system documentation and configuration on where
  492. to look for such a message):
  493. </p><pre class="programlisting">
  494. Out of Memory: Killed process 12345 (postgres).
  495. </pre><p>
  496. This indicates that the <code class="filename">postgres</code> process
  497. has been terminated due to memory pressure.
  498. Although existing database connections will continue to function
  499. normally, no new connections will be accepted. To recover,
  500. <span class="productname">PostgreSQL</span> will need to be restarted.
  501. </p><p>
  502. One way to avoid this problem is to run
  503. <span class="productname">PostgreSQL</span> on a machine where you can
  504. be sure that other processes will not run the machine out of
  505. memory. If memory is tight, increasing the swap space of the
  506. operating system can help avoid the problem, because the
  507. out-of-memory (OOM) killer is invoked only when physical memory and
  508. swap space are exhausted.
  509. </p><p>
  510. If <span class="productname">PostgreSQL</span> itself is the cause of the
  511. system running out of memory, you can avoid the problem by changing
  512. your configuration. In some cases, it may help to lower memory-related
  513. configuration parameters, particularly
  514. <a class="link" href="runtime-config-resource.html#GUC-SHARED-BUFFERS"><code class="varname">shared_buffers</code></a>
  515. and <a class="link" href="runtime-config-resource.html#GUC-WORK-MEM"><code class="varname">work_mem</code></a>. In
  516. other cases, the problem may be caused by allowing too many connections
  517. to the database server itself. In many cases, it may be better to reduce
  518. <a class="link" href="runtime-config-connection.html#GUC-MAX-CONNECTIONS"><code class="varname">max_connections</code></a>
  519. and instead make use of external connection-pooling software.
  520. </p><p>
  521. On Linux 2.6 and later, it is possible to modify the
  522. kernel's behavior so that it will not <span class="quote">“<span class="quote">overcommit</span>”</span> memory.
  523. Although this setting will not prevent the <a class="ulink" href="https://lwn.net/Articles/104179/" target="_top">OOM killer</a> from being invoked
  524. altogether, it will lower the chances significantly and will therefore
  525. lead to more robust system behavior. This is done by selecting strict
  526. overcommit mode via <code class="command">sysctl</code>:
  527. </p><pre class="programlisting">
  528. sysctl -w vm.overcommit_memory=2
  529. </pre><p>
  530. or placing an equivalent entry in <code class="filename">/etc/sysctl.conf</code>.
  531. You might also wish to modify the related setting
  532. <code class="varname">vm.overcommit_ratio</code>. For details see the kernel documentation
  533. file <a class="ulink" href="https://www.kernel.org/doc/Documentation/vm/overcommit-accounting" target="_top">https://www.kernel.org/doc/Documentation/vm/overcommit-accounting</a>.
  534. </p><p>
  535. Another approach, which can be used with or without altering
  536. <code class="varname">vm.overcommit_memory</code>, is to set the process-specific
  537. <em class="firstterm">OOM score adjustment</em> value for the postmaster process to
  538. <code class="literal">-1000</code>, thereby guaranteeing it will not be targeted by the OOM
  539. killer. The simplest way to do this is to execute
  540. </p><pre class="programlisting">
  541. echo -1000 &gt; /proc/self/oom_score_adj
  542. </pre><p>
  543. in the postmaster's startup script just before invoking the postmaster.
  544. Note that this action must be done as root, or it will have no effect;
  545. so a root-owned startup script is the easiest place to do it. If you
  546. do this, you should also set these environment variables in the startup
  547. script before invoking the postmaster:
  548. </p><pre class="programlisting">
  549. export PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
  550. export PG_OOM_ADJUST_VALUE=0
  551. </pre><p>
  552. These settings will cause postmaster child processes to run with the
  553. normal OOM score adjustment of zero, so that the OOM killer can still
  554. target them at need. You could use some other value for
  555. <code class="envar">PG_OOM_ADJUST_VALUE</code> if you want the child processes to run
  556. with some other OOM score adjustment. (<code class="envar">PG_OOM_ADJUST_VALUE</code>
  557. can also be omitted, in which case it defaults to zero.) If you do not
  558. set <code class="envar">PG_OOM_ADJUST_FILE</code>, the child processes will run with the
  559. same OOM score adjustment as the postmaster, which is unwise since the
  560. whole point is to ensure that the postmaster has a preferential setting.
  561. </p><p>
  562. Older Linux kernels do not offer <code class="filename">/proc/self/oom_score_adj</code>,
  563. but may have a previous version of the same functionality called
  564. <code class="filename">/proc/self/oom_adj</code>. This works the same except the disable
  565. value is <code class="literal">-17</code> not <code class="literal">-1000</code>.
  566. </p><div class="note"><h3 class="title">Note</h3><p>
  567. Some vendors' Linux 2.4 kernels are reported to have early versions
  568. of the 2.6 overcommit <code class="command">sysctl</code> parameter. However, setting
  569. <code class="literal">vm.overcommit_memory</code> to 2
  570. on a 2.4 kernel that does not have the relevant code will make
  571. things worse, not better. It is recommended that you inspect
  572. the actual kernel source code (see the function
  573. <code class="function">vm_enough_memory</code> in the file <code class="filename">mm/mmap.c</code>)
  574. to verify what is supported in your kernel before you try this in a 2.4
  575. installation. The presence of the <code class="filename">overcommit-accounting</code>
  576. documentation file should <span class="emphasis"><em>not</em></span> be taken as evidence that the
  577. feature is there. If in any doubt, consult a kernel expert or your
  578. kernel vendor.
  579. </p></div></div><div class="sect2" id="LINUX-HUGE-PAGES"><div class="titlepage"><div><div><h3 class="title">18.4.5. Linux Huge Pages</h3></div></div></div><p>
  580. Using huge pages reduces overhead when using large contiguous chunks of
  581. memory, as <span class="productname">PostgreSQL</span> does, particularly when
  582. using large values of <a class="xref" href="runtime-config-resource.html#GUC-SHARED-BUFFERS">shared_buffers</a>. To use this
  583. feature in <span class="productname">PostgreSQL</span> you need a kernel
  584. with <code class="varname">CONFIG_HUGETLBFS=y</code> and
  585. <code class="varname">CONFIG_HUGETLB_PAGE=y</code>. You will also have to adjust
  586. the kernel setting <code class="varname">vm.nr_hugepages</code>. To estimate the
  587. number of huge pages needed, start <span class="productname">PostgreSQL</span>
  588. without huge pages enabled and check the
  589. postmaster's anonymous shared memory segment size, as well as the system's
  590. huge page size, using the <code class="filename">/proc</code> file system. This might
  591. look like:
  592. </p><pre class="programlisting">
  593. $ <strong class="userinput"><code>head -1 $PGDATA/postmaster.pid</code></strong>
  594. 4170
  595. $ <strong class="userinput"><code>pmap 4170 | awk '/rw-s/ &amp;&amp; /zero/ {print $2}'</code></strong>
  596. 6490428K
  597. $ <strong class="userinput"><code>grep ^Hugepagesize /proc/meminfo</code></strong>
  598. Hugepagesize: 2048 kB
  599. </pre><p>
  600. <code class="literal">6490428</code> / <code class="literal">2048</code> gives approximately
  601. <code class="literal">3169.154</code>, so in this example we need at
  602. least <code class="literal">3170</code> huge pages, which we can set with:
  603. </p><pre class="programlisting">
  604. $ <strong class="userinput"><code>sysctl -w vm.nr_hugepages=3170</code></strong>
  605. </pre><p>
  606. A larger setting would be appropriate if other programs on the machine
  607. also need huge pages. Don't forget to add this setting
  608. to <code class="filename">/etc/sysctl.conf</code> so that it will be reapplied
  609. after reboots.
  610. </p><p>
  611. Sometimes the kernel is not able to allocate the desired number of huge
  612. pages immediately, so it might be necessary to repeat the command or to
  613. reboot. (Immediately after a reboot, most of the machine's memory
  614. should be available to convert into huge pages.) To verify the huge
  615. page allocation situation, use:
  616. </p><pre class="programlisting">
  617. $ <strong class="userinput"><code>grep Huge /proc/meminfo</code></strong>
  618. </pre><p>
  619. </p><p>
  620. It may also be necessary to give the database server's operating system
  621. user permission to use huge pages by setting
  622. <code class="varname">vm.hugetlb_shm_group</code> via <span class="application">sysctl</span>, and/or
  623. give permission to lock memory with <code class="command">ulimit -l</code>.
  624. </p><p>
  625. The default behavior for huge pages in
  626. <span class="productname">PostgreSQL</span> is to use them when possible and
  627. to fall back to normal pages when failing. To enforce the use of huge
  628. pages, you can set <a class="xref" href="runtime-config-resource.html#GUC-HUGE-PAGES">huge_pages</a>
  629. to <code class="literal">on</code> in <code class="filename">postgresql.conf</code>.
  630. Note that with this setting <span class="productname">PostgreSQL</span> will fail to
  631. start if not enough huge pages are available.
  632. </p><p>
  633. For a detailed description of the <span class="productname">Linux</span> huge
  634. pages feature have a look
  635. at <a class="ulink" href="https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt" target="_top">https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt</a>.
  636. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="server-start.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="runtime.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="server-shutdown.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">18.3. Starting the Database Server </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 18.5. Shutting Down the Server</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1