SaaS architecture
could not resize shared memory segment: Postgres in Docker
PostgreSQL could not resize shared memory segment: No space left on device is /dev/shm, not disk. Measured in Docker on 16 to 18; why 256 MB fell short.
ERROR: could not resize shared memory segment "/PostgreSQL.3588151154" to 16777216 bytes: No space left on device has SQLSTATE 53100, disk_full, but your disk is fine. The full device is /dev/shm, a RAM-backed tmpfs in which parallel query workers build a shared hash table. In a Docker container /dev/shm is 64 MB by default, and that is where we got the error: with PostgreSQL’s defaults (work_mem 4MB, 2 workers) our test join passed on its own, from work_mem 16MB it failed every time, and even at the defaults at least one of four concurrent runs failed in every round.
We measured on the official postgres images 16.15, 17.11 and 18.6 under Docker 29.1.3 and Compose 2.40.3, on 6 November 2026, each container with a 3 GB memory limit. The test was a join of two tables of 3 million rows (290 MB and 196 MB), with enable_mergejoin = off, parallel_setup_cost = 0 and parallel_tuple_cost = 0, so that the planner chose a Parallel Hash Join. Without a version in the text, the result is from 17.11. In short:
- The 256MB that the Docker image’s documentation suggests held for one heavy query, but with four sessions at once at least 3 of 4 failed in every one of eleven rounds, and in eight of them all four did.
--shm-size=1gheld all four in each of eleven rounds;/dev/shmpeaked at 623 MB every time. - A query that is too big fails every time. A query that just fits fails only when other sessions fill
/dev/shmat the same moment, which makes the error look random. - Afterwards
/dev/shmis empty again: 99 % full during the error, 1.1 MB a moment later. Adfafter the fact shows nothing. - Raising
max_locks_per_transactiondoes not help, andshared_buffersis not the cause: a 1.5 GB main segment (shared_buffers1GB) started in 64 MB of/dev/shm.
Why does PostgreSQL fail in Docker with the default /dev/shm?
Because Docker gives a container 64 MB of /dev/shm, and a parallel hash join puts its hash table there. The documentation describes the node: “In a parallel hash join, the inner side is a parallel hash that divides the work of building a shared hash table over the cooperating processes.” That shared table lives in dynamic shared memory segments, which with the default dynamic_shared_memory_type = posix are files in /dev/shm. We ran the join with every combination of work_mem and max_parallel_workers_per_gather:
Workers \ work_mem | 4MB | 16MB | 32MB | 64MB | 128MB | 256MB | 1GB |
|---|---|---|---|---|---|---|---|
| 0 | ok | ok | ok | ok | ok | ok | ok |
| 1 | ok | ok | fails | fails | fails | fails | fails |
| 2 (default) | ok | fails | fails | fails | fails | fails | fails |
| 4 (3 launched) | ok | fails | fails | fails | fails | fails | fails |
Measured in 64 MB /dev/shm, and identical on 16.15, 17.11 and 18.6. At 4 the planner launched 3 workers, because of the table size.
So with the defaults one query fit and with work_mem 16MB it did not. The defaults are not safe either: with four sessions running the join at once, at least 1 of 4 failed in each of eight rounds (2 of 4 once), and with eight sessions 3 of 8 in each of three rounds. The boundary fits a simple budget: work_mem × hash_mem_multiplier × (workers + the leader). The documentation gives the first two: “The final limit is determined by multiplying work_mem by hash_mem_multiplier. The default value is 2.0”. Adding one share per process is our reading. In 64 MB it put the boundary in the right place for every cell of the grid, and the peak we measured for one query alone, in a container with 1 GB of /dev/shm, sat close to that budget:
As long as the hash does not fit, the budget is close to what a query really puts in /dev/shm, at 8MB and 16MB even slightly above it. Once the whole hash fits, use stops: one query with work_mem 1GB and 3 workers peaked at 160,296 KB (157 MB), not gigabytes, and EXPLAIN ANALYZE with 2 workers showed Batches: 1 Memory Usage: 150208kB, the whole table in one go. Do not read Memory Usage as the /dev/shm figure, though: with work_mem 16MB the same hash showed Batches: 8 Memory Usage: 22912kB, yet /dev/shm peaked at 102,844 KB.
In our runs only the parallel hash used more of /dev/shm than the 1.1 MB the server holds from start. A parallel B-tree build with 4 workers and maintenance_work_mem 1GB completed in 64 MB, and /dev/shm peaked at 1.3 MB.
Is “No space left on device” about my disk?
No. The device is /dev/shm. We sampled df inside the container while the query ran:
shm 65536 64576 960 99% /dev/shm
That is 960 KB free, and the segment it wanted was 16 MiB. The moment the query had failed, df -h /dev/shm showed 1.1M used, the same as right after the server started: after an ordinary ERROR, PostgreSQL removed its segments and nothing was left behind. Whoever checks the disk after an incident finds an almost empty tmpfs.
53100 is in class 53, Insufficient Resources, in the error codes appendix; what other codes look like in practice is in our measured SQLSTATE overview. With \set VERBOSITY verbose, all three versions gave the same location, down to the line number:
ERROR: 53100: could not resize shared memory segment "/PostgreSQL.3588151154" to 16777216 bytes: No space left on device
LOCATION: dsm_impl_posix, dsm_impl.c:307
The rest of the message, as we saw it:
- It is an ordinary
ERROR. The session stays open: the next query in it worked, and:LAST_ERROR_SQLSTATEwas53100. The query failed within about 100 to 135 ms, while it was building the hash table. - The name changes every time.
/PostgreSQL.NNNis a random segment name, so do not search for the number. - The byte count is one segment, not the total. It was usually
16777216(16 MiB); we also saw4194304,8388608,16814080and33554432. It does not tell you how much the query needed. - The leader and the workers both log it. The postmaster then logs
background worker "parallel worker" (PID 93) exited with exit code 1. When a worker hit it first, the leader repeated it withCONTEXT: parallel worker.
Why does the error come and go?
Because /dev/shm is shared by every session on the server. We saw two patterns in 64 MB:
- Too big on its own: it fails every time.
work_mem1GB with 2 workers failed 5 times out of 5. Retrying does not help. - Fits on its own: it fails under concurrency.
work_mem8MB with 2 workers passed alone. Four sessions running it at once: in fifteen rounds, 3 of 4 failed fourteen times and all four once, and the one that got through was not always the same session. Even the defaults are not immune: four concurrent runs atwork_mem4MB lost at least one session in each of eight rounds, and eight concurrent runs lost three.
The second pattern is the one that “goes away by itself”. No restart and no cleanup were needed; after the rounds /dev/shm was back at 1.1 MB.
Is —shm-size=256m enough?
For one query, yes; for several at once, it was not. The image’s documentation says: “If the shared memory is exhausted you will encounter ERROR: could not resize shared memory segment . . . : No space left on device. You will want to pass --shm-size=256MB for example to docker run”. With --shm-size=256m and with --shm-size=1g, every combination with workers in the grid above passed. Then we ran four sessions at once, each with work_mem 1GB and 2 workers:
So size /dev/shm for the parallel hashes that run at the same time, not for one query. A large --shm-size reserves nothing at start: with --shm-size=1g, df showed 1.1 MB used. It is a ceiling. Files in tmpfs are memory, though, and Kubernetes documents that they count against the container’s memory limit; we did not test whether Docker’s --memory limit counts them the same way.
The Docker CLI reference lists --shm-size with a default of 0; what you get is 64 MB (HostConfig.ShmSize=67108864).
How do I set shm_size in Docker Compose?
At the service level, next to image, not under deploy.resources:
services:
db:
image: postgres:17
shm_size: 256mb
Without it, Compose gave the same 64 MB as docker run, and the join failed at work_mem 16MB and 1GB with 2 workers. With shm_size: 256mb the container had 256M in /dev/shm, and all three values we tried (4MB, 16MB, 1GB) passed for a single query. The concurrency result above still applies: pick a size for the load, not for one query. The Compose reference describes it as the size “of the shared memory (/dev/shm partition on Linux) allowed by the service container.”
How do I fix it in Kubernetes?
We did not measure Kubernetes. It has no shm_size field; the usual way is a memory-backed emptyDir mounted over /dev/shm. The Kubernetes documentation on volumes says such a volume is tmpfs, and “files you write count against the memory limit of the container that wrote them”, and “If no size is specified, memory-backed volumes are sized to node allocatable memory.” From that, this pattern:
spec:
containers:
- name: postgres
image: postgres:17
volumeMounts:
- { name: dshm, mountPath: /dev/shm }
resources:
limits: { memory: 4Gi }
volumes:
- name: dshm
emptyDir: { medium: Memory, sizeLimit: 1Gi }
Set a sizeLimit. And since what sits in /dev/shm counts against the container’s limit, a limit that is too tight could turn the 53100 into an OOM kill; we did not test that either.
Which fix costs what?
Each row is measured on 17.11 unless marked, in 64 MB of /dev/shm unless the fix is a larger size; “ok” means the error did not occur in that test, and concurrency was tested only where the column says so:
| Fix | Measured effect | Price |
|---|---|---|
--shm-size=1g (or Compose shm_size) | whole grid ok; four sessions at work_mem 1GB all ok in 11 of 11 rounds, peak 623 MB | RAM when used; nothing reserved at start |
--shm-size=256m | whole grid ok for one query; four at once: 3 or 4 of 4 failed, in each of 11 rounds | not enough under concurrency in our test |
lower work_mem (4MB) | one query ok at 0, 1, 2 and 4 workers; four at once: 1 or 2 of 4 failed in each of 8 rounds | hashes spill to temporary files sooner: 8 batches at 16MB, 1 at 1GB |
hash_mem_multiplier = 1 | 16MB with 2 workers: ok (fails at the default 2) | every hash node gets half the memory |
max_parallel_workers_per_gather = 0 | ok at every work_mem up to 1GB | no parallel query in that session or server |
dynamic_shared_memory_type = mmap | whole grid ok; /dev/shm empty, up to 311 MB of files in pg_dynshmem | writes on the data volume; about 8 % slower here; discouraged by the docs; restart |
dynamic_shared_memory_type = sysv | whole grid ok; nothing in /dev/shm; four at once at 1GB all ok in 3 of 3 rounds | not capped by --shm-size; restart |
min_dynamic_shared_memory = 256MB (18.6) | the four single queries we ran ok (16MB and 1GB, 2 and 3 workers); four at once: 3 of 4 failed on 18.6, 3 or 4 of 4 in two runs on 17.11 | 256 MB held from start; restart |
max_locks_per_transaction = 10000 | no change | not a fix: wrong limit |
work_mem and hash_mem_multiplier. Halving the multiplier worked as well as halving work_mem at the boundary. The documentation warns about the other side of work_mem: “several running sessions could be doing such operations concurrently. Therefore, the total memory used could be many times the value of work_mem”. That is the same arithmetic that fills /dev/shm. Both settings can be set per session, so a reporting job can get more than the web requests. We measured the batch count only for work_mem.
No workers. With max_parallel_workers_per_gather = 0 the process builds a private hash table in its own memory, and no dynamic shared memory segment is involved, which fits the 0 row of the grid passing at every work_mem. It costs parallel query; we did not time the join without workers.
mmap and sysv. Both are set at server start; we passed them with -c on the container’s command line. With mmap the segments became files in $PGDATA/pg_dynshmem, up to 311 MB, on the data volume. In three warm runs of the join it took 919, 903 and 894 ms, against 825, 840 and 856 ms for posix with 1 GB /dev/shm; under write load we did not measure. The documentation says mmap “is generally discouraged because the operating system may write modified pages back to disk repeatedly, increasing system I/O load”. With sysv nothing appeared in /dev/shm or in pg_dynshmem (731 to 832 ms). System V segments are limited by kernel.shmmax and kernel.shmall, which inside the container read 18446744073692774399: we read the value and did not test the limit. Docker’s --shm-size then no longer applies: four sessions at work_mem 1GB all passed in a container with 64 MB of /dev/shm, in three rounds out of three. What still limits them is kernel.shmmax/shmall and, presumably, the container’s memory limit; we tested neither.
min_dynamic_shared_memory. This reserves a pool at server start, in the main shared memory segment and not in /dev/shm (df showed 32K used). With 256MB on 18.6, the four single queries we ran passed in 64 MB, including work_mem 1GB with 3 workers. With four sessions at once the same error came back: 3 of 4 failed on 18.6, and 4 and 3 of 4 in two runs on 17.11. The documentation explains why: “When this memory region is insufficient or exhausted by concurrent queries, new parallel queries try to allocate extra shared memory temporarily from the operating system using the method configured with dynamic_shared_memory_type”. After a reset and a restart the error came back.
Is it the same as “out of shared memory”?
No. could not resize shared memory segment … No space left on device (53100) is a full /dev/shm, where parallel queries create temporary segments. out of shared memory with the hint max_locks_per_transaction (53200) is a full lock table, fixed at server start, explained in out of shared memory: the lock table. The fix for one does nothing for the other.
We tested it on 16.15 in 64 MB of /dev/shm, with shared_buffers 1GB and max_locks_per_transaction 10000:
max_locks_per_transaction|10000|
shared_buffers|131072|8kB
shared_memory_size|1544|MB
shared_memory_type|mmap|
shm 64M 1.1M 63M 2% /dev/shm
ERROR: could not resize shared memory segment "/PostgreSQL.3928368458" to 16777216 bytes: No space left on device
A main segment of 1,544 MB started without trouble in a container with 64 MB of /dev/shm, because it is anonymous shared memory (shared_memory_type = mmap) and not a file in /dev/shm. So on Linux, where the main segment is anonymous memory, shared_buffers does not end up in /dev/shm: not the default 128MB, and not 1GB. The resize error stayed exactly the same with 10,000 locks per transaction.
What does “pq: could not resize shared memory segment” mean?
The same server error, passed on by a client. The pq: prefix fits Go’s lib/pq passing the server’s message on; we only ran psql and did not test lib/pq or any other driver. The fix is on the server, as above: the driver cannot change what fits in /dev/shm.
Reproduce it
run.sh starts five containers from one postgres image, prefixed pgshm-: 64 MB, 256 MB and 1 GB of /dev/shm, and 64 MB with mmap and with sysv. It loads the two tables, runs the grid, samples /dev/shm during the error, runs the concurrent sessions in three rounds each (also at the defaults), the hash_mem_multiplier and index-build checks, the main-segment test and min_dynamic_shared_memory. It needs Docker, about 3 GB of disk and 10 to 15 minutes, limits each container to 3 GB of memory, and removes its containers by name at the end; an existing container with the same name is replaced. Run ./run.sh 2>&1 | tee results.txt; IMAGE=postgres:16 ./run.sh uses another version. Byte counts, peaks, timings and how many of the concurrent sessions fail will be your own: the sessions start as separate docker exec calls, not at exactly the same moment. The min_dynamic_shared_memory check runs on the chosen image, 17 by default, where 3 or 4 of 4 sessions failed in our runs; our table above shows 18.6. Not included: Docker Compose, the EXPLAIN ANALYZE batch counts and the single-query peaks in the first figure.
What we did not measure
Kubernetes: the pattern above comes from its documentation. Clients other than psql, such as lib/pq, pgx, JDBC and psycopg. Other parallel nodes that use shared memory, such as Parallel Bitmap Heap Scan and Parallel Append, and parallel GIN and BRIN index builds; we only built a B-tree. Podman, Docker Desktop on macOS and Windows, and the daemon option default-shm-size. Whether tmpfs use in /dev/shm counts toward the container’s --memory limit: we never set a limit tight enough to find out. huge_pages with min_dynamic_shared_memory. mmap under write load or on a slow volume. PostgreSQL 15 and older.
Sources: PostgreSQL 17 documentation on resource consumption (dynamic_shared_memory_type, min_dynamic_shared_memory, hash_mem_multiplier, work_mem), parallel plans and error codes; the postgres image documentation; the Docker CLI reference for docker container run and the Compose file reference; and the Kubernetes documentation on volumes; all read on 6 November 2026. All measurements are our own, on PostgreSQL 16.15, 17.11 and 18.6 in the official images, with Docker 29.1.3 and Docker Compose 2.40.3 on Linux 6.8, on 6 November 2026.