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 1pst04-000323-UU for pgsql-bugs@arkaria.postgresql.org; Sat, 29 Apr 2023 22:25:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pst02-00035R-Us for pgsql-bugs@arkaria.postgresql.org; Sat, 29 Apr 2023 22:25:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pst02-00033v-Ly for pgsql-bugs@lists.postgresql.org; Sat, 29 Apr 2023 22:25:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1psszv-005qZs-Ef for pgsql-bugs@lists.postgresql.org; Sat, 29 Apr 2023 22:25:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33TMOlij1849611; Sat, 29 Apr 2023 18:24:47 -0400 From: Tom Lane To: Alexander Lakhin cc: pgsql-bugs@lists.postgresql.org, Richard Guo , Andres Freund Subject: Re: BUG #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger In-reply-to: References: <17798-0907404928dcf0dd@postgresql.org> <87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com> Comments: In-reply-to Alexander Lakhin message dated "Sat, 29 Apr 2023 12:00:00 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1849609.1682807087.1@sss.pgh.pa.us> Date: Sat, 29 Apr 2023 18:24:47 -0400 Message-ID: <1849610.1682807087@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alexander Lakhin writes: > Could you please confirm the issue, Richard's and my conclusions, and the > correctness of the patch, in light of the upcoming May releases? > Maybe I'm wrong, but I think that this bug can lead to data corruption in > databases where BRU triggers are used. Clearly it could, though the probability seems low since the just-released buffer would have to get recycled very quickly. I am not entirely comfortable with blaming this on 86dc90056 though, even though "git bisect" seems to finger that. The previous coding there with ExecFilterJunk() also produced a virtual tuple, as you can see by examining ExecFilterJunk, so why didn't the problem manifest before? I think the answer may be that before 86dc90056, we were effectively relying on the underlying SeqScan node to keep the buffer pinned, but now we're re-fetching the tuple and no pin is held by lower plan nodes. I'm not entirely sure about that though; ISTM a SeqScan ought to hold pin on its current tuple in any case. However, if the UPDATE plan were more complicated (involving a join) then it's quite believable that we'd get here with no relevant pin being held. The practical impact of that concern is that I'm not convinced that the proposed patch fixes all variants of the issue. Maybe we need to materialize even if newslot != epqslot_clean. Maybe we need to do it even if there wasn't a concurrent update. Maybe we need to do it in pre-v14 branches, too. It's certainly not obvious that this function ought to assume anything about which slots are pointing at what. Also, if we do materialize here, does this code a little further down become redundant? if (should_free_trig && newtuple == trigtuple) ExecMaterializeSlot(newslot); A completely different way of looking at it is that we should not treat this as ExecBRUpdateTriggers's bug at all. The slot mechanisms are supposed to protect the data referenced by a slot, so why is that failing to happen in this example? The correct fix might involve newslot acquiring a buffer pin, for example. Bottom line is that I'm afraid the present patch is band-aiding a specific problem without solving the general case. regards, tom lane