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 1ulMid-00E7tl-GT for pgsql-general@arkaria.postgresql.org; Mon, 11 Aug 2025 07:13:19 +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 1ulMic-00AeTY-7N for pgsql-general@arkaria.postgresql.org; Mon, 11 Aug 2025 07:13:18 +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.94.2) (envelope-from ) id 1ulJIm-009fnQ-KR for pgsql-general@lists.postgresql.org; Mon, 11 Aug 2025 03:34:24 +0000 Received: from mail-yb1-xb30.google.com ([2607:f8b0:4864:20::b30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ulJIk-001tuE-1Q for pgsql-general@lists.postgresql.org; Mon, 11 Aug 2025 03:34:24 +0000 Received: by mail-yb1-xb30.google.com with SMTP id 3f1490d57ef6-e8e22a585bdso2675704276.0 for ; Sun, 10 Aug 2025 20:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1754883260; x=1755488060; 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=qqM1fr7mwQoCw9wzq4i4ILFDAmj4ORMG3qXNxshwBts=; b=T10+hPPKEW559KNrtjl6x6T/M0E0ry3LFLczdcl/wJq2D2dtAu8jw+VJ5Q5eWArQOG 88XcwJgCi/3NU9BRl7sh4YGmjB6cv5MGfYZ78JVGELNNnai89X5eG7984OBL22JMg6m0 /rp+Pq+/0sf4IbWnlx8qvzoZXvQwtHAeR4g1NrFx+8/D0xBoqxadeacGK3rtZRK/6cei mv/Pyocb32QWabtgcqVPcst4TCHvvh0uvmR39s8QUVHgz42sUNHm67LaJMRfEuKGgW3N WnH4UbuBVnDKG5P8tA4Yx/gmpusBVkv/yLaJiGuSdXwVce8VhNlMizwJ7oTHGbdLIklw MqxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754883260; x=1755488060; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qqM1fr7mwQoCw9wzq4i4ILFDAmj4ORMG3qXNxshwBts=; b=u9j9DBbWB25sd40P3orxvHGXDkf1K8BWZtQSneE9Qk62FveAwO3lQcLV0dLcOqGMLy e11pKG9lXD28OsQ+/lwjVgB6LyMMpYKOzG8lKm4X3cHHW2Ac4WBq+O2+adQ+t0xXYyD2 xhPl2QNMCvrbxlqep6HrsBK1yRYZ5TeX9RxhuEirAuZiszs8kyGkhSUfmANyi6GnpAq0 qIsezEbAr4atpbTYSy2GiwqyeMBtSrYqqt6dRFMe2Ru0gXPx6qzdfYo/nqBrPBGkuFgT RYbGHialduV+MXL6E3ZdGyou1mj/lMw9AxMQvRJf9jsvvsVaq85+Q0PFqS6LpGnZFQwR ma7g== X-Forwarded-Encrypted: i=1; AJvYcCVE/FwQyG7OWIEHRCG4GrFEWh2PomWPBJ0m4vz00d/NX53xUW45s9AJ+HIcKXaX4nESXRquSuqh9+o6LfU0@lists.postgresql.org X-Gm-Message-State: AOJu0Yz2zKLuL2LGI/GlULsDmqUAsBvkDpUX7HO0OhVmmA6aQdZA7nt7 osgVVEZT9//wnvhKAkaWDpA9XlqYayOGsrnp+Fh4k+xcCDWQBKEP16w3YRXvWMBkMJeqEwbuFyB nmcTOjNukfhLf5NAUekwBW/5CzFjFr5Y= X-Gm-Gg: ASbGncsEepRGTNK82MDWgohAR6aa6+4hpp+Ve3uMQOkCfg2Ya5kw25Fc+VOfPsurSE0 M1g6HLAtA/MGC9tLzPvP6U4YMWzBLldWYyrhNbu/l2dGhK31Za9zGIpNOoq30sq2DOwPt7mewHV FFRcSUkSFqtkWTtVWVCF6UcpVOFvqpAiInoM6T0Sp6nz8wFWFjlcCq+kmqdY3Up/XLigN9Ncf68 ha4TA== X-Google-Smtp-Source: AGHT+IGzrQLwpyitNpYTH8MDiJvVw9PRfRWv5MvZE7zHOH0dty+3HaCSxRtGykfFmOQiU6h5wfw1b0vltWM2DNJ0V20= X-Received: by 2002:a05:690c:6383:b0:71c:1a46:48cc with SMTP id 00721157ae682-71c1a4654e2mr56103117b3.9.1754883260395; Sun, 10 Aug 2025 20:34:20 -0700 (PDT) MIME-Version: 1.0 References: <1f99e5c1-a203-4441-aa5c-33a3baaf852c@aklaver.com> <93c76ed6cabcd32993203f03c7ce4fc88b20c087.camel@cybertec.at> In-Reply-To: <93c76ed6cabcd32993203f03c7ce4fc88b20c087.camel@cybertec.at> From: Charles Qi Date: Mon, 11 Aug 2025 11:34:08 +0800 X-Gm-Features: Ac12FXwvlyysP-duqzx-_w1FSMKsjQBv62ElYWEvmqqpCIGP6l2PYSrL7JFnh4U Message-ID: Subject: Re: When UPDATE a row in a table with BEFORE ROW UPDATE trigger, the XMAX of new tuple is set to current XID To: Laurenz Albe Cc: Adrian Klaver , pgsql-general@lists.postgresql.org 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 On Mon, Aug 11, 2025 at 3:34=E2=80=AFAM Laurenz Albe wrote: > > On Fri, 2025-08-08 at 09:20 +0800, Charles Qi wrote: > > Let me clarify the question, when the BEFORE ROW UPDATE trigger is pres= ented > > Q. Why do we need to set the XMAX of the new tuple to the current xid? > > Because the row gets locked, I'd say (without looking at your example). > With or without the trigger, the row gets locked and unlocked while the update is doing its thing. The problem here is that HEAP_XMAX_KEYSHR_LOCK and XMAX are set with the trigger even if the update transaction is finished, while both are not set without the trigger. > > which risks piling up multixacts quickly in savepoint/exception block > > scenarios. > > Why is that a problem for you? > > Perhaps the trigger could use SELECT ... FOR ... to lock the row in the > strongest level your transaction needs. A multixact is only necessary > if a subtransaction needs to take a stronger lock on the row than what > was there before. > > Yours, > Laurenz Albe The piling up of multixacts are related to the performance topic, which is not in the scope of this mail. The trigger function in example is doing nothing but return new, the row is actually locked by the trigger itself (trigger.c > ExecBRUpdateTriggers > GetTupleForTrigger) You mentioned a very important behavior: > A multixact is only necessary > if a subtransaction needs to take a stronger lock on the row than what > was there before. We are doing two no key updates in example, and should not need a stronger lock on the same row.