• Sources: primary
  • Summary: The post compiles a two-store writer with GCC 14.2 at -O2 and shows the compiler emitting the done flag store before the tuples store, then fixes it with pg_compiler_barrier, an empty inline asm with a memory clobber. It walks through MESI cache coherency, store buffers that let later stores overtake earlier ones on ARM but not x86, and invalidate queues that let readers see stale values. It then maps these to pg_memory_barrier, pg_write_barrier and pg_read_barrier, and notes PostgreSQL 19 raised its requirement from C99 to C11.
  • Why it matters: Code with shared-memory flags that works on x86 can fail on Graviton or Apple Silicon unless it uses the right barrier pair.
  • Background: PostgreSQL runs one process per connection and coordinates them through shared memory, so ordering between processes matters as much as between threads. The C11 memory model lets compilers reorder accesses that a single thread cannot observe, and CPUs with weak memory models, such as ARM, also reorder stores and loads at runtime. A write barrier on the producer must pair with a read barrier on the consumer to guarantee ordering.