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 1pt5k7-0003dX-Ks for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Apr 2023 12:01:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pt5j6-0004h3-Ah for pgsql-bugs@arkaria.postgresql.org; Sun, 30 Apr 2023 12:00:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pt5j5-0004gt-SS for pgsql-bugs@lists.postgresql.org; Sun, 30 Apr 2023 12:00:23 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1pt5j3-002hAZ-1C for pgsql-bugs@lists.postgresql.org; Sun, 30 Apr 2023 12:00:22 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-4f0025f4686so1995916e87.3 for ; Sun, 30 Apr 2023 05:00:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682856019; x=1685448019; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=RaNk1nbUb3+DLBKgIL25WxVz30ts0/T6snmKwj39Q8o=; b=bQkhg+AEjlHjfVPROONOOt03q/k9iafGtUB9oUZ5X/aRbXhzfdw94xL6ZNw4QDWG/5 tYk9V7L+684l/9kGkV4Schdwx9dGUyI7pBGHWmuyWDsVWJ89ZRublkeJmKKmrvv8vkVP uyX/6Lqi17TC1VxPBiPt3fZml5nIULoss6wwUsx8DNPEvqAmtRTHyM2uzjwkqRbcSlKD ECbaZ/VdP2IFcs2OF+YyBq0lmwsPsBlrgfWcl2ycXuN9RmQKzbawzNDOu1rVE+qUDOkE cf+42d6Gdl0pthBaSTTXjcLjf4EFJjkXUd7dYhCQsASTNBP6SIkHI6VBzmtY4uQgteQP DIvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682856019; x=1685448019; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RaNk1nbUb3+DLBKgIL25WxVz30ts0/T6snmKwj39Q8o=; b=M3n4+Pp5BRzVLUBOw7Ml+lXSbgmlq4mTU5cpbMDxLsyzdgQWpbGbRYQOCfGNQfBbkL WKCShOPsmj/SEoDNUsJfkl2euOI1cI1+vO/5+avQjJQlmXXkoWJ3sYE4CA6lmOR3d0TI 1jROZT3l7mntftGuuwLGmMntqE4x27OU365wQ4h8Fk1/nReSXYHQSZUNdUOZ3DGETEYb iCwzu1ToZbJZqjg6AqgD0mA7Zf7p8CL+Fb8WuZMTAJ9lIPtFevMG1pXsKzBcj2AfsH5K avH+LtB1fdfsEUsqDBAQgj0QP77CDSMOR/xzvXzSSulRySfzWkujMblJBm/ctFjSLWfR JLNg== X-Gm-Message-State: AC+VfDwmXaVPqaH/cw+yP06LhFGfc80U4Y42UcKJaJFOq5hajpxhl5CA YCM5+JHaL9ASSLNH/pn6VA0= X-Google-Smtp-Source: ACHHUZ4t6wVbYFCIT5D+vF1r2xLim2BdLhzY3PZqDaCGqYSh10YZJlalHakeIv1yHyJGvm5RyZ2aSg== X-Received: by 2002:a19:f816:0:b0:4e9:bafc:88d0 with SMTP id a22-20020a19f816000000b004e9bafc88d0mr3335242lff.23.1682856018547; Sun, 30 Apr 2023 05:00:18 -0700 (PDT) Received: from [1.0.0.7] ([178.155.28.115]) by smtp.gmail.com with ESMTPSA id c2-20020ac24142000000b004e8452972a2sm4172195lfi.247.2023.04.30.05.00.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 30 Apr 2023 05:00:17 -0700 (PDT) Message-ID: Date: Sun, 30 Apr 2023 15:00:16 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: BUG #17798: Incorrect memory access occurs when using BEFORE ROW UPDATE trigger Content-Language: en-US To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org, Richard Guo , Andres Freund References: <17798-0907404928dcf0dd@postgresql.org> <87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com> <1849610.1682807087@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: <1849610.1682807087@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 30.04.2023 01:24, Tom Lane wrote: > 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. So I've made several mistakes when trying to reach the solution walking on thin ice. I'd initially observed the issue with asan (and perhaps that had happened under some specific conditions (memory pressure? (I can't say now)), but when I produced and simplified the repro for the bug report, I was focused on valgrind, so the final repro doesn't demonstrate a real memory corruption and it's not clear now, which conditions needed to cause it. So I really can't estimate the probability of the corruption. > 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. Yeah, also the environment changed since 86dc90056, so I couldn't just revert that commit to test the ExecFilterJunk() behavior on master. All the questions you raised require a more thorough investigation. > 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. Yes, it was tough to decide how to fix it (and the correct buffer pinning seemed more plausible), but 75e03eabe gave me hope that it could be fixed by the simple one-line change. > Bottom line is that I'm afraid the present patch is band-aiding a > specific problem without solving the general case. I agree with your analysis and conclusions, so I'm going to turn the alarm mode off and research the issue deeper. Thank you for the detailed answer! Best regards, Alexander