<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M Barendregt on Medium]]></title>
        <description><![CDATA[Stories by M Barendregt on Medium]]></description>
        <link>https://medium.com/@mbarendregt?source=rss-240ffba90f21------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1P9_Zj7g3M2kaZ-TFoZL8g.jpeg</url>
            <title>Stories by M Barendregt on Medium</title>
            <link>https://medium.com/@mbarendregt?source=rss-240ffba90f21------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:10:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mbarendregt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Speeding up indexing — tuning your AWS RDS Postgres instance]]></title>
            <link>https://medium.com/@mbarendregt/speeding-up-indexing-tuning-your-aws-rds-postgres-instance-67d3c91526a4?source=rss-240ffba90f21------2</link>
            <guid isPermaLink="false">https://medium.com/p/67d3c91526a4</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[indexing]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[rds]]></category>
            <dc:creator><![CDATA[M Barendregt]]></dc:creator>
            <pubDate>Mon, 15 Nov 2021 13:14:59 GMT</pubDate>
            <atom:updated>2021-11-15T13:29:19.399Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Speeding up indexing — tuning your AWS RDS Postgres instance</strong></h3><figure><img alt="Running man increasing speed" src="https://cdn-images-1.medium.com/max/1024/1*lqY4aP1ib7BH7tdWbgDAFw.png" /></figure><p>The cloud is awesome because it is designed as a turnkey solution to accommodate the <a href="https://en.wikipedia.org/wiki/Anthropometry">95%</a>. But sometimes, you are just part of that 5% or you want to understand the why rather than just tuning up your resources. If that is you, and you are facing RDS Postgres performance bottlenecks, continue reading. I will be discussing some Postgres database settings that can help you increase your databases <strong>indexing speed</strong>, such as <strong>maintenance workers</strong>, <strong>maintenance worker memory</strong>, <strong>max_wal_size</strong><em> </em>and <strong>checkpoint timeout</strong><em> </em>as well as AWS specific settings such as <strong>Multi-AZ</strong> versus <strong>Single-AZ </strong>and <strong>gp2</strong><em> </em>versus<em> </em><strong>io1</strong><em> </em>storage.</p><h3><strong>Cloud architecture can quickly become a matter of just adding more resources</strong></h3><p>With the introduction of cloud computing, it has become very easy to mitigate performance bottlenecks by simply scaling vertically (e.g., increasing storage size or provision a heavier machine) or horizontally (e.g., simply adding a few more instances). In most cases, this might simply be true. Logically so, because many default settings are default for a reason and you never need to worry about it. In our case however, this principle did not apply anymore, and it seemed the finer arts of a database specialist were still very much needed.</p><h3><strong>Quickly summarise our case</strong></h3><p>Before I end up writing a massive post about all the ins and outs of our use case, without sharing any results, I will simply jump to the part that is important and what we’ve learned. Basically, we were batch inserting big chunks of data on a db.t3.2xlarge RDS Postgres instance and we needed to apply multiple indexes on multiple large tables. Since this was a recurring task, we wanted this to be done as quickly as possible.</p><h4><strong>What happened</strong></h4><p>According to best practices, we dropped indexes before ingesting data, and reapplied indexes afterwards. For databases consisting of 500Gb of data, this was cringingly slow, taking over 10 hours to complete. We were monitoring CPU and memory closely and noticed that memory usage kept well below 1Gb and CPU usage never went beyond 60%. Read and write IOPS did go as high as 14000 (although <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Storage.html#Concepts.Storage.GeneralSSD">according to AWS</a> we could have gone till 12000) only to completely die afterwards. This was the moment we started investigating how we can tune our database.</p><h3><strong>Indexes on AWS Postgres databases — how it really works</strong></h3><p>Creating indexes on Postgres databases are handled by <em>maintenance workers</em><strong><em>.</em></strong> Furthermore, the create index command is the only utility that can run in parallel (when building <a href="https://en.wikipedia.org/wiki/B-tree">BTREE</a>) and parallelization is handled by the setting <strong>max_parallel_maintenance_workers</strong><em>. </em>The amount of memory that these workers use can be configured by adjusting the <strong>maintenance_worker_mem</strong> setting. Furthermore, two settings to consider when doing batch inserts are <strong>checkpoint_timeout</strong> and <strong>max_wal_size</strong>.</p><p>When you create an RDS Postgres instance, it will reference the <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_WorkingWithParamGroups.html">default DB parameter group</a>. This parameter group allows you to adjust Postgres settings without the need to login to your database, and ensures you don’t lose your settings in case of a reboot. The settings we are interested in are configured by default as follows:</p><pre><strong>max_parallel_maintenance_workers</strong><em> = </em>2</pre><pre><strong>maintenance_worker_mem</strong><em> = </em>GREATEST({DBInstanceClassMemory*1024/63963136},65536)</pre><pre><strong>checkpoint_timeout</strong><em> = </em>5 minutes</pre><pre><strong>max_wal_size</strong> = 2Gb</pre><p>Each worker can effectively use a CPU core and especially with batch inserts you can fairly safely increase the amount of memory which the workers are allowed to use to be half of your memory. Explaining how checkpoints work is beyond the scope of this article, but <a href="https://www.postgresql.org/docs/9.1/wal-configuration.html">see the official documentation</a> if you are interested, but it is safe to say that checkpoints are fairly expensive in terms of I/O load.</p><h4><strong>How do these settings contribute individually to the speed of indexing?</strong></h4><p>Because we ended up adjusting all the settings at the same time, I wanted to know how each setting individually contributed to the speed of indexing. Therefor I created a fresh database, ingested 25Gb of random numeric data into a table and performed 3 isolated experiments.</p><p><strong>Experiment 1: Tuning Postgres settings</strong></p><p>The first experiment is about tuning Postgres settings, without tweaking AWS settings. The results show that tweaking each of these settings only had marginal effects when applying a single index on a Single-AZ 3000 IOPS io1 db.t3.2xlarge machine.</p><figure><img alt="The effect of increasing maintenance worker memory, increasing maintenance worker count, increasing max_wal_size and increasing checkpoint timeout measured in minutes. Each of these settings marginally contribute to lowering the index speed." src="https://cdn-images-1.medium.com/max/714/1*TWkuVIFwEzZIauzUgMbEGg.png" /><figcaption><em>The effect of increasing maintenance worker memory, increasing maintenance worker count, increasing max_wal_size and increasing checkpoint timeout measured in minutes. Each of these settings marginally contribute to lowering the index speed.</em></figcaption></figure><p><strong>Experiment 2: Tuning AWS settings</strong></p><p>In this second experiment I wanted to measure the impact of multi versus single AZ and gp2 versus io1. In this experiment you can clearly see that moving from a Multi-AZ to Single-AZ configuration significantly lowers the indexing time. But wait, what about my disaster recovery? Fair point. In our case we are running a read replica and we accepted a possibility of small data loss and a small increase in the recovery time. The reason we can do this is because we are running batch inserts and can simply rerun a batch which is possibly not the case for most of you out there. But definitely something to consider instead of blindly following the Multi-AZ best practices.</p><p>The effect of gp2 versus io1 on indexing time is negligible. However, this could be explained by the fact that I only applied a single index, which in turn uses just a single core and a single maintenance worker that has a max of 4Gb memory. So, there seems no demonstrable advantage of switching your storage to the more expensive provisioned IOPS in this case.</p><figure><img alt="The effect of moving from a Multi-AZ to a Single-AZ and moving from gp2 to io1. Choosing a single-AZ setting when you do not need a very tight RTO/RPO strategy is definitely something to consider speeding up your database. *Multi-az io1 is missing in this overview, but same paradigm is true for all storage types." src="https://cdn-images-1.medium.com/max/597/1*Y9wq69c5ry52rFYEv5xKYA.png" /><figcaption><em>The effect of moving from a Multi-AZ to a Single-AZ and moving from gp2 to io1. Choosing a single-AZ setting when you do not need a very tight RTO/RPO strategy is definitely something to consider speeding up your database. *Multi-az io1 is missing in this overview, but same paradigm is true for all storage types.</em></figcaption></figure><p><strong>Experiment 3: Putting some pressure on the database</strong></p><p>In the end the real test is applying multiple indexes at the same time, to see the power of increasing the maintenance worker count, as well as the storage type. In the last experiment I applied 3 indexes simultaneously to 3 tables each containing 25Gb random generated numeric data. Really interesting to see here is that io1 is +- 23% quicker than gp2, while both had enough IOPS provisioned to be able to handle the job. We scheduled a call with AWS RDS experts on this matter and according to them it has to do with the underlying mechanism and how files are written to disk, but the conclusion of that call was ‘9000 IOPS for gp2 storage is <strong>not</strong> the same as 9000 provisioned IOPS for io1’ and this experiment proves that. Furthermore, as the graph clearly shows, using more workers helps significantly!</p><figure><img alt="The effect of moving to multiple maintenance workers when applying multiple indexes simultaneously. Increasing your maintenance workers and running the queries in parallel gives a major performance bonus. Furthermore, changing storage type from gp2 to io1 also lowers the time required to apply multiple indexes. *2MW with gp2 is missing, but same paradigm is true for all storage types. Increasing your maintenance workers results in a performance boost, more so for io1 compared to gp2." src="https://cdn-images-1.medium.com/max/605/1*70US7AQfESMYx-pSe6CCPA.png" /><figcaption><em>The effect of moving to multiple maintenance workers when applying multiple indexes simultaneously. Increasing your maintenance workers and running the queries in parallel gives a major performance bonus. Furthermore, changing storage type from gp2 to io1 also lowers the time required to apply multiple indexes. *2MW with gp2 is missing in this overview, but the same paradigm is true for all storage types. Increasing your maintenance workers results in a performance boost, more so for io1 compared to gp2.</em></figcaption></figure><h3><strong>Conclusion</strong></h3><p>In cloud computing it has become very easy and attractive to simply move to larger instance sizes or add additional storage to your database in order to mitigate performance bottlenecks. However, in this blog post I hope I showed you that there are many other things to configure before putting your RDS on steroids, and emptying your wallet in the process! You really can benefit from changing your maintenance worker memory and count (and it is not scary to do so). Moreover, if your RPO/RTO allows, you can move away from that Multi-AZ setup, and simply go with a Single-AZ and a read replica (this setup costs exactly the same), and if you like fast things and really want to blaze through your indexes, chuck that io1 storage under your RDS. Happy indexing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67d3c91526a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>