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 1sloHH-001KkE-Q5 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 11:34:24 +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 1sloHE-008Rwi-QT for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 11:34:21 +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 1sloHE-008RwZ-Fs for pgsql-hackers@lists.postgresql.org; Wed, 04 Sep 2024 11:34:20 +0000 Received: from mail-oo1-xc2d.google.com ([2607:f8b0:4864:20::c2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sloHC-00072F-Ty for pgsql-hackers@postgresql.org; Wed, 04 Sep 2024 11:34:20 +0000 Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5de8ca99d15so3839816eaf.0 for ; Wed, 04 Sep 2024 04:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725449656; x=1726054456; 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=RT2UEziPaBe75hAWUwv3vwkORUNdhrc19n3CFl//zx8=; b=HC03Bdmsqd+rmcgiiCz1EgC2/YSNSsTANU2nmpLGl+kucnkhaf6D8hsP86praacTBI sXJnTRntjKtb53VJThsdqDV2c/9ZZ8IgO6AOhS23fDi9ZtGMP7piTJnnCstkKh4Rgc8B BCb0hHlj3dGX8JHD2bWe8sjMQbQzWfRclPPzxhfpd5xTXoNDxjP4qn/+FhINwZhp0Zyx 3fXhWRtfy37ExF8gMZ+nkUD0yLoh7z1lmV8NB4waoXDs+wqxFUdDKRVKHwqAizKmZbDs sC/iCvvU1NNQWrT/6OxkZ+7y5IKsJ2XinRluHJxydFKZ3tafySNd1o0gfDTwKBEabsJ8 ikQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725449656; x=1726054456; 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=RT2UEziPaBe75hAWUwv3vwkORUNdhrc19n3CFl//zx8=; b=VQf6HJhQ6NFMGpxSOaF4EpNjiqE68yEWFgxX3lo/2fl0nspCEp+yxWhpE/NODcpX/v HMlU5R+4lhRRZZiwpOsh0j1OKJa0L41IGpVy6y+5+hqolEHzlF/20o5CXKrCe4vMFAqs /5O1tV7mV5wDXsO9LLUdPjmTmyPM6GWlPc4N6lIcMKPNSPIK5HETdii+ZVun3i/XQ8Zb SuM/+94zEs9DBpVRcHsCkKYZuy5YlWxhhYfQ4A4Re2hfezG5d/202MfkaEwMtyqQ4YM4 Ytlmy0ZbdXfaKiH0g31NU74ndR8ekp7FghwSTTLEDCy/F4PvOetQDSd9EzLPugvVd3Fy q97Q== X-Gm-Message-State: AOJu0YwHs0mXmvT+5mvBd48LCKt66g8RjPC/K9wuQpYXw+O5fd4WrZeb 48nFVYAgZeyRTpUr+nmizTrXJ1lq3MrhFOO+Zy5zoffr4x4POLQaj4XT23FBH4sd4giGIo9UMdm EjW72uegDxxH1zLDe5tpxAR7dhgg= X-Google-Smtp-Source: AGHT+IHB/thAkGQc9D8z/ox27zs0plAyC663pKxbVTVE3lvGEpgsovmPkhHnzHwr6dvMuzmjriZ9jA1usibXhuy3tqA= X-Received: by 2002:a05:6871:5821:b0:277:db20:f9ba with SMTP id 586e51a60fabf-277fffc2416mr9885207fac.9.1725449655960; Wed, 04 Sep 2024 04:34:15 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Amit Kapila Date: Wed, 4 Sep 2024 17:04:05 +0530 Message-ID: Subject: Re: Commit Timestamp and LSN Inversion issue To: Aleksander Alekseev Cc: pgsql-hackers , shveta malik , "Zhijie Hou (Fujitsu)" , tomas@vondra.me 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 Wed, Sep 4, 2024 at 2:05=E2=80=AFPM Aleksander Alekseev wrote: > > > 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. > > [...] > > 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. > > I don't think you can rely on a system clock for conflict resolution. > In a corner case a DBA can move the clock forward or backward between > recordings of Ts1 and Ts2. On top of that there is no guarantee that > 2+ servers have synchronised clocks. It seems to me that what you are > proposing will just hide the problem instead of solving it in the > general case. > It is possible that we can't rely on the system clock for conflict resolution but that is not the specific point of this thread. As mentioned in the subject of this thread, the problem is "Commit Timestamp and LSN Inversion issue". The LSN value and timestamp for a commit are not generated atomically, so two different transactions can have them in different order. Your point as far as I can understand is that in the first place, it is not a good idea to have a strategy like "last_write_wins" which relies on the system clock. So, even if LSN->Timestamp ordering has any problem, it won't matter to us. Now, we can discuss whether "last_write_wins" is a poor strategy or not but if possible, for the sake of the point of this thread, let's assume that users using the resolution feature ("last_write_wins") ensure that clocks are synced or they won't enable this feature and then see if we can think of any problem w.r.t the current code. --=20 With Regards, Amit Kapila.