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 1snXcc-004Imo-SO for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Sep 2024 06:11:36 +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 1snXcc-006tLg-Eq for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Sep 2024 06:11:34 +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 1snXcb-006tKH-Vq for pgsql-hackers@lists.postgresql.org; Mon, 09 Sep 2024 06:11:34 +0000 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1snXcU-000Fik-F5 for pgsql-hackers@postgresql.org; Mon, 09 Sep 2024 06:11:33 +0000 Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2f7657f9f62so6728341fa.3 for ; Sun, 08 Sep 2024 23:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725862286; x=1726467086; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=GtCCEKryS3ewffF0hHmlPVtwWdwzBZt6wua4ovpHt+A=; b=eoACHVEvKDwzTUQpKbcWjldBm/iY+YYaqtZ1CL8+bbkzJi24EkwRA28Gk+XC2fa8Fc Gh6ZneIwDvOYB4q1438Bls4hWPYe/xsjjXmWaUgv1neVATEYHSM0CMxlKATGLrZaUF/g 56ZQzuaeopMJ1SY9XlWHYMKWGiQxRr8zIfGEnNE9HXPzvuG9LQwXJUhL+PZbmbV2BPdB bTn8DDkYxWk3OOAXHGdX3EgjQjfkb/OT3UxENboK2Q3MhukkTi47QzybX+KqhEhvxYXK AaBPFe8wG/NNXGCn6tgGsNabzvkDHm1ibPKumH2bRJb5yJK9dbrDU34gxVV1mQeQvFsS nNmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725862286; x=1726467086; h=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=GtCCEKryS3ewffF0hHmlPVtwWdwzBZt6wua4ovpHt+A=; b=A602rK+UDoiSgh2fC1YW1AzOYkNYnozcFG7o1owwnmy6WuuP9yxZgve6CJOenJeJik Mlm7rbOCDG/wfZb+tFJZVN2JpJDY8sAC4Rq4nLbA2gdvVTUthBdUvrLLrKCdf3ZWcWGZ z+LyWaQmrsGy4X8j42dZG1o5vYbG7sNN/l+cPWTvVDc1MYZsthyv9a7DmMUxFIYa4h/a GQ1+uqQeFQpaEwYF1tPufnK0TtfGTykLoFxZjbbbO9qOVv7n7/lnIY9Uo+ujA2QOTRyI To7gRipNlzeZ6AqxZ4wxpyJ+4uKD/YAeL750PPpNfzv8wyxQNTR0oKcSLAyCOql8Ufsc n7AQ== X-Gm-Message-State: AOJu0Yy4S/etuFZk0AypUjZ/JnioGfi2o3sibJPUHsyueC9eUHB/4xie fytrU6NYLTauNxJDG0ULYdzF/PrUkj8bNWv03TuagBuLk6hMVCXkD+AHbT2d6JIhUsnCVo6By4y zV9TqyWrj3Z6asmFjApiiligzIQ== X-Google-Smtp-Source: AGHT+IHWMYA92wc2ETnGLii2QFVgzlX5StFT3LkR7vRbQiExzsaw5rioKxbDGCPWU6B50QgDXXWWry2e9zq/uc1JzgM= X-Received: by 2002:a05:6512:b1e:b0:52e:7f09:aaae with SMTP id 2adb3069b0e04-536587c51c9mr5193056e87.27.1725862284686; Sun, 08 Sep 2024 23:11:24 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Nisha Moond Date: Mon, 9 Sep 2024 11:41:13 +0530 Message-ID: Subject: Re: Commit Timestamp and LSN Inversion issue To: shveta malik Cc: pgsql-hackers , Amit Kapila , "Zhijie Hou (Fujitsu)" , tomas@vondra.me Content-Type: multipart/mixed; boundary="0000000000006937bc0621a9a05c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006937bc0621a9a05c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Sep 4, 2024 at 12:23=E2=80=AFPM shveta malik wrote: > > Hello hackers, > (Cc people involved in the earlier discussion) > > I would like to discuss the $Subject. > > While discussing Logical Replication's Conflict Detection and > Resolution (CDR) design in [1] , it came to our notice that the > commit LSN and timestamp may not correlate perfectly i.e. commits may > happen with LSN1 < LSN2 but with Ts1 > Ts2. This issue may arise > because, during the commit process, the timestamp (xactStopTimestamp) > is captured slightly earlier than when space is reserved in the WAL. > > ~~ > > Reproducibility of conflict-resolution problem due to the timestamp inve= rsion > ------------------------------------------------ > It was suggested that timestamp inversion *may* impact the time-based > resolutions such as last_update_wins (targeted to be implemented in > [1]) as we may end up making wrong decisions if timestamps and LSNs > are not correctly ordered. And thus we tried some tests but failed to > find any practical scenario where it could be a problem. > > Basically, the proposed conflict resolution is a row-level resolution, > and to cause the row value to be inconsistent, we need to modify the > same row in concurrent transactions and commit the changes > concurrently. But this doesn't seem possible because concurrent > updates on the same row are disallowed (e.g., the later update will be > blocked due to the row lock). See [2] for the details. > > We tried to give some thoughts on multi table cases as well e.g., > update table A with foreign key and update the table B that table A > refers to. But update on table A will block the update on table B as > well, so we could not reproduce data-divergence due to the > LSN/timestamp mismatch issue there. > > ~~ > > Idea proposed to fix the timestamp inversion issue > ------------------------------------------------ > There was a suggestion in [3] to acquire the timestamp while reserving > the space (because that happens in LSN order). The clock would need to > be monotonic (easy enough with CLOCK_MONOTONIC), but also cheap. The > main problem why it's being done outside the critical section, because > gettimeofday() may be quite expensive. There's a concept of hybrid > clock, combining "time" and logical counter, which might be useful > independently of CDR. > > On further analyzing this idea, we found that CLOCK_MONOTONIC can be > accepted only by clock_gettime() which has more precision than > gettimeofday() and thus is equally or more expensive theoretically (we > plan to test it and post the results). It does not look like a good > idea to call any of these when holding spinlock to reserve the wal > position. As for the suggested solution "hybrid clock", it might not > help here because the logical counter is only used to order the > transactions with the same timestamp. The problem here is how to get > the timestamp along with wal position > reservation(ReserveXLogInsertLocation). > Here are the tests done to compare clock_gettime() and gettimeofday() performance. Machine details : Intel(R) Xeon(R) CPU E7-4890 v2 @ 2.80GHz CPU(s): 120; 800GB RAM Three functions were tested across three different call volumes (1 million, 100 million, and 1 billion): 1) clock_gettime() with CLOCK_REALTIME 2) clock_gettime() with CLOCK_MONOTONIC 3) gettimeofday() --> clock_gettime() with CLOCK_MONOTONIC sometimes shows slightly better performance, but not consistently. The difference in time taken by all three functions is minimal, with averages varying by no more than ~2.5%. Overall, the performance between CLOCK_MONOTONIC and gettimeofday() is essentially the same. Below are the test results - (each test was run twice for consistency) 1) For 1 million calls: 1a) clock_gettime() with CLOCK_REALTIME: - Run 1: 0.01770 seconds, Run 2: 0.01772 seconds, Average: 0.01771 seco= nds. 1b) clock_gettime() with CLOCK_MONOTONIC: - Run 1: 0.01753 seconds, Run 2: 0.01748 seconds, Average: 0.01750 seco= nds. 1c) gettimeofday(): - Run 1: 0.01742 seconds, Run 2: 0.01777 seconds, Average: 0.01760 seco= nds. 2) For 100 million calls: 2a) clock_gettime() with CLOCK_REALTIME: - Run 1: 1.76649 seconds, Run 2: 1.76602 seconds, Average: 1.76625 seco= nds. 2b) clock_gettime() with CLOCK_MONOTONIC: - Run 1: 1.72768 seconds, Run 2: 1.72988 seconds, Average: 1.72878 seco= nds. 2c) gettimeofday(): - Run 1: 1.72436 seconds, Run 2: 1.72174 seconds, Average: 1.72305 seco= nds. 3) For 1 billion calls: 3a) clock_gettime() with CLOCK_REALTIME: - Run 1: 17.63859 seconds, Run 2: 17.65529 seconds, Average: 17.64694 seconds. 3b) clock_gettime() with CLOCK_MONOTONIC: - Run 1: 17.15109 seconds, Run 2: 17.27406 seconds, Average: 17.21257 seconds. 3c) gettimeofday(): - Run 1: 17.21368 seconds, Run 2: 17.22983 seconds, Average: 17.22175 seconds. ~~~~ Attached the scripts used for tests. -- Thanks, Nisha --0000000000006937bc0621a9a05c Content-Type: application/x-zip-compressed; name="clock_gettime_test.zip" Content-Disposition: attachment; filename="clock_gettime_test.zip" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_m0ulp4i50 UEsDBBQAAAAIAAdVJlmQWhEA4QEAAIAEAAAtAAAAY2xvY2tfZ2V0dGltZV90ZXN0L3Rlc3RfY2xv Y2tfZ2V0dGltZV9tb25vLnNolVNNb9swDL3rV3ANstpNYmfHtUkvxgYMS2tg6HGAoch0IlSWDFnu DkP/+/Rhp7G3AJ0OgiQ+ko+P4uxDuucy3dP2SMgMMo3UIFDIoNHqoGkNRkGNtO00gjkiNKgrpWsq GYKqgAnFnosDGsNrjGLCqIHNBq6/5F+v4X5sLpxvYbA1CSMzLpnoSoRNa0qukuP92ZNDuxfCpYGa chnF8JuAXa3RHTPgAG2DzN6pNktAWd55u1DyEDbnioI2LZaFpFK1AVCqbi/wZGmRKVn2NucilGq2 n9bDejPwcExTeBjksCTAigGMCsFtyokYUHfC8EYEZEu8/xiT7fLse/GQP+ZP+eO3bAkffUFxyOVi Rxy2sL4DDhvPzZ4Wi0GN9wS00vThXt9FIOCHWjMqWCfcn+gVC1VzCV7ToJ4Hj7S2nCMbKDEvTmFY hT711xhu4E3h3Q4WJ7CcoN39nI2SL6jNeW73P/9FYzBvJ8TSs9TJug/daNvhKrp6crUZ+ozSaz8v fWt9jmlvf3FzhIl6tzAXohyxi+bJ52ogGP+UV0vfxeWY1XJKu++YRtNpadtPXomdKD+hqm64CLN4 GlJyYFY2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