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

Commit 735e472

Browse files
committed
isolation bench
1 parent e0f266a commit 735e472

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

contrib/postgres_fdw/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ DATA = postgres_fdw--1.0.sql
1212

1313
REGRESS = postgres_fdw
1414

15+
EXTRA_INSTALL += contrib/pg_tsdtm
16+
1517
ifdef USE_PGXS
1618
PG_CONFIG = pg_config
1719
PGXS := $(shell $(PG_CONFIG) --pgxs)
@@ -23,3 +25,9 @@ top_builddir = ../..
2325
include $(top_builddir)/src/Makefile.global
2426
include $(top_srcdir)/contrib/contrib-global.mk
2527
endif
28+
29+
# Poor's man overload of already defined target: just copy it
30+
check: submake $(REGRESS_PREP)
31+
$(pg_regress_check) $(REGRESS_OPTS) $(REGRESS)
32+
env DESTDIR='$(abs_top_builddir)'/tmp_install make install
33+
$(prove_check)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use strict;
2+
use warnings;
3+
4+
use PostgresNode;
5+
use TestLib;
6+
use Test::More tests => 1;
7+
8+
my $master = get_new_node("master");
9+
$master->init;
10+
$master->append_conf('postgresql.conf', qq(
11+
max_prepared_transactions = 10
12+
log_checkpoints = true
13+
postgres_fdw.use_tsdtm = on
14+
));
15+
$master->start;
16+
17+
my $shard1 = get_new_node("shard1");
18+
$shard1->init;
19+
$shard1->append_conf('postgresql.conf', qq(
20+
max_prepared_transactions = 10
21+
log_checkpoints = true
22+
shared_preload_libraries = 'pg_tsdtm'
23+
));
24+
$shard1->start;
25+
26+
my $shard2 = get_new_node("shard2");
27+
$shard2->init;
28+
$shard2->append_conf('postgresql.conf', qq(
29+
max_prepared_transactions = 10
30+
log_checkpoints = true
31+
shared_preload_libraries = 'pg_tsdtm'
32+
));
33+
$shard2->start;
34+
35+
###############################################################################
36+
37+
$master->psql('postgres', "CREATE EXTENSION postgres_fdw");
38+
$master->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
39+
40+
foreach my $node ($shard1, $shard2)
41+
{
42+
my $port = $node->port;
43+
my $host = $node->host;
44+
45+
$node->psql('postgres', "CREATE EXTENSION pg_tsdtm");
46+
$node->psql('postgres', "CREATE TABLE accounts(id integer primary key, amount integer)");
47+
48+
$master->psql('postgres', "CREATE SERVER shard_$port FOREIGN DATA WRAPPER postgres_fdw options(dbname 'postgres', host '$host', port '$port')");
49+
$master->psql('postgres', "CREATE FOREIGN TABLE accounts_fdw_$port() inherits (accounts) server shard_$port options(table_name 'accounts')");
50+
$master->psql('postgres', "CREATE USER MAPPING for stas SERVER shard_$port options (user 'stas')");
51+
52+
diag("done $host $port");
53+
}
54+
55+
$shard1->psql('postgres', "insert into accounts select 2*id-1, 0 from generate_series(1, 1000) as id;");
56+
$shard2->psql('postgres', "insert into accounts select 2*id, 0 from generate_series(1, 1000) as id;");
57+
58+
###############################################################################
59+
60+
my ($err, $rc);
61+
my $seconds = 30;
62+
my $total = 0;
63+
my $oldtotal = 0;
64+
my $isolation_error = 0;
65+
66+
my $pgb_handle = $master->pgbench_async(-n, -c => 1, -T => $seconds, -f => "$TestLib::log_path/../../t/bank.pgb", 'postgres' );
67+
68+
my $started = time();
69+
while (time() - $started < $seconds)
70+
{
71+
($rc, $total, $err) = $master->psql('postgres', "select sum(amount) from accounts");
72+
if ($oldtotal != $total)
73+
{
74+
$isolation_error = 1;
75+
$oldtotal = $total;
76+
diag("Isolation error. Total = $total");
77+
}
78+
}
79+
80+
$master->pgbench_await($pgb_handle);
81+
82+
is($isolation_error, 0, 'check proper isolation');
83+
84+
$master->stop;
85+
$shard1->stop;
86+
$shard2->stop;

contrib/postgres_fdw/t/bank.pgb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\set id random(1, 2000)
2+
3+
BEGIN;
4+
UPDATE accounts SET amount = amount - 1 WHERE id = :id;
5+
UPDATE accounts SET amount = amount + 1 WHERE id = (:id + 1);
6+
COMMIT;

src/test/perl/PostgresNode.pm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,37 @@ sub pg_recvlogical_upto
16611661
}
16621662
}
16631663

1664+
sub pgbench()
1665+
{
1666+
my ($self, $node, @args) = @_;
1667+
my $pgbench_handle = $self->pgbench_async($node, @args);
1668+
$self->pgbench_await($pgbench_handle);
1669+
}
1670+
1671+
sub pgbench_async()
1672+
{
1673+
my ($self, @args) = @_;
1674+
1675+
my ($in, $out, $err, $rc);
1676+
$in = '';
1677+
$out = '';
1678+
1679+
my @pgbench_command = (
1680+
'pgbench',
1681+
-h => $self->host,
1682+
-p => $self->port,
1683+
@args
1684+
);
1685+
my $handle = IPC::Run::start(\@pgbench_command, $in, $out);
1686+
return $handle;
1687+
}
1688+
1689+
sub pgbench_await()
1690+
{
1691+
my ($self, $pgbench_handle) = @_;
1692+
IPC::Run::finish($pgbench_handle) || BAIL_OUT("pgbench exited with $?");
1693+
}
1694+
16641695
=pod
16651696
16661697
=back

0 commit comments

Comments
 (0)