Skip to content
Postgres memory stays high after restart
nathanmlu
OP

24 days ago

My postgres service rises to about 3–4 GB soon after a restart and stays there even when traffic is low. Restarting only helps briefly. Most of the memory appears to be Linux file cache. A 2 GB memory limit caused database errors.

Is there a supported way to reclaim that cache without restarting Postgres or setting a limit that risks errors? Our PITR is off, but this looks similar to this Railway question.

https://station.railway.com/questions/railway-postgres-pitr-backup-leaves-6-gb-e762df27

after more digging, seems like we'd need some sort of reclaim action using cgroup v2memory.reclaim. Most of our Postgres memory is clean inactive_file cache, but /sys/fs/cgroup is read-only inside the container. Restarting clears it only briefly, and a 2 GB hard limit caused database errors.

Service: https://railway.com/project/8df3be11-1778-4a24-b5f9-15502ddbc96d/service/3db687b9-e9e9-43e9-9d57-8e1f0ed84a9c/database?environmentId=c40cacf0-c01a-46da-adc3-093ab1f0739a

Solved

1 Reply

Status changed to Awaiting Railway Response Railway • 24 days ago


dizzydes90
EMPLOYEE

24 days ago

There's no supported way to drop that cache from inside the service: the cgroup files are read-only in the container and we don't run a manual reclaim on request. The memory figure is the container's cgroup memory.current plus swap, sampled every 30 seconds, and it includes clean file cache, so the 3 to 4 GB is mostly Postgres reading its data files through the kernel cache. The kernel only drops that cache when the cgroup gets close to its limit, which is why a restart clears it briefly and it climbs back. Those cached pages count toward billed memory too.

On your service: the limit was the Pro default of 32 GB until the deploy on October 25 at 19:10 UTC set it to 6 GB. Since then usage dropped to about 0.5 GB and has climbed back to about 3.2 GB, which is the cache refilling under the new ceiling. A memory limit is the only lever that bounds it, and it has to stay above what Postgres itself needs, so leave headroom above the errors you saw at 2 GB rather than chasing the cache number. The PITR note in the question you linked doesn't apply here since PITR is off on this service.


Status changed to Awaiting User Response Railway • 24 days ago


Status changed to Solved nathanmlu • 21 days ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...