Subject: Suggestion for memory parameters

From: yudhi s <[email-masked]>
Date: Thu, 26 Sep 2024 16:33:28 +0530

Hello All,

In a RDS postgres we are seeing some select queries when running and doing
sorting on 50 million rows(as its having order by clause in it) , the
significant portion of wait event is showing as "IO:BufFileWrite" and it
runs for ~20minutes+.

Going through the document in the link below, it states we should monitor
the "FreeLocalStorage" metric and when monitoring that, I see it showing up
to ~535GB as the max limit and when these queries run this goes down till
100GB. Note-  (it's a R7g8xl instance)

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/apg-waits.iobuffile.html

We were thinking of bumping up the work_mem to a higher value in database
level , which is currently having size 4MB default. But we will also have
~100 sessions running at same time and majority were from other
applications which execute other single row "insert" queries and I hope
that will not need high "work_mem" . And setting it at database level will
consume 100 times that set work_mem value. So how to handle this situation?
 Or
 Is it fine to let it use "FreeLocalStorage" unless it goes till zero?

Also I am confused between the local storage (which is showing as 535GB) vs
the memory/RAM which is 256GB for this instance class with ~128TB max
storage space restriction, how are these storage different, (mainly the
535GB space which it's showing vs the 128TB storage space restriction)?
Appreciate your guidance.

select query looks something as below with no Joins but just single table
fetch:-

Select....
from <table_name>
where
order by column1, column2 LIMIT $b1 OFFSET $B2 ;

Regards
Yudhi

---

From: Philip Semanchuk <[email-masked]>
Date: Mon, 30 Sep 2024 11:16:20 -0400

> On Sep 26, 2024, at 7:03 AM, yudhi s <[email-masked]> wrote:
> 
> In a RDS postgres ...

>  Is it fine to let it use "FreeLocalStorage" unless it goes till zero?

Hi Yudhi,
FreeLocalStorage and some of the other things you ask about are specific to AWS RDS, so you might have better luck getting answers on an RDS-specific mailing list. We also use RDS-hosted Postgres and so I completely understand how Postgres and RDS are intertwined.

We have had runaway queries exhaust FreeLocalStorage. It has been quite a while since that happened, so my memories are hazy, but I’m pretty sure that when we used all of FreeLocalStorage, the result was that Postgres restarted. It might be equivalent to using all memory and disk space on a standalone system. Once there’s no storage left, behavior is unpredictable but we can’t be surprised if things crash. Usually our runaway queries got killed before FreeLocalStorage filled up, but not always.

I second Veem’s suggestion to set work_mem on a per-session basis. Also note that the doc for work_mem says, “the total memory used could be many times the value of work_mem; it is necessary to keep this fact in mind when choosing the value."

https://www.postgresql.org/docs/current/runtime-config-resource.html#GUC-WORK-MEM

Cheers
Philip

---

From: yudhi s <[email-masked]>
Date: Tue, 1 Oct 2024 12:21:37 +0530

On Mon, Sep 30, 2024 at 8:46 PM Philip Semanchuk <
[email-masked]> wrote:

> FreeLocalStorage and some of the other things you ask about are specific
> to AWS RDS, so you might have better luck getting answers on an
> RDS-specific mailing list. We also use RDS-hosted Postgres and so I
> completely understand how Postgres and RDS are intertwined.
>
> We have had runaway queries exhaust FreeLocalStorage. It has been quite a
> while since that happened, so my memories are hazy, but I’m pretty sure
> that when we used all of FreeLocalStorage, the result was that Postgres
> restarted. It might be equivalent to using all memory and disk space on a
> standalone system. Once there’s no storage left, behavior is unpredictable
> but we can’t be surprised if things crash. Usually our runaway queries got
> killed before FreeLocalStorage filled up, but not always.
>
> I second Veem’s suggestion to set work_mem on a per-session basis. Also
> note that the doc for work_mem says, “the total memory used could be many
> times the value of work_mem; it is necessary to keep this fact in mind when
> choosing the value."
>
>
Thank you.

When I execute the query with explain (analyze, buffers),I see the section
below in the plan having "sort method" information in three places
each showing ~75MB size, which if combined is coming <250MB. So , does that
mean it's enough to set the work_mem as ~250MB for these queries before
they start?

 But yes somehow this query is finished in a few seconds when i execute
using explain(analyze,buffers) while if i run it without using explain it
runs for ~10minutes+. My expectation was that doing (explain analyze)
should actually execute the query fully. Is my understanding correct here
and if the disk spilling stats which I am seeing is accurate enough to go
with?

Limit  (cost=557514.75..592517.20 rows=300000 width=1430) (actual
time=2269.939..2541.527 rows=300000 loops=1)
  Buffers: shared hit=886206, temp read=38263 written=56947
  I/O Timings: temp read=70.040 write=660.073
  ->  Gather Merge  (cost=557514.75..643393.02 rows=736048 width=1430)
(actual time=2269.938..2513.748 rows=300000 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=886206, temp read=38263 written=56947
        I/O Timings: temp read=70.040 write=660.073
        ->  Sort  (cost=556514.73..557434.79 rows=368024 width=1430)
(actual time=2227.392..2279.389 rows=100135 loops=3)
              Sort Key: column1, column2
              Sort Method: external merge  Disk: *77352kB*
              Buffers: shared hit=886206, temp read=38263 written=56947
              I/O Timings: temp read=70.040 write=660.073
              Worker 0:  Sort Method: external merge  Disk: *75592kB*
              Worker 1:  Sort Method: external merge  Disk: *74440kB*
              ->  Parallel Append  (cost=0.00..64614.94 rows=368024
width=1430) (actual time=0.406..570.105 rows=299204 loops=3)