postgresql/src
Andres Freund 93fd6de141 Fix fallback implementation of pg_atomic_write_u32().
I somehow had assumed that in the spinlock (in turn possibly using
semaphores) based fallback atomics implementation 32 bit writes could be
done without a lock. As far as the write goes that's correct, since
postgres supports only platforms with single-copy atomicity for aligned
32bit writes.  But writing without holding the spinlock breaks
read-modify-write operations like pg_atomic_compare_exchange_u32(),
since they'll potentially "miss" a concurrent write, which can't happen
in actual hardware implementations.

In 9.6+ when using the fallback atomics implementation this could lead
to buffer header locks not being properly marked as released, and
potentially some related state corruption.  I don't see a related danger
in 9.5 (earliest release with the API), because pg_atomic_write_u32()
wasn't used in a concurrent manner there.

The state variable of local buffers, before this change, were
manipulated using pg_atomic_write_u32(), to avoid unnecessary
synchronization overhead. As that'd not be the case anymore, introduce
and use pg_atomic_unlocked_write_u32(), which does not correctly
interact with RMW operations.

This bug only caused issues when postgres is compiled on platforms
without atomics support (i.e. no common new platform), or when compiled
with --disable-atomics, which explains why this wasn't noticed in
testing.

Reported-By: Tom Lane
Discussion: <14947.1475690465@sss.pgh.pa.us>
Backpatch: 9.5-, where the atomic operations API was introduced.
2016-10-07 17:00:58 -07:00
..
backend Fix fallback implementation of pg_atomic_write_u32(). 2016-10-07 17:00:58 -07:00
bin Don't allow both --source-server and --source-target args to pg_rewind. 2016-10-07 14:35:45 +03:00
common pgindent run for 9.5 2015-05-23 21:35:49 -04:00
include Fix fallback implementation of pg_atomic_write_u32(). 2016-10-07 17:00:58 -07:00
interfaces Clear OpenSSL error queue after failed X509_STORE_load_locations() call. 2016-10-07 12:53:42 +03:00
makefiles Link $(WIN32RES) into single-file modules only when PGFILEDESC is set. 2015-08-05 20:45:21 -04:00
pl In plpgsql, don't try to convert int2vector or oidvector to expanded array. 2016-08-18 14:48:51 -04:00
port Include <sys/select.h> where needed 2016-09-27 01:05:21 -03:00
template AIX: Test the -qlonglong option before use. 2015-07-17 03:01:29 -04:00
test Don't share SSL_CTX between libpq connections. 2016-10-07 12:22:19 +03:00
timezone Add regression test coverage for non-default timezone abbreviation sets. 2016-09-04 20:02:16 -04:00
tools MSVC: Include pg_recvlogical in client-only install. 2016-09-19 14:27:08 -04:00
tutorial Remove no-longer-required function declarations. 2015-05-24 12:20:23 -04:00
.gitignore Convert cvsignore to gitignore, and add .gitignore for build targets. 2010-09-22 12:57:04 +02:00
bcc32.mak Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00
DEVELOPERS Replace a couple of references to files that no longer exist in the source 2009-05-04 08:08:47 +00:00
Makefile Install TAP test infrastructure so it's available for extension testing. 2016-09-23 15:50:00 -04:00
Makefile.global.in Make TAP test suites to work, when @INC does not contain current dir. 2016-10-07 22:00:24 +03:00
Makefile.shlib Make the AIX case of Makefile.shlib safe for parallel make. 2016-07-23 20:30:40 -04:00
nls-global.mk nls-global.mk: search build dir for source files, too 2016-06-07 18:55:18 -04:00
win32.mak Autoconfiscate selection of 64-bit int type for 64-bit large object API. 2012-10-07 21:52:43 -04:00