Fix doc: explanation of how postgres works when OOM killer is invoked
Fix doc: explanation of how postgres works when OOM killer is invoked
Hi postgres-hackers While reviewing a doc patch[1] , I noticed that current explanation about how postgres behaves when an OOM killer is invoked is not quite correct. Postgres doc [2] says: > This indicates that the postgres process > has been terminated due to memory pressure. > Although existing database connections will continue to function > normally, no new connections will be accepted. To recover, > PostgreSQL will need to be restarted. But I believe all postgres processes will be terminated in this case. When a backend process is terminated by an OOM killer, the whole postgres will be restarted or stop depending on restart_after_crash setting. In a special case where postmaster is killed, each backend proccesses will also eventually be terminated. The backend process will be stopped when it notices postmaster is terminated. In this case, the postgres will just stop and a user needs to restart it. I tested above two cases and checked the relevant codes. The statement was very old and committed at 02cd2b96acdb in 2003. I assume it was correct at the time but I believe it's not as of now though I haven't checked. Could you check attached patch? The original document mentions to postmaster explicitly. So, in my patch I explain how postmaster and other processes work when OOM killer is invoked. [1] https://www.postgresql.org/message-id/flat/CABH8dKwkp1_TzR7nxfvSpRSLD1b33xw7N_VLMLy3MiaEjAEPNw%40mail.gmail.com [2] https://www.postgresql.org/docs/devel/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT Thank you, Takeshi Ideriha
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Hi Takeshi, > But I believe all postgres processes will be terminated in this case. > [...] The backend process will be stopped when it notices postmaster > is terminated. I looked into this as well, and the patch matches what I see. In the reaper (postmaster.c) any child that exits abnormally goes through HandleChildCrash, so there is no special case for an OOM SIGKILL. I reproduced it: killing a client backend, the checkpointer, the walwriter, the archiver, or the autovacuum launcher each ends with the postmaster logging "all server processes terminated; reinitializing" (with restart_after_crash on), i.e. every connection is gone. Killing the postmaster instead leaves the backends up until they notice its death and exit, and new connections are refused until a manual restart. So the old "existing database connections will continue to function normally" no longer holds for the usual case. Two small things: - With restart_after_crash off, the server does not stay down passively: it logs 'shutting down because "restart_after_crash" is off' and shuts the whole cluster down, so a manual restart is needed. The patch gives the "on" case; it might be worth saying what "off" does too. - A little further down, this same section recommends protecting the postmaster with a low oom_score_adj so the OOM killer picks a child. With that in place the victim is a backend -- your "another server process" case -- while "the postmaster is terminated" is mostly what happens without that protection. Relating the two might help the reader, though it may be more than this fix intends. The wording reads well to me otherwise. Regards, Manu
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
Hi Manu Thank you for the comments. I registered this to CF https://commitfest.postgresql.org/patch/7407/ . > I looked into this as well, and the patch matches what I see. I checked the almost same thing as you did. Thank you for the confirmation. > > Two small things: > > - With restart_after_crash off, the server does not stay down passively: > it logs 'shutting down because "restart_after_crash" is off' and shuts > the whole cluster down, so a manual restart is needed. The patch gives > the "on" case; it might be worth saying what "off" does too. > > - A little further down, this same section recommends protecting the > postmaster with a low oom_score_adj so the OOM killer picks a child. > With that in place the victim is a backend -- your "another server > process" case -- while "the postmaster is terminated" is mostly what > happens without that protection. Relating the two might help the > reader, though it may be more than this fix intends. Both of the points make sense. I added them in v2 patch. Regards, Takeshi Ideriha
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Manu <manuelreyesbravo@gmail.com>
Дата:
Hi Takeshi, > Both of your points make sense. I added them in v2 patch. v2 reads correctly to me -- both the restart_after_crash=off case and the pointer to the postmaster oom_score_adj are now covered, and the wording matches what I reproduced. One thing that might further help the reader, if it is in scope: the section shows the kernel message but not what PostgreSQL itself logs, and since every process is named "postgres" the reader cannot tell from "Killed process 12345 (postgres)" alone which of your two cases applies. The server log is what separates them. When a child is killed, the postmaster logs (with restart_after_crash on): LOG: client backend (PID 769432) was terminated by signal 9: Killed LOG: terminating any other active server processes LOG: all server processes terminated; reinitializing LOG: database system is ready to accept connections When the postmaster itself is killed there is no such reinitialization sequence -- the log simply ends and the backends exit as they notice it is gone. That mapping also answers the "where to look" the kernel-message note leaves open: the kernel log for the OOM line, the server log for the effect. Might be more than this fix intends, so feel free to leave it for a follow-up. Regards, Manu
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Takeshi Ideriha <iderihatakeshi@gmail.com>
Дата:
Hi Manu >> Both of your points make sense. I added them in v2 patch. > > v2 reads correctly to me -- both the restart_after_crash=off case and the > pointer to the postmaster oom_score_adj are now covered, and the wording > matches what I reproduced. Thank you. > One thing that might further help the reader, if it is in scope: the > section shows the kernel message but not what PostgreSQL itself logs, and > since every process is named "postgres" the reader cannot tell from > "Killed process 12345 (postgres)" alone which of your two cases applies. > The server log is what separates them. When a child is killed, the > postmaster logs (with restart_after_crash on): > > LOG: client backend (PID 769432) was terminated by signal 9: Killed > LOG: terminating any other active server processes > LOG: all server processes terminated; reinitializing > LOG: database system is ready to accept connections > > When the postmaster itself is killed there is no such reinitialization > sequence -- the log simply ends and the backends exit as they notice it > is gone. That mapping also answers the "where to look" the kernel-message > note leaves open: the kernel log for the OOM line, the server log for the > effect. Might be more than this fix intends, so feel free to leave it for > a follow-up. Thank you for the suggestion. I agree that mapping the kernel message to the server log would be useful for readers. But I'd like to focus on correcting the wrong sentence for now. Current "Linux Memory Overcommit" section is somewhat long and these logs are easy to understand so I'd like to make this out of scope. Regards, Takeshi Ideriha
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Wed, Oct 14, 2026 at 5:33 PM Takeshi Ideriha wrote: > But I'd like to focus on correcting the wrong sentence for now. +1 So, IMO, it's better to omit the OOM score description because it's not needed to correct the existing text and is already explained later in the same section. + If a child process is terminated, all connections + will be terminated and PostgreSQL will + automatically restart when + restart_after_crash + is on. If it is off, PostgreSQL will stop and + must be restarted manually. This description seems misleading because not all child process terminations have this effect. For example, terminating the syslogger does not terminate existing connections. But, to address this, listing all the exceptions here would be overkill, though. So, how about using "typically" instead, as follows? --------------- If a child process is terminated, PostgreSQL will typically terminate all remaining connections and either recover automatically, if restart_after_crash is enabled, or shut down. If PostgreSQL shuts down, it will need to be restarted to recover. --------------- Regards, -- Fujii Masao
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
Hi >On 2026/10/14 20:36, Fujii Masao wrote: > On Wed, Oct 14, 2026 at 5:33 PM Takeshi Ideriha > wrote: >> But I'd like to focus on correcting the wrong sentence for now. > > +1 > > So, IMO, it's better to omit the OOM score description because it's not > needed to correct the existing text and is already explained later in > the same section. On second thought, I agree with you. I'd like to keep the postgres doc concise as possible. Fixed in v3 patch. > + If a child process is terminated, all connections > + will be terminated and PostgreSQL will > + automatically restart when > + restart_after_crash > + is on. If it is off, PostgreSQL will stop and > + must be restarted manually. > > This description seems misleading because not all child process > terminations have this effect. For example, terminating the syslogger > does not terminate existing connections. But, to address this, > listing all the exceptions here would be overkill, though. So, how about > using "typically" instead, as follows? > > --------------- > If a child process is terminated, PostgreSQL will typically terminate > all remaining connections and either recover automatically, if > restart_after_crash is enabled, or shut down. If PostgreSQL shuts > down, it will need to be restarted to recover. > --------------- Oh, syslogger. It's true. Thank you for pointing this out and great suggestion. Your suggestion looks much more correct and simple. I use your wordings in v3 patch as is. Regards, Takeshi Ideriha
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Thu, Oct 15, 2026 at 5:27 PM Takeshi Ideriha wrote: > On second thought, I agree with you. I'd like to keep the postgres doc > concise as possible. Fixed in v3 patch. I made a few minor adjustments and pushed the patch. Thanks! Regards, -- Fujii Masao
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
Takeshi Ideriha <iderihatakeshi@gmail.com>
Дата:
On 2026/10/16 22:50, Fujii Masao wrote: > On Thu, Oct 15, 2026 at 5:27 PM Takeshi Ideriha > wrote: >> On second thought, I agree with you. I'd like to keep the postgres doc >> concise as possible. Fixed in v3 patch. > > I made a few minor adjustments and pushed the patch. Thanks! >Clarify that the OOM killer can terminate either the postmaster or a >child process. Thank you for additional clarification and push! Regards, Takeshi Ideriha
Re: Fix doc: explanation of how postgres works when OOM killer is invoked
От:
C W <clarklwilliamson@gmail.com>
Дата:
Hi Takeshi, Fujii,
I tested v3 (CF 7407) on master at 94e1385ed28, debug build on Fedora 44, with the patch applied from the cfbot branch. I'm a sysadmin and have dealt with the OOM killer and postgres in production, so I wanted to check each claim in the new text against a real cluster.
1. Child backend killed, restart_after_crash = on
Two sessions running pg_sleep(30), kill -9 on only one of them. The other session was dropped too:
WARNING: terminating connection because of crash of another server process
Server log:
LOG: client backend (PID 4038002) was terminated by signal 9: Killed
LOG: terminating any other active server processes
LOG: all server processes terminated; reinitializing
...
LOG: database system is ready to accept connections
New connections worked right after. This matches v3, and it also confirms the current text ("existing database connections will continue to function normally") is wrong.
2. Same thing with restart_after_crash = off
LOG: terminating any other active server processes
LOG: shutting down because "restart_after_crash" is off
LOG: database system is shut down
Server stays down. Matches v3.
3. Syslogger killed (logging_collector = on)
A running query finished normally, the postmaster started a new syslogger, and nothing else was terminated. So "typically" is accurate and needed.
4. Postmaster killed
This is the one case where I think the text is missing something. I had two sessions open, one in pg_sleep(60) and one running a CPU bound query (count(*) over generate_series(1,400000000)), then kill -9 on the postmaster.
The pg_sleep session was dropped within about a second. The busy one kept running as an orphan for about 1m42s, finished, returned its result to the client, and only then exited with:
FATAL: terminating connection due to unexpected postmaster exit
While that orphan was still running, pg_ctl start failed:
FATAL: pre-existing shared memory block (key 555093, ID 131122) is still in use
HINT: Terminate any old server processes associated with data directory "..."
It started fine once the orphan exited.
"All connections will eventually be terminated" is true, but "To recover, PostgreSQL will need to be restarted" doesn't mention that the restart will fail until the leftover backends are gone. An admin following the docs during an incident will hit that FATAL with no explanation. I couldn't find this mentioned anywhere else in the docs. Something like this after the restart sentence might help:
Note that the server cannot be restarted until all remaining server
processes have exited; they may need to be terminated manually.
I know Fujii wanted to keep this change narrow, so I'd consider that optional.
5. Docs build
Built the HTML docs with the patch applied, no errors or warnings. The paragraph renders correctly and the restart_after_crash link resolves.
Minor nit, not worth blocking on: "To recover, PostgreSQL will need to be restarted." and "If PostgreSQL shuts down, it will need to be restarted to recover." say nearly the same thing in the same paragraph.
Overall v3 is correct and a clear improvement over the current text. I'm fine with it going in as is, with or without the extra sentence.
Disclosure: I used an AI assistant (Claude) to help set up the test environment and draft this email. I ran and checked all the tests myself and I'm happy to answer questions about any of it.
Thanks for working on this!
-- Clark
I tested v3 (CF 7407) on master at 94e1385ed28, debug build on Fedora 44, with the patch applied from the cfbot branch. I'm a sysadmin and have dealt with the OOM killer and postgres in production, so I wanted to check each claim in the new text against a real cluster.
1. Child backend killed, restart_after_crash = on
Two sessions running pg_sleep(30), kill -9 on only one of them. The other session was dropped too:
WARNING: terminating connection because of crash of another server process
Server log:
LOG: client backend (PID 4038002) was terminated by signal 9: Killed
LOG: terminating any other active server processes
LOG: all server processes terminated; reinitializing
...
LOG: database system is ready to accept connections
New connections worked right after. This matches v3, and it also confirms the current text ("existing database connections will continue to function normally") is wrong.
2. Same thing with restart_after_crash = off
LOG: terminating any other active server processes
LOG: shutting down because "restart_after_crash" is off
LOG: database system is shut down
Server stays down. Matches v3.
3. Syslogger killed (logging_collector = on)
A running query finished normally, the postmaster started a new syslogger, and nothing else was terminated. So "typically" is accurate and needed.
4. Postmaster killed
This is the one case where I think the text is missing something. I had two sessions open, one in pg_sleep(60) and one running a CPU bound query (count(*) over generate_series(1,400000000)), then kill -9 on the postmaster.
The pg_sleep session was dropped within about a second. The busy one kept running as an orphan for about 1m42s, finished, returned its result to the client, and only then exited with:
FATAL: terminating connection due to unexpected postmaster exit
While that orphan was still running, pg_ctl start failed:
FATAL: pre-existing shared memory block (key 555093, ID 131122) is still in use
HINT: Terminate any old server processes associated with data directory "..."
It started fine once the orphan exited.
"All connections will eventually be terminated" is true, but "To recover, PostgreSQL will need to be restarted" doesn't mention that the restart will fail until the leftover backends are gone. An admin following the docs during an incident will hit that FATAL with no explanation. I couldn't find this mentioned anywhere else in the docs. Something like this after the restart sentence might help:
Note that the server cannot be restarted until all remaining server
processes have exited; they may need to be terminated manually.
I know Fujii wanted to keep this change narrow, so I'd consider that optional.
5. Docs build
Built the HTML docs with the patch applied, no errors or warnings. The paragraph renders correctly and the restart_after_crash link resolves.
Minor nit, not worth blocking on: "To recover, PostgreSQL will need to be restarted." and "If PostgreSQL shuts down, it will need to be restarted to recover." say nearly the same thing in the same paragraph.
Overall v3 is correct and a clear improvement over the current text. I'm fine with it going in as is, with or without the extra sentence.
Disclosure: I used an AI assistant (Claude) to help set up the test environment and draft this email. I ran and checked all the tests myself and I'm happy to answer questions about any of it.
Thanks for working on this!
-- Clark