Postgress memory usage
jcmaad
HOBBYOP

2 months ago

My website is getting essentially no traffic and I myself barely visit it. Meanwhile, my postgress memory usage is steady 300mb for which I am paying. AI is telling that this is likely the floor for Railway's managed Postgres, but it was lower before. As you can see by CPU usage, there is no activity on the site (30 day log). The DB itself is about 100mb. Am I doing something wrong?

Solved$10 Bounty

Pinned Solution

vicnas
HOBBY

2 months ago

Restarting will drop it temporarily but memory will climb right back once Postgres warms its cache again — so that's not really a fix.

The floor you're seeing is Railway's fixed shared_buffers allocation (~256 MB) plus OS overhead, which exists regardless of actual usage. Since your whole DB fits in RAM, Postgres just holds it there. That's by design and won't change short of migrating to a serverless Postgres like Neon, which actually scales to near-zero when idle.

12 Replies

Status changed to Open Railway about 2 months ago


The increases in your memory graph show postgres caching data into RAM. Because your server isn't under memory pressure, postgres is holding onto that cache. When a query runs, postgres pulls data from the disk into its memory cache (shared_buffers) to make future lookups faster.

If you want to prove that this is just cached data, you can simply redeploy/restart your postgres service. You will likely see the memory drop back down to 150MB-200MB or even less.


vicnas
HOBBY

2 months ago

Restarting will drop it temporarily but memory will climb right back once Postgres warms its cache again — so that's not really a fix.

The floor you're seeing is Railway's fixed shared_buffers allocation (~256 MB) plus OS overhead, which exists regardless of actual usage. Since your whole DB fits in RAM, Postgres just holds it there. That's by design and won't change short of migrating to a serverless Postgres like Neon, which actually scales to near-zero when idle.


vicnas

Restarting will drop it temporarily but memory will climb right back once Postgres warms its cache again — so that's not really a fix. The floor you're seeing is Railway's fixed `shared_buffers` allocation (\~256 MB) plus OS overhead, which exists regardless of actual usage. Since your whole DB fits in RAM, Postgres just holds it there. That's by design and won't change short of migrating to a serverless Postgres like **Neon**, which actually scales to near-zero when idle.

Restarting/redeploying isn't a "fix". As I said, it's to prove the caching behavior of postgres...


jcmaad
HOBBYOP

2 months ago

I restarted/redeployed and the memory usage didn't go down.

Storage costs is way less than memory cost, so i increase the size of DB so postgress doesn't save it in RAM, my cost should go down, in theory. Is this a correct assumption?


jcmaad

I restarted/redeployed and the memory usage didn't go down. Storage costs is way less than memory cost, so i increase the size of DB so postgress doesn't save it in RAM, my cost should go down, in theory. Is this a correct assumption?

No, that assumption is incorrect. Increasing your disk storage space will not lower your RAM usage. Databases cache data in RAM for speed, not because they are running out of storage space.

As for not seeing a drop in memory usage, it's because it doesn't update in real time, but every minute IIRC. But nonetheless, If a restart didn't even temporarily drop the memory, it confirms that 300MB is the baseline required to keep your specific setup running.

If you really want to lower the memory usage you can tune shared_buffers


jcmaad
HOBBYOP

2 months ago

Thanks, what should I tune in shared_buffers?


jcmaad

Thanks, what should I tune in shared\_buffers?

You can either run an ALTER SYSTEM SQL command or edit the postgresql.conf file directly. Because shared_buffers dictates how much memory is allocated when the server starts.

Using SQL for example: ALTER SYSTEM SET shared_buffers = '64MB'; (adjust the value as needed). Then you would need to restart the postgres service for the changes to take effect.


You can also run SHOW shared_buffers; to see what the current value is.


You can also use a tool like PGTune to help you tune postgres settings for your application needs.


jcmaad
HOBBYOP

2 months ago

Thank you, I will give it a shot.

What about enabling Serverless on Postgress service? It will put it to sleep when not being used


jcmaad

Thank you, I will give it a shot. What about enabling Serverless on Postgress service? It will put it to sleep when not being used

Yeah, you can definitely enable serverless since you don't have a lot of traffic. This way, your postgres service won't consume any resources when sleeping. Thus, saving you money. But you need to keep cold start (first request takes longer waiting for the service to be awaken) in mind in this case.


One more thing I wanted to ask you, did you really redeploy your postgres service? Because it always restarts RAM usage. And my postgres service sits at around 30MB ~ 50MB when there is no traffic or when I redeploy/restart.


Status changed to Solved brody about 2 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...