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 1tqvlO-00CFt1-N0 for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Mar 2025 15:06:54 +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 1tqvlN-002DYt-Eo for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Mar 2025 15:06:53 +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 1tqvlN-002DYU-4o for pgsql-hackers@lists.postgresql.org; Sat, 08 Mar 2025 15:06:53 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tqvlL-001gug-0o for pgsql-hackers@postgresql.org; Sat, 08 Mar 2025 15:06:52 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-abf4802b242so506725966b.1 for ; Sat, 08 Mar 2025 07:06:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741446410; x=1742051210; 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=hvSJ1/UjCC1v/0IpomSEjO9MYF6tYgfr0ZOQ74JPR4M=; b=DDH1+cvnms4i6xr3YkU1h4vXvUO2W4tEpLsOLCfx8SxcTVfJXUoee3YYSf/3ha8Omc KvrtqZmmmI7ciFs5H6znQFU69iJQSIRmUNDahGNfZInwBcWqHBxf2Gy59Eo+oY/vEXRF eAnMaJkLiPUnASP/EdYOX5vOi9ssT+Ny3a1s3NKNx7v/blFGP48S21lN0yw6ve8Rv/kV IkzhnAELzYZZebphhhdhK/thl905sb49iqctu9MRmZu8Yqeqv8rD1rNlRjGu7M6C+Zcs vQW+IDMDAcxpFpvhKX/kcLAGx8qk6GcTOucIrPNd2UEHqy59D1CY3yvFKuqDdgvQLnQx SCHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741446410; x=1742051210; 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=hvSJ1/UjCC1v/0IpomSEjO9MYF6tYgfr0ZOQ74JPR4M=; b=mB+O5CrzqcWPclSqBBZrR/HyPaaj20ZXwsNooffy18nztexlRfHKTkIxuxZOtEdyPa qUIrLdBeHlHLdrtBUunB7TLe1ns5w9/PQ/qoaI0108ThmrAgDNXBC6AvWIaCAz6ShYsz aGmX5s4NgU60CYvI09pLAr40zxBtwk/vxwkyLa3hJ3e8Y0e21Jxv4LU6cgFyVJaB7ff9 QTUg64ymC3GDghTH+nvJzCDKh8aECNHTZjMPmUzaaBFktpwjtiUVlzYrtiXCTyBP+or3 tpWjGrIsroUvz8TH6gPQ30QDi5sOxcrdjU7vz36CzhEh5DBSGjFXA7nkgZEQBYRnEmG/ m07w== X-Gm-Message-State: AOJu0YyyJK1HQRSnEX/7P1ogVN6u6HAml/bliUpm4KGdmuB4F1LVLcFQ joaCKgKmA/NDtj4tufBvY82RrA9ZWq8pB+yNHRWSxtmZlJDT1GaKThriCuiV6sXt0e/XH6Lj8O/ TTJzXBL/N6EZy/hDFLkgTePSOhTL8+RaY X-Gm-Gg: ASbGncuw0tgcYkLRHJpk20teepdr4x7h+D9X3NSwPDzRZL9K4fehGwv6Xw57ePdQews Hy63mDOgDa36xUCy19Ed3o2RDnoUVZBUSiEeS7g1DlnOUo8dPxZTw+CWPwxLmBOVPN7TX5EyGPd 6LoxM0/wNP5HXzRLUY6WNMGe7NoQ== X-Google-Smtp-Source: AGHT+IEUB7A586FcKhpFerzlGsczcb2tY0Sro2mH5WbHCe8dMaR2g02R++rl+sh4tEIcDDRjrM/jLVWvBXz3rVS1vS8= X-Received: by 2002:a17:906:6b8d:b0:ac2:62a6:3538 with SMTP id a640c23a62f3a-ac262a63dd5mr515522866b.52.1741446409419; Sat, 08 Mar 2025 07:06:49 -0800 (PST) MIME-Version: 1.0 References: <2muwyx6a5vojkg7iegknhnkcch3lfxptsxk7icwuh7szkvvu2y@vc3ukkfvnu6i> <6ybtypq2v3kvskiqj7izl2rmfrcluilsmbobtpylcnp7moa7vq@2q3cplokvcza> In-Reply-To: From: Alexander Korotkov Date: Sat, 8 Mar 2025 17:06:38 +0200 X-Gm-Features: AQ5f1Jrn_7M5AiFr_WkM71y_1RZoH8bcQ49e6bfAZh8rQtyxI9f39kDVXkIxXQw Message-ID: Subject: Re: pg_atomic_compare_exchange_*() and memory barriers To: Andres Freund Cc: pgsql-hackers 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 Sat, Mar 8, 2025 at 3:41=E2=80=AFPM Andres Freund w= rote: > > On 2025-03-08 08:02:41 -0500, Andres Freund wrote: > > From the C/C++ standard atomics model it doesn't make sense to say that= a > > failed CAS has release semantics, as there simply isn't a write that co= uld be > > ordered! What their barriers guarantee is ordering between multiple me= mory > > operation, you can't order multiple writes if you don't have multiple > > writes... The synchronization in the C/C++ model is only established b= etween > > accesses of the same variable and there's no write in the case of a fai= led > > CAS, so there's nothing that could establish a release-acquire ordering= . > > > > Unfortunately that model doesn't mesh well with barriers that aren't at= tached > > to read/modify operations. Which is what we ended up with... > > Adding a full barrier to failed CAS would be a rather large overhead, > undesirable in just about any sane algorithm. As a consequence, I think w= hat > we ought to do is to redefine the barrier semantics to only imply an acqu= ire > barrier in case CAS fails. Thank you, I'm good with this solution. Can I leave this on you? I'm not feeling myself strong to word this correctly. ------ Regards, Alexander Korotkov Supabase