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.
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.
Figure 1. PostgreSQL Flexible Server parameters page in Azure Portal.
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:
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.
This memory are is used by maintenance operations , such as VACUUM, REINDEX, etc.
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:
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(CLOG) keeps the states of all transactions (e.g., in_progress, committed, aborted) for Concurrency Control (CC) mechanism.
Figure 2. PostgreSQL memory architecture
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:
Figure 3. Shared Buffers parameter on Flexible Server parameters page in Azure Portal
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, which will be faster than spilling to disk. However, if there is not enough work_mem allocated, Postgres will create temporary disk files to handle the operations, slowing down performance.
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 beneficial. In this case, increasing work_mem may not provide significant benefits. Be cautious, however, as a single complex query may involve multiple sort and hash operations running concurrently. Each of these operations will use as much memory as the work_mem value allows before it starts writing to disk-based temporary files. Therefore, on a relatively busy system, total memory usage can be many times the individual work_mem parameter. When you consume more memory than is available on your machine, you can start to see out-of-memory errors within your Postgres logs, or in worse cases, the OOM killer may start randomly killing running processes to free up memory.
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;
This parameter estimates the amount of memory available for disk caching and serves as a hint for the PostgreSQL query planner. In Azure Flexible Server, the effective_cache_size value is aligned with the SKU and typically requires minimal tuning. It seldom significantly affects performance unless set to very low values.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.