title: PostgreSQL use of Linux hugepages
body: <p>I am having some trouble understanding linux hugepages within PostgreSQL. From what I have googled:</p>
  <ul>
  <li>Configured huge pages will be allocated and not swapped out of RAM.</li>
  <li>Huge pages may improve performance due to a lower number of pages to be managed by the kernel.</li>
  <li>Individual huge pages are contiguous blocks of memory.</li>
  </ul>
  <p>My Postgres cluster conf:</p>
  <ul>
  <li>shared_buffers set to 4GB</li>
  <li>max_connections 30</li>
  <li>all other conf is set as default</li>
  </ul>
  <p>Huge pages is set but when starting defaulted to not using them. After setting huge pages on in PostgreSQL and in Linux to 4096/2 with <strong>vm.nr_hugepages</strong> and <strong>sysctl -p</strong> PostgreSQL would't start because it couldn't allocate memory enough. After trying several vm.nr_hugepages values the lowest it seamed to work was <strong>3500</strong>.</p>
  <p>My questions are:</p>
  <ul>
  <li>How does PostgreSQL calculate the amount of memory it will need in advance?</li>
  <li>Read that a memlimit should be set to PostgreSQL after setting hugepages ¿If it is only going to use huge pages wouldn't this be a limit already? Assuming no other process uses them.</li>
  <li>Related to previous question: what would happen if it ran out of hugepages?</li>
  <li>Will hugepages be used by all PostgreSQL processes, a sort operation for instance. The fact of having to allocate 3500 pages in order to have PostgreSQL starting ok is a bit confusing because I thought they would be mainly used by shared_buffers.</li>
  </ul>
  <p>Thanks in advance!</p>
  <p><strong>Edit</strong>:
  The system I am testing on is an Intel 8 cores with 32GB RAM. The main purpose of the setup is ETLs, receive files which will be loaded with COPY (several GBs per file), transform with some more or less complex SQL, persist results in several tables with a design similar to a DWH (star schemas), 40TB of total storage for PostgreSQL (4x10TB drives) and 512GB SSD for Ubuntu server 18.04. Some of the transformations that will be done will require tablescans or scans of a big part of the tables, in DB2 I have the option of using a block-based buffer pool (<a href="https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.admin.perf.doc/doc/c0009651.html" rel="nofollow noreferrer">https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.admin.perf.doc/doc/c0009651.html</a>). I thought I could achieve something &quot;similar&quot; with hugepages in PostgreSQL ¿Would this be possible?</p>