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.96) (envelope-from ) id 1x55aN-007cDP-1X for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Sep 2026 18:02:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x55aM-00HHPJ-1R for pgsql-hackers@arkaria.postgresql.org; Fri, 11 Sep 2026 18:02:50 +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.96) (envelope-from ) id 1x55aM-00HHP2-0T for pgsql-hackers@lists.postgresql.org; Fri, 11 Sep 2026 18:02:50 +0000 Received: from mail-yx1-xb132.google.com ([2607:f8b0:4864:20::b132]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x55aK-00000005B5Z-1BFt for pgsql-hackers@lists.postgresql.org; Fri, 11 Sep 2026 18:02:49 +0000 Received: by mail-yx1-xb132.google.com with SMTP id 956f58d0204a3-671323f4bd8so213462d50.1 for ; Fri, 11 Sep 2026 11:02:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789149767; x=1789754567; darn=lists.postgresql.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=JWfm+BZIJzIQM4a1XnMZs3Cwi+KMQciiNzVBs94nNqw=; b=cCZAr2uQJz4QGd3g3CziJhhlcjrpKGPhYbQGed+Pa+YyFaPM1HF1druNbayz+j4Fgu VtmiGAPxmIKYftTiLGKsiJFC1tfaxNae6W6/lb6Crd02skEfEdJ6y5sb//R+uuHOiF7t 1P3njqYnqvVN6RSVw4PQVf4TOnXBYi7QewUXquybFl9bNib/p+v0QB7vZUUrCS2d8w6e zbxEuK/FAUCXER+O60xPLRfaM64oiL78xqa3Hwz9ldrBcITvzQiKwjdGvWOhScSC78yz ot6u5z0uI4EfSS74frKj2aSDuwFwH5Q18/o7WCYeTvavg8mLSq4aDauhWJ/CW33foEG7 KZAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789149767; x=1789754567; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JWfm+BZIJzIQM4a1XnMZs3Cwi+KMQciiNzVBs94nNqw=; b=OvtJBBsF4uHuaWVwh4wjJYiYI8vgU5LMkS16mi2kYYWM+u7U7/nX2Xrr1UGO4qmS0S R6MrOrjLO1oRAbYQkuWjyZC3YoatrNKRlx7drYcVkLDfiOgUKoGFp1jL54xMoZCqktv1 nPLS/2bd2AOGIoErVN9En07/VjTbQBua/Biq3ndKNjDhIk2Qswi3t8Up40iE95Nambhy 3kSNdOrek2mQI/ouDq3iRuVVQCB7gAANGfqyMAnbTA97NtG+RWSuT6txfAwEpgbYVblz AMBj1+WDOlCCxcGyF1daTLqgX0cjsERMM/aXl01aycHXgIApA9GfPxMb4C07FQkIkiFL exUw== X-Gm-Message-State: AFuF++lLjqKBVgvlC+jGUXrXQbodgz6seMmkzgqrMv8BsG/ztxl2FlUJ Szoa+Fg2pMsnegT3u+FwiO4BsJsJdA9IL567JYC01nr5zXt75nUZlOVT X-Gm-Gg: AYBFou1UzMTKhrxaIyVZfOhyV9Uyy+xYCuDfweNtiO3D8TGpz/OOHjKo4wcplpUC0Cf UY/CuWTU6uh910/9hUGH4jH6v3PgPInvSw5FUvrLqxfLTz/hEiZTqegRHWWYv8AdfKCXs3eSb6u 8bHtMI6INhFJplyS6rY3BUvLbIwB1b+cWlhhAfmvvbuupb1E4AS2UmdeX96t9LqxRjk2zpfRxwg i4VC+8M/1ZYmjAceirlg6BriXjO5fW9w42qfO6UKmlVWplg7nRj8IXstFfcv4PLj3kwrSeb0/AH eny/PDRnxZzkk6WBQwZFxqWbdFihLsXo4wyxH03JMzYFQmcEbTClrgdnqqOx+CTkziWpXk6pOgz iV3a4Vk9tkKOoWL0a/iKXGe4EnDm2k/hoRUyIbFcNBPJ/S38di9kVCplQAmRxOnq5lXePAxFZ4F mYeZ0KUUwyK/230l3ULYP7kPBx1kRbgOxg6IFRw1ZMnTaBOkkqhuDn09R4Ki7pXUyevUj5WbZ1H r69jXxHmoDhZOrAW6samEUTBe/FON/LM01XRe83yq9KJAUL7gOa8mgjj1wpbp1z10e2CJ2ehinW YcTNgvaG5as= X-Received: by 2002:a05:690e:5:b0:66f:c1be:84f3 with SMTP id 956f58d0204a3-671247db69fmr1602603d50.97.1789149766995; Fri, 11 Sep 2026 11:02:46 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9120f45ab7csm26293466d6.15.2026.09.11.11.02.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 11:02:46 -0700 (PDT) Date: Fri, 11 Sep 2026 13:02:44 -0500 From: Nathan Bossart To: Yura Sokolov Cc: pgsql-hackers@lists.postgresql.org Subject: Re: convert various variables to atomics Message-ID: References: <207c0bfb-6e06-4358-bb2f-c961915efc36@eisentraut.org> <3856d1cf-53a8-414b-98d9-829d5a455a86@iki.fi> <55e6a615-ec9d-4a57-8f36-7fbd1674da2c@postgrespro.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <55e6a615-ec9d-4a57-8f36-7fbd1674da2c@postgrespro.ru> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Sep 11, 2026 at 05:43:22PM +0300, Yura Sokolov wrote: > Personally, I don't like current implementation of > pg_atomic_read_membarrier_u32 because it writes into shared variable. I think your dislike of the membarrier implementation is misguided. The write is important and helps reduce the cognitive load of reading the code. A spinlock guarantees that whoever takes the lock sees everything the previous holder did before releasing the lock. The membarrier functions keep that guarantee because every access is a read-modify-write, i.e., whoever touches the variable second must read what the first one wrote. Take the following example: /* thread A */ x = 1; z = pg_atomic_read_membarrier_u32(&y); /* thread B */ pg_atomic_write_membarrier_u32(&y, 1); x = 2; Let's say thread A's read of "y" returns 0. That must mean that thread A wrote "x" before thread B did, which is same as what you'd get with a spinlock. If the read was just a plain load behind a barrier, we can't know the order of the writes to "x" on non-TSO architectures. > That is why in [1] (thread [2]) I used explicit pg_memory_barrier before > and pg_read_barrier after reading segP->maxMsgNum. (pg_memory_barrier > writes onto stack - process's private memory, and pg_read_barrier does > nothing on x86_64). My patch is intended to be a straightforward spinlock-to-atomics conversion, so I'd like to keep the membarrier accessors for now. Further optimizations should be handled in their own threads. Two that come to mind are an x86-specific implementation of pg_atomic_read_membarrier_u32() (since it _is_ a TSO architecture), and something like your patch for sinvaladt.c, i.e., using explicit barriers for that code. -- nathan