r/Supabase • • 2d ago

storage Memory commitment hits the limit during book imports on Micro (Postgres 17), with only ~30 active users. What's using it?

Post image

My app (Readbit) has about 70 users and ~30 active. The database restarted on 22 Nov at 18:45 UTC with "database system was not properly shut down; automatic recovery in progress". Memory commitment was at the limit (~2 GB) and swap was ~1 GB. I've since raised the compute plan, but memory still climbs during imports.

What I measured (controlled tests, UTC):

• Memory commitment rises ~20-30 MB/min while a book import runs. It falls back to baseline ~7 min after I pause it.

• 20:34-20:49: ~160 books processed. Commitment went 1.33 → 1.81 GB, swap 200 → 524 MB.

• 21:16-21:21: 19 books. Commitment rose to 1.42 GB.

• 21:23-21:28: same scheduled jobs and calls, but only 4 books. No rise.

• pg_stat_statements shows only ~2.5 s of total DB time per run, no temp files, and Realtime volume of about 1 change/sec. So query work doesn't explain the memory. How to resolve this? There is a long memory swap going onm

3 Upvotes

17 comments sorted by

1

u/burggraf2 Supabase team 1d ago

Have you looked at the Performance and Query Advisors in the dashboard? See if they offer any tips and grab the query that's taking up the most memory and maybe use EXPLAIN ANALYZE on that query.

1

u/burggraf2 Supabase team 1d ago

Also, QQ: how exactly are you doing your book import? Importing into Postgres can be excruciatingly slow to blazing fast depending on the method. I've found using the COPY command to be by far the fastest import method, sometimes orders of magnitude faster than insert statements, etc.

1

u/Classic-Remote-5860 1d ago

30 active users. thirty. you keep writing that number like it explains anything and it doesnt, memory commitment doesnt care how many people are browsing your app, it cares what ONE backend is doing at that second. a book import that sorts a big table with work_mem at whatever the default is will eat your whole Micro plan by itself, zero users required. and the fact that it drops back to baseline 7 min after you pause the import is your answer right there, that isnt a leak, thats work still sitting in memory and something taking its time to hand it back, could be a long transaction, could be autovacuum deciding thats the moment, could just be the sort spilling to disk and back. swapping 1GB and a kernel killing a backend hard enough that you get "not properly shut down" is not a plan size problem, raising the compute plan just means you crash the next size up with slightly prettier numbers on the graph. you doubled the RAM and it still climbs, that should tell you it was never capacity. go run pg_stat_activity while the import is actually running, not after, after tells you nothing, look at how many backends and what state and how long the xact has been open. and check work_mem against max_connections, everyone sets work_mem to 128MB thinking its per database and then wonders why 20 imports in parallel commit 2GB of memory the kernel never agreed to back.

1

u/Weak_Particular_5799 1d ago

With 2.5s of DB time and no temp files I doubt it's query memory. I'd run select state, count(*) from pg_stat_activity group by 1 during an import. If connections climb with the number of books (a new client per book, say), every backend keeps its own caches until it's closed, which would also fit the slow drop after you pause.

1

u/snap-dev 1d ago

on micro you've only got 1gb so a couple of things stack up fast. if the import runs as one long transaction, or there's a trigger or generated tsvector column firing per row, memory just climbs until it finishes. splitting it into batches of a few hundred rows with a commit each time usually flattens it, and so does dropping heavy indexes during the import and rebuilding after. is the import running from an edge function or straight from the client?

1

u/One_Efficiency_3961 1d ago

Raising the compute plan and still climbing should tell you it's not the plan. 30 active users on a 2GB Micro is just tight, and a book import with a work_mem sort on top will eat it. Run pg_stat_activity and pg_stat_statements, and check if any session is sitting idle in transaction. The 7 min after pause is the thing to explain.

1

u/Safe-Practice-3871 1d ago

30 active users means nothing here, a single import doing sorts and index builds will hold more than 30 idle sessions combined. You paused it and it went back to baseline in 7 min, so nothing is leaking.

1

u/Cold-Elderberry4357 1d ago

30 active users and you're already at 1.85 GB committed on a 2 GB box, and the move is to buy a bigger plan before knowing what's eating it? Thats the part that gets me, you raised the plan and memory "still climbs during imports", which it was always going to do, you moved the wall instead of the thing pushing on it. 20-30 MB/min while an import runs and it only comes back 7 min after you pause points at the import session, one big sort, a fat work_mem, an index build, not at 30 people reading books. So you're one import away from the same restart on whatever plan you buy next. Go watch pg_stat_activity during a single import run and see which query is fat.

1

u/Negative-Contest-145 1d ago

you raised the compute plan and it still climbs, so the plan was never the problem. 30 active users on a book app don't put out 30 mb a minute.

1

u/RelativeResearch3580 1d ago

Where does the 20-30 MB/min actually come from, the graph or something you measured? A line that climbs during the import and falls back to baseline 7 min after you pause looks like page cache being reclaimed, not a leak. And "30 active users" tells us nothing here, one import with a generous work_mem on 2 GB will do the same thing with zero other users online.

1

u/hugazow 1d ago

I dunno. That’s what’s observability is for. Check your logs, trace calls, profile them, check the pain points, reiterate

1

u/GoldTalk3375 1d ago

Small thing: the commitment chart tops out at 1.85 GB, not ~2 GB. If the limit really is 2 GB you were still ~150 MB short of it, so it's worth saying whether that was a hard cap or the swap doing the damage.

1

u/AcceptableStart3044 1d ago

1 GB de swap con 30 usuarios, en fin