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 1t8e8O-005FwK-OH for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2024 11:23: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 1t8e8L-005RXw-QN for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2024 11:23:34 +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 1t8e8L-005RXn-GV for pgsql-hackers@lists.postgresql.org; Wed, 06 Nov 2024 11:23:34 +0000 Received: from mail-oo1-xc35.google.com ([2607:f8b0:4864:20::c35]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t8e8J-000SZk-6I for pgsql-hackers@postgresql.org; Wed, 06 Nov 2024 11:23:32 +0000 Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-5ec6bdd9923so3236997eaf.0 for ; Wed, 06 Nov 2024 03:23:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730892210; x=1731497010; 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=WCyB5+/pErb2Mt7j9IapKL6m7r1HkfClNYj4roWcyNg=; b=fxcfH5EYapEse6Y95onD3bfDMGLATrjPJAOdatUP5M5ibTge7EPssvaHO0JblK1Tie JdmAHil+ZU31r+FO6d0GnVeG3lCKONeDDEVcDnSZETKsoaTNqVpTsNmKaDI/ufwLsV+r 5obDpZNALgcy0IPefQMYp+49CbIvzVswsS7wI1VVS1Z9kW65WtWYTpN4esFsEaaefaNj GMZTAxw/E2VJVOMbZf8akpdUQWS6i5vrp5s5siiSiHehRyZ5Oe+AgD9FxDSpo4SdyaCR hnQLedfIeKaKCL7e1wLyTU2ROLvvo0rGsla3xiYr06puBHM8cBnJR9WEjZ2LPAAkIZGQ v3EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730892210; x=1731497010; 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=WCyB5+/pErb2Mt7j9IapKL6m7r1HkfClNYj4roWcyNg=; b=KRRS8a0+ZX4ygDqxeA5qKXwKL+38toQdIqYxyvDni8HoQfOWrMHK3Ja1IZJih3el4F KTg6GwwHP31/SUpRl9N8F3ffWwTpfeesotgBFFeQAbnsJ0KR18iPBs+Fk3TPG7OLtVpN A2JeHK6GxA/rAWLJCAKNFsT4t/6CdFmYG5EqGssVvT4/eNlZBX6YTVKekbBMoMKeHmr9 acBilPGv9doUoc7Y4fLX97/jIv4RqXl1aRbGtlH25qeL6IpgdaWDHqHQL+phZBUugH/D raaTedjnvU0glIfBqY5LrTPMadkjAG0cgPh22RA9JzCSsEiYszjvGxKXQ5t7+7CAumV/ DuIA== X-Forwarded-Encrypted: i=1; AJvYcCUmVVqs88cjpgLp937cAc85ZSWM09iCLzFFdBS6JpIYsG9p3BciR02hwJEobTsKg3bQPJCMvQxjpEgiB9OC@postgresql.org X-Gm-Message-State: AOJu0YxIDip/UyKUYz25W2qJK+kDb7vp3DFOpYok6NGHmdwM4xpB1eeK ujpMeSTdcLB1VZYABmUmeQ7jkI1VTAJIZpdIHdAwahB0397OQupgHKsIRcK+pKCn/lFKikf1P70 tMBvG8UX+TVaPFo1pe2L6qqCyX9KwHQZ96hU= X-Google-Smtp-Source: AGHT+IFASa28qqin3fn9AxT6fmOtP0OGro+hsNe2YqznsUmvUEcFdLWkrk8ViD2VB0tS+n7TdjmHY5JIeGrXJn/DMbs= X-Received: by 2002:a4a:ec44:0:b0:5e5:c517:4d88 with SMTP id 006d021491bc7-5ee3b15eea7mr1203673eaf.0.1730892210277; Wed, 06 Nov 2024 03:23:30 -0800 (PST) MIME-Version: 1.0 References: <9f7f4def-7ea8-4ad3-83e7-a8cc9d18c58a@wi3ck.info> In-Reply-To: <9f7f4def-7ea8-4ad3-83e7-a8cc9d18c58a@wi3ck.info> From: Amit Kapila Date: Wed, 6 Nov 2024 16:53:19 +0530 Message-ID: Subject: Re: Commit Timestamp and LSN Inversion issue To: Jan Wieck Cc: Aleksander Alekseev , 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 Tue, Nov 5, 2024 at 7:28=E2=80=AFPM Jan Wieck wrote: > > > > > We can't forget CDR completely as this could only be a potential > > problem in that context. Right now, we don't have any built-in > > resolution strategies, so this can't impact but if this is a problem > > then we need to have a solution for it before considering a solution > > like "last_write_wins" strategy. > > I agree that we can't forget about CDR. This is precisely the problem we > ran into here at pgEdge and why we came up with a solution (attached). > I would like to highlight that we need to solve LSN<->Timestamp inversion issue not only for resolution strategies like 'last_write_wins' but also for conflict detection as well. In particular, while implementing/discussing the patch to detect the update_deleted conflict type, we came across the race conditions [1] where the inversion issue discussed here would lead to removing the required rows before we could detect the conflict. So, +1 to solve this issue. > > Now, instead of discussing LSN<->timestamp inversion issue, you > > started to discuss "last_write_wins" strategy itself which we have > > discussed to some extent in the thread [2]. BTW, we are planning to > > start a separate thread as well just to discuss the clock skew problem > > w.r.t resolution strategies like "last_write_wins" strategy. So, we > > can discuss clock skew in that thread and keep the focus of this > > thread LSN<->timestamp inversion problem. > > Fact is that "last_write_wins" together with some implementation of > Conflict free Replicated Data Types (CRDT) is good enough for many real > world situations. Anything resembling a TPC-B or TPC-C is quite happy > with it. > > The attached solution is minimally invasive because it doesn't move the > timestamp generation (clock_gettime() call) into the critical section of > ReserveXLogInsertLocation() that is protected by a spinlock. Instead it > keeps track of the last commit-ts written to WAL in shared memory and > simply bumps that by one microsecond if the next one is below or equal. > There is one extra condition in that code section plus a function call > by pointer for every WAL record. > I think we avoid calling hook/callback functions after holding a lock (spinlock or LWLock) as the user may do an expensive operation or acquire some other locks in those functions which could lead to deadlocks or impact the concurrency. So, it would be better to directly call an inline function to perform the required operation. This sounds like a good idea to solve this problem. Thanks for sharing the patch. [1] - https://www.postgresql.org/message-id/CAA4eK1LKgkjyNKeW5jEhy1%3DuE8z0= p7Pdae0rohoJP51eJGd%3Dgg%40mail.gmail.com --=20 With Regards, Amit Kapila.