Memory Tuning for workloads in PostgreSQL Flexible Server on Azure

Published Oct 21 2021 11:04 AM 3,684 Views
Microsoft

Queries against large sets of table rows may need a lot of server memory to sort, join, aggregate, or sub select data. Azure Database for PostgreSQL – Flexible Server exposes number of important parameters on portal parameters page that mirror important global settings exposed by PostgreSQL via postgres.conf configuration file.  

 

black-and-white-photo-of-large-elephant-standing-in-safari.jpg

 

 

You can navigate to Flexible Server global parameters page via Azure Portal in Azure Portal -> Azure Database for PosgreSQL Servers -> Your Flexible Server -> Server Parameters.  But before we discuss some of the memory related parameters, lets first take a look at general Postgres memory architecture. 

 

screenshot-from-Azure-portal-showing-PostgreSQL-server-parameters.PNG

Figure 1. PostgreSQL Flexible Server parameters page in Azure Portal. 

 

PostgreSQL Memory Architecture

Memory usage in Postgres can be classified in two main categories.

 

First is known as Local Memory. This is memory allocated by each Postgres backend process for itself.  In PostgreSQL, each backend process allocates local memory for query processing; each area is divided into sub-areas whose sizes are either fixed or variable. These sub areas are listed below:

 

  • Work_mem. 

    PostgreSQL execution engine, aka Executor,  uses this area for sorting tuples by ORDER BY and DISTINCT operations, and for joining tables by merge-join and hash-join operations.

  • Maintenance_work_mem. 

    This memory are is used  by maintenance operations , such as VACUUM, REINDEX, etc.

  • Temp_buffers.  

    Executor uses this area for temporary table storage

Second is Shared Memory , which is memory used by all Postgres backend processes. This area is allocated by a PostgreSQL server when it starts up. This area is also divided into several sub-areas, such as:

 

  • Shared Buffer Pool. PostgreSQL loads pages within tables and indexes from persistent storage to a shared buffer pool, and then operates on them directly.
  • WAL Buffer.  

    PostgreSQL supports the WAL (Write ahead log) mechanism to ensure that no data is lost after a server failure. WAL data is really a transaction log in PostgreSQL and WAL buffer is a buffering area of the WAL data before writing it to a persistent storage.

  • Commit Log.  

    Commit Log(CLOG) keeps the states of all transactions (e.g., in_progress, committed, aborted) for Concurrency Control (CC) mechanism.

postgresql-diagram-memory-architecture.png

Figure 2. PostgreSQL memory architecture

 

Tuning Azure PostgreSQL Flexible Server memory server parameters

 

Based on above here are some important server parameters that impact performance you may wish to know about for memory management in Azure Database for PostgreSQL – Flexible Server:

 

  • Shared Buffers. The PostgreSQL shared_buffers is the major component of the shared memory used by the server. It is a large block allocated for caching blocks when reading from and writing to the database. This can be set via parameter that is called same – shared buffers. The default value for this parameter is 128 MB, which is fairly low.

    On Azure Database for PostgreSQL -Flexible Server the shared_buffers parameter setting changes depending on the selected SKU (SKU determines the memory available). General Purpose servers have 2GB shared_buffers for 2 vCores; Memory Optimized servers have 4GB shared_buffers for 2 vCores. The shared_buffers setting scales linearly (approximately) as vCores increase in a tier. Therefore, you will like not have to tune this value manually in most of the cases. Since this parameter is static you may wish to restart the service for the value to take effect if you do decide to manually change it.

