Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5882ca6

Browse files
committed
Call xlc __isync() after, not before, associated compare-and-swap.
Architecture reference material specifies this order, and s_lock.h inline assembly agrees. The former order failed to provide mutual exclusion to lwlock.c and perhaps to other clients. The two xlc buildfarm members, hornet and mandrill, have failed sixteen times with duplicate key errors involving pg_class_oid_index or pg_type_oid_index. Back-patch to 9.5, where commit b64d92f introduced atomics. Reviewed by Andres Freund and Tom Lane.
1 parent 481725c commit 5882ca6

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/bin/pgbench/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/exprparse.c
22
/exprscan.c
33
/pgbench
4+
/tmp_check/

src/bin/pgbench/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ clean distclean:
4040

4141
maintainer-clean: distclean
4242
rm -f exprparse.c exprscan.c
43+
44+
check:
45+
$(prove_check)
46+
47+
installcheck:
48+
$(prove_installcheck)

src/bin/pgbench/t/001_pgbench.pl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use strict;
2+
use warnings;
3+
4+
use PostgresNode;
5+
use TestLib;
6+
use Test::More tests => 3;
7+
8+
# Test concurrent insertion into table with UNIQUE oid column. DDL expects
9+
# GetNewOidWithIndex() to successfully avoid violating uniqueness for indexes
10+
# like pg_class_oid_index and pg_proc_oid_index. This indirectly exercises
11+
# LWLock and spinlock concurrency. This test makes a 5-MiB table.
12+
my $node = get_new_node('main');
13+
$node->init;
14+
$node->start;
15+
$node->psql('postgres',
16+
'CREATE UNLOGGED TABLE oid_tbl () WITH OIDS; '
17+
. 'ALTER TABLE oid_tbl ADD UNIQUE (oid);');
18+
my $script = $node->basedir . '/pgbench_script';
19+
append_to_file($script,
20+
'INSERT INTO oid_tbl SELECT FROM generate_series(1,1000);');
21+
$node->command_like(
22+
[ qw(pgbench --no-vacuum --client=5 --protocol=prepared
23+
--transactions=25 --file), $script ],
24+
qr{processed: 125/125},
25+
'concurrent OID generation');

src/include/port/atomics/generic-xlc.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ static inline bool
4040
pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr,
4141
uint32 *expected, uint32 newval)
4242
{
43+
/*
44+
* XXX: __compare_and_swap is defined to take signed parameters, but that
45+
* shouldn't matter since we don't perform any arithmetic operations.
46+
*/
47+
bool ret = __compare_and_swap((volatile int*)&ptr->value,
48+
(int *)expected, (int)newval);
49+
4350
/*
4451
* xlc's documentation tells us:
4552
* "If __compare_and_swap is used as a locking primitive, insert a call to
4653
* the __isync built-in function at the start of any critical sections."
54+
*
55+
* The critical section begins immediately after __compare_and_swap().
4756
*/
4857
__isync();
4958

50-
/*
51-
* XXX: __compare_and_swap is defined to take signed parameters, but that
52-
* shouldn't matter since we don't perform any arithmetic operations.
53-
*/
54-
return __compare_and_swap((volatile int*)&ptr->value,
55-
(int *)expected, (int)newval);
59+
return ret;
5660
}
5761

5862
#define PG_HAVE_ATOMIC_FETCH_ADD_U32
@@ -69,10 +73,12 @@ static inline bool
6973
pg_atomic_compare_exchange_u64_impl(volatile pg_atomic_uint64 *ptr,
7074
uint64 *expected, uint64 newval)
7175
{
76+
bool ret = __compare_and_swaplp((volatile long*)&ptr->value,
77+
(long *)expected, (long)newval);
78+
7279
__isync();
7380

74-
return __compare_and_swaplp((volatile long*)&ptr->value,
75-
(long *)expected, (long)newval);;
81+
return ret;
7682
}
7783

7884
#define PG_HAVE_ATOMIC_FETCH_ADD_U64

src/tools/msvc/clean.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ if exist src\bin\pg_basebackup\tmp_check rd /s /q src\bin\pg_basebackup\tmp_chec
9494
if exist src\bin\pg_config\tmp_check rd /s /q src\bin\pg_config\tmp_check
9595
if exist src\bin\pg_ctl\tmp_check rd /s /q src\bin\pg_ctl\tmp_check
9696
if exist src\bin\pg_rewind\tmp_check rd /s /q src\bin\pg_rewind\tmp_check
97+
if exist src\bin\pgbench\tmp_check rd /s /q src\bin\pgbench\tmp_check
9798
if exist src\bin\scripts\tmp_check rd /s /q src\bin\scripts\tmp_check
9899

99100
REM Clean up datafiles built with contrib

0 commit comments

Comments
 (0)