pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: aio: Stop using enum bitfields due to bad code generation
2+ messages / 1 participants
[nested] [flat]

* pgsql: aio: Stop using enum bitfields due to bad code generation
@ 2025-08-27 23:16 Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Andres Freund @ 2025-08-27 23:16 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

aio: Stop using enum bitfields due to bad code generation

During an investigation into rather odd aio related errors on macos, observed
by Alexander and Konstantin, we started to wonder if bitfield access is
related to the error. At the moment it looks like it is related, we cannot
reproduce the failures when replacing the bitfields. In addition, the problem
can only be reproduced with some compiler [versions] and not everyone has been
able to reproduce the issue.

The observed problem is that, very rarely, PgAioHandle->{state,target} are in
an inconsistent state, after having been checked to be in a valid state not
long before, triggering an assertion failure. Unfortunately, this could be
caused by wrong compiler code generation or somehow of missing memory barriers
- we don't really know. In theory there should not be any concurrent write
access to the handle in the state the bug is triggered, as the handle was idle
and is just being initialized.

Separately from the bug, we observed that at least gcc and clang generate
rather terrible code for the bitfield access. Even if it's not clear if the
observed assertion failure is actually caused by the bitfield somehow, the bad
code generation alone is sufficient reason to stop using bitfields.

Therefore, replace the enum bitfields with uint8s and instead cast in each
switch statement.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Reported-by: Konstantin Knizhnik <knizhnik@garret.ru>
Discussion: https://postgr.es/m/1500090.1745443021@sss.pgh.pa.us
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/5865150b6d535ecea241e9ad3038564cb7768b9a

Modified Files
--------------
src/backend/storage/aio/aio.c             | 10 +++++-----
src/backend/storage/aio/aio_funcs.c       |  2 +-
src/backend/storage/aio/aio_io.c          |  8 ++++----
src/backend/storage/aio/method_io_uring.c |  2 +-
src/include/storage/aio_internal.h        | 14 ++++++++++----
5 files changed, 21 insertions(+), 15 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* pgsql: aio: Stop using enum bitfields due to bad code generation
@ 2025-08-27 23:16 Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Andres Freund @ 2025-08-27 23:16 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

aio: Stop using enum bitfields due to bad code generation

During an investigation into rather odd aio related errors on macos, observed
by Alexander and Konstantin, we started to wonder if bitfield access is
related to the error. At the moment it looks like it is related, we cannot
reproduce the failures when replacing the bitfields. In addition, the problem
can only be reproduced with some compiler [versions] and not everyone has been
able to reproduce the issue.

The observed problem is that, very rarely, PgAioHandle->{state,target} are in
an inconsistent state, after having been checked to be in a valid state not
long before, triggering an assertion failure. Unfortunately, this could be
caused by wrong compiler code generation or somehow of missing memory barriers
- we don't really know. In theory there should not be any concurrent write
access to the handle in the state the bug is triggered, as the handle was idle
and is just being initialized.

Separately from the bug, we observed that at least gcc and clang generate
rather terrible code for the bitfield access. Even if it's not clear if the
observed assertion failure is actually caused by the bitfield somehow, the bad
code generation alone is sufficient reason to stop using bitfields.

Therefore, replace the enum bitfields with uint8s and instead cast in each
switch statement.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Reported-by: Konstantin Knizhnik <knizhnik@garret.ru>
Discussion: https://postgr.es/m/1500090.1745443021@sss.pgh.pa.us
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ce161b194e84c941c468261fb72f10ed3a99b958

Modified Files
--------------
src/backend/storage/aio/aio.c             | 10 +++++-----
src/backend/storage/aio/aio_funcs.c       |  2 +-
src/backend/storage/aio/aio_io.c          |  8 ++++----
src/backend/storage/aio/method_io_uring.c |  2 +-
src/include/storage/aio_internal.h        | 14 ++++++++++----
5 files changed, 21 insertions(+), 15 deletions(-)



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-08-27 23:16 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-08-27 23:16 pgsql: aio: Stop using enum bitfields due to bad code generation Andres Freund <andres@anarazel.de>
2025-08-27 23:16 pgsql: aio: Stop using enum bitfields due to bad code generation Andres Freund <andres@anarazel.de>

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