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 1w5Gta-00354r-0h for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 05:35:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5GtY-00BwzH-14 for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 05:35:08 +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 1w5GtY-00Bwz9-02 for pgsql-hackers@lists.postgresql.org; Wed, 25 Mar 2026 05:35:08 +0000 Received: from mail-qt1-x82c.google.com ([2607:f8b0:4864:20::82c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5GtW-00000000ukN-2lfg for pgsql-hackers@lists.postgresql.org; Wed, 25 Mar 2026 05:35:07 +0000 Received: by mail-qt1-x82c.google.com with SMTP id d75a77b69052e-50b35f3e489so5832591cf.0 for ; Tue, 24 Mar 2026 22:35:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774416906; cv=none; d=google.com; s=arc-20240605; b=I1IrWqu48TmTqzbii865dQlZSxEPAp2hi6BUvfAhs5RfCsa9eOSpqGVGIVrg8rZmzg twd6tsw1ph6XsXlQWI0AKWe7arrxhMqxYGMkzHbljHzxMRfWEPxJCNwihe+hLi7pZHhx 8o0Y0vtUuSNQj6N0bY0404j0RmroTgDv2oLDkxX+Sxm6TXWJ9vklxHCzHQLsnT17VPzP yHSDIUUDRmzgzi3k3vVGlM/xuAtDiMilqo0TI2XOfZuM6qelu4r57mvyu6FkmMLEYBKQ ImHX/rMYFQphudeoec3kGFswrsppMNITPKHDC3a5iFFdRXNFxphsIsSKWp7ZnCDUDJsu AEyg== 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=Jw7LyprUNu2CBf0lT9SmTPSfSN6ywrj9+i8rW2vW2HQ=; fh=GvyHzMtQP8nKuSwKNgOb6z15kUWB9t4VQQTa4cVjY7I=; b=UIB9iawf4A6A9774bzqZw7qlLh3obXAZuv4IlfS1tG3wvmwMtUJyGLfIcW7KsrKTjx IpkKKi/Vl3jwUV+MAZsydj5ObHHAs7FXVCy0KFC8rGwahrQ/vu6Y3veUHurMzKbiH5By tGgVjZAuoA6ydq9QwKA9X9tImO3IeIXBY/b84LZmuf29/uvmtdgJhXVaZ5hvnJZIwxDm rv8UtRj5NSblssw56SwuQObH4VDV5keq7hOswOujcGlx4vdLIojRJx5AD6zsmKFjBStq nBemvs25jioBtoFKaEN86TCO3ZndjGuxvOg/KvL+OWYa3rmoJiQfnYIUD7OW413y3kKo XyKw==; 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=1774416906; x=1775021706; 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=Jw7LyprUNu2CBf0lT9SmTPSfSN6ywrj9+i8rW2vW2HQ=; b=QQUdhwYHuC2NA2Oirc2pqYz1K/dmTp9y+5IqHEWWqtjx99i9iXf3ItFsbJbZRbIdMR +0vejYkVUJnifPoyGS51lHXoFvPpaoxGikto1Ws3JcmE9FKDnnl+mKgcqzzF0DEWLW4s MgqVdnS92gCvcvmqqqOSVcrbpDb62I61NYgAg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774416906; x=1775021706; 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=Jw7LyprUNu2CBf0lT9SmTPSfSN6ywrj9+i8rW2vW2HQ=; b=qZdtnPAL+J03oJ9M9rC5UkVRf8CG/VhCzQi14pLnejaISTQkORX3oScVhhjZCexAtP b3va4TkNP2Z1o6ojhbz9NqiBQegJ0pIWyBeaRgo/cEA5zAJA6dougREbIGugkULZLeZF PJLWMjNhGerzmx7ykdeVFJ10Ecqg8fseOK156oXkH6+AZ0N6Gv6xnpqlf3SFy21A9ijB Se8cPfzANvT3/QrLjPUUHeM6mczjLioMqoR5nLstc6KzI/p6JLh+GFwxgBvXjvBsx2at 6Ap+noSd4APO4rl2kMW9yGWnqVcJYZR3BlPVi/smPKj4pWbRrUh59A3AQDDQ4IqoZF6P K/Qg== X-Forwarded-Encrypted: i=1; AJvYcCUPq/r6CB4Q3G9ZoKsbMThr73/bjgNKE41RI9QMtQidTSPvLI4No4O0p5XGC8CTDlYxybjJkX4f0m/9asua@lists.postgresql.org X-Gm-Message-State: AOJu0YwHc7Jm2KAKGguXooMDf/a7j0qP/UyT01wUFg4VBJvI0Q6S9zNf NzipZlbd8rgBIcyqQTblNtCPRqo6iIldTcZnKg2qhcsN1zJ4wz/8S4m9p50NbbiljSu1re9C6cs DsD9/67jllnTxAK2DzN/sOQMBsXoLvPzzeq9vgaP7 X-Gm-Gg: ATEYQzwpqsUdFHLzfU1V1tm2IBNTd9DEv6+Qc19Pz+DBq3Wq+tLtdNStuzSdeLNMM9s 20/10gW7QWW7qNmObkOxAGQ/SI8kHtmilAle9XZlUBndS21OElfhBs6NzeaQmP8SW61zv3Xuid4 JbCYrIKobVatrZUHhk/4FjY/4Y92dPkxWNXbtMMlB1nXVTF4lU21zhJs08B/Ph2Z/O6zalIX+0W +tWeZOud1H19a082B9GgYz4XqGE++OZlH4ad6aj6QZo6P1UOPyoT7JvB8iVQ2ECo0ToSboiEXFm 9AASv+rnFG1J1/TqyVjyXJH68po18fjaAe/XgDM/ X-Received: by 2002:ac8:58d5:0:b0:50b:8676:3cb7 with SMTP id d75a77b69052e-50b86763e64mr8117721cf.1.1774416905893; Tue, 24 Mar 2026 22:35:05 -0700 (PDT) MIME-Version: 1.0 References: <06086cb4-881b-4f5a-96af-f275220ff52d@vondra.me> In-Reply-To: From: Lukas Fittl Date: Tue, 24 Mar 2026 22:34:29 -0700 X-Gm-Features: AaiRm52A8sbO6DzopcMxCUkuQlChsm4bAiX58nmYaGyCdkSxenSHH5pvb8UmMJ0 Message-ID: Subject: Re: Stack-based tracking of per-node WAL/buffer usage To: Zsolt Parragi Cc: Heikki Linnakangas , Andres Freund , Tomas Vondra , PostgreSQL Hackers , 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 Hi Zsolt, On Tue, Mar 24, 2026 at 3:59=E2=80=AFPM Zsolt Parragi wrote: > I like the new approach, but doesn't `EXPLAIN (BUFFERS)` leak some > memory because the resource owner isn't registered on that path? It > seems to be visible with pg_log_backend_memory_contexts. Ah, yes, good catch! Its nice how the separate memory context makes this very clear now. I think this was actually an existing problem that only surfaced now. The issue is that EXPLAIN (BUFFERS) will allocate the per-query instrumentation, but not actually use it, since the buffer tracking is only relevant for planning, which has its own instrumentation. I can fix this locally by adding the following to FreeExecutorState: /* * Make sure the instrumentation context gets freed. This usually gets * re-parented under the per-query context in InstrQueryStopFinalize, b= ut * that won't happen during EXPLAIN (BUFFERS) since ExecutorFinish neve= r * gets called, so we would otherwise leak it in TopMemoryContext. */ if (estate->es_instrument && estate->es_instrument->instr.need_stack) MemoryContextDelete(estate->es_instrument->instr_cxt); I'll include this in the next revision unless I come up with a better idea. FWIW, I also considered just not setting INSTRUMENT_BUFFERS in ExplainOnePlan unless ANALYZE is active, but I think there might be other cases where that doesn't work as expected, so I think the explicit delete is better. > > #define INSTR_BUFUSAGE_ADD(fld,val) do { \ > - pgBufferUsage.fld +=3D val; \ > + instr_stack.current->bufusage.fld +=3D val; \ > > #define INSTR_WALUSAGE_ADD(fld,val) do { \ > pgWalUsage.fld +=3D val; \ > + instr_stack.current->walusage.fld +=3D val; \ > > Nitpick, but these could use +=3D (val) Ack, makes sense - I'll adjust in the next revision. I'll give it a day or so for further feedback before posting the next updat= e. Thanks, Lukas --=20 Lukas Fittl