Commit 6b93fcd1 authored by Andres Freund's avatar Andres Freund

Avoid atomic operation in MarkLocalBufferDirty().

The recent patch to make Pin/UnpinBuffer lockfree in the hot
path (48354581), accidentally used pg_atomic_fetch_or_u32() in
MarkLocalBufferDirty(). Other code operating on local buffers was
careful to only use pg_atomic_read/write_u32 which just read/write from
memory; to avoid unnecessary overhead.

On its own that'd just make MarkLocalBufferDirty() slightly less
efficient, but in addition InitLocalBuffers() doesn't call
pg_atomic_init_u32() - thus the spinlock fallback for the atomic
operations isn't initialized. That in turn caused, as reported by Tom,
buildfarm animal gaur to fail.  As those errors are actually useful
against this type of error, continue to omit - intentionally this time -
initialization of the atomic variable.

In addition, add an explicit note about only using pg_atomic_read/write
on local buffers's state to BufferDesc's description.

Reported-By: Tom Lane
Discussion: 1881.1460431476@sss.pgh.pa.us
parent 95ef43c4
...@@ -294,10 +294,14 @@ MarkLocalBufferDirty(Buffer buffer) ...@@ -294,10 +294,14 @@ MarkLocalBufferDirty(Buffer buffer)
bufHdr = GetLocalBufferDescriptor(bufid); bufHdr = GetLocalBufferDescriptor(bufid);
buf_state = pg_atomic_fetch_or_u32(&bufHdr->state, BM_DIRTY); buf_state = pg_atomic_read_u32(&bufHdr->state);
if (!(buf_state & BM_DIRTY)) if (!(buf_state & BM_DIRTY))
pgBufferUsage.local_blks_dirtied++; pgBufferUsage.local_blks_dirtied++;
buf_state |= BM_DIRTY;
pg_atomic_write_u32(&bufHdr->state, buf_state);
} }
/* /*
...@@ -431,6 +435,13 @@ InitLocalBuffers(void) ...@@ -431,6 +435,13 @@ InitLocalBuffers(void)
* is -1.) * is -1.)
*/ */
buf->buf_id = -i - 2; buf->buf_id = -i - 2;
/*
* Intentionally do not initialize the buffer's atomic variable
* (besides zeroing the underlying memory above). That way we get
* errors on platforms without atomics, if somebody (re-)introduces
* atomic operations for local buffers.
*/
} }
/* Create the lookup hash table */ /* Create the lookup hash table */
......
...@@ -165,8 +165,10 @@ typedef struct buftag ...@@ -165,8 +165,10 @@ typedef struct buftag
* wait_backend_pid and setting flag bit BM_PIN_COUNT_WAITER. At present, * wait_backend_pid and setting flag bit BM_PIN_COUNT_WAITER. At present,
* there can be only one such waiter per buffer. * there can be only one such waiter per buffer.
* *
* We use this same struct for local buffer headers, but the lock fields * We use this same struct for local buffer headers, but the locks are not
* are not used and not all of the flag bits are useful either. * used and not all of the flag bits are useful either. To avoid unnecessary
* overhead, manipulations of the state field should be done without actual
* atomic operations (i.e. only pg_atomic_read/write).
* *
* Be careful to avoid increasing the size of the struct when adding or * Be careful to avoid increasing the size of the struct when adding or
* reordering members. Keeping it below 64 bytes (the most common CPU * reordering members. Keeping it below 64 bytes (the most common CPU
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment