the memory leak that had been there for months
this had probably been going on for three months or more. mamaskin's memory kept creeping up. i'd already done some work on ingredient scans, but i was still left with the same question. where was it all going?
for context, mamaskin is the skincare ingredient checker i built for pregnancy and breastfeeding. its backend runs on supabase. i wanted to understand whether this was normal growth, something in the app, or something else on the server.
the first thing to untangle was what the chart actually meant.
committed memory includes memory processes have allocated even if they haven't used it yet. physical ram in use is a separate measurement. so the investigation also needed available ram, swap, connections and errors. the linux documentation explains those measurements.
we'd already compared samples with the same number of database connections. even at exactly 24 connections, average committed memory was about 100 mib higher in early november than in late october. that made connection count alone a less convincing explanation. it didn't tell us which process was responsible, because the requests and other work could still differ.
the query changes hadn't settled it either. the follow-up showed roughly the same daily committed memory across three full-day comparisons.
then, on 12 november, there was a much sharper increase. between 19:26 and 20:26 utc, the measurements looked like this.
| memory measurement | 19:26 | 20:26 |
|---|---|---|
| anonymous memory in ram | 460 mib | 1,099 mib |
| occupied swap | 461 mib | 1,024 mib |
| available ram | 815 mib | 67 mib |
file cache fell by about 712 mib during that hour. there was actual swapping too. this was real memory pressure, with less room available for the app and other services.
the first outage alert came at 20:32, after the rise. the database restarted around 20:40. so the restart hadn't caused the earlier pressure.
okay, so what was doing it?
i didn't want to just buy more ram and move on. i'd asked supabase about the earlier increase, but hadn't received a technical answer. i kept digging with gpt-6.1 ultra.
one lead was the monitoring itself. there were 48 errors from a query reading database statistics, all on the same persistent database connection. those errors started before the sharp rise.
that led to a postgres bug report about pg_stat_statements, the extension used for query statistics. it described a buffer being left allocated when reading saved query text failed because of invalid encoding.
the useful bit was the cleanup problem. memory allocated for that buffer could stay around when an error interrupted the normal path.
our database was on postgres 17.6. we checked its published supabase build and the query-text path in use. the build didn't include the fix, and that path was enabled. the postgres 17.10 release notes list the memory-leak fix.
so we had a known leak in the version we were running, and a monitoring query using the affected path. the logs didn't prove this was causing our memory increase, but there was a specific fix available to test.
supabase offered 17.11, which included the fix, on the same small instance. so the next decision was about upgrading the database software, with the memory problem still needing a proper follow-up.
before clicking anything, i wanted to know what could break. we checked the installed extensions, relevant custom operators and encryption calls, and replication slots. no blocker was found in those checks. there was also a recent physical backup.
the confirmation warned that services could be offline for up to an hour and that i couldn't downgrade afterwards. i was okay with planned downtime, but wanted the app and website checks ready for when it came back. we also checked supabase's service status before the maintenance.
i updated to 17.11 on 13 november and kept the same small instance.
the first memory reading had about 1.28 gib available and almost no swap in use. the upgrade had just restarted postgres too, so that reading on its own doesn't tell me what it'll look like after a week.
the apple session i already had open still worked, with pro access. search and both assessments loaded, favourites and routines were still there, and the ingredient checker worked too. no database errors or api 5xx responses showed up in the short log check afterwards.
this had been in the back of my head for months. i'd be working on something else in mamaskin, open the dashboard, and there it was again. i wanted to understand what was going on before spending more money on the server. it took a few detours to get here, but we finally found something we could actually fix.