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 1pGIhL-0004Uq-2a for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Jan 2023 11:58:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pGIhJ-000377-7U for pgsql-bugs@arkaria.postgresql.org; Fri, 13 Jan 2023 11:58:13 +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 1pGIhI-00036f-2e for pgsql-bugs@lists.postgresql.org; Fri, 13 Jan 2023 11:58:12 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pGIhD-0004Rn-Vw for pgsql-bugs@lists.postgresql.org; Fri, 13 Jan 2023 11:58:11 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id DD66332004ED; Fri, 13 Jan 2023 06:58:04 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 13 Jan 2023 06:58:05 -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=fm3; t=1673611084; x=1673697484; bh=E MLMFAKLTC1uxq1ehPBPuxghcmLRPLOz5RdBRkmQisg=; b=dRXDs++81C++ZF8nd f2paRLY5fyLeVBxmOJq0Z/akOh/Gvdr3yYz1e5provvaLR7s5afY4SwNThokHvGp 5XqNRF/AcKZKdZ2PWlpQ8R+e1ESSwIU9vblhEKSetC3g1SkKzMOVR4MFbmEMSZYt hXEST0lj3c4WDxLXDAMhtjSPtfC6e0HitDDy84ayzQQO1aUDGYiqoV1nV/td1kpS Wgch0Rb2DSxeL1F7kXCERRBTuIgy6hWnf1Li/3ldy7eJPuUMbOYcF5t8Q0j+fgv+ B/2rrYLOEpyHAW22r09AtjkiqyMTixcVynNnK/6omVgEz89txlrb7u0zTPb0ObUY uTNtQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrleekgdeftdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeekfeeiheekkefftdffgeegtdffleekiefguddvkefgudfg leeutdeihfektedugfenucffohhmrghinhepghhithhhuhgsrdgtohhmpdgvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 13 Jan 2023 06:58:03 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id EEC701D; Fri, 13 Jan 2023 12:53:52 +0100 (CET) Date: Fri, 13 Jan 2023 12:53:52 +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: <20230113115352.sbuv5egpjyoxn52i@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 2023-Jan-13, Mats Kindahl wrote: > I have an improved patch based on raising an error when malloc fails. I > used the version of mallocfail that is available at > https://github.com/ralight/mallocfail which allows you to run the program > with incrementally failing memory allocations but since the count is around > 6886 or so when the postmaster starts to allow backend starts and other > processes are starting in between, this count is not always accurate and > something "smarter" is needed to get a reliable failure test. Hmm, but I'm not sure I like what you did to BackendStartup: @@ -4054,14 +4073,7 @@ BackendStartup(Port *port) * Create backend data structure. Better before the fork() so we can * handle failure cleanly. */ - bn = (Backend *) malloc(sizeof(Backend)); - if (!bn) - { - ereport(LOG, - (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory"))); - return STATUS_ERROR; - } + CHECKED_CALL(bn = malloc(sizeof(Backend))); With this code, now we will have postmaster raise ERROR if this malloc fails, rather than returning STATUS_ERROR as previously. How does postmaster react to this? I think it won't go very well. (Stylistically, I'd rather have the test and corresponding reaction at each call site rather than using a macro to hide it. I especially don't like the fact that the errdetail you propose would report a line of C code to the user, rather than an intelligible explanation of what that code is trying to do.) > It is quite straightforward to test it by attaching a debugger and setting > the return value of malloc, but to be able to have some "smarter" failures > only when, for example, starting a backend it would be necessary to have > something better integrated with the postgres code. Being able to attach a > uprobe using bpftrace and override the return value would be the most > elegant solution, but that is currently not possible, so considering > whether to add something like the mallocfail above to the regular testing. Hmm, I'm not sure that this type of testing is something to do on an ongoing basis. > Note: there are a bunch of other core dumps that occur during startup > before the postmaster is ready to accept connections, but I think that is > less of a problem since it fails during startup before the system is ready > to accept connections. That sounds reasonable. The service watcher should restart postmaster appropriately in that case (be it old SysV init, systemd, your container management layer, or whatever.) -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Ellos andaban todos desnudos como su madre los parió, y también las mujeres, aunque no vi más que una, harto moza, y todos los que yo vi eran todos mancebos, que ninguno vi de edad de más de XXX años" (Cristóbal Colón)