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 1twtiY-000nIH-3K for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Mar 2025 02:08:38 +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 1twtiW-00DNca-IL for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Mar 2025 02:08:36 +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 1twtiW-00DNcS-8y for pgsql-hackers@lists.postgresql.org; Tue, 25 Mar 2025 02:08:36 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1twtiT-000zDz-2U for pgsql-hackers@postgresql.org; Tue, 25 Mar 2025 02:08:35 +0000 Received: by mail-ej1-x644.google.com with SMTP id a640c23a62f3a-ac2bb7ca40bso118438766b.3 for ; Mon, 24 Mar 2025 19:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742868513; x=1743473313; 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=a7mTp9JyjXpO9q92N4YxktIPteU3RDB+PS4YUsy7H4E=; b=k5Fk9ZfH6FL7EVYSbLLQhc0mFtV2shjF2gayaWdPnCWxVNlL56BfkwU2HX4KeL7lpb qEOZOIOL9M93q84XPyuu+TpDmiP0AZeHhW1N9g301lOO26XtrfygkJu8HCeA1cCmfdaW 1THUHzrmeeTQA7hOOX5hgz1MHzgjtI2lWHmtvkz+fn5BcH3zfU8YWiKw9jE+PPJnzxIn LR8Od7SlxJpkztJlh0/ZfMbivdORSpsfzO3RFa37zEwM3BM21s6mUPv2YGKmDKCRPJqr rpRGW/yozkGcFaOK6Zw9BLT/qYMz1bqendZ9u6CkmM/J7v1XQnKs7JcNbqAWDfud6f2u 9WdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742868513; x=1743473313; 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=a7mTp9JyjXpO9q92N4YxktIPteU3RDB+PS4YUsy7H4E=; b=V/kFduKyigK8SXdr59P93qWDpikOcOBlZH1YlzTgXHb+/QOVqnGy3h+Gu7PowLynR6 R1+5wxjYCvg9JHQtcMHwIOIr4UH9/t801GyvPYcc8sFwd4TQMyb61eupjm8UqO3oyjBz tFS/cVCIVHnrverXeOL/+n0K4fkgBFOCj3y/R9wbhCxT+xVUmEfvX8Mn5dLGoLOrF80j 1H0fpGNJRNC7DZ6vuVJneuAFuF0Vl0DQnY3dCs/Is7c1s3jccWxI604UEiwLpKzfZy9g Yk74VBSIIqTlnNt4NuAizTA/HxmGGGoLUPrRP/bcSzsr1/S0IzVDNrHrsyy+F9tQorQL qDTA== X-Forwarded-Encrypted: i=1; AJvYcCXZC+Ykmqfas/+WaoSgudM8eNM3Ax9MI5fkDgQGv50EoD+b4W5wsLnsV0Ettdtj3CimciNcaGCSQfG7whQE@postgresql.org X-Gm-Message-State: AOJu0YxohGpzN5Wiy3ldr1++e+W49DE7tuus4zNtx/35JI4MHR9DgduB 0aMRLPOMjNiV8W6GYWhLVXAuwkWL3DkUaecAGIPynWLVv6mmgNPrjJGkS2R5AaPE4pwOsIW/18V LEFHzogTLA3ETKuMLbxYIJlAK0Q0q499sWZHAZg== X-Gm-Gg: ASbGnctZbJpNRsVYY1RgQ0i3D8mS2Bda9UluhBzG/DZBLbAMPySKM1rUQWJu9l4Nzmg Yo2zuFqowf14ZfV10CvmNaOcf6MbD9O5RR1ZRaADjKUGC/G2AH6k1PNSEI4vhu52g7os8myUXng oM328tl14Oa38XsW+Nr1HOpPGajt0= X-Google-Smtp-Source: AGHT+IHHst6aWVF2onK5PUTgi/4BG3tH+gsm/nShjjMDTVhV6SuGrmRNUXyJdeSQZpdtA4/0C3nZbcTOVFEniUJ6SEk= X-Received: by 2002:a17:907:6089:b0:ac2:b414:ba2a with SMTP id a640c23a62f3a-ac3f2524e79mr1452234266b.37.1742868512381; Mon, 24 Mar 2025 19:08:32 -0700 (PDT) MIME-Version: 1.0 References: <6ybtypq2v3kvskiqj7izl2rmfrcluilsmbobtpylcnp7moa7vq@2q3cplokvcza> In-Reply-To: From: James Hunter Date: Mon, 24 Mar 2025 19:08:20 -0700 X-Gm-Features: AQ5f1JrejOe4nKeI7H2AsR1359SaMU2FFXWb3pl6336Pv4hcmJqNDtPFFl6lXRk Message-ID: Subject: Re: pg_atomic_compare_exchange_*() and memory barriers To: Andres Freund Cc: Alexander Korotkov , 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 7:21=E2=80=AFAM Andres Freund w= rote: > > FWIW, I am fairly certain that any non-toy algorithm that requires a full > memory barrier instead of just an acquire in case of a CAS failure is cho= ck > full of concurrency bugs. Yeah -- off the top of my head, I can think of only two CAS patterns: (1) retry the CAS until success (in which case the memory semantics of a CAS failure don't matter); or (2) whoever wins the CAS is responsible for doing some work. But, in (2), there's no reason to expect that the "winner" has *completed* the work, so the memory semantics of a CAS failure don't matter, since you need some other way to say that the work has been completed. Barriers are useful for seqlocks [1], which (IIRC) is the same technique PostgreSQL uses for PG_STAT_BEGIN_{read,WRITE}_ACTIVITY. But that's when you check the control (sequence) variable both before and *after* touching the data it protects. James [1] https://en.wikipedia.org/wiki/Seqlock