From 1d94cafa05d78d11b6e89d3fb3ca5288e88729f1 Mon Sep 17 00:00:00 2001 From: Jon Robertson Date: Wed, 20 Apr 2016 15:54:46 -0700 Subject: [PATCH] PostgreSQL hot-standby replication support Add the is_replicating and lag_behind_master calls that DBIx::Class::Storage::DBI::Replicated::Pool::validate_replicants needs. This works with PostgreSQL's hot-standby functionality that was introduced in PostgreSQL 9.0. It may not work with other replication methods, like BDR or Slony. More info on hot-standby is available here: * https://wiki.postgresql.org/wiki/Hot_Standby Signed-off-by: Karl Kornel --- lib/DBIx/Class/Storage/DBI/Pg.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index 87a237d66..b0d388222 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -237,6 +237,23 @@ sub deployment_statements { $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); } +sub is_replicating { + my $repl = 'select pg_last_xlog_receive_location() AS replicating'; + my $status = shift->_get_dbh->selectrow_hashref($repl); + return 1 if defined $status->{replicating}; + return 0; +} + +sub lag_behind_master { + my $repl = 'SELECT ' + .'CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() ' + .'THEN 0 ' + .'ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())' + .'END AS log_delay'; + my $status = shift->_get_dbh->selectrow_hashref($repl); + return $status->{log_delay}; +} + 1; __END__