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 1w7J0V-005ARp-0B for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Mar 2026 20:14:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7J0T-0067uL-1Z for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Mar 2026 20:14:41 +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.96) (envelope-from ) id 1w7J0T-0067uD-0b for pgsql-hackers@lists.postgresql.org; Mon, 30 Mar 2026 20:14:41 +0000 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7J0Q-000000023Qi-2t5q for pgsql-hackers@lists.postgresql.org; Mon, 30 Mar 2026 20:14:41 +0000 Received: by mail-lj1-x22b.google.com with SMTP id 38308e7fff4ca-38c551f2497so44285951fa.2 for ; Mon, 30 Mar 2026 13:14:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774901678; cv=none; d=google.com; s=arc-20240605; b=PiNkGSQ8AJ0MQzsgYQqYAKsO6XMbgFPpsA1Wx46bGY/O6B+ZhrGrzTPLXYmZ5c9J2P Jxw4tU4LH10izAawK3XWrbufnSYeSLVGfl53kuL/ngmuW+zhggEusH52DHr/bnHGDOqn Vog2+u4QmIPMIyUq542zeUefvtK1j6NvqunNXnfNtwZChw1vcnlWolMkkjb201xEw2pJ 3o+5ZSCn2/FX+cU7I9glCnVwhvR7r/CVHqfAie91XbthOo9uVPYs197fEkTx9dSenoWw pOmaIM+fUcyl5HaqBNWbQNlvelInF3ZxCyIiEGLzmIFPnsx0h4Ls/AYhVP25jKuSJ9Qq b0YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=thiFpWPsTY2JPLm2dTlEmkNnsG3z/huC8YSUO3c0SIY=; fh=SaF624OWiWE3nFNyLzOvnqW9OUhpc/GL6YrDIeZdieE=; b=LFO6LfY/bSQ3wdYe19kIYx46SShq2fDmfuxIdLW52Hud3WdvtP3xqt0OLijiRH4Xat eMTnMaEXSC5U4LmU1qTx/dTWfW7T2BRPosCkncD+GKqpzqVZhiQeqaydnyKFLEutlYLB k2+EevPjoT8ZSRNmxoyURYI+AczREapQ/0zbkIh6RSgRKe8IyJcMH7tZedJBn5XO16yy MWO1S6fjZWAj/aSSzEbgNCMWFMScf9uYlx5vVEPvatJ7icsizEM2x4GcDYTFz6qybJbo DG5s1Xz7Bb6/77cuTkFfScgipvCguzk/NWvUK6qXn+cLCGnDMmn95YBTgENJACajthDJ HfcQ==; 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=gmail.com; s=20251104; t=1774901678; x=1775506478; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=thiFpWPsTY2JPLm2dTlEmkNnsG3z/huC8YSUO3c0SIY=; b=CLrEEa9H4uIZejuJdAFZWY5RRScl99cgiPb8/hs2/AzYbylfVVpqcD6NKdxdOcFtMu C7mMCeLivC2PGSsajd9EfBlcmCX0ikByVd18C7vxiOhfskuQD8jsU+v+u0zigTfFltrP riWZ+j8FUC1c00oWBKexcr7roI0c9pjSctbXAbur6TZnLQ1nZzF2hiuvRBT2M7oQlovR qWOODNakCbhQr2WV23nH/1cC4LruTpx/UYsqykSha+xZzNfpz0/V76mAbBYxyfUJwrJg FLCaOCdQTtRbATw7LU7xFL86Wop8qON9l00k3hKkvvRy3ac6NTk1arLPW+HymuMBe8M4 Ix9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774901678; x=1775506478; h=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=thiFpWPsTY2JPLm2dTlEmkNnsG3z/huC8YSUO3c0SIY=; b=nuGwgB1tvj/OofQItwmYHoEhFpvL8iynMd2K1170VD0ojrHhFbHM4un7VUbk4gtwM7 fS191WJvb+TOYPbQX60FdiiUwjGP1+nQ/g/NX0SJIObHvrbYECkb3k/0EQAi1ynHtS33 BzIBuf5HMz2//6QNqMVulI+vLKkszFaAim+2LM+AYjPTFvAmKlS/aSzp0d2r+rmZULoM WNYoHurZ3U6OLF5f+hQWTJz0aIHzH8m8XtUax3Um7Bc5TTFTQmoKTUHt6zrGSBYEyldz wKwyzNPqFfkdJkNP6A4EH79xTD0IMtfa/LYV4rfMWY6BegGdTCo/2LXKmqvXtl0kvJIT FbSA== X-Forwarded-Encrypted: i=1; AJvYcCW4twFlpkO7XQNhBlriMJRqNU64GKSViatRXR9bP+WNMywlCVHn7SGSv1Y7rAr6LnJoFG5CwYHu01KPvQ8v@lists.postgresql.org X-Gm-Message-State: AOJu0Yyd7o6d1xAK0Y2XcsVFn6MEBgWjvyvCHyrGT9ebsdC5g3pEUueN gaU1XE2G6SEJSZSLgOrN38SGOPofqK5+ZjAb1N3T6y4MoBb1ryUE5Is5/1oqC3m8hkxulrrnTdy PmgwVNsRUYP5LTiMPoif9nx/15tTQq9OOstyg X-Gm-Gg: ATEYQzzL91x+68Nkt3hmT9RRWWu1wny7+AfkzOtTO+nsvrajrWso3Qd4i0wKriWNYsW fW+u5bTe71wfeAZhujW4hD4ySzmmWRiUpFxEujr50rsYOmVsZuj5ct0wmD8mwldCdh+Fu/+32Id CV8gVBAkRXd73PWV+JG7V3MtNNQmIIBaWiE7O7btz+uOcsjKQhGXrvUvORki6m/4UDRb/j/Pdnd mP5SxqgUmak+9dHoD1S3bPLlgNMEBDmttfJlOSHsEnaLe/Z6DQXLnj2Tc7emYQZpZ+6N9JIQi+t Yo/wYyD+oaxMNuTV5LGzZCOQjddsU1YMMr8YhlyRfKKHVORL9VYBVdw4NlBHpTqxGTyC7tYfrlA Ep6UI X-Received: by 2002:a05:651c:1502:b0:38c:6a46:b9b1 with SMTP id 38308e7fff4ca-38c730c4999mr44908701fa.8.1774901677492; Mon, 30 Mar 2026 13:14:37 -0700 (PDT) MIME-Version: 1.0 References: <2546907.1774898070@sss.pgh.pa.us> In-Reply-To: <2546907.1774898070@sss.pgh.pa.us> From: Matthias van de Meent Date: Mon, 30 Mar 2026 22:14:25 +0200 X-Gm-Features: AQROBzAjcAeorTthsfHbYsDOCeQ8nMjvTl3nHS2-gXtI4c6XJlSt7ApFFUMUyTE Message-ID: Subject: Re: [BUG] Excessive memory usage with update on STORED generated columns. To: Tom Lane Cc: "Anton A. Melnikov" , PostgreSQL Hackers , Peter Eisentraut Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 30 Mar 2026 at 21:14, Tom Lane wrote: > > Matthias van de Meent writes: > > The actual issue is that ExecComputeStoredGenerated uses > > ri_GeneratedExprsU's NULL-ness to check whether the generated columns' > > expressions have been initialized, whilst for UPDATE ResultRelInfos > > the initialized-indicator is stored in ri_extraUpdatedCols_valid. > > Sorry, I missed that you'd already responded. That's allright. > I don't like using ri_extraUpdatedCols_valid here: it requires callers > to know more than they should about how ExecInitGenerated works, and > it does not fix the comparable performance problem that probably > exists in the INSERT path. I'm not sure which comparable performance problem you're referring to; I don't see one mentioned, and INSERT doesn't have the same issue because we never call into ExecInitGenerated for inserts unless 1.) there are any generated stored columns, and 2.) it hasn't been called already for this ResultRelInfo (*) by checking nonnull-after-initialization ri_GeneratedExprsI. (*) the issue here, of course, being that we *do* call ExecInitGenerated many times in the same query for the same RRI when UPDATE only changes columns that aren't referenced in generated columns, this caused due to an incorrect check which checks the wrong field. > I think the right fix is to have three > booleans specifically reflecting the validity of ri_GeneratedExprsU, > ri_GeneratedExprsI, and ri_extraUpdatedCols. I'll defer to Peter as primary author of this code, but personally I think that it isn't needed: In the insert case (ri_GeneratedExprsI) the field is always non-null once the generated columns' exprstates are initialized, whilst in the update case the current boolean is indicative of the fields having been populated. Yes, it might benefit from better naming, but the boolean itself is already sufficient to indicate that you can rely on the update-related fields to be populated by ExecInitGenerated. > There's at least three bytes free after ri_extraUpdatedCols_valid, > so we could cram two more bools in there without an ABI break. > Admittedly, it's not pretty that those bools wouldn't be close > to the fields they describe. But we could improve that in HEAD > with some more-substantial reordering of the contents of > ResultRelInfo; it's not like the current ordering has much rhyme > or reason to it. I personally try to avoid adding new fields in backbranches if that's possible (even in alignment gaps), so that if we actually must add data to the struct we still have space to pick from. Kind regards, Matthias van de Meent Databricks (https://www.databricks.com)