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.94.2) (envelope-from ) id 1rOH8v-002s1H-MO for pgsql-bugs@arkaria.postgresql.org; Fri, 12 Jan 2024 13:00:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rOH8r-001mRk-6w for pgsql-bugs@arkaria.postgresql.org; Fri, 12 Jan 2024 13:00:09 +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.94.2) (envelope-from ) id 1rOH8q-001mRc-HQ for pgsql-bugs@lists.postgresql.org; Fri, 12 Jan 2024 13:00:08 +0000 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rOH8n-001BF9-FU for pgsql-bugs@lists.postgresql.org; Fri, 12 Jan 2024 13:00:07 +0000 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2cd880ceaf2so21201101fa.2 for ; Fri, 12 Jan 2024 05:00:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705064404; x=1705669204; darn=lists.postgresql.org; 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=VwdDBK37BNWs3clET1gNkMGTGZub3Gs5oQ5uPZr7KyM=; b=GJZoNUyLGD/sprpk3Hz/LUbazWCkYRgcMRk0/NfhlYHFRtUKKqCqBUCaGxuaS1K8AG nDhp8YGhFt6aCJsunhVlERVXzlMNq65QAAygmUxzAyJP4B7iBzCnTr0OUazyMJMwfSgO lkDjExuLybVLLqwRFs9voO34l8g7LINQhrBKmrSMlb39xSaOVdIXXv0a/09FJFOh1l+d F7cxV5QAGjkdBWmkIBahTPeDCQOpkHc/TrgP7Kpm3EzS/AXL72aSGXF8W6QSd+9QVfPv 11iqRMiR77OwJCOj1ByufHYkJJLOVfO5ZK1vAReRiFtHT41GdsjHes8NrD3E/Esjz/Bi 4CwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705064404; x=1705669204; 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=VwdDBK37BNWs3clET1gNkMGTGZub3Gs5oQ5uPZr7KyM=; b=WRCpdxMmn+L/9d5BTseucr9CNLStayPNLyry2kfZHB69HejjAbSzPFnUsEo57G3bg+ PwQe19WdqZMo7Nl7Gk62HA3H2eEe0ZAnThp/WXcOUZ8K6Y5JPyEVO26N0letAdjQuhlp cI74ylFq5TlEdypUExV4/0VQ1u2UqytjkpMfodvks+8hArsaNyeVqg8mDniVkt1+8+2/ ENdK4En8+0Pk36ZdjButmGcH5Vfv7nMFyOu45yUkbQDDXIGekB5b/ZpsawYJXqtNCg2I hd6QYsmUQZltqTF59R5dgf7RjLQHhvY1dG5+0jc9AXk0LvEdmBYwjTjUFlv+nUlGb4ZM Bh6g== X-Gm-Message-State: AOJu0Yxqx89B8HyhQJppDL9L4cF7NgZYPSrjRTk07g/9E+C2Bfg03Qol eZvbwBhzu8yINtCkaRngAwo= X-Google-Smtp-Source: AGHT+IHHplHfDySELSRUAxne1nZ4ZTtqyhbQxCR7xZ4BSNmxpRrdY7jrwSz4HI3DasQKs6TQ9n5nng== X-Received: by 2002:a2e:9b0b:0:b0:2cd:492a:1231 with SMTP id u11-20020a2e9b0b000000b002cd492a1231mr694540lji.62.1705064403513; Fri, 12 Jan 2024 05:00:03 -0800 (PST) Received: from [1.0.0.7] ([91.185.77.50]) by smtp.gmail.com with ESMTPSA id h19-20020a2e9ed3000000b002cd35d8b018sm452061ljk.113.2024.01.12.05.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 12 Jan 2024 05:00:02 -0800 (PST) Message-ID: <94ca869c-472b-f1cf-3d7c-aa8530f5475d@gmail.com> Date: Fri, 12 Jan 2024 16:00:01 +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 , Robert Haas Cc: pgsql-bugs@lists.postgresql.org, Richard Guo , Andres Freund , Peter Geoghegan References: <17798-0907404928dcf0dd@postgresql.org> <87fe6c28-80b2-f709-a8db-d105e5240e39@gmail.com> <1849610.1682807087@sss.pgh.pa.us> <494cd7c4-c109-6886-c0e3-138a95e7e38b@gmail.com> <74335d44-819d-60e0-859a-efe74d9c84c2@gmail.com> <325298.1705009396@sss.pgh.pa.us> From: Alexander Lakhin In-Reply-To: <325298.1705009396@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello Tom, 12.01.2024 00:43, Tom Lane wrote: > Robert Haas writes: >> On Wed, Jan 10, 2024 at 10:00 PM Alexander Lakhin wrote: >>> I was discouraged by that vast distance and implicit buffer usage too, but >>> I found no other feasible way to fix it. On the other hand, 75e03eabe and >>> Andres's words upthread made me believe that it's an acceptable solution. >> I agree that it's potentially acceptable. I just wonder if Tom or >> someone else is going to want to propose a bigger change to avoid some >> of this messiness. I don't know what that would look like, though. > I'm still feeling itchy about it. Maybe the problem could be fixed > better if we didn't re-use slots with such abandon in this code, > but I don't have a concrete proposal. Also, that'd likely be a > nontrivial rewrite bringing its own possibilities of adding bugs. I'm not too excited by this approach either, but maybe it's the only solution which can be implemented for now. I have rechecked other ExecGetUpdateNewTuple() calls and confirmed that all of these materialize newslot returned. Namely, 1) ExecCrossPartitionUpdate() called by ExecUpdateAct(), where in case of a  retry we have:     /* ensure slot is independent, consider e.g. EPQ */     ExecMaterializeSlot(slot); 2) ExecModifyTable() passes the slot returned to ExecUpdate(), which begins  with a call to ExecUpdatePrologue(), which materializes the slot. 3) ExecUpdate() itself calls ExecGetUpdateNewTuple() in a loop (redo_act),  where ExecUpdateAct() called, which also materializes the slot. So this place in ExecBRUpdateTriggers() is the only one where we have no  materialization after ExecGetUpdateNewTuple(). Maybe another option is to always materialize the slot inside  ExecGetUpdateNewTuple(), but I'm afraid such a change is not suitable for  back-patching... > Some concrete thoughts: > > * Maybe better to Assert(newslot == epqslot_clean) instead of > what you did here? Not sure. Me too. Other ExecGetUpdateNewTuple() calls don't have such Asserts nearby, but I haven't studied yet whether they could have ones... > * Why is the ExecMaterializeSlot step needed if we don't have > an epqslot_candidate? I had decided to move that step out of "if (epqslot_candidate != NULL)" in v3 to play safe when removing ExecMaterializeSlot(newslot) brought by 75e03eabe (more details upthread [1]). I thought that if that commit stated that the slot needs materialization (it was time before 86dc90056, which raised the current issue), then the materialization must be preserved. (The same commit can be found in zheap repository [2], but I found no other explanation of it's purpose...) Though looking at the current core code, I'm inclined to perform ExecMaterializeSlot() only when we have epqslot_candidate, if we can afford not to bother about that zheap-specific or similar scenario... [1] https://www.postgresql.org/message-id/e989408c-1838-61cd-c968-1fcf47c6fbba%40gmail.com [2] https://github.com/EnterpriseDB/zheap/commit/75e03eabe Best regards, Alexander