Guildwise HQ

← All articles

Sizing /dev/shm for a test database container: measure first, then pin it

The Guildwise HQ team · · 5 min read

Abstract branded cover with stacked horizontal bars of varying length over a dark grid, suggesting measured memory capacity.

Start with the mount, not the database settings

The resize error is not a disk problem. When a parallel query starts, PostgreSQL asks for dynamic shared memory. On Linux that memory is backed by /dev/shm, and inside a container that mount has its own ceiling. If the segment needs to grow past that ceiling, the query fails with a "No space left on device" message that names a shared memory segment.

The PostgreSQL Docker Official Image documentation states that the default /dev/shm size for containers is 64 MB, and that exhausting it produces this error. It points readers to a larger --shm-size on docker run, or the equivalent setting in Compose.

The Compose container specification describes shm_size as the size of the shared memory partition that a service container is allowed, given as a byte value. That is the setting to pin in a test stack.

Two memory paths, two different knobs

It helps to separate two things that are often lumped together. The main buffer pool, shared_buffers, is a separate region. One practitioner who tested this reported that shared_buffers did not use /dev/shm in their tests, so the shm size has to be planned for parallel queries on its own terms.

PostgreSQL also has min_dynamic_shared_memory. According to the PostgreSQL 19 documentation, it sets how much memory is allocated at server startup for parallel queries, and its default is 0. When that region is insufficient or is used up by concurrent queries, new parallel queries take extra memory from the operating system using dynamic_shared_memory_type. The parameter can only be set at server start.

The practical consequence is that raising min_dynamic_shared_memory does not remove the need for a container shm size. It changes where the first allocation comes from, and the fallback path still ends in /dev/shm.

Why a test database reaches the ceiling

Test databases usually hold small data, so the failure can seem surprising. The trigger is usually concurrency and plan shape rather than table size. One reported test failed a single hash join at a 16 MB work_mem, and three joins at once at the default 4 MB.

The PostgreSQL 15 documentation warns that parallel queries can use far more resources than serial ones, because each worker is a separate process with roughly the impact of an extra user session. A test suite that runs several queries at once, each with workers, can multiply that cost quickly.

A 2018 message on the PostgreSQL mailing list makes the same point from the container side. The author noted that the container technology was limiting shared memory, and that it is hard to recommend a single --shm-size value. That is the core of the problem: no official formula exists, so the number has to come from measurement.

One rough planning model is that peak dynamic shared memory demand scales with concurrent queries, times parallel workers, times work_mem. Treat that as a starting estimate for planning, not a guarantee. Work_mem applies per operation and per worker, so a complex query can use more than one share at once.

A measure-then-set routine

The routine below avoids both guessing and over-provisioning. It works for a single developer machine and for a CI runner.

  1. Start the test Postgres container with a generous value for the measurement run, such as 1g. Record the value with docker exec and df -h /dev/shm.
  2. Run the heaviest test or query set in the suite. While it runs, sample /dev/shm usage. Files named with a PostgreSQL prefix are the live dynamic shared memory segments.
  3. Find the queries that use parallel plans. Their EXPLAIN output includes a Gather node. Those are the queries that set the peak.
  4. Record the observed peak. Set the container limit to that peak plus a margin you can justify. Do not size it to the container memory limit. The shared memory mount counts against that limit, so the two budgets need to be planned together.
  5. Pin the value in the Compose file for every environment that runs the suite, so a query that passes in local tests also passes in CI.
  6. Recreate the container after the change. A restart of an existing container does not reliably pick up a new shm value.
  7. Verify with docker exec and df -h /dev/shm, then rerun the heavy set and confirm no resize error appears in the logs.

A worked example makes the arithmetic concrete. Suppose a measurement run shows three concurrent queries, each using two workers, with a work_mem of 64 MB. The planning model gives 3 × 2 × 64 MB, or 384 MB. A team might then set a limit above that figure after checking the measured peak. The number is an illustration of the method, not a recommendation for any particular suite.

services:
  postgres:
    image: postgres:18
    shm_size: "1gb"
    environment:
      POSTGRES_PASSWORD: example-only

The example uses a placeholder password, and the image tag and size should come from your own measurement.

Options that trade away something

Some teams reach for a different fix. Setting max_parallel_workers_per_gather to 0 disables parallel query execution, according to the PostgreSQL 15 documentation. That removes the error for the session, but it also removes the plan shape you may be trying to test. Use it only for tests that do not target parallel behaviour, and record that choice in the test notes.

A second option is to give the container a larger shared memory limit without measuring first. That can hide the problem until a heavier test or a production-sized dataset arrives. A measured number is easier to defend in review than a large round number.

Keep the check in the pipeline

A resize error should fail a test run loudly, not appear as a flaky test. Add two checks to the pipeline. First, print the /dev/shm size at container start. Second, scan the database logs after the suite for the resize message. When either check changes, the team learns about it before a merge.

Key takeaways

  • The resize error comes from the container's /dev/shm ceiling, which Docker sets to 64 MB by default unless you raise it.
  • Set shm_size in the Compose file or --shm-size on docker run, and recreate the container after any change.
  • There is no official formula. Measure the peak during the heaviest test run, then set the limit with a margin.
  • Disabling parallel query is a trade-off that changes what the tests exercise, not a substitute for sizing.
  • Verify the value with df inside the container and scan logs in CI so the problem stays visible.

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

Sources

  1. Container specification - Docker Compose documentation
  2. PostgreSQL 19 Documentation: Resource Consumption
  3. PostgreSQL 15 Documentation: Resource Consumption
  4. Re: Query error: could not resize shared memory segment (pgsql-general)
  5. official-images-docs: postgres (docker-library documentation mirror)
  6. All Your GUCs in a Row: dynamic_shared_memory_type (The Build)
  7. Postgres Docker shm_size vs shared_buffers on a Small VPS (Milan Jovanovic)

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