Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slvIR-001zCn-8A for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 19:04:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1slvIO-00DsSD-8l for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 19:04:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1slvIN-00DsR3-Ut for pgsql-hackers@lists.postgresql.org; Wed, 04 Sep 2024 19:04:00 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1slvIM-000AG7-D5 for pgsql-hackers@postgresql.org; Wed, 04 Sep 2024 19:03:59 +0000 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-7091558067eso2749117a34.3 for ; Wed, 04 Sep 2024 12:03:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1725476636; x=1726081436; darn=postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=tHL3ap70CyLauyX928eKnu+YylB4WNepkRBQYDmr2wM=; b=C6Uo/+efHeqw1qsln4YopFj7y+wsR46rI5I75aKRxCrdEDJVac18wRFrDh11PGzhHO PJfamOhGko/900dTubc7PGDsFtPDeeQiD1MdAoA4WtFzlliwlfa1MTGgH4mBrDJGFela rUSRI1lX39EDx/srWzNLKvpsxn7o5aYSAT160= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725476636; x=1726081436; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tHL3ap70CyLauyX928eKnu+YylB4WNepkRBQYDmr2wM=; b=nbCvS35K9pm7I8kKiUTvHw/R9+gy/9fFVhUv3FZiIY+BohUmJdJBt/C9VYRA6WPPtq 3DBuaLJU4kbABeqeWfmUcbRZFcweKjQ2zF1F/7TmiJitpjLs17R4K4q2zIlEdI6Y3Own ZsgM+CwJzFRfjT8ccQtwJUtS5xW60MvW2UydeRHJsf5vh5mqfA+I5anTMbUjz6nTVHKv hz2vlXNctFiHjZ2KhTvXcuTGvaJGK66sJPIX7XPuTuPlCE5Y0ZWS/To7JoJs8jtajLHh wasjg3b88NkUoxD+i6V+aXh7TML4DO0B1vyjy8XJZQ3F92CHyNgTB/K1/cO9V0L6QRvX XOoA== X-Gm-Message-State: AOJu0YzdNpdm2EL5qdp0gZZvlt3aFuAf/ZYZ3/ERy04xnkOnJtQYrH88 avpbEnElqYevIBGmhDPywMyr+JDbj9gawybEYFAL7xUUZwAPQMqnMCuwxTb4fw== X-Google-Smtp-Source: AGHT+IFijgrMmx4TivpD0dNtPEfHphiWFRNLETekO8Pl2jxcFzM7frzwLvq0M7eONS/PL41Poi7LOg== X-Received: by 2002:a05:6830:2702:b0:703:5c50:82e2 with SMTP id 46e09a7af769-70f5c394179mr25105938a34.16.1725476636072; Wed, 04 Sep 2024 12:03:56 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-70f67173febsm3024635a34.46.2024.09.04.12.03.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Sep 2024 12:03:55 -0700 (PDT) Date: Wed, 4 Sep 2024 12:03:53 -0700 From: Noah Misch To: Andres Freund Cc: pgsql-hackers@postgresql.org Subject: Re: GetRelationPath() vs critical sections Message-ID: <20240904190353.7b.nmisch@google.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Sep 04, 2024 at 11:58:33AM -0400, Andres Freund wrote: > In general emitting a LOG inside a critical section isn't a huge issue - we > made sure that elog.c has a reserve of memory to be able to log without > crashing. > > However, the current message for buffer IO issues use relpath*() (ending up in > a call to GetRelationPath()). Which in turn uses psprintf() to generate the > path. Which in turn violates the no-memory-allocations-in-critical-sections > rule, as the containing memory context will typically not have > ->allowInCritSection == true. > > It's not obvious to me what the best way to deal with this is. > > One idea I had was to add an errrelpath() that switches to > edata->assoc_context before calling relpath(), but that would end up leaking > memory, as FreeErrorDataContents() wouldn't know about the allocation. > > Obviously we could add a version of GetRelationPath() that just prints into a > caller provided buffer - but that's somewhat awkward API wise. Agreed. > A third approach would be to have a dedicated memory context for this kind of > thing that's reset after logging the message - but that comes awkwardly close > to duplicating ErrorContext. That's how I'd try to do it. Today's ErrorContext is the context for allocations FreeErrorDataContents() knows how to find. The new context would be for calling into arbitrary code unknown to FreeErrorDataContents(). Most of the time, we'd avoid reset work for the new context, since it would have no allocations. Ideally, errstart() would switch to the new context before returning true, and errfinish() would switch back. That way, you could just call relpath*() without an errrelpath() to help. This does need functions called in ereport() argument lists to not use CurrentMemoryContext for allocations that need to survive longer. I'd not be concerned about imposing that in a major release. What obstacles would arise if we did that? > I wonder if we're lacking a bit of infrastructure here... Conceptually, the ereport() argument list should be a closure that runs in a suitable mcxt. I think we're not far from the goal.