pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Andres Freund <andres@anarazel.de>
Cc: Peter Eisentraut <peter@eisentraut.org>
Cc: pgsql-hackers@postgresql.org
Subject: Re: convert various variables to atomics
Date: Tue, 8 Sep 2026 12:13:57 -0500
Message-ID: <aqBCVaif0HXXM7OO@nathan> (raw)
In-Reply-To: <3856d1cf-53a8-414b-98d9-829d5a455a86@iki.fi>
References: <alAJeRRzehDjLaF1@nathan>
	<9d8c317d-d933-46c7-b675-4b9308eaca2b@eisentraut.org>
	<amDAZAVob4DJjUfW@nathan>
	<ycvruij7554tlhw6w7bg4lqtibd52qguo52iojyou7ejdv5jrm@qyyr5f3hdsue>
	<amDGRZxnlmTVjkCe@nathan>
	<amJx4Lwx4nuuExYT@nathan>
	<207c0bfb-6e06-4358-bb2f-c961915efc36@eisentraut.org>
	<hi4rsxwu3ioas5rmuwfnu2gisqb2rd6g2uq56r3pvzk7clismo@oyndzrrylnir>
	<3856d1cf-53a8-414b-98d9-829d5a455a86@iki.fi>

On Thu, Aug 06, 2026 at 11:37:46AM +0300, Heikki Linnakangas wrote:
> On 04/08/2026 17:32, Andres Freund wrote:
>> I'm quite hesitant to do that, at least without a lot more clear cut examples
>> where it actually would make the code better. I think it's rarely a good idea
>> to use signed variables for atomics, because you get undefined behaviour on
>> overflow, there's problems with bit masking, etc. IME most data in atomically
>> modified should actually be unsigned and probably should have been unsigned
>> before the conversion to atomics.
> 
> We could provide pg_atomic_read/write_i32() and
> pg_atomic_compare_exchange_i32() but leave out fetch-and-add and other such
> instructions that have overflow or bit masking issues.

I would do both of these, i.e., first try switching to unsigned, and if
that's not an option for whatever reason, use signed atomics.  If those
existed, I'd use them for v2-0002, which uses an atomic variable for an
enum value, and v2-0005, which uses an atomic variable for a Buffer.
Neither needs to do any sort of atomic arithmetic on the value, so the lack
of fetch-and-add, etc., isn't a problem.

That being said, adding signed atomics just for these small patches seems
rather extreme, so unless we see ourselves using them quite a bit more down
the road, my feeling is that the juice isn't worth the squeeze.  I'm
curious how others feel about this.

-- 
nathan






view thread (22+ messages)  latest in thread

Message-ID: <aqBCVaif0HXXM7OO@nathan>
Permalink:  ../aqBCVaif0HXXM7OO@nathan/
Also on:    postgresql.org/message-id/aqBCVaif0HXXM7OO@nathan

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, hlinnaka@iki.fi, andres@anarazel.de, peter@eisentraut.org
  Subject: Re: convert various variables to atomics
  In-Reply-To: <aqBCVaif0HXXM7OO@nathan>
  References: <alAJeRRzehDjLaF1@nathan> <9d8c317d-d933-46c7-b675-4b9308eaca2b@eisentraut.org> <amDAZAVob4DJjUfW@nathan> <ycvruij7554tlhw6w7bg4lqtibd52qguo52iojyou7ejdv5jrm@qyyr5f3hdsue> <amDGRZxnlmTVjkCe@nathan> <amJx4Lwx4nuuExYT@nathan> <207c0bfb-6e06-4358-bb2f-c961915efc36@eisentraut.org> <hi4rsxwu3ioas5rmuwfnu2gisqb2rd6g2uq56r3pvzk7clismo@oyndzrrylnir> <3856d1cf-53a8-414b-98d9-829d5a455a86@iki.fi> <aqBCVaif0HXXM7OO@nathan>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox