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 1sllTq-0012OT-B4 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 08:35:10 +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 1sllTo-006M9D-P5 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Sep 2024 08:35:09 +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 1sllTo-006M8O-E0 for pgsql-hackers@lists.postgresql.org; Wed, 04 Sep 2024 08:35:08 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sllTl-0005jY-F7 for pgsql-hackers@postgresql.org; Wed, 04 Sep 2024 08:35:07 +0000 Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-70f660d2250so277926a34.2 for ; Wed, 04 Sep 2024 01:35:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=timescale.com; s=google; t=1725438903; x=1726043703; 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=w8SRusWfCds/3z6r6Mdb+lYuq1k3vnT/cAxq3f1WUnE=; b=Au8hzBFSYH6DOmpLBy7A0GGtP6gjpTGtj6zcIznPUoKazUUweUZZEZljrAIcp3JzWu ctIC8e8dcF55FULG2gXMexLzlsz+IsMEvnLrYCPqFlbuWUr36jPu+kUQChXIbIvjbmhJ CapykLtxRd3Pl9KkBTrBR4z1Xkbon5lmXISN0kSypj4pJydAR0l/GaMd8iHed81H/kMq FQY92hnytet0IcrPBXMBdzA3W5fA8JsYIfFKaP4wN1JkWipCBeKAUy/95KFk2NCGZP/1 GBm0D/JyGiyM/LxDJ8d8Zq/9lgSlOFwhWGSc6unYDQQbjSnAkaoTED7ijiqFPBEJmJu6 p40w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725438903; x=1726043703; 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=w8SRusWfCds/3z6r6Mdb+lYuq1k3vnT/cAxq3f1WUnE=; b=fESaBRHFoCeqja7mDPqCthlPEdt6D6rlJ9ouu6vMXTp75ITeB7UL/6S6UwJjdwIjdB nOaOArybpGdHFvrhiecHspwoQZUDCvdTvlCDsSZrlbsjnA1TNTvxgMwLOgaM+s00T8xV zqCX+ncc+goj4WF7oyG/N/cxJJRMiGj3MszoODcVR2k1Yg/JEnY5pasANEg3pbRL1Viz k755oSI2kMJYHlzpjRKgHHuO4o4HWUrMfrONhoATE44o6rLgO46ufKyKMRBYXuNjg0c8 YrHLnjq7JTyW8gv+c17gQs7ATGLYRG5GeLtp5iLinxTAILHwUW8IhLJVKS/+AUBNkrqe oszA== X-Gm-Message-State: AOJu0YxBk/Xwaffxp7aN3/TOXuXOYRHUM9cbCFtg4lfOBvZgxptfy5Rf BTlxhSLnGfIPJLUIFPWI/VRScf5SVIK9xIRVzqHgrfv429U4dmU3yzpmA0EtEFHVTVh6euUvB4T x9r26RYq0+BpNFNFIIzG1GHbCY6jgjqatz/5BdC2A7oUQ3YpjcCL/zA== X-Google-Smtp-Source: AGHT+IF98zavS7Qsxsnh2qTKUO2FdtQM8Y3e6WBOrQcT1GB8cYmf18Ptpwg/MH+ZeP2ueTtx6jHturx4W7nlPeVTAtc= X-Received: by 2002:a05:6359:4129:b0:1b1:a666:2bba with SMTP id e5c5f4694b2df-1b603cb4f67mr2369451255d.24.1725438903378; Wed, 04 Sep 2024 01:35:03 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Aleksander Alekseev Date: Wed, 4 Sep 2024 11:34:51 +0300 Message-ID: Subject: Re: Commit Timestamp and LSN Inversion issue To: pgsql-hackers Cc: shveta malik , Amit Kapila , "Zhijie Hou (Fujitsu)" , tomas@vondra.me Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Shveta, > 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. This is the reason why {latest|earliest}_timestamp_wins strategies you are proposing to use for CDR are poor strategies. In practice they work as random_key_wins which is not extremely useful (what it does is basically _deleting_ random data, not solving conflicts). On top of that strategies like this don't take into account checks and constraints the database may have, including high-level constraints that may not be explicitly defined in the DBMS but may exist in the application logic. Unfortunately I can't reference a particular article or white paper on the subject but I know that "last write wins" was widely criticized back in the 2010s when people were building distributed systems on commodity hardware. In this time period I worked on several projects as a backend software engineer and I can assure you that LWW is not something you want. IMO the right approach to the problem would be defining procedures for conflict resolution that may not only semi-randomly choose between two tuples but also implement a user-defined logic. Similarly to INSERT INTO ... ON CONFLICT ... semantics, or similar approaches from long-lived and well-explored distributed system, e.g. Riak. Alternatively / additionally we could support CRDTs in Postgres. -- Best regards, Aleksander Alekseev