Replace pg_atomic_flag with pg_atomic_bool

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Replace pg_atomic_flag with pg_atomic_bool
Date: 2026-07-05 22:27:45
Message-ID: bb0ba423-816c-4e21-a40f-b1be13b54c5f@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I've always found the pg_atomic_flag functions counterintuitive. I
remember by now pretty well how the pg_atomic_u32/u64 functions work,
but any time I have to look at the pg_atomic_flag functions, I feel
not-like I'm in a foreign land.

I think that's because I think of pg_atomic_flag as an atomic version of
"bool". I'd assume there to be functions similar to the u32/u64
functions, like pg_atomic_read_bool() and pg_atomic_write_bool(). But
alas, neither of those functions exist. To read the value, you have to
use pg_atomic_unlocked_test_flag(), and remember that when the flag is
set, it returns *false*, which feels completely backwards to me. It
makes more sense with pg_atomic_test_set_flag(), which returns 'false'
if the flag was already set and hence was not set again, but it
nevertheless just feels wrong to me.

I propose that we replace pg_atomic_flag with pg_atomic_bool, which
behaves more like pg_atomic_u32/u64.

We currently only use pg_atomic_bool in two places: in the shared memory
structs of pg_stash_advise and autovacuum. I actually considered just
removing pg_test_flag altogether and replacing those two uses directly
with pg_atomic_u32, but it's still nice to have a distinct boolean type.
It's worth noting that the current usage is not very performance
critical (not that I'd expect the u32 functions to be any slower).

Thomas's patches to switch to standard C <stdatomic.h> at [1] is also
touching this. They keep the the application-facing functions unchanged,
but changes the implementation to use the same atomic-exchange
instructions as for u32/u64. This is complementary and an independent
topic to discuss, although the patches will conflict.

[1]
https://www.postgresql.org/message-id/CA%2BhUKGKFvu3zyvv3aaj5hHs9VtWcjFAmisOwOc7aOZNc5AF3NA%40mail.gmail.com

- Heikki

Attachment Content-Type Size
0001-Replace-pg_atomic_flag-with-pg_atomic_bool.patch text/x-patch 23.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-07-05 23:21:56 Re: pg_dump: use threads for parallel workers on all platforms
Previous Message Noah Misch 2026-07-05 22:21:15 Re: PG19 FK fast path: OOB write and missed FK checks during batched