Exploring Postgres's arena allocator by writing an HTTP server from scratch

submited by
Style Pass
2024-11-06 21:00:11

Postgres manages memory through nested arenas called MemoryContexts. MemoryContexts are convenient because, for the most part, you don't need to worry about explicitly freeing memory because memory will be freed when the arena is freed. So the next two things you worry about, things that took me a while to understand, are 1) in which MemoryContext should some particular object be allocated (i.e. how long should some particular object live) and 2) when should I reach for creating a new MemoryContext?

To help get a feel for how we can use MemoryContexts we're going to do something a little strange. We're going to build a little HTTP server and web framework from scratch inside of a Postgres extension. This is not (and many other choices made in this post are also not) a good idea for a production extension.  But it may prove a sufficiently interesting scenario in which to explore the use of MemoryContexts.

I'm going to assume you are on a Debian machine so I can give some precise instructions but aside from the package names I'll ask you to install, this should all work on any Linux distro or on a Mac.

Leave a Comment