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 1tAiQ2-00HMeC-Cr for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Nov 2024 04:22:21 +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 1tAiP0-002p4M-0P for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Nov 2024 04:21:18 +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 1tAiOz-002p4E-B3 for pgsql-hackers@lists.postgresql.org; Tue, 12 Nov 2024 04:21:18 +0000 Received: from mail-oo1-xc32.google.com ([2607:f8b0:4864:20::c32]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tAiOx-001QET-9u for pgsql-hackers@postgresql.org; Tue, 12 Nov 2024 04:21:16 +0000 Received: by mail-oo1-xc32.google.com with SMTP id 006d021491bc7-5ee1c3dbeb9so2657574eaf.1 for ; Mon, 11 Nov 2024 20:21:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731385274; x=1731990074; 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=iOxTshcWFhO6BXo0hdFS5LHYuaZ+3ZCKWn010WzXsXc=; b=ExRAwGHmwesCN4QUih0WJSRrWHpVwG+2G2biSll5olf5RAHPdMbbnlOdefnWCdAoCZ y5URc8b4Gmu9e+CRoVf4lFUog7ExuC9W+hQrU6feO+ChswQBdtze11LOLM8m5NbYAuWE ugNsGv908ghkHW36dSK8ouA1evUJZVD3eYTFI17C0J1tKWA/id2WaEOne28POtNwsYt+ xokOUP7LiMbFX5M03j1DRxPRIiJyzIQyZXomyBFV+v3Fg2DRZKBF2EKOuxeIdQuztcXi isIEAmA/u8nteoqhr0eYUKcO0//BHJktf1xmyJAMo/QeyxmdsUBi1THjcSYxXlLsnJGK eSYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731385274; x=1731990074; 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=iOxTshcWFhO6BXo0hdFS5LHYuaZ+3ZCKWn010WzXsXc=; b=Ri84tW7CtX0HB2GPeLkh+YTJAdEz+rmERrnHE5Q7KmHAwspCavbkX15Fx+6k0Y4B/2 zU6iVJrEO3039B9ti1zPckaimUSIC8+6zXCl1NeYK7m1bwXg08IYyj7edMMFXtJGgvjT J60SN/2ckkAKygOlctxA8fhA4Qi8cD0hZN8+IKh7I2DGUSn/WGBKKkLnrnyT9ca2ZLpQ am00wZvFg6zSqwl+XbECA/dXyLVgGmyrE3GFdPB17CznuX53UgZlidCvvaPiDUdBg7Qu o+s2BsOGyQTtMhchELlTyxxiMiACtRfj+eZOQcC+7xxxuCAhFCDIMpDI72xPhQeubp87 mIqw== X-Forwarded-Encrypted: i=1; AJvYcCUuW6JDKU+kOjBdLuQlEFAn5DNgylDtvuhW/FdFfhYyMQl9qwhTXDiJfWzh1AqxBn4FsMbLEU6nkFBqmX2F@postgresql.org X-Gm-Message-State: AOJu0YxtQ6CphQEHwfcXpRsUFasc/E7EEwZUMnVF1ochZCmUgpWCRCxC 4EV8A3fLcAwZ2ywV/pCmadGcBBii0vzvtWlGHH7PrRFipt/JAuMBdJzsmkYdTSMsi219G2OgHWl Lt97j7XL4gTRgdSEmqtIGslZG/rk= X-Google-Smtp-Source: AGHT+IEeMg4ds0FS3brhLfm2928qbTGfTHgqeex1ndFyPUy1cyFSu1QT7dnnVy7tC8rkgYI3zpUH26lHNfoQ6Epc2qY= X-Received: by 2002:a05:6820:4c89:b0:5dc:99d3:d3bc with SMTP id 006d021491bc7-5ee58990659mr8640495eaf.3.1731385274421; Mon, 11 Nov 2024 20:21:14 -0800 (PST) MIME-Version: 1.0 References: <9f7f4def-7ea8-4ad3-83e7-a8cc9d18c58a@wi3ck.info> <11ee3489-40ba-45d6-ba6a-7c3def725e84@vondra.me> In-Reply-To: From: Amit Kapila Date: Tue, 12 Nov 2024 09:51:03 +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 Tue, Nov 12, 2024 at 8:35=E2=80=AFAM Amit Kapila wrote: > > On Mon, Nov 11, 2024 at 9:05=E2=80=AFPM Tomas Vondra wr= ote: > > > > 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." > As the inversion issue can mainly hamper logical replication-based solutions we can do any of this additional work under spinlock only when the current record is a commit record (which the currently proposed patch is already doing) and "wal_level =3D logical" and also can have another option at the subscription level to enable this new code path. I am not sure what is best but just sharing the ideas here. --=20 With Regards, Amit Kapila.