Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4CVJ-0004uI-Uc for pgsql-admin@arkaria.postgresql.org; Fri, 17 Jan 2014 16:42:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W4CVJ-0004vl-Bz for pgsql-admin@arkaria.postgresql.org; Fri, 17 Jan 2014 16:42:45 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4CVH-0004tm-Ek for pgsql-admin@postgresql.org; Fri, 17 Jan 2014 16:42:43 +0000 Received: from cerberus.pinpointresearch.com ([66.7.238.130] helo=polaris.pinpointresearch.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W4CVE-0003Av-GA for pgsql-admin@postgresql.org; Fri, 17 Jan 2014 16:42:42 +0000 Received: from [192.168.1.179] (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id 2C9BBE00EC82; Fri, 17 Jan 2014 08:42:39 -0800 (PST) Message-ID: <52D95D7F.70303@pinpointresearch.com> Date: Fri, 17 Jan 2014 08:42:39 -0800 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Mario Splivalo , pgsql-admin@postgresql.org Subject: Re: Checking replication slave state References: <52D95689.7070101@splivalo.hr> In-Reply-To: <52D95689.7070101@splivalo.hr> Content-Type: multipart/alternative; boundary="------------030504060704080202060208" X-Pg-Spam-Score: -2.2 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org This is a multi-part message in MIME format. --------------030504060704080202060208 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 01/17/2014 08:12 AM, Mario Splivalo wrote: > Hello. > > What would be the preferred/best way to check replication state of a slave? > > When I fire up slave, it can either be in 'can not connect to master' > state, it can be in 'catching up' state, or it can be in 'streaming' state. > > I can check what pg_is_in_recovery() function returns, but this will > return true regardless of weather server is catching up, failing to > catch up or is in streaming replication with the master. > > Is there a way, other then checking the logs, to verify that? > > Mario > > If you want to know if the standby is reasonably up-to-date take a look at some of the system administration functions: http://www.postgresql.org/docs/current/static/functions-admin.html Among my monitoring scripts is one that does a query that uses pg_last_xact_replay_timestamp(), pg_last_xlog_receive_location() and pg_last_xlog_replay_location() to report a lag. Specifically, if receive and replay locations are the same then it returns zero otherwise it reports the time between the current timestamp and the replay_timestamp to get an approximate lag. As an additional cross-check I have a roll-your-own check consisting of a "sync_status" table with a single row and single timestamptz column which is updated every minute on the master so there will always be some traffic to the master. The standby server(s) verify that the timestamp in that table is (reasonably) recent. Failure to connect to a standby or excessive lag set off alarms. Since my standby is not handling load it is virtually always up-to-date and rarely more than a few seconds behind the master. Cheers, Steve | | --------------030504060704080202060208 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 01/17/2014 08:12 AM, Mario Splivalo wrote:
Hello.

What would be the preferred/best way to check replication state of a slave?

When I fire up slave, it can either be in 'can not connect to master'
state, it can be in 'catching up' state, or it can be in 'streaming' state.

I can check what pg_is_in_recovery() function returns, but this will
return true regardless of weather server is catching up, failing to
catch up or is in streaming replication with the master.

Is there a way, other then checking the logs, to verify that?

	Mario



If you want to know if the standby is reasonably up-to-date take a look at some of the system administration functions:
http://www.postgresql.org/docs/current/static/functions-admin.html

Among my monitoring scripts is one that does a query that uses pg_last_xact_replay_timestamp(), pg_last_xlog_receive_location() and
pg_last_xlog_replay_location() to report a lag. Specifically, if receive and replay locations are the same then it returns zero otherwise it reports the time between the current timestamp and the replay_timestamp to get an approximate lag.

As an additional cross-check I have a roll-your-own check consisting of a "sync_status" table with a single row and single timestamptz column which is updated every minute on the master so there will always be some traffic to the master. The standby server(s) verify that the timestamp in that table is (reasonably) recent.

Failure to connect to a standby or excessive lag set off alarms.

Since my standby is not handling load it is virtually always up-to-date and rarely more than a few seconds behind the master.

Cheers,
Steve

--------------030504060704080202060208--