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.96) (envelope-from ) id 1w4lYf-002XEX-1F for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Mar 2026 20:07:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w4lYc-0025hx-2g for pgsql-hackers@arkaria.postgresql.org; Mon, 23 Mar 2026 20:07:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w4lVI-00215B-0Z for pgsql-hackers@lists.postgresql.org; Mon, 23 Mar 2026 20:04:00 +0000 Received: from mail-qk1-x736.google.com ([2607:f8b0:4864:20::736]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w4lVG-00000000g0V-2uN8 for pgsql-hackers@lists.postgresql.org; Mon, 23 Mar 2026 20:03:59 +0000 Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-8cfbbf35354so434687785a.0 for ; Mon, 23 Mar 2026 13:03:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774296238; cv=none; d=google.com; s=arc-20240605; b=hdztJutd09hSuwxHj4L+Yh9HOWomdstuAlEybEELj5cK53PqaYA/0LLKSQ6wE0ykkr fbsJoRUCnlaz2VXR0JM7cwKVG0B1b7+19NwcLFF1erthL6x7uuMjwA/XyoHFvSR0oCDV bk0BSxtEbCk6F9r7F0LfCrnDPLydZlFvHYM/m6MXTtlE+8mxxzS/iRCPKKiCItiQT81d nInXQYXEANWIp+kIg3Osj5t9Le8iQnCI3y1UtmBuTK73xtvTRBd28SkxrG3pTc4l8DSY WGpy2uRpPt03+SGiYWOrwlL0Iid3peDKWQdDt0DkHEMQvVcKjTvN86uj2esj4t/B3QaP elfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=tL3a1b9M9FOpAziiTnul8O8InWXfSsuj78SXWi92uJA=; fh=XkqgDQHUgeqY7xE9Rzw2AD71fm1L9++tDCUIiEr90Wg=; b=P89axVgJnns4X+qWOlL8OoQwXcp0R8UbBWVWhjRm8qWABskHDciIFRfX8+XFfh6K0K WvfEesDFeCWNOsY32oHmDnqiPwW0Ss5bBXi5hWFwOJ6/K34G3Cu/ELwNxu44nGVYxBRc El4+CgJI9Ss0DxXIJM6PTxCB4J0OFwkvPse4hwPPDDu8BoeEqdiHhlY1Lt3jSu9b5byS mB2Z4bb1SCChJK0xoal4TfiTB2sTIxYUG8t7Loaj5l6SG8sFeCBWN9sjp5pb7zDPIo1+ yC3yHbhe8hj4a8vqkDW3QoXmcv2CjqbkarZIc9GOj2DBNEwwmeCjwOTiXUP3OTLuKHnx NZsw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fittl.com; s=google; t=1774296238; x=1774901038; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=tL3a1b9M9FOpAziiTnul8O8InWXfSsuj78SXWi92uJA=; b=kbfEB14664CxgII55/QA0oOArGUR2lDiyCwbJK4bZg96/DF/9GoXonWyw1NlZXXzXH ymI+juYoFXwZmkR1fh9ZB6gRszJej75ZmLS+zbTWycHQcXrQIEzZLeu31jl4GSkanUOc tPph2ECn3yNdFIJ5fxkADK3y5utAP6JqAE2qc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774296238; x=1774901038; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=tL3a1b9M9FOpAziiTnul8O8InWXfSsuj78SXWi92uJA=; b=CDGtGmJSkB5eyvc3ZNwhXqUudOkAd1gjSbcPWuGeKTG5ze3gJxSiJ1HjKrVA9RJnKV /p0UovUYrx7s3k+fqfc0v/LkS/64l1J6YAeXqwFYl80fOKyuQ1C9/F/DejCbp1bbW/aN V9jSsT+q/6I1HN+sLX4Ck4xRhpJeIcpCgmznKG3CmVadxsgw2ipjZwOWnQQfbXXf2RvX QhkVhCtgb5vAQqzcmVbFmlScYAyn4fFMK+F1bpCeuGjZiucdmpTobrsJggL8kfNZZP4W 2SpvEUWCIvg29bKfQk5rDBFdInHu4Aa6WzFFVyTbMws0TFU5jSe/KrPoCSh8+9cctimu AWog== X-Forwarded-Encrypted: i=1; AJvYcCXoLIur9Y7mb/8KixheoAu97Sp5YTdb4zGWAlTkq+fdURllWLP2r+BsWdBypSOTta/bKEv+2WfLJJQBCYp7@lists.postgresql.org X-Gm-Message-State: AOJu0YyGmK2K1dMsV8B1tNlIaR+5TBYo70ZzsKhRgS4Rs188D+6MZ7hx +N8dnlbxitFlYoFVhZNQNPCUknm3IuHJ4gVW42XkBbLScEtdwCqTYYuC/WiQDCAO1Jo6Yij28f0 GG5qHDDsCm4DJPHISybpKqixnzIztB3Pj7eQPqzrEVk7xWKd+kQ4feg== X-Gm-Gg: ATEYQzylbC3pZEZJ7LKPOqOY4vWs1TJxivrpgLPrklnkp6MdfIDQkyFcJom9/EBJKvb 8EpJ888h/HtuNrU4R+ZO3bnwjzJBLrHe1D9MJ7KIGxNWD1RYr98tQefNSaDesCtZ0Htsig4zkTh W4dxO15cEJa0/nX3ULhczaJwp6hAYeQPh+SXEDwua4qkJfENuKSEg1DFJlPhaxS+T+pubB6e1F5 hBZvX8K9SY0gkqRsUXS/8NGYHXpES8N6EiQseeFeHs3SmvgK+Jcd/ZF08rLnIxe9o75bHYoqcnO qcZuqpaQAnAmMsynDXu+yOiHh0PEBwekBcrm5XJE0S+rxpmoXs//6yDCUDnIRetpmXUzhMYZdgn z7TLtZRA= X-Received: by 2002:a05:6214:5f05:b0:89c:896e:1bea with SMTP id 6a1803df08f44-89c896e1f92mr206432376d6.29.1774296237821; Mon, 23 Mar 2026 13:03:57 -0700 (PDT) MIME-Version: 1.0 References: <06086cb4-881b-4f5a-96af-f275220ff52d@vondra.me> In-Reply-To: From: Lukas Fittl Date: Mon, 23 Mar 2026 13:03:21 -0700 X-Gm-Features: AQROBzCg60NBw_BgJJjlnFuiuq0-x1ih9TZy0bYPSsPeh0PG1VvEm50ptQpTDuM Message-ID: Subject: Re: Stack-based tracking of per-node WAL/buffer usage To: Zsolt Parragi , Heikki Linnakangas Cc: Tomas Vondra , PostgreSQL Hackers , Andres Freund , Peter Smith Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Mar 23, 2026 at 12:07=E2=80=AFPM Zsolt Parragi wrote: > > > I'm looking at this finalize at resowner part of this patch, and this > > maybe a stupid question, but: > > > > Why does the instrumentation need to be "finalized" on abort? If you ru= n > > EXPLAIN ANALYZE and the query aborts, you don't get to see the stats an= yway. > > The pg_session_buffer_usage in 0009 makes the information available, I > was able to see the issue with failing triggers with that. Even if > that part doesn't get committed in the end, a 3rd party extension > could still implement the same thing, and notice the missing > statistics. (And maybe it is useful to see some statistics about > failing queries?) Right, there are basically two reasons we need the resource owner logic, or something equivalent: 1) To ensure the active stack entry gets reset correctly to where it was before, so that we don't corrupt it after an abort (if that's not done, we'll fail during regular regression tests) 2) To accumulate statistics to parent stack entries that did not participate in the abort As Zsolt noted, (2) matters for the pg_session_buffer_usage module, which is mainly intended to ensure that the logic we're adding keeps a top-level instrumentation available that includes the activity of aborted transactions/queries, since we're removing pgBufferUsage. It also matters for some edge cases in-tree today, e.g. procedures being tracked in pg_stat_statements. And I do see us being interested in tracking failed query activities in the future as Zsolt noted. --- FWIW, on the topic of resource owners and allocations, I've done a test over the weekend, and here is a question: It seems we could switch the Instrumentation allocations we're doing when inside a portal to PortalContext, and CurrentMemoryContext when outside a portal - instead of allocating in TopMemoryContext/TopTransactionContext. That works in practice, because resource owner cleanup happens before PortalContext cleanup, and simplifies the code a bit since we can skip copying into the current memory context (because the caller wants to be able to use the result after the finalize call). And if we leak we'd only leak until PortalContext gets cleaned up, instead of TopMemoryContext. To expand on that, in the previously posted v9 we have the following allocations: A) InstrStackState allocated under TopMemoryContext (long-lived, never free= d) B) QueryInstrumentation allocated under TopMemoryContext (short-lived during query execution, explicitly freed up on abort or finalize call) C) NodeInstrumentation allocated under TopTransactionContext (short-lived during query execution, explicitly freed up on abort or finalize call) D) In other use cases, e.g. ANALYZE command that logs buffer usage, QueryInstrumentation allocated under TopMemoryContext (short-lived during command execution, explicitly freed up on abort or finalize call) And we could switch it instead to: A) InstrStackState allocated under TopMemoryContext (long-lived, never free= d) B) QueryInstrumentation allocated under PortalContext (short-lived during query execution, *automatically* freed up on abort, manually on ExecutorEnd to avoid waiting for holdable cursors to free PortalContext) C) NodeInstrumentation allocated under PortalContext (short-lived during query execution, *automatically* freed up on abort, manually on ExecutorEnd to avoid waiting for holdable cursors to free PortalContext) D) In other use cases, e.g. ANALYZE command that logs buffer usage, QueryInstrumentation allocated under CurrentMemoryContext (short-lived during command execution, *automatically* freed up on abort and success case) However, this goes against the principle noted by Heikki over in [0] that ResOwners should use TopMemoryContext to avoid relying on the ordering of clean up operations. Thoughts? Thanks, Lukas [0]: https://www.postgresql.org/message-id/flat/a3197b31-f40d-4164-872d-906= d8e9b374a%40iki.fi#526984c1be0662a7526bcfe0b358564b -- Lukas Fittl