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 1tB9BI-0028qP-Sq for pgsql-hackers@arkaria.postgresql.org; Wed, 13 Nov 2024 08:56:56 +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 1tB9BG-00CWwl-7I for pgsql-hackers@arkaria.postgresql.org; Wed, 13 Nov 2024 08:56:54 +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 1tB9BF-00CWwd-Q3 for pgsql-hackers@lists.postgresql.org; Wed, 13 Nov 2024 08:56:54 +0000 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tB9BB-001gxE-V3 for pgsql-hackers@postgresql.org; Wed, 13 Nov 2024 08:56:53 +0000 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3e612437c09so4089486b6e.2 for ; Wed, 13 Nov 2024 00:56:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731488209; x=1732093009; 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=dD/8mgs2RRMBhNDtMV+byBDHttTsqu4mxNElzHwI/2g=; b=fSqY6Ty1NFG8pUPqDRmVe0xep0SIgnB8EDvnGkF6M+WEpjw4JZE796E2mvIuUb3yEi cneojGBg0irMA5bV1Alcj7BIvnTKOVGQuFcd/pSDtpdx4Mnpj2SnsfuVv3P9QC4big6x Kk5ZqKstVp7lV4u4m16/YoSex6hcH4NYuTzF79gDSHSlRcf81aAcFYS1PJSvQXmJ0g0x 5TEoiYt+0qzovPgxmPiwKbHQ4RVyWuUh99dnDdYzuA3ZuKQ/vok0kZboHtG7qPWTRAZd 3JJddJnDy4O9GJJkkozvPjD/LaL2p/n6GXiew5Jl1iqbJdVjubiY4/YYbMEK3qRZ/qZB wo3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731488209; x=1732093009; 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=dD/8mgs2RRMBhNDtMV+byBDHttTsqu4mxNElzHwI/2g=; b=ER5c5hC0YwqSavIFfZH8H4Ck5+770p6fMYrZqT+OcavscU+RJJS8GJJOjq0E7ggGJu 1vVtcNrl0lkm+xa0dr7uyHOwpSdiSs/oqTc8f0YyohFMLrOpgMYKFTLrvynXHwQGgYHp R1bqit2HhuerLwnK0bTM06o0yWDREUUlp5k0v0rL7Xr1Is75KLn20+EF0KGVAr22enWF L/elwA1YRv1UlxmCt59xUCp5rplxwH3rC55Ebl6OU1c9GpNzVFiozVsXR0caL/H0TRRP SgItRJUAr+i0B4BZox8q1ivB4HOZolVGR67Y8B9jnSuAgOVIcruMFk1ySLr+qNJNS49B 4NHw== X-Forwarded-Encrypted: i=1; AJvYcCVG75OXDPkvY6BUOQL7O3ju8IqV5kdzXw6ENt3mdyB8WhT5lcJAtR9IvxAW6wiY/nTabkcmrIG2ANEH474J@postgresql.org X-Gm-Message-State: AOJu0YyXg44YsmcWkL9frtqkEDP0p0oEIRxDP0MtE34L0TD2nR36QxPA 4iUXWG3VB8HXrfKwe/trC+uPwkRQyTiHdqR6rhQPzk3AlU8pGqLA8/e439S93DEeOyAgcRdUMUw deY2BMXbxSbPfOUpSWp7xKRYYwC8= X-Google-Smtp-Source: AGHT+IFyamt2r2jYq9FKYmMlp2vQowVGE5EP2lqCmHHjDzEOXMjfr0Fw9Na9iWeQ68bypyYkQEgGR5PvX0YTzSpckF0= X-Received: by 2002:a05:6808:2212:b0:3e0:c13:9837 with SMTP id 5614622812f47-3e7aae09629mr6354962b6e.37.1731488208762; Wed, 13 Nov 2024 00:56:48 -0800 (PST) MIME-Version: 1.0 References: <9f7f4def-7ea8-4ad3-83e7-a8cc9d18c58a@wi3ck.info> <11ee3489-40ba-45d6-ba6a-7c3def725e84@vondra.me> <2c754a6c-478f-4a9e-9f3f-4ea6d8db5188@vondra.me> In-Reply-To: <2c754a6c-478f-4a9e-9f3f-4ea6d8db5188@vondra.me> From: Amit Kapila Date: Wed, 13 Nov 2024 14:26:37 +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 5:30=E2=80=AFPM Tomas Vondra wrot= e: > > On 11/12/24 04:05, Amit Kapila wrote: > > > > 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." > > > > In a way, yes. Except that each backend first sets the commit timestamp > the usual way too. And then it also has to recalculate the checksum of > the record - which happens under lwlock and not the main spinlock, but > it still seems it might be quite expensive. > > I wonder how often we'd need to do this - it seems to me that for high > throughput systems we might easily get into a situation where we have to > "correct" the timestamp and recalculate the checksum (so calculating it > twice) for most of the commits. > > Would be good to see some numbers from experiments - how often this can > happen, what's the impact on throughput etc. > The initial numbers shared by Kuroda-San [1] didn't show any significant impact due to this. It will likely happen in some cases under stress but shouldn't be often because normally the backend that acquired the timestamp first should reserve the LSN first as well. We can do more tests as well to see the impact but the key point Andres is raising is that we won't be able to convert the operation in ReserveXLogInsertLocation() to use atomics after such a solution. Now, even, if the change is only in the *commit* code path, we may or may not be able to maintain two code paths for reserving LSN, one using atomics and the other (for commit record) using spinlock. I think even if we consider your solution to directly increment the commit_timestamp in spinlock to make it monotonic and then later rectify the commit_ts, will it be any more helpful in making the entire operation atomics? > Ofc, you may argue that this would only affect people with the hook, and > those people may be OK with the impact. And in a way I agree. But I also > understand the concerns expressed by Andres, that adding a hook here may > be problematic / easy to get wrong, and then we'd be the ones holding > the brown bag ... > BTW, the latest patch [2] doesn't have any hook. > >> Of course, this timestamp would be completely > >> detached from "normal" timestamps, it'd just be a sequence. But we cou= ld > >> 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 timeou= t > >> 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. > > > > Not sure. But I think we already have some of the necessary parts in > commit_ts. It'd need some improvements, but it might also be a good > place to adjust the timestamps. The inversion can only happens within a > small group of recent transactions (due to NUM_XLOGINSERT_LOCKS), so it > shouldn't be hard to keep a small buffer for those XIDs. Ofc, subxacts > would complicate stuff, and it'd mean the timestamps written to WAL and > to commit_ts would differ. Not great. > Won't it be difficult to predict the number of transactions that could be impacted in this model? In general, I understand the concern here related to the difficulty in converting the operation in ReserveXLogInsertLocation() to atomics but leaving the LSN<->Timestamp inversion issue open forever also doesn't give any good feeling. I feel any solution to fixup commit_timestamps afterward as we discussed above [3] would be much more complex and difficult to maintain. [1] - https://www.postgresql.org/message-id/TYAPR01MB569222C1312E7A6C3C6353= 9DF5582%40TYAPR01MB5692.jpnprd01.prod.outlook.com [2] - https://www.postgresql.org/message-id/OS0PR01MB57162A227EC357482FEB44= 70945D2%40OS0PR01MB5716.jpnprd01.prod.outlook.com [3] - https://www.postgresql.org/message-id/CAA4eK1%2BOZLh7vA1CQkoq0ba4J_P-= 8JFHnt0a_YC2xfB0t3%2BakA%40mail.gmail.com --=20 With Regards, Amit Kapila.