|
1 | 1 | use strict;
|
2 | 2 | use warnings;
|
3 | 3 |
|
| 4 | +use Config; |
4 | 5 | use PostgresNode;
|
5 | 6 | use TestLib;
|
6 |
| -use Test::Moretests=> 27; |
| 7 | +use Test::Moretests=> 15; |
| 8 | + |
| 9 | +use constant |
| 10 | +{ |
| 11 | +READ_COMMITTED=> 0, |
| 12 | +REPEATABLE_READ=> 1, |
| 13 | +SERIALIZABLE=> 2, |
| 14 | +}; |
| 15 | + |
| 16 | +my@isolation_level_sql = ('read committed','repeatable read','serializable'); |
| 17 | +my@isolation_level_abbreviations = ('RC','RR','S'); |
7 | 18 |
|
8 | 19 | # Test concurrent update in table row with different default transaction
|
9 | 20 | # isolation levels.
|
10 | 21 | my$node = get_new_node('main');
|
11 | 22 | $node->init;
|
12 | 23 | $node->start;
|
13 | 24 | $node->safe_psql('postgres',
|
14 |
| -'CREATE UNLOGGED TABLE xy (x integer, y integer);' |
15 |
| - .'INSERT INTO xy VALUES (1, 2);'); |
| 25 | +'CREATE UNLOGGED TABLE xy (x integer, y integer);' |
| 26 | + .'INSERT INTO xy VALUES (1, 2);'); |
16 | 27 |
|
17 | 28 | my$script =$node->basedir .'/pgbench_script';
|
18 | 29 | append_to_file($script,"\\set delta random(-5000, 5000)\n");
|
19 | 30 | append_to_file($script,"UPDATE xy SET y = y + :delta WHERE x = 1;");
|
20 | 31 |
|
21 |
| -# Test transactions with Read committed default isolation level: |
22 |
| -$node->command_like( |
23 |
| -[qw(pgbench --no-vacuum --client=5 --transactions=10 |
24 |
| - --default-isolation-level=RC --file),$script ], |
25 |
| -qr{default transaction isolation level: read committed}, |
26 |
| -'concurrent update: Read Committed: check default isolation level'); |
27 |
| - |
28 |
| -$node->command_like( |
29 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
30 |
| - --default-isolation-level=RC --file),$script ], |
31 |
| -qr{processed: 12000/12000}, |
32 |
| -'concurrent update: Read Committed: check processed transactions'); |
33 |
| - |
34 |
| -$node->command_like( |
35 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
36 |
| - --default-isolation-level=RC --file),$script ], |
37 |
| -qr{serialization failures: 0\(0\.000 %\)}, |
38 |
| -'concurrent update: Read Committed: check serialization failures'); |
39 |
| - |
40 |
| -# Test transactions with Repeatable read default isolation level: |
41 |
| -$node->command_like( |
42 |
| -[qw(pgbench --no-vacuum --client=5 --transactions=10 |
43 |
| - --default-isolation-level=RR --file),$script ], |
44 |
| -qr{default transaction isolation level: repeatable read}, |
45 |
| -'concurrent update: Repeatable Read: check default isolation level'); |
46 |
| - |
47 |
| -$node->command_like( |
48 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
49 |
| - --default-isolation-level=RR --file),$script ], |
50 |
| -qr{processed: 12000/12000}, |
51 |
| -'concurrent update: Repeatable Read: check processed transactions'); |
52 |
| - |
53 |
| -$node->command_like( |
54 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
55 |
| - --default-isolation-level=RR --file),$script ], |
56 |
| -qr{serialization failures: [1-9]\d*}, |
57 |
| -'concurrent update: Repeatable Read: check serialization failures'); |
58 |
| - |
59 |
| -# Test transactions with Serializable default isolation level: |
60 |
| -$node->command_like( |
61 |
| -[qw(pgbench --no-vacuum --client=5 --transactions=10 |
62 |
| - --default-isolation-level=S --file),$script ], |
63 |
| -qr{default transaction isolation level: serializable}, |
64 |
| -'concurrent update: Serializable: check default isolation level'); |
65 |
| - |
66 |
| -$node->command_like( |
67 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
68 |
| - --default-isolation-level=S --file),$script ], |
69 |
| -qr{processed: 12000/12000}, |
70 |
| -'concurrent update: Serializable: check processed transactions'); |
71 |
| - |
72 |
| -$node->command_like( |
73 |
| -[qw(pgbench --no-vacuum --client=30 --transactions=400 |
74 |
| - --default-isolation-level=S --file),$script ], |
75 |
| -qr{serialization failures: [1-9]\d*}, |
76 |
| -'concurrent update: Serializable: check serialization failures'); |
| 32 | +subtest_pgbench |
| 33 | +{ |
| 34 | +my ($isolation_level) =@_; |
| 35 | + |
| 36 | +my$isolation_level_sql =$isolation_level_sql[$isolation_level]; |
| 37 | +my$isolation_level_abbreviation = |
| 38 | +$isolation_level_abbreviations[$isolation_level]; |
| 39 | + |
| 40 | +local$ENV{PGPORT} =$node->port; |
| 41 | +my ($h_psql,$in_psql,$out_psql); |
| 42 | +my ($h_pgbench,$in_pgbench,$out_pgbench,$stderr); |
| 43 | + |
| 44 | +# Open the psql session and run the parallel transaction: |
| 45 | +print"# Starting psql\n"; |
| 46 | +$h_psql = IPC::Run::start ['psql' ], \$in_psql, \$out_psql; |
| 47 | + |
| 48 | +$in_psql = |
| 49 | +"begin transaction isolation level" .$isolation_level_sql .";\n"; |
| 50 | +print"# Running in psql:" .join("",$in_psql); |
| 51 | +$h_psql->pump()until$out_psql =~/BEGIN/; |
| 52 | + |
| 53 | +$in_psql ="update xy set y = y + 1 where x = 1;\n"; |
| 54 | +print"# Running in psql:" .join("",$in_psql); |
| 55 | +$h_psql->pump()until$out_psql =~/UPDATE 1/; |
| 56 | + |
| 57 | +# Start pgbench: |
| 58 | +my@command = ( |
| 59 | +qw(pgbench --no-vacuum --default-isolation-level), |
| 60 | +$isolation_level_abbreviation, |
| 61 | +"--file", |
| 62 | +$script); |
| 63 | +print"# Running:" .join("",@command) ."\n"; |
| 64 | +$h_pgbench = IPC::Run::start \@command, \$in_pgbench, \$out_pgbench, |
| 65 | + \$stderr; |
| 66 | + |
| 67 | +# Let pgbench run the update command in the transaction: |
| 68 | +sleep 10; |
| 69 | + |
| 70 | +# In psql, commit the transaction and end the session: |
| 71 | +$in_psql ="end;\n"; |
| 72 | +print"# Running in psql:" .join("",$in_psql); |
| 73 | +$h_psql->pump()until$out_psql =~/COMMIT/; |
| 74 | + |
| 75 | +$in_psql ="\\q\n"; |
| 76 | +print"# Running in psql:" .join("",$in_psql); |
| 77 | +$h_psql->pump()whilelength$in_psql; |
| 78 | + |
| 79 | +$h_psql->finish(); |
| 80 | + |
| 81 | +# Get pgbench results |
| 82 | +$h_pgbench->pump()untillength$out_pgbench; |
| 83 | +$h_pgbench->finish(); |
| 84 | + |
| 85 | +# On Windows, the exit status of the process is returned directly as the |
| 86 | +# process's exit code, while on Unix, it's returned in the high bits |
| 87 | +# of the exit code (see WEXITSTATUS macro in the standard <sys/wait.h> |
| 88 | +# header file). IPC::Run's result function always returns exit code >> 8, |
| 89 | +# assuming the Unix convention, which will always return 0 on Windows as |
| 90 | +# long as the process was not terminated by an exception. To work around |
| 91 | +# that, use $h->full_result on Windows instead. |
| 92 | +my$result = |
| 93 | + ($Config{osname}eq"MSWin32") |
| 94 | + ? ($h_pgbench->full_results)[0] |
| 95 | + :$h_pgbench->result(0); |
| 96 | + |
| 97 | +# Check pgbench results |
| 98 | +ok(!$result,"@command exit code 0"); |
| 99 | +is($stderr,'',"@command no stderr"); |
| 100 | + |
| 101 | +like($out_pgbench, |
| 102 | +qr{default transaction isolation level:$isolation_level_sql}, |
| 103 | +"concurrent update:$isolation_level_sql: check default isolation level"); |
| 104 | + |
| 105 | +like($out_pgbench, |
| 106 | +qr{processed: 10/10}, |
| 107 | +"concurrent update:$isolation_level_sql: check processed transactions"); |
| 108 | + |
| 109 | +my$regex = |
| 110 | +($isolation_level == READ_COMMITTED) |
| 111 | + ?qr{serialization failures: 0\(0\.000 %\)} |
| 112 | + :qr{serialization failures: [1-9]\d*\([1-9]\d*\.\d* %\)}; |
| 113 | + |
| 114 | +like($out_pgbench, |
| 115 | +$regex, |
| 116 | +"concurrent update:$isolation_level_sql: check serialization failures"); |
| 117 | +} |
| 118 | + |
| 119 | +test_pgbench(READ_COMMITTED); |
| 120 | +test_pgbench(REPEATABLE_READ); |
| 121 | +test_pgbench(SERIALIZABLE); |