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 1tAhDs-00HGVZ-Cw for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Nov 2024 03:05:43 +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 1tAhDp-002T35-Ct for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Nov 2024 03:05: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.94.2) (envelope-from ) id 1tAhDp-002T2w-2y for pgsql-hackers@lists.postgresql.org; Tue, 12 Nov 2024 03:05:41 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tAhDl-001Snp-Lf for pgsql-hackers@postgresql.org; Tue, 12 Nov 2024 03:05:41 +0000 Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-7181b86a749so2201416a34.3 for ; Mon, 11 Nov 2024 19:05:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731380737; x=1731985537; darn=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=/bD8f78Ib9yshV7E6ywvD+ni/GpFbqYQqH8i9GsJ3c8=; b=iJEnuz2pTcwTBiOZAAbgU5Ad44mVGM+wm8B1cUa79c/Ns82tKqiINek15luCT8UUG0 W/BAXzD7Uz2miosjUTzT2hoc97LfFs+vOkyrvH0/5Pj5m3vM65wBS7B1vIZ2hZ0D0ASt PAwV3S2nB6XZemQotNzbE2uOUaRQs9NlumpHfSM/cW1kdoSS65ns4JK08DdwNhmRfvqg pygSuaY6GjcLI4ICr+lLEPyPI4DgGSA3E/riDRPpHh0eisS9rQ+V5NBpCKlGKqpzXJ/X aFxYiGopRLnDS/bom7vx6DOhazK6u4rPVPE5gBF20GqpDejB5IeWRtqM1a4aoBzhXpp/ WYMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731380737; x=1731985537; 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=/bD8f78Ib9yshV7E6ywvD+ni/GpFbqYQqH8i9GsJ3c8=; b=X9xVshxyLOfuGLrFwnWbslcvz9ewdY/no20G0AdftHE6y8D/DB0OD1wBzlPSmITvJA 6546oFmQhmSel7FWiCPLB4GjSrkeLg5WKOOYepA7Zt9jFCSvyGakMhI5htx7J4pPAHLb lGWUZhl+1Pc9I4Ozyfw5Cdj+RaZAnbEgCHkdyINdW2YDz3ZPpmWYBE9/Ioh1kBkY/EzY jIlcqQIF+p8fzy104gcFAC0y5E2WLDa9j76oN8gRv6ODs6coFI3SSR44ddV73nTqZDUl gJdM5Mzs+61US/+ph7ZrHSaLksr1h1pWm3DlgbKvnPyzPQazfJY7KAMKMidOpgz1V6mk NpCw== X-Forwarded-Encrypted: i=1; AJvYcCW2Z1WQW7hibVJAHV7mA2D/VXnBuQPykAJUzcm41XsaXpDYal1sMIh2U2yH86ReLCWRn4/3gn+HwtbKCeZL@postgresql.org X-Gm-Message-State: AOJu0Yy1IjxCbIz33hFVZHGeQHSm+OP4+68I58eUHxfOHBDBAFVDK3Pl pG6QJEY3EUcs8A81ZGNs2ITHt3Z790K8NFF6ByBMcs+G46MD7eYARxHwTzUWicsYPR/WpKF36+L MJC7sU8S1WzrTR3Qi4jnR4YBAPDg= X-Google-Smtp-Source: AGHT+IElW4ibTzf6AiTFdtHkYoakn0Z48MNe9yl+uHXnvWBSECBk/YiCjPABOlb/gwJvZ2SVEz+DpOFn595VvzdT5V8= X-Received: by 2002:a05:6830:9c3:b0:710:edf4:a6f8 with SMTP id 46e09a7af769-71a1c2088b5mr13425102a34.9.1731380736580; Mon, 11 Nov 2024 19:05:36 -0800 (PST) MIME-Version: 1.0 References: <9f7f4def-7ea8-4ad3-83e7-a8cc9d18c58a@wi3ck.info> <11ee3489-40ba-45d6-ba6a-7c3def725e84@vondra.me> In-Reply-To: <11ee3489-40ba-45d6-ba6a-7c3def725e84@vondra.me> From: Amit Kapila Date: Tue, 12 Nov 2024 08:35:25 +0530 Message-ID: Subject: Re: Commit Timestamp and LSN Inversion issue To: Tomas Vondra Cc: Andres Freund , "Zhijie Hou (Fujitsu)" , Aleksander Alekseev , pgsql-hackers , shveta malik , Jan Wieck 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, Nov 11, 2024 at 9:05=E2=80=AFPM Tomas Vondra wrot= e: > > On 11/11/24 09:19, Amit Kapila wrote: > > > > I can't think of a solution other than the current proposal where we > > do both the operations (reserve WAL space for commit and adjust > > commit_timestamp, if required) together to resolve LSN<->Timestamp > > invertion issue. Please let us know if you have any other ideas for > > solving this. Even, if we want to change ReserveXLogInsertLocation to > > make the locked portion an atomic operation, we can still do it for > > records other than commit. Now, if we don't have any other solution > > for LSN<->Timestamp inversion issue, changing the entire locked > > portion to atomic will close the door to solving this problem forever > > unless we have some other way to solve it which can make it difficult > > to rely on commit_timestamps for certain scenarios. > > > > I don't know what the solution is, isn't the problem that > > (a) we record both values (LSN and timestamp) during commit > > (b) reading timestamp from system clock can be quite expensive > > It seems to me that if either of those two issues disappeared, we > wouldn't have such an issue. > > For example, imagine getting a timestamp is super cheap - just reading > and updating a simple counter from shmem, just like we do for the LSN. > Wouldn't that solve the problem? > Yeah, this is exactly what I thought. > For example, let's say XLogCtlInsert has two fields > > int64 CommitTimestamp; > > and that ReserveXLogInsertLocation() also does this for each commit: > > commit_timestamp =3D Insert->commit_timestamp++; > > while holding the insertpos_lock. Now we have the LSN and timestamp > perfectly correlated. > Right, and the patch sent by Hou-San [1] (based on the original patch by Jan) is somewhat on these lines. The idea you have shared or implemented by the patch is a logical clock stored in shared memory. So, what the patch does is that if the time recorded by the current commit record is lesser than or equal to the logical clock (which means after we record time in the commit code path and before we reserve the LSN, there happens a concurrent transaction), we shall increment the value of logical clock by one and use that as commit time. So, in most cases, we need to perform one additional "if check" and "an assignment" under spinlock, and that too only for the commit record. In some cases, when inversion happens, there will be "one increment" and "two assignments." > Of course, this timestamp would be completely > detached from "normal" timestamps, it'd just be a sequence. But we could > also once in a while "set" the timestamp to GetCurrentTimestamp(), or > something like that. For example, there could be a "ticker" process, > doing that every 1000ms, or 100ms, or whatever we think is enough. > > Or maybe it could be driven by the commit itself, say when some timeout > expires, we assign too many items since the last commit, ... > If we follow the patch, we don't need this additional stuff. Isn't what is proposed [1] quite similar to your idea? If so, we can save this extra maintenance of commit timestamps. [1] - https://www.postgresql.org/message-id/OS0PR01MB57162A227EC357482FEB44= 70945D2%40OS0PR01MB5716.jpnprd01.prod.outlook.com With Regards, Amit Kapila.