sharedbuffers-parameter-screenshot-from-Azure-portal-PostgreSQL-server-oarameters-page.png

        Figure 3. Shared Buffers parameter on Flexible Server parameters page in Azure Portal

 

  •  Work_mem.  This parameter specifies the amount of memory to be used by internal sort operations and hash tables before writing to temporary disk files. If your workload has few queries with a lot of complex sorting and you have a lot of available memory, increasing this parameter may allow Postgres to do larger scans in-memory vs. spilling to disk, which will be faster.

    If you have a number of short running queries that run very frequently and perform simple lookups and joins then maintaining a lower work_mem is great, as in this case upping work_mem may simply lead to memory going unused.  Be careful however, as one complex query may have number of sort, hash operations running concurrently. Each one of those operations will use as much memory as it value allows before it starts writing to disk based temporary files. Therefore on a relatively busy system total memory usage will be many times of individual work_mem parameter. When you consume more memory than is available on your machine you can start to see out of out of memory errors within your Postgres logs, or in worse cases the OOM killer can start to randomly kill running processes to free up memory.

    Also, work_mem is allocated by powers of two, so round to the nearest binary step.  The default value of work_mem = 4MB. You can set work_mem value on multiple levels including at the server level via parameters page in Azure Portal, at the current session level and even on individual transaction level (to fine tune memory usage for specific queries without affecting the users and connections that perform other queries).  


    The example below shows how to allow specific query use up to 128 MB physical memory to perform sorting and then resets work_mem value for the current session to the current default value.

    SET work_mem = ‘128MB’;
    SELECT * FROM products ORDER BY LOWER(product_name);
    RESET work_mem;
    ​

    You can use following workflow to set work_mem to value other than default:

    1.  Check your max_connections parameter on parameters page for Azure Database for PostgreSQL – Flexible Server.  You can tune max_connections on Postgres Flexible Server, where it can be set to 5,000 maximum connections. 

    2.  You can use the formula below to calculate the optimal work_mem value for the database server - Total RAM * 0.25 / max_connections. 

    3. Due to large global memory impact we would recommend changing work_mem on transaction or session level for queries that can benefit from higher value before contemplating server level change.  

      To understand whether you should tune  work_mem for a particular query you can execute this query with EXPLAIN PLAN.  If the parameter Sort Method: external merge  Disk: xxxxkB is in the output, it means you should consider enlarging  the work_mem setting to increase performance as you may be seeing disk spillover.  Next, test with setting up work_mem value for transaction higher than those <XXXX>kB that you see in EXPLAIN ANALYZE output. As a rule of thumb, you can round up that value to the nearest megabyte. Finally, rerun EXPLAIN ANALYZE to check if ...external merge Disk... message has disappeared. 

  • Effective_cache_size. 

    This parameter estimates how much memory is available for disk caching by the operating system and within the database itself. The PostgreSQL query planner decides whether it’s fixed in RAM or not. Index scans are most likely to be used against higher values; otherwise, sequential scans will be used if the value is low. The default value of effective_cache_size = 4GB.  If your Flexible Server has higher than 8 GB of RAM you can look at setting the value to the 50% of total system memory.

We are hoping that you find this blog article helpful and are always interested how you plan to use Flexible Server offering to drive performance to your critical applications.  Additional information on topics discussed above can be found in following documents:

