Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPaZr-0005th-AJ for pgsql-admin@arkaria.postgresql.org; Mon, 17 Mar 2014 16:39:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WPaZq-0006eS-NQ for pgsql-admin@arkaria.postgresql.org; Mon, 17 Mar 2014 16:39:50 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPaZp-0006eL-Fy for pgsql-admin@postgresql.org; Mon, 17 Mar 2014 16:39:49 +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 1WPaZm-0003vE-Sz for pgsql-admin@postgresql.org; Mon, 17 Mar 2014 16:39:48 +0000 Received: from [192.168.1.179] (betelgeuse.pinpointresearch.com [192.168.1.179]) by polaris.pinpointresearch.com (Postfix) with ESMTP id CD31BE00EC83; Mon, 17 Mar 2014 09:39:45 -0700 (PDT) Message-ID: <53272551.6050205@pinpointresearch.com> Date: Mon, 17 Mar 2014 09:39:45 -0700 From: Steve Crawford User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: =?ISO-8859-2?Q?Strahinja_Kustudi=E6?= , "pgsql-admin@postgresql.org" Subject: Re: Safe way to check for PostgreSQL replication delay/lag References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.9 (-) 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 On 03/15/2014 12:38 PM, Strahinja Kustudi=E6 wrote: > Looking at the documentation and all the blog posts about how to=20 > monitor replication delay I don't think there is one good and most=20 > importantly safe solution which works all the time. I have a basic homegrown script, the core of which is below, that runs=20 this query on my servers (running 9.1 - settings may need tweaking for=20 other versions). It is a bash script that runs every minute via cron and=20 sets $standby_delay to a value in seconds. The script sends varying=20 levels of alert depending on the severity of the delay. (We must be=20 sized appropriately since we essentially never get alerts unless we have=20 restarted a server for maintenance.) The script uses a one-record/one-column table called sync_status to hold=20 a timestamptz. To keep things standard across machines, it determines=20 whether it is a master or replica based on the value of=20 'transaction_read_only' in pg_settings and either updates sync_status or=20 reads it, along with other xact_replay information, to determine the=20 worst-case delay. The core of the script writes a query to a temporary file then executes=20 that query. The output will be the worst-case measurement of delay=20 seconds (or zero if the master) which is saved in $standby_delay. An=20 empty value indicates a failure of the script to run. Obviously there is=20 more to the script both in setting the value of creating and setting=20 $tempquery, cleaning up temp files, and generating the appropriate=20 alerts based on the severity of the delay. Your alert methods and severity levels are up to you. Note that when run=20 by cron every minute there exists a reasonable possibility that the=20 slave can report a 60-second delay when actually caught up so you will=20 need to account for that in setting your alert levels. standby_delay=3D$( psql -q --tuples-only --no-align 2>/dev/null <