Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p5ru5-0002ns-H0 for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Dec 2022 17:20:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p5ru4-00061k-DN for pgsql-bugs@arkaria.postgresql.org; Thu, 15 Dec 2022 17:20:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p5ru3-0005z1-BX for pgsql-bugs@lists.postgresql.org; Thu, 15 Dec 2022 17:20:16 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p5ru0-0002qK-D0 for pgsql-bugs@lists.postgresql.org; Thu, 15 Dec 2022 17:20:14 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id CBFFD5C010A; Thu, 15 Dec 2022 12:20:10 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 15 Dec 2022 12:20:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1671124810; x=1671211210; bh=X hfRXFKRSHrgklBwsu+IZbQ+h//dLuOTmUBYPRB0jqc=; b=cMKs5hEkHCDjxEFdV Uoj93wXJgu+u9EoldlKdLcVAFcRM9nHtxtQVRLRmdG8WzG7u2FvkZBLsac+Guz6b kvDhBiPM61N3/TtiIr4fljrMLpZf3LhISBRp0gbkN0tvTaO9Pzs/2hJzh1Od8H4C S/K7958C7RtZ3jLSY9iQ9skprU8yCCS/W1TcR6J0VPTuuQy/U3Npx0w7ABXXEd92 OG3jRCPvpThhNuRAGUmNR+njpKd7BcAMQENgcratSl/kST/d/HzQQzSLP1nGRj+z xOlFHldR6rfhF+P1ilJD1/Vk2AksYpTbtLR9SJexgxAzXoFsZD053AFlKIKRKnt/ nHZCQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeehgdeljecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeeuiefgffeukedtgfetueeugfetvdeuhefgheelfeehleej leefffejtddtkedvjeenucffohhmrghinhepphhoshhtghhrrdgvshdpvghnthgvrhhprh hishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 15 Dec 2022 12:20:10 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 589E5E98; Thu, 15 Dec 2022 18:20:08 +0100 (CET) Date: Thu, 15 Dec 2022 18:20:08 +0100 From: Alvaro Herrera To: Mats Kindahl Cc: daniel@yesql.se, pgsql-bugs@lists.postgresql.org Subject: Re: Crash during backend start when low on memory Message-ID: <20221215172008.islvqfyysom3ufxy@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2022-Dec-14, Mats Kindahl wrote: > PostmasterContext is set quite early in the procedure so it should not be a > problem to use it. I think we can use PostmasterContext rather than > TopMemoryContext. The memory for the allocations are passed down to > PostmasterMain() and used inside InitPostgres() but as far as I can tell, > they are not used after the InitPostgres() call. The PostmasterContex is > destroyed right after the call to InitPostgres() inside PostmasterMain() > and then the rest is allocated in other contexts. Well, this is what PostgresMain has to say about it: /* * If the PostmasterContext is still around, recycle the space; we don't * need it anymore after InitPostgres completes. Note this does not trash * *MyProcPort, because ConnCreate() allocated that space with malloc() * ... else we'd need to copy the Port data first. Also, subsidiary data * such as the username isn't lost either; see ProcessStartupPacket(). */ if (PostmasterContext) { MemoryContextDelete(PostmasterContext); So I think blowing away part of struct Port independently of the containing struct is a bad idea. Why isn't more appropriate to do what ConnCreate does? if (!(port = (Port *) calloc(1, sizeof(Port)))) { ereport(LOG, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); ExitPostmaster(1); } ... ugh. I have to admit that killing postmaster due to a transient out-of-memory makes me a bit nervous -- do note that this ExitPostmaster() happens before we've forked the backend, so this code does bring the server down (!!). If we want to make postmaster more robust in the face of OOM, we should also change this code somehow, yes? I agree with Mats' premise when starting the thread -- even if the server is not in great shape, it doesn't seem like taking the whole service down is a great response. It would be better to just refuse the connection with some error and let existing connections chug along, poorly though they can. Maybe they'll also die soon, freeing memory; but that'll be no good if Postmaster is gone altogether. Now, it would be better if we could test this more thoroughly. Perhaps it's possible to use something like the mallocfail.c lib that Heikki wrote a few years ago, teaching it to handle calloc https://postgr.es/m/547480DE.4040408@vmware.com -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "Oh, great altar of passive entertainment, bestow upon me thy discordant images at such speed as to render linear thought impossible" (Calvin a la TV)