We’re always eager to get your feedback, so please reach out via email to Ask Azure DB for PostgreSQL.

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2863440%22%20slang%3D%22en-US%22%3EMemory%20Tuning%20for%20workloads%20in%20PostgreSQL%20Flexible%20Server%20on%20Azure%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2863440%22%20slang%3D%22en-US%22%3E%3CP%3EQueries%20against%20large%20sets%20of%20table%20rows%20may%20need%20a%20lot%20of%20server%20memory%20to%20sort%2C%20join%2C%20aggregate%2C%20or%20sub%20select%20data.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fwhat-is-flexible-server-in-azure-database-for-postgresql%2Fba-p%2F1741346%22%20target%3D%22_self%22%3EAzure%20Database%20for%20PostgreSQL%20%E2%80%93%20Flexible%20Server%3C%2FA%3E%20exposes%20number%20of%20important%20parameters%20on%20portal%20parameters%20page%20that%20mirror%20important%20global%20settings%20exposed%20by%20PostgreSQL%20via%20postgres.conf%20configuration%20file.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22black-and-white-photo-of-large-elephant-standing-in-safari.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318768iC937525DAB1D8585%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22black-and-white-photo-of-large-elephant-standing-in-safari.jpg%22%20alt%3D%22black-and-white-photo-of-large-elephant-standing-in-safari.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20navigate%20to%20Flexible%20Server%20global%20parameters%20page%20via%20Azure%20Portal%26nbsp%3Bin%20Azure%20Portal%20-%26gt%3B%20Azure%20Database%20for%20PosgreSQL%20Servers%20-%26gt%3B%20Your%20Flexible%20Server%20-%26gt%3B%20Server%20Parameters.%26nbsp%3B%20But%20before%20we%20discuss%20some%20of%20the%20memory%20related%20parameters%2C%20lets%20first%20take%20a%20look%20at%20general%20Postgres%20memory%20architecture.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22screenshot-from-Azure-portal-showing-PostgreSQL-server-parameters.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F319021i8F6F2FA77D220343%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22screenshot-from-Azure-portal-showing-PostgreSQL-server-parameters.PNG%22%20alt%3D%22screenshot-from-Azure-portal-showing-PostgreSQL-server-parameters.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3EFigure%201.%20PostgreSQL%20Flexible%20Server%20parameters%20page%20in%20Azure%20Portal.%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--322924179%22%20id%3D%22toc-hId--322089997%22%3EPostgreSQL%20Memory%20Architecture%3C%2FH2%3E%0A%3CP%3EMemory%20usage%20in%20Postgres%20can%20be%20classified%20in%20two%20main%20categories.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20is%20known%20as%20%3CSTRONG%3ELocal%20Memory%3C%2FSTRONG%3E.%20This%20is%20memory%26nbsp%3Ballocated%20by%20each%20Postgres%20backend%20process%20for%20itself.%26nbsp%3B%26nbsp%3BIn%20PostgreSQL%2C%20each%20backend%20process%20allocates%20local%20memory%20for%20query%20processing%3B%20each%20area%20is%20divided%20into%20sub-areas%20whose%20sizes%20are%20either%20fixed%20or%20variable.%20These%20sub%20areas%20are%20listed%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EWork_mem.%3C%2FSTRONG%3E%26nbsp%3B%3CP%3E%3CSPAN%3EPostgreSQL%20execution%20engine%2C%20aka%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F9.4%2Fexecutor.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EExecutor%3C%2FA%3E%2C%20%26nbsp%3Buses%20this%20area%20for%20sorting%20tuples%20by%20ORDER%20BY%20and%20DISTINCT%20operations%2C%20and%20for%20joining%20tables%20by%20merge-join%20and%20hash-join%20operations.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3EMaintenance_work_mem.%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3E%3CP%3E%3CSPAN%3EThis%20memory%20are%20is%20used%26nbsp%3B%20by%20maintenance%20operations%20%2C%20such%20as%20VACUUM%2C%20REINDEX%2C%20etc.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3ETemp_buffers.%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3E%3CP%3E%3CSPAN%3EExecutor%20uses%20this%20area%20for%20temporary%20table%20storage%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ESecond%20is%20%3CSTRONG%3EShared%20Memory%3C%2FSTRONG%3E%20%2C%20which%20is%20memory%20used%20by%20all%20Postgres%20backend%20processes.%20This%20area%20is%20allocated%20by%20a%20PostgreSQL%20server%20when%20it%20starts%20up.%20This%20area%20is%20also%20divided%20into%20several%20sub-areas%2C%20such%20as%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EShared%20Buffer%20Pool.%26nbsp%3B%3C%2FSTRONG%3EPostgreSQL%20loads%20pages%20within%20tables%20and%20indexes%20from%20persistent%20storage%20to%20a%20shared%20buffer%20pool%2C%20and%20then%20operates%20on%20them%20directly.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3EWAL%20Buffer.%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3E%3CP%3E%3CSPAN%3EPostgreSQL%20supports%20the%20WAL%20(Write%20ahead%20log%3C%2FSPAN%3E%3CSPAN%3E)%20mechanism%20to%20ensure%20that%20no%20data%20is%20lost%20after%20a%20server%20failure.%20WAL%20data%20is%20really%20a%20transaction%20log%20in%20PostgreSQL%20and%20WAL%20buffer%20is%20a%20buffering%20area%20of%20the%20WAL%20data%20before%20writing%20it%20to%20a%20persistent%20storage.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3ECommit%20Log.%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3E%3CP%3E%3CSPAN%3ECommit%20Log(CLOG)%20keeps%20the%20states%20of%20all%20transactions%20(e.g.%2C%20in_progress%2C%20committed%2C%20aborted)%20for%20Concurrency%20Control%20(CC)%20mechanism.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22postgresql-diagram-memory-architecture.png%22%20style%3D%22width%3A%20377px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318772i06C4AADBB9098F53%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22postgresql-diagram-memory-architecture.png%22%20alt%3D%22postgresql-diagram-memory-architecture.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CEM%3EFigure%202.%20PostgreSQL%20memory%20architecture%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--2130378642%22%20id%3D%22toc-hId--2129544460%22%3ETuning%20Azure%20PostgreSQL%20Flexible%20Server%20memory%20server%20parameters%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBased%20on%20above%20here%20are%20some%20important%20server%20parameters%20that%20impact%20performance%20you%20may%20wish%20to%20know%20about%20for%20memory%20management%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fflexible-server%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%20%E2%80%93%20Flexible%20Server%3C%2FA%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EShared%20Buffers.%3C%2FSTRONG%3E%26nbsp%3BThe%20PostgreSQL%20shared_buffers%20is%20the%20major%20component%20of%20the%20shared%20memory%20used%20by%20the%20server.%20It%20is%20a%20large%20block%20allocated%20for%20caching%20blocks%20when%20reading%20from%20and%20writing%20to%20the%20database.%20This%20can%20be%20set%20via%20parameter%20that%20is%20called%20same%20%E2%80%93%20%3CSTRONG%20style%3D%22font-family%3A%20inherit%3B%22%3E%3CEM%3Eshared%20buffers.%26nbsp%3B%3C%2FEM%3E%3C%2FSTRONG%3EThe%20default%20value%20for%20this%20parameter%20is%20128%20MB%2C%20which%20is%20fairly%20low.%20%3CBR%20%2F%3E%3CBR%20%2F%3EOn%20Azure%20Database%20for%20PostgreSQL%20-Flexible%20Server%20the%20shared_buffers%20parameter%20setting%20changes%20depending%20on%20the%20selected%20SKU%20(SKU%20determines%20the%20memory%20available).%20General%20Purpose%20servers%20have%202GB%20shared_buffers%20for%202%20vCores%3B%20Memory%20Optimized%20servers%20have%204GB%20shared_buffers%20for%202%20vCores.%20The%20shared_buffers%20setting%20scales%20linearly%20(approximately)%20as%20vCores%20increase%20in%20a%20tier.%20Therefore%2C%20you%20will%20like%20not%20have%20to%20tune%20this%20value%20manually%20in%20most%20of%20the%20cases.%20Since%20this%20parameter%20is%20static%20you%20may%20wish%20to%20restart%20the%20service%20for%20the%20value%20to%20take%20effect%20if%20you%20do%20decide%20to%20manually%20change%20it.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sharedbuffers-parameter-screenshot-from-Azure-portal-PostgreSQL-server-oarameters-page.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F319022iCC4E40D8A319F3B0%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22sharedbuffers-parameter-screenshot-from-Azure-portal-PostgreSQL-server-oarameters-page.png%22%20alt%3D%22sharedbuffers-parameter-screenshot-from-Azure-portal-PostgreSQL-server-oarameters-page.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%3CEM%3E%26nbsp%3B%20%26nbsp%3B%20Figure%203.%20Shared%20Buffers%20parameter%20on%20Flexible%20Server%20parameters%20page%20in%20Azure%20Portal%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3E%26nbsp%3BWork_mem.%26nbsp%3B%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3EThis%20parameter%20specifies%20the%20amount%20of%20memory%20to%20be%20used%20by%20internal%20sort%20operations%20and%20hash%20tables%20before%20writing%20to%20temporary%20disk%20files.%20If%20your%20workload%20has%20few%20queries%20with%20a%20lot%20of%20complex%20sorting%20and%20you%20have%20a%20lot%20of%20available%20memory%2C%20increasing%20this%20parameter%20may%20allow%20Postgres%20to%20do%20larger%20scans%20in-memory%20vs.%20spilling%20to%20disk%2C%20which%20will%20be%20faster.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20you%20have%20a%20number%20of%20short%20running%20queries%20that%20run%20very%20frequently%20and%20perform%20simple%20lookups%20and%20joins%20then%20maintaining%20a%20lower%20work_mem%20is%20great%2C%20as%20in%20this%20case%20upping%20work_mem%20may%20simply%20lead%20to%20memory%20going%20unused.%26nbsp%3B%20Be%20careful%20however%2C%20as%20one%20complex%20query%20may%20have%20number%20of%20sort%2C%20hash%20operations%20running%20concurrently.%20Each%20one%20of%20those%20operations%20will%20use%20as%20much%20memory%20as%20it%20value%20allows%20before%20it%20starts%20writing%20to%20disk%20based%20temporary%20files.%20Therefore%20on%20a%20relatively%20busy%20system%20total%20memory%20usage%20will%20be%20many%20times%20of%20individual%20work_mem%20parameter.%20When%20you%20consume%20more%20memory%20than%20is%20available%20on%20your%20machine%20you%20can%20start%20to%20see%20out%20of%26nbsp%3Bout%20of%20memory%26nbsp%3Berrors%20within%20your%20Postgres%20logs%2C%20or%20in%20worse%20cases%20the%20OOM%20killer%20can%20start%20to%20randomly%20kill%20running%20processes%20to%20free%20up%20memory.%3CBR%20%2F%3E%3CBR%20%2F%3EAlso%2C%20work_mem%20is%20allocated%20by%20powers%20of%20two%2C%20so%20round%20to%20the%20nearest%20binary%20step.%26nbsp%3B%20The%20default%20value%20of%20work_mem%20%3D%204MB.%20You%20can%20set%20work_mem%20value%20on%20multiple%20levels%20including%20at%20the%20server%20level%20via%20parameters%20page%20in%20Azure%20Portal%2C%20at%20the%20current%20session%20level%20and%20even%20on%20individual%20transaction%20level%20(%3CSPAN%3E%3CSPAN%3Eto%20fine%20tune%20memory%20usage%20for%20specific%20queries%20without%20affecting%20the%20users%20and%20connections%20that%20perform%20other%20queries).%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3CP%3E%3CBR%20%2F%3EThe%20example%20below%20shows%20how%20to%20allow%20specific%20query%20use%20up%20to%20128%20MB%20physical%20memory%20to%20perform%20sorting%20and%20then%20resets%26nbsp%3B%3CSTRONG%3Ework_mem%3C%2FSTRONG%3E%26nbsp%3Bvalue%20for%26nbsp%3B%3CSTRONG%3Ethe%20current%20session%3C%2FSTRONG%3E%26nbsp%3Bto%20the%20current%20default%20value.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESET%20work_mem%20%3D%20%E2%80%98128MB%E2%80%99%3B%0ASELECT%20*%20FROM%20products%20ORDER%20BY%20LOWER(product_name)%3B%0ARESET%20work_mem%3B%0A%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3EYou%20can%20use%20following%20workflow%20to%20set%20work_mem%20to%20value%20other%20than%20default%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3COL%3E%0A%3CLI%3E%26nbsp%3BCheck%20your%20%3CSTRONG%3Emax_connections%3C%2FSTRONG%3E%20parameter%20on%20parameters%20page%20for%20Azure%20Database%20for%20PostgreSQL%20%E2%80%93%20Flexible%20Server.%26nbsp%3B%26nbsp%3BYou%20can%20tune%20%3CSTRONG%3Emax_connections%3C%2FSTRONG%3E%20on%20Postgres%20Flexible%20Server%2C%20where%20it%20can%20be%20set%20to%205%2C000%20maximum%20connections.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3BYou%20can%26nbsp%3B%3CSPAN%3E%3CSPAN%3Euse%20the%20formula%20below%20to%20calculate%20the%20optimal%20work_mem%20value%20for%20the%20database%20server%20-%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3ETotal%20RAM%20*%200.25%20%2F%20max_connections.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FEM%3E%3C%2FLI%3E%0A%3CLI%3EDue%20to%20large%26nbsp%3Bglobal%20memory%20impact%20we%20would%20recommend%20changing%20work_mem%20on%20transaction%20or%20session%20level%20for%20queries%20that%20can%20benefit%20from%20higher%20value%20before%20contemplating%20server%20level%20change.%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3ETo%20understand%20whether%20you%20should%20tune%26nbsp%3B%20work_mem%20for%20a%20particular%20query%20you%20can%20execute%20this%20query%20with%20%3CEM%3EEXPLAIN%20PLAN%3C%2FEM%3E.%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EIf%20the%20parameter%26nbsp%3B%3CEM%3ESort%20Method%3A%20external%20merge%26nbsp%3B%20Disk%3A%20xxxxkB%26nbsp%3B%3C%2FEM%3Eis%20in%20the%20output%2C%20it%20means%20you%20should%20consider%20enlarging%26nbsp%3B%20the%26nbsp%3Bwork_mem%26nbsp%3Bsetting%20to%20increase%20performance%20as%20you%20may%20be%20seeing%20disk%20spillover.%26nbsp%3B%20Next%2C%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Etest%20with%20setting%20up%20work_mem%20value%20for%20transaction%20higher%20than%20those%20%3CXXXX%3EkB%20that%20you%20see%20in%20EXPLAIN%20ANALYZE%20output.%20As%20a%20rule%20of%20thumb%2C%20you%20can%20round%20up%20that%20value%20to%20the%20nearest%20megabyte.%20Finally%2C%20r%3C%2FXXXX%3E%3C%2FSPAN%3E%3CSPAN%3Eerun%3CEM%3E%20EXPLAIN%20ANALYZE%3C%2FEM%3E%20to%20check%20if%20.%3CEM%3E..external%20merge%20Disk...%3C%2FEM%3E%20message%20has%20disappeared.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3E%3CSTRONG%3EEffective_cache_size.%26nbsp%3B%3C%2FSTRONG%3E%3C%2FSTRONG%3E%3CP%3E%3CSPAN%3EThis%20parameter%20estimates%20how%20much%20memory%20is%20available%20for%20disk%20caching%20by%20the%20operating%20system%20and%20within%20the%20database%20itself.%20The%20PostgreSQL%20query%20planner%20decides%20whether%20it%E2%80%99s%20fixed%20in%20RAM%20or%20not.%20Index%20scans%20are%20most%20likely%20to%20be%20used%20against%20higher%20values%3B%20otherwise%2C%20sequential%20scans%20will%20be%20used%20if%20the%20value%20is%20low.%20The%20default%20value%20of%20effective_cache_size%20%3D%204GB.%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EIf%20your%20Flexible%20Server%20has%20higher%20than%208%20GB%20of%20RAM%20you%20can%20look%20at%20setting%20the%20value%20to%20the%2050%25%20of%20total%20system%20memory.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%20are%20hoping%20that%20you%20find%20this%20blog%20article%20helpful%20and%20are%20always%20interested%20how%20you%20plan%20to%20use%20Flexible%20Server%20offering%20to%20drive%20performance%20to%20your%20critical%20applications.%20%26nbsp%3BAdditional%20information%20on%20topics%20discussed%20above%20can%20be%20found%20in%20following%20documents%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fruntime-config-resource.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EPostgreSQL%20docs%20%E2%80%93%20Resource%20Consumption%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fflexible-server%2Fconcepts-server-parameters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%20%E2%80%93%20Flexible%20Server%20Parameters%3C%2FA%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%E2%80%99re%20always%20eager%20to%20get%20your%20feedback%2C%20so%20please%20reach%20out%20via%20email%20to%26nbsp%3B%3CA%20href%3D%22mailto%3AAskAzureDBforPostgreSQL%40service.microsoft.com%3Fsubject%3DAzure%2520Database%2520for%2520PostgreSQL%2520MemoryParameter%2520blog%2520feedback%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAsk%20Azure%20DB%20for%20PostgreSQL.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2863440%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22postgresql-elephant-teaser.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F318236i7D61BAA5912F0469%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22postgresql-elephant-teaser.jpg%22%20alt%3D%22postgresql-elephant-teaser.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EDelve%20into%20Postgres%20memory%20architecture%20and%20discuss%20some%20of%20the%20memory%20related%20global%20parameters%20you%20may%20want%20to%20tune.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2863440%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Database%20for%20PostgreSQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFlexible%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Oct 21 2021 11:04 AM
Updated by: