use strict;
 use warnings;
 use Data::Dumper;
-use Test::More tests => 53;
+use Test::More tests => 52;
 use lib 't','.';
 use CP_Testing;
 
 
 $dbh = $cp->test_database_handle();
 
+## Remove any old fake schema
+$cp->drop_schema_if_exists();
+
 my $S = q{Action 'backends'};
 my $label = 'POSTGRES_BACKENDS';
 
 like ($cp->run("-c -$num"), qr{^$label CRITICAL}, $t);
 
 $t=qq{$S works when no items caught by pg_stat_activity};
-
-$cp->drop_schema_if_exists();
-$cp->create_fake_pg_table('pg_stat_activity');
-like ($cp->run(), qr{^$label OK: .+No connections}, $t);
-
-$t=qq{$S returns correct MRTG output when no rows};
-is ($cp->run('--output=MRTG'), qq{0\n0\n\nDB=postgres Max connections=10\n}, $t);
+$cp->create_fake_pg_table('pg_stat_activity','', ' WHERE procpid = pg_backend_pid()');
+like ($cp->run(), qr{^$label OK: .+1 of }, $t);
 
 $t=qq{$S fails as expected when max_connections cannot be determined};
 $cp->create_fake_pg_table('pg_settings');
 like ($cp->run(), qr{^$label UNKNOWN: .+max_connections}, $t);
-$cp->drop_schema_if_exists();
+$cp->remove_fake_pg_table('pg_settings');
 
 $t=qq{$S works when include forces no matches};
 like ($cp->run('--include=foobar'), qr{^$label OK: .+No connections}, $t);
     $goodver or skip 'Cannot test backends completely with older versions of Postgres', 2;
 
     $t=qq{$S returns correct MRTG output when rows found};
-    is ($cp->run('--output=MRTG'), qq{3\n0\n\nDB=postgres Max connections=10\n}, $t);
+    like ($cp->run('--output=MRTG'), qr{^1\n0\n\nDB=postgres}, $t);
 
     $t=qq{$S works when include has valid database};
-    like ($cp->run('--include=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--include=postgres'), qr{^$label OK: .+1 of 10}, $t);
 }
 
 $t=qq{$S works when exclude forces no matches};
     $goodver or skip 'Cannot test backends completely with older versions of Postgres', 4;
 
     $t=qq{$S works when exclude excludes nothing};
-    like ($cp->run('--exclude=foobar'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--exclude=foobar'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S works when include and exclude make a match};
-    like ($cp->run('--exclude=postgres --include=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--exclude=postgres --include=postgres'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S works when include and exclude make a match};
-    like ($cp->run('--include=postgres --exclude=postgres'), qr{^$label OK: .+3 of 10}, $t);
+    like ($cp->run('--include=postgres --exclude=postgres'), qr{^$label OK: .+1 of 10}, $t);
 
     $t=qq{$S returned correct performance data with include};
-    like ($cp->run('--include=postgres'), qr{ \| time=\d\.\d\ds postgres=3;9;9;0;10}, $t);
+    like ($cp->run('--include=postgres'), qr{ \| time=\d\.\d\ds postgres=1}, $t);
 }
 
 my %dbh;
 $t=qq{$S returns critical when too many clients to even connect};
 like ($cp->run('-w -10'), qr{^$label CRITICAL: .+too many connections}, $t);
 
-$cp->drop_schema_if_exists();
-
 exit;
 
     my $self = shift;
     my $name = shift || die;
     my $args = shift || '';
+       my $where = shift || '';
     my $dbh = $self->{dbh} || die;
     my $dbuser = $self->{testuser} || die;
     if ($self->schema_exists($dbh,$fakeschema)) {
         $funcargs = qq{($funcargs)};
     }
 
-    $dbh->do("CREATE TABLE $fakeschema.$name AS SELECT * FROM $name$funcargs LIMIT 0");
+       my $SQL = "CREATE TABLE $fakeschema.$name AS SELECT * FROM $name$funcargs$where ";
+       $SQL .= $where ? 'LIMIT 1' : 'LIMIT 0';
+
+    $dbh->do($SQL);
 
     if ($args) {
         $self->drop_function_if_exists($fakeschema,$name,$args);
 } ## end of create_fake_pg_table
 
 
+sub remove_fake_pg_table {
+
+    my $self = shift;
+    my $name = shift || die;
+    my $dbh = $self->{dbh} || die;
+    my $dbuser = $self->{testuser} || die;
+    if (! $self->schema_exists($dbh,$fakeschema)) {
+               ## No schema means no table!
+               return;
+       }
+
+       my $SQL = "DROP TABLE $fakeschema.$name";
+
+    $dbh->do($SQL);
+
+    $dbh->commit();
+
+    return;
+
+} ## end of remove_fake_pg_table
+
+
 sub get_fake_schema {
     return $fakeschema;
 }
     my $count = $dbh->selectall_arrayref($SQL)->[0][0];
     if ($count) {
         $dbh->{Warn} = 0;
-        $dbh->do("DROP TABLE $name CASCADE");
+               my $fullname = $schema ? "$schema.$name" : $name;
+        $dbh->do("DROP TABLE $fullname CASCADE");
         $dbh->{Warn} = 1;
         $dbh->commit();
     }