3232# Prepare nodes
3333# ##############################################################################
3434
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+ ] );
3840
3941foreach my $node ($shard1 , $shard2 )
4042{
4143 my $port = $node -> port;
4244 my $host = $node -> host;
4345
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)" );
4548
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+ ] )
4954}
5055
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+ ] );
5360
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+ ] );
5665
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+ } );
58100
59101# ##############################################################################
60102# Helpers
63105sub count_and_delete_rows
64106{
65107 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 ;
74109
110+ $count = $node -> safe_psql(' postgres' ," select count(*) from $table " );
111+ $node -> safe_psql(' postgres' ," delete from $table " );
75112 diag($node -> name, " : completed $count transactions" );
76-
77113 return $count ;
78114}
79115
@@ -92,20 +128,20 @@ sub count_and_delete_rows
92128
93129my $pgb_handle ;
94130
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' );
96132
97133$started = time ();
98134$selects = 0;
99135while (time () - $started < $seconds )
100136{
101- ( $rc , $ total, $err ) = $master -> psql (' postgres' , " select sum(amount) from accounts" );
137+ $ total = $master -> safe_psql (' postgres' , " select sum(amount) from accounts" );
102138 if ( ($total ne $oldtotal ) and ($total ne ' ' ) )
103139 {
104140 $isolation_errors ++;
105141 $oldtotal = $total ;
106142 diag(" Isolation error. Total = $total " );
107143 }
108- if (( $err eq ' ' ) and ( $ total ne ' ' ) ) { $selects ++; }
144+ if ($ total ne ' ' ) { $selects ++; }
109145}
110146
111147$master -> pgbench_await($pgb_handle );
@@ -124,25 +160,25 @@ sub count_and_delete_rows
124160my ($pgb_handle1 , $pgb_handle2 , $pgb_handle3 );
125161
126162# 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' );
128164
129165# 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' );
132168
133169$started = time ();
134170$selects = 0;
135171$oldtotal = 0;
136172while (time () - $started < $seconds )
137173{
138- ( $rc , $ total, $err ) = $master -> psql (' postgres' , " select sum(amount) from accounts" );
174+ $ total = $master -> safe_psql (' postgres' , " select sum(amount) from accounts" );
139175 if ( ($total ne $oldtotal ) and ($total ne ' ' ) )
140176 {
141177 $isolation_errors ++;
142178 $oldtotal = $total ;
143179 diag(" Isolation error. Total = $total " );
144180 }
145- if (( $err eq ' ' ) and ( $ total ne ' ' ) ) { $selects ++; }
181+ if ($ total ne ' ' ) { $selects ++; }
146182}
147183
148184diag(" selects = $selects " );
@@ -168,10 +204,10 @@ sub count_and_delete_rows
168204my $stable ;
169205
170206# 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' );
172208# 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' );
175211
176212$selects = 0;
177213$started = time ();
0 commit comments