32
32
# Prepare nodes
33
33
# ##############################################################################
34
34
35
- $master -> psql(' postgres' ," CREATE EXTENSION postgres_fdw" );
36
- $master -> psql(' postgres' ," CREATE TABLE accounts(id integer primary key, amount integer)" );
37
- $master -> psql(' postgres' ," CREATE TABLE global_transactions(tx_time timestamp)" );
35
+ $master -> safe_psql(' postgres' ,qq[
36
+ CREATE EXTENSION postgres_fdw;
37
+ CREATE TABLE accounts(id integer primary key, amount integer);
38
+ CREATE TABLE global_transactions(tx_time timestamp);
39
+ ] );
38
40
39
41
foreach my $node ($shard1 ,$shard2 )
40
42
{
41
43
my $port =$node -> port;
42
44
my $host =$node -> host;
43
45
44
- $node -> psql(' postgres' ," CREATE TABLE accounts(id integer primary key, amount integer)" );
46
+ $node -> safe_psql(' postgres' ,
47
+ " CREATE TABLE accounts(id integer primary key, amount integer)" );
45
48
46
- $master -> psql(' postgres' ," CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host ', port '$port ')" );
47
- $master -> psql(' postgres' ," CREATE FOREIGN TABLE accounts_fdw_$port () inherits (accounts) server shard_$port options(table_name 'accounts')" );
48
- $master -> psql(' postgres' ," CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas')" );
49
+ $master -> safe_psql(' postgres' ,qq[
50
+ CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host ', port '$port ');
51
+ CREATE FOREIGN TABLE accounts_fdw_$port () inherits (accounts) server shard_$port options(table_name 'accounts');
52
+ CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas');
53
+ ] )
49
54
}
50
55
51
- $shard1 -> psql(' postgres' ," insert into accounts select 2*id-1, 0 from generate_series(1, 10010) as id;" );
52
- $shard1 -> psql(' postgres' ," CREATE TABLE local_transactions(tx_time timestamp)" );
56
+ $shard1 -> safe_psql(' postgres' ,qq[
57
+ insert into accounts select 2*id-1, 0 from generate_series(1, 10010) as id;
58
+ CREATE TABLE local_transactions(tx_time timestamp);
59
+ ] );
53
60
54
- $shard2 -> psql(' postgres' ," insert into accounts select 2*id, 0 from generate_series(1, 10010) as id;" );
55
- $shard2 -> psql(' postgres' ," CREATE TABLE local_transactions(tx_time timestamp)" );
61
+ $shard2 -> safe_psql(' postgres' ,qq[
62
+ insert into accounts select 2*id, 0 from generate_series(1, 10010) as id;
63
+ CREATE TABLE local_transactions(tx_time timestamp);
64
+ ] );
56
65
57
- $master -> pgbench(-n,-c => 20,-t => 30,-f => " $TestLib::log_path /../../t/bank.sql" ,' postgres' );
66
+ # ##############################################################################
67
+ # pgbench scripts
68
+ # ##############################################################################
69
+
70
+ my $bank = File::Temp-> new();
71
+ append_to_file($bank ,q{
72
+ \set id random(1, 20000)
73
+ BEGIN;
74
+ WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = :id RETURNING *)
75
+ INSERT into global_transactions SELECT now() FROM upd;
76
+ UPDATE accounts SET amount = amount + 1 WHERE id = (:id + 1);
77
+ COMMIT;
78
+ } );
79
+
80
+ my $bank1 = File::Temp-> new();
81
+ append_to_file($bank1 ,q{
82
+ \set id random(1, 10000)
83
+ BEGIN;
84
+ WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = (2*:id + 1) RETURNING *)
85
+ INSERT into local_transactions SELECT now() FROM upd;
86
+ UPDATE accounts SET amount = amount + 1 WHERE id = (2*:id + 3);
87
+ COMMIT;
88
+ } );
89
+
90
+ my $bank2 = File::Temp-> new();
91
+ append_to_file($bank2 ,q{
92
+ \set id random(1, 10000)
93
+
94
+ BEGIN;
95
+ WITH upd AS (UPDATE accounts SET amount = amount - 1 WHERE id = 2*:id RETURNING *)
96
+ INSERT into local_transactions SELECT now() FROM upd;
97
+ UPDATE accounts SET amount = amount + 1 WHERE id = (2*:id + 2);
98
+ COMMIT;
99
+ } );
58
100
59
101
# ##############################################################################
60
102
# Helpers
63
105
sub count_and_delete_rows
64
106
{
65
107
my ($node ,$table ) =@_ ;
66
- my ($rc ,$count ,$err );
67
-
68
- ($rc ,$count ,$err ) =$node -> psql(' postgres' ," select count(*) from$table " ,
69
- on_error_die => 1);
70
-
71
- die " count_rows:$err " if ($err ne ' ' );
72
-
73
- $node -> psql(' postgres' ," delete from$table " ,on_error_die => 1);
108
+ my $count ;
74
109
110
+ $count =$node -> safe_psql(' postgres' ," select count(*) from$table " );
111
+ $node -> safe_psql(' postgres' ," delete from$table " );
75
112
diag($node -> name," : completed$count transactions" );
76
-
77
113
return $count ;
78
114
}
79
115
@@ -92,20 +128,20 @@ sub count_and_delete_rows
92
128
93
129
my $pgb_handle ;
94
130
95
- $pgb_handle =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank.sql " ,' postgres' );
131
+ $pgb_handle =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank ,' postgres' );
96
132
97
133
$started =time ();
98
134
$selects = 0;
99
135
while (time () -$started <$seconds )
100
136
{
101
- ( $rc , $ total, $err ) =$master -> psql (' postgres' ," select sum(amount) from accounts" );
137
+ $ total =$master -> safe_psql (' postgres' ," select sum(amount) from accounts" );
102
138
if ( ($total ne $oldtotal )and ($total ne ' ' ) )
103
139
{
104
140
$isolation_errors ++;
105
141
$oldtotal =$total ;
106
142
diag(" Isolation error. Total =$total " );
107
143
}
108
- if (( $err eq ' ' ) and ( $ totalne ' ' ) ) {$selects ++; }
144
+ if ($ totalne ' ' ) {$selects ++; }
109
145
}
110
146
111
147
$master -> pgbench_await($pgb_handle );
@@ -124,25 +160,25 @@ sub count_and_delete_rows
124
160
my ($pgb_handle1 ,$pgb_handle2 ,$pgb_handle3 );
125
161
126
162
# global txses
127
- $pgb_handle1 =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank.sql " ,' postgres' );
163
+ $pgb_handle1 =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank ,' postgres' );
128
164
129
165
# concurrent local
130
- $pgb_handle2 =$shard1 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank1.sql " ,' postgres' );
131
- $pgb_handle3 =$shard2 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank2.sql " ,' postgres' );
166
+ $pgb_handle2 =$shard1 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank1 ,' postgres' );
167
+ $pgb_handle3 =$shard2 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank2 ,' postgres' );
132
168
133
169
$started =time ();
134
170
$selects = 0;
135
171
$oldtotal = 0;
136
172
while (time () -$started <$seconds )
137
173
{
138
- ( $rc , $ total, $err ) =$master -> psql (' postgres' ," select sum(amount) from accounts" );
174
+ $ total =$master -> safe_psql (' postgres' ," select sum(amount) from accounts" );
139
175
if ( ($total ne $oldtotal )and ($total ne ' ' ) )
140
176
{
141
177
$isolation_errors ++;
142
178
$oldtotal =$total ;
143
179
diag(" Isolation error. Total =$total " );
144
180
}
145
- if (( $err eq ' ' ) and ( $ totalne ' ' ) ) {$selects ++; }
181
+ if ($ totalne ' ' ) {$selects ++; }
146
182
}
147
183
148
184
diag(" selects =$selects " );
@@ -168,10 +204,10 @@ sub count_and_delete_rows
168
204
my $stable ;
169
205
170
206
# global txses
171
- $pgb_handle1 =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank.sql " ,' postgres' );
207
+ $pgb_handle1 =$master -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank ,' postgres' );
172
208
# concurrent local
173
- $pgb_handle2 =$shard1 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank1.sql " ,' postgres' );
174
- $pgb_handle3 =$shard2 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => " $TestLib::log_path /../../t/ bank2.sql " ,' postgres' );
209
+ $pgb_handle2 =$shard1 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank1 ,' postgres' );
210
+ $pgb_handle3 =$shard2 -> pgbench_async(-n,-c => 5,-T => $seconds ,-f => $ bank2 ,' postgres' );
175
211
176
212
$selects = 0;
177
213
$started =time ();