{"id":107,"date":"2023-09-24T22:04:26","date_gmt":"2023-09-24T19:04:26","guid":{"rendered":"http:\/\/pgdataeraold.local\/?p=107"},"modified":"2023-09-24T22:04:26","modified_gmt":"2023-09-24T19:04:26","slug":"memory-structure","status":"publish","type":"post","link":"https:\/\/pgdataera.com\/en\/memory-structure\/","title":{"rendered":"Postgresql Memory Structure"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"495\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/09\/image-2.png\" alt=\"\" class=\"wp-image-108\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-2.png 771w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-2-300x193.png 300w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p> <span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">When we compare both I\/O and OIPS performances, RAM is many times faster than<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: 0.05pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">SSD<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.3pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">disk.<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">Magnetic<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">disks<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">are<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">also<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">slower<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">than<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">SSD<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">disks.<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">For<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">performance,<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">it<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">is<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">desirable<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: 0.05pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">to<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.1pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">perform<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.1pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">I\/O operations<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.1pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">on<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.05pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">memory<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.05pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">as<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.1pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">much as<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.1pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">possible.<\/span> <span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">Memory<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">structures<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">in<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">Postgresql<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">have<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.2pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">different<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.15pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">uses<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">and<\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5);letter-spacing: -0.25pt\"> <\/span><span style=\"font-size: revert;background-color: var(--ast-global-color-5)\">methods.<\/span> <\/p>\n\n\n\n<p><mark style=\"background-color:#c3ecec\" class=\"has-inline-color\">For example, one part (memory structure) is used to hold code, while another part is<br>used to hold data blocks.<br>Some memory areas are shared for all users, while others are private to the user.<\/mark><\/p>\n\n\n\n<p><strong><mark style=\"background-color:#c3ecec\" class=\"has-inline-color\">Shared Memory:<\/mark><\/strong> When the Postgresql instance is opened, it takes the memory allocated for it from the system.<\/p>\n\n\n\n<p>The amount of memory it will take is determined by <code>shared_buffer, wal_buffer<\/code>, etc. parameters. Some of these areas are also divided into subsections within themselves.<\/p>\n\n\n\n<p>Shared memory structures are shared with all users and backend processes.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:#c3ecec\" class=\"has-inline-color\">Per Backend Memory (local memory area):<\/mark><\/strong> It is used for SQL query operations. Each backend process takes this memory space from the system for its own use. It is not shared. It is divided into subsections within itself.<\/p>\n\n\n\n<p>It will take memory equal to the product of <mark style=\"background-color:var(--ast-global-color-6)\" class=\"has-inline-color\">(session number) * (Backend Memory)<\/mark><br>from the system.<br>So, care should be taken when calculating the number of sessions and Backend<br>memory areas. You can find details in the following topics.<\/p>\n\n\n\n<p>RAM: Random Access Memory<br>I\/O: Read write<br>IOPS: Input\/output operations per second. The number of operations that can be<br>performed per second.<br>(The speed of copying a 1 GB image file and a folder containing 10240 1 KB files are<br>different.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Shared Memory<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Shared Buffer<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"345\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/5.shared-buffers.png\" alt=\"\" class=\"wp-image-1401\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/5.shared-buffers.png 543w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/5.shared-buffers-300x191.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code has-ast-global-color-8-color has-ast-global-color-6-background-color has-text-color has-background has-small-font-size\" style=\"border-width:2px;border-radius:10px;margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);line-height:0.9\"><code><mark style=\"color:#000000\" class=\"has-inline-color\"><strong>a<\/strong><\/mark><mark class=\"has-inline-color\"><strong>lter system set shared_buffers ='24G<\/strong><\/mark><strong><mark style=\"color:#000000\" class=\"has-inline-color\">B<\/mark><\/strong><span style=\"background-color: initial;color: var(--ast-global-color-8)\"><mark style=\"color:#000000\" class=\"has-inline-color\"><strong>';<\/strong><\/mark><\/span><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is shared. It contains data blocks. <\/li>\n\n\n\n<li>It works integrated with OS Cache.<br>The shared buffer area is shared by all users and background processes. <\/li>\n\n\n\n<li>The default value is 128MB.<\/li>\n\n\n\n<li>If the unit (MB, GB) is not given when setting shared_buffers, it will perceive the given<br>number as the number of blocks and multiply it by the block size (size) and configure it<br>in KB.<\/li>\n<\/ul>\n\n\n\n<p>Example: <code data-enlighter-language=\"postgresql\" class=\"EnlighterJSRAW\">alter system set shared_buffers=100;<\/code><br>Since the block size is 8KB (default value), it assigns shared_buffers=800KB.<\/p>\n\n\n\n<p><mark style=\"background-color:#1843b0\" class=\"has-inline-color has-ast-global-color-5-color\">In order for the change made in shared_buffers to take effect, it is necessary to close<br>and open the instance.<\/mark><\/p>\n\n\n\n<p>The recommended value for the shared buffer is 1\/4 of the RAM in the system.<br>In some high-load instances, up to 40% of RAM can be assigned to shared buffer,<br>since postgresql works as an integrated part of the operating system cache (OS<br>Cache), it does not mean that more than 40% of memory will have a positive effect on<br>performance.<\/p>\n\n\n\n<p>Increasing the shared suffer will increase the size of modified (<code>update\/delete<\/code>) and\/<br>or new (<code>insert<\/code>) data (increasing DML capacity). The <code><strong>max_wal_size<\/strong><\/code> value should also<br>be increased in order to extend the writing process of memory to disk over a longer<br>period of time.<\/p>\n\n\n\n<p><mark style=\"background-color:var(--ast-global-color-6)\" class=\"has-inline-color\">Note: The operating system has a dependency on the kernel settings. If you get an<br>error while updating the parameter values, it may be related to the kernel settings of<br>your operating system.<\/mark><br>For detailed information about buffer cache usage, <code>pg_buffercache extension<\/code> can<br>be installed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Shared Buffer and OS Cache<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"851\" height=\"346\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/6.-Shared-Buffer-ve-OS-Cache.png\" alt=\"\" class=\"wp-image-1402\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/6.-Shared-Buffer-ve-OS-Cache.png 851w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/6.-Shared-Buffer-ve-OS-Cache-300x122.png 300w\" sizes=\"auto, (max-width: 851px) 100vw, 851px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>When a query is executed;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The backend process first checks the shared buffer if it is available, logical I\/O takes<br>place.<\/li>\n\n\n\n<li>If it is not in the buffer cache, the OS cache is checked. If it is found in the OS cache,<br>logical I\/O takes place.<\/li>\n\n\n\n<li>If they are not in the OS cache, the requested blocks are read from the disk and<br>physical I\/O is done, and the cost of physical I\/O is high.<\/li>\n<\/ol>\n\n\n\n<p>You do not want to make settings in Postgresql, such as giving most of the RAM to the<br>buffer cache and ignoring the OS cache, because it will reduce performance at another<br>point.<br>The purpose of both cache areas is to keep the data with intensive I\/O in memory and<br>to reduce the I\/O operations on the disk as much as possible.<\/p>\n\n\n\n<p>Continuous read and write operations are performed in the <code>pg_xact (before 9.6,<br>pg_clog)<\/code> folder, where the commit log information is kept. Therefore, the operating<br>system wants to keep the files in the <code>pg_xact<\/code> folder in memory (OS Cache). In this<br>case, keeping the share of OS cache high also contributes to performance.<\/p>\n\n\n\n<p>Postgresql is better at memory (shared buffer) management than OS Cache. Postgresql<br>assigns values in the range of 0-5 to the blocks, based on the processing frequency.<br>It gives high value to blocks with more transactions and low value to blocks with<br>a smaller number of transactions. When it is necessary to free up memory, blocks<br>with low values are removed from memory. This way of working is called the <code><strong>clock<br>sweep algorithm<\/strong><\/code>.<br>OS Cache usually uses various methods of LRU (Least Recently Used) as an algorithm.<br>Blocks to be cached are given a chance or two. It does not go into a detailed analysis.<br>That\u2019s why postgresql\u2019s shared buffer management is better than OS Cache.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">WAL Buffer<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"360\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/7.Wal-Buffer.png\" alt=\"\" class=\"wp-image-1403\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/7.Wal-Buffer.png 541w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/7.Wal-Buffer-300x200.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code has-ast-global-color-8-color has-ast-global-color-6-background-color has-text-color has-background\" style=\"border-width:2px;border-radius:10px;line-height:0\"><code><strong><mark class=\"has-inline-color has-ast-global-color-2-color\">alter system set wal_buffer = '64MB' ;<\/mark><\/strong><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is shared, it keeps track of changes made in the database.<\/li>\n\n\n\n<li>It keeps the information necessary to redo the changes made with DML and DDL.<\/li>\n\n\n\n<li>Postgresql supports the write-ahead log mechanism, which is necessary to <mark style=\"background-color:#ed9406\" class=\"has-inline-color has-ast-global-color-5-color\">ensure data consistency.<\/mark><\/li>\n\n\n\n<li>WAL (transaction log) keeps the metadata information of the changes made in the database.<\/li>\n\n\n\n<li>When necessary, it regenerates (REDO) or modifies the data using the records here.<\/li>\n\n\n\n<li>WAL mechanism processes the committed data to disk even in case of system errors (crash, power outage, hardware failure,  etc.) and prevents data loss and inconsistency by rolling back the uncommitted data.<\/li>\n\n\n\n<li>WAL Buffer is the buffer area, where WAL data that has not yet been written to the disk is kept temporarily.<\/li>\n\n\n\n<li>It is shared, and accessible by all background server and backend processes. <\/li>\n\n\n\n<li>In each commit operation, the data in the WAL buffer area is written to the disk.Even MB level increases can provide performance boosts.<\/li>\n\n\n\n<li>The default is \u201c-1\u201d, which is the automatic tuning setting. Takes 1 out of 32 of shared_buffers. (shared_buffers\/32).  For example, if <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">shared_buffers=512MB<\/code>, it takes <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">wal_buffer=16MB<\/code>.<\/li>\n\n\n\n<li>It cannot be less than 64KB and larger than the WAL segment size. <\/li>\n\n\n\n<li><mark style=\"background-color:#ed9406\" class=\"has-inline-color has-ast-global-color-5-color\">It is a static parameter, the instance must be closed and opened for the change to take effect.<\/mark><\/li>\n<\/ul>\n\n\n\n<p><mark style=\"background-color:var(--ast-global-color-6)\" class=\"has-inline-color\">Note: In order for the WAL mechanism to work properly, your system and database must be adjusted according to the best practice rules.<\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Clog Buffer<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"349\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/8.-clog-buffer.png\" alt=\"\" class=\"wp-image-1404\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/8.-clog-buffer.png 538w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/8.-clog-buffer-300x195.png 300w\" sizes=\"auto, (max-width: 538px) 100vw, 538px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is the shared memory area where the commit logs are kept.<\/li>\n\n\n\n<li>A transaction has 4 states. The commit log keeps the below statuses.\n<ul class=\"wp-block-list\">\n<li>in-progress<\/li>\n\n\n\n<li>committed<\/li>\n\n\n\n<li>aborted<\/li>\n\n\n\n<li>sub_committed<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Accessible by all background server and backend processes.<br>It is part of the concurrent access mechanism (Concurrency Control mechanism).<\/li>\n\n\n\n<li>Although there is no parameter that can change its size, it can be increased by<br>compiling from the source code (<code>CLOGShmemBuffers()<\/code>) or patching during the first<br>installation.<\/li>\n\n\n\n<li>Writes to the <code>pg_xact<\/code> directory. (Before version 10 &#8211;&gt; <code>pg_clog<\/code>).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Lock Space<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"362\" height=\"239\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/09\/image-4.png\" alt=\"\" class=\"wp-image-320\" style=\"width:568px;height:375px\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-4.png 362w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-4-300x198.png 300w\" sizes=\"auto, (max-width: 362px) 100vw, 362px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>It is shared. It has no parameters that can be changed directly.<br>It is the memory area where more than average locks are stored. This lock is shared by<br>the background and backend user processes.<br>Changes in <code>max_locks_per_transaction<\/code> and <code>max_pred_locks_per_transaction<\/code> parameters also affect <strong>lock space<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-center\">Per Backend Memory<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-size:28px\">Work Memory<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"457\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/10.-work-memory.png\" alt=\"\" class=\"wp-image-1405\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/10.-work-memory.png 575w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/10.-work-memory-300x238.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is not shared, each session has its own memory space.<\/li>\n\n\n\n<li>Sort is used for order by, distinct, merge joins, and hash table operations.<\/li>\n\n\n\n<li>If the work_mem space is not enough, it writes to temporary files on the disk (physical I\/O).<\/li>\n\n\n\n<li>Each session and process uses its own private space. It takes space from the memory space from the system based on the calculation, the number of <mark style=\"background-color:#55713d\" class=\"has-inline-color has-ast-global-color-5-color\">sessions * work_mem<\/mark>. The default value of <code>work_mem<\/code> is <code>4MB<\/code>. If your instance has 300 sessions, 4MB * 300 = 1200MB = 1GB memory from your operating system (not from <code>shared_buffers<\/code>).<\/li>\n\n\n\n<li>In complex queries, several sort and hash operations can run in parallel, each of these operations can allocate a separate <code>work_mem<\/code> space for itself.<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># To change system-wide;\nalter system set work_mem= '16MB';\n\n# To change the work_mem value of the current session ;\nset local work_mem = 128MB;\n\n# Due to a bug in Postgres 13, this setting gives an error. It can be run as follows;\nset local work_mem = 102400\\;\n\n# To set work_mem specific to a specific user or role;\nalter user mehmed set work_mem= '256MB';<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>Hash-based operations require more memory than sort operations.<br>In hash table operations, the amount of memory that can be used is calculated by<br>multiplying the value in the <code>hash_mem_multiplier<\/code> parameter with the value<br>of<code> work_mem<\/code>. Therefore, hash-based operations can use more than the standard<br>amount of <code>work_mem<\/code>. The default unit of the <code>work_mem<\/code> parameter is KB. If no unit is<br>specified when assigning a value, it assigns a value in KB.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Temp Memory<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"435\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/11.-temp-memory.png\" alt=\"\" class=\"wp-image-1406\" style=\"aspect-ratio:1;width:547px;height:auto\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/11.-temp-memory.png 546w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/11.-temp-memory-300x239.png 300w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># To change system-wide;\nalter system set set temp_buffers='16MB';\n\n# To change the temp_buffers of the current session;\nset local temp_buffers = 128MB;\n\n# Due to a bug in Postgres 13, this setting gives an error.\n# It does not give an error as below.\nset local temp_buffers = 102400\\;\n\n# To set work_mem specific to a specific user or role;\nalter user mehmed set work_mem='256MB';<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is the buffer area used for temp tables.<\/li>\n\n\n\n<li>It is not shared.<\/li>\n\n\n\n<li>Each session has its own temp buffer area.<\/li>\n\n\n\n<li>temp_buffers determines the maximum temp buffer space that each session can use.<\/li>\n\n\n\n<li>The default value is 8MB.<\/li>\n\n\n\n<li>If the unit is not given when changing, it calculates the new value by multiplying it by the block size.<\/li>\n\n\n\n<li>It does not reserve the amount of memory given in the parameter as soon as the session starts.<\/li>\n\n\n\n<li>It is a field that is used when needed, even if large values are given. Changes can be made based on session.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-size:30px\">Maintenance Work Memory<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"472\" height=\"356\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/12.-maintence-work-memory.png\" alt=\"\" class=\"wp-image-1407\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/12.-maintence-work-memory.png 472w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/12.-maintence-work-memory-300x226.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code has-ast-global-color-8-color has-ast-global-color-6-background-color has-text-color has-background\" style=\"border-width:2px;border-radius:10px;margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);line-height:0\"><code><strong><mark class=\"has-inline-color has-ast-global-color-2-color\">alter system set maintenance_work_mem= '128MB';<\/mark><\/strong><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is the memory area used for maintenance operations like <code><strong>vacuum, create index, alter table<\/strong><\/code> <code><strong>add foreign key<\/strong><\/code> etc.<\/li>\n\n\n\n<li>It determines the upper limit of the memory that can be used for support and maintenance operations. <\/li>\n\n\n\n<li>The default value is 64 MB.<\/li>\n\n\n\n<li>If the value is changed without giving a unit, the default unit is kilobyte.<\/li>\n\n\n\n<li>A session runs only one maintenance task. So large <code>work_mem<\/code> can be given. <mark style=\"background-color:#e57d1e\" class=\"has-inline-color has-ast-global-color-5-color\">Thus, the performance of <code><strong>vacuum and dump restore<\/strong><\/code> operations is increased. <\/mark><\/li>\n\n\n\n<li>While the <code>autovacuum<\/code> process is running, it can take up to <code>autovacuum_max_workers<\/code> times memory.<\/li>\n\n\n\n<li>The <code>autovacuum_max_workers<\/code> default value is 3, so be careful when increasing it.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-size:30px\">Catalog Cache<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"559\" height=\"430\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/11\/13.-catalog-cache.png\" alt=\"\" class=\"wp-image-1408\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/13.-catalog-cache.png 559w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/11\/13.-catalog-cache-300x231.png 300w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It is the memory area where the system tables are kept.<\/li>\n\n\n\n<li>Schema, table, view, index etc. in system tables. Contains information about objects.<\/li>\n\n\n\n<li>Most transactions require catalog information. <mark style=\"background-color:#8f52bd\" class=\"has-inline-color has-ast-global-color-5-color\">For example, the table select statement needs information like does table really exist, does it have a column queried, does the user requesting has privilege, etc. a lot of information is accessed from the system catalog<\/mark>. Reading this data from the disk every time will cause performance bottlenecks, so it is kept in the catalog cache.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"font-size:30px\">Autovacuum Work Mem<\/h3>\n\n\n\n<pre class=\"wp-block-code has-ast-global-color-8-color has-text-color\" style=\"border-width:2px;border-radius:10px;margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);line-height:0\"><code><strong><mark class=\"has-inline-color has-ast-global-color-2-color\">alter system set autovacuum_work_mem = '128MB';<\/mark><\/strong><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Determines the maximum amount of memory that each autovacuum worker process can use.<\/li>\n\n\n\n<li>If the unit is changed without giving it, it takes KB as the default unit.<\/li>\n\n\n\n<li>Its default value is \u201c-1\u201d. In this case it takes the same value as <code><strong>maintenance_work_mem<\/strong><\/code>.<\/li>\n\n\n\n<li>Autovacuum can use a maximum of <strong>1 GB<\/strong> of memory when collecting <code>dead tuple<\/code> identifiers. So having value of more than 1GB will have no effect<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>When we compare both I\/O and OIPS performances, RAM is many times faster than SSD disk. Magnetic disks are also slower than SSD disks. For performance, it is desirable to perform I\/O operations on memory as much as possible. Memory structures in Postgresql have different uses and methods. For example, one part (memory structure) is <a href=\"https:\/\/pgdataera.com\/en\/memory-structure\/\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Postgresql Memory Structure<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[277],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-postgresql-database-architecture"],"_links":{"self":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}