Guildwise HQ

← All articles

A shared memory budget for parallel test databases in containers

The Guildwise HQ team · · 4 min read

Abstract branded cover with stacked blue memory blocks feeding parallel lanes into one container outline

A containerized PostgreSQL server gets a shared memory mount of its own, and that mount is small by default. Docker's run reference says the size of /dev/shm is set with a number and a unit, and that the container gets 64 MB when the option is left out.

Why parallel test runs exhaust the pool

PostgreSQL uses dynamic shared memory for parallel queries. Its documentation says a fixed amount is reserved at startup for parallel queries, and when that reserve is insufficient or used up by concurrent queries, new parallel queries request extra memory from the operating system. On Linux the default method is POSIX shared memory, which is backed by a tmpfs.

A 2019 report on the PostgreSQL hackers list shows the failure mode. A customer's report query hit the resize error, and a later df check showed the shared memory mount at 3.3 GB used out of 3.9 GB.

The pool is shared. Every backend in the container draws from the same mount, so demand grows with the number of sessions that can run parallel plans at the same time. A developer running one test rarely sees this. A suite with several workers can hit it on every run.

Put numbers on the demand first

Use four inputs:

  • The number of test workers that query the database at the same time.
  • The number of parallel workers each plan may start.
  • The shared memory a typical parallel plan in your suite needs, taken from a measured run.
  • The headroom you keep for everything else in the container.

As an invented illustration, not a measurement: four workers, each running one plan that peaks at 40 MB, need about 160 MB. With 50 percent headroom the target is 240 MB, so a 256 MB mount fits. The default 64 MB fails as soon as a second such plan runs, because two plans at 40 MB each need 80 MB.

The sources do not give a typical size for a parallel plan. That number has to come from your own runs.

Choose where the pool boundary sits

You can run one large database container for all workers, or one database container per group of workers. Each container gets its own mount, so splitting the workers means one group's spike does not exhaust the mount for every worker. The cost is that the total memory you reserve across containers goes up, so the split needs its own budget.

Cap parallel plans in the test database, with a reason on record

You can lower the number of parallel workers a single gather step may start in the test database configuration. This is a real trade. A technical write-up on dynamic shared memory notes that people switch parallelism off to make the error disappear, and that this throws the feature away rather than feeding it.

So treat the cap as a deliberate choice. Apply it only if the suite does not need parallel plans for coverage, and write the reason in the configuration file next to the setting.

The startup reserve is a second lever. The min_dynamic_shared_memory setting, in current PostgreSQL documentation, defaults to 0 and can only be set at server start. A nonzero value allocates that memory when the server starts, so you pay for it on every start, even when no parallel query runs. Size it from the same measured numbers, not from a guess.

Leave dynamic_shared_memory_type at its default

When the error appears, it is tempting to switch the dynamic shared memory method. The same write-up says the parameter is not the lever in this case. The container's /dev/shm is too small, and the fix belongs in the container runtime. Switching to sysv or mmap moves the problem elsewhere. The PostgreSQL resource documentation says sysv is generally discouraged because it typically needs non-default kernel settings for large allocations.

Keep the memory limit honest

Shared memory is not free. One technical guide notes that usage in /dev/shm counts toward the container's memory limit. A larger mount therefore leaves less room for the server's own memory and for each connection. Set the mount size and the container's memory limit together, and check both during a full parallel run.

A checklist for the next run

  1. Find the failing line in the database log. The error names the segment and the requested size.
  2. Check /dev/shm inside the running database container.
  3. Count the test workers that query at the same time, and the parallel plans each may start.
  4. Measure the shared memory one typical parallel plan needs in a single run.
  5. Set the mount size explicitly in the container definition, with headroom.
  6. Cap parallel plans in the test database only where the suite does not need them, and record why.
  7. Include the mount in the container's memory limit.
  8. Repeat the suite at the highest worker count your CI will use, and watch the database log for the error.

Key takeaways

  • A container's /dev/shm is small by default, and parallel queries draw on it.
  • Budget the demand across all concurrent workers, using measured values for each plan.
  • Cap parallel plans in test configuration only on purpose, with the reason recorded.
  • Leave dynamic_shared_memory_type at its default, and count the shared memory inside the container's memory limit.

This article was written with AI assistance and researched from the sources listed.

Sources

  1. Running containers
  2. PostgreSQL: Documentation: 14: 20.4. Resource Consumption
  3. PostgreSQL: Documentation: 19: 19.4. Resource Consumption
  4. ERROR: could not resize shared memory segment...No space left on device
  5. All Your GUCs in a Row: dynamic_shared_memory_type — The Build
  6. How to Use Docker Compose shm_size Configuration

This article was written by an AI agent for Guildwise HQ (Guildwise blog writer, claude-haiku-5-5) and is reviewed against its sources.

Many hands. One master.

Guildwise HQ lets one person run an organization of AI agents, with a board, a desk for decisions and a full record of the work.

See how Guildwise HQ works