Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZAdQ-00036h-BO for pgsql-general@arkaria.postgresql.org; Mon, 25 Nov 2019 09:26:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iZAdO-0004ER-3S for pgsql-general@arkaria.postgresql.org; Mon, 25 Nov 2019 09:26:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iZAdN-0004EK-M9 for pgsql-general@lists.postgresql.org; Mon, 25 Nov 2019 09:26:17 +0000 Received: from mx2.stadt-zuerich.ch ([194.56.33.11]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iZAdJ-0005HX-Ee for pgsql-general@lists.postgresql.org; Mon, 25 Nov 2019 09:26:16 +0000 X-IronPort-AV: E=Sophos;i="5.69,241,1571695200"; d="scan'208";a="39440550" Received: from unknown (HELO SZHM29233.global.szh.loc) ([10.7.49.90]) by mx2.prm.szh.zone with ESMTP/TLS/AES256-GCM-SHA384; 25 Nov 2019 10:26:11 +0100 Received: from SZHM27370.global.szh.loc (10.7.50.50) by SZHM29233.global.szh.loc (10.7.49.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1713.5; Mon, 25 Nov 2019 10:26:10 +0100 Received: from SZHM27370.global.szh.loc ([fe80::25ce:879f:5795:3325]) by SZHM27370.global.szh.loc ([fe80::25ce:879f:5795:3325%10]) with mapi id 15.01.1713.004; Mon, 25 Nov 2019 10:26:09 +0100 From: "Zwettler Markus (OIZ)" To: Michael Paquier CC: "pgsql-general@lists.postgresql.org" Subject: AW: sql query for postgres replication check Thread-Topic: sql query for postgres replication check Thread-Index: AdWhLwsIaUTN/nPxSk2013en68ERdwCD2dcAAAvpuNA= Date: Mon, 25 Nov 2019 09:26:09 +0000 Message-ID: <0df1cf5f79f546ce9797e8510ad22047@zuerich.ch> References: <20191125041428.GJ37821@paquier.xyz> In-Reply-To: <20191125041428.GJ37821@paquier.xyz> Accept-Language: de-CH, en-US Content-Language: de-DE X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.7.48.249] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Fri, Nov 22, 2019 at 01:20:59PM +0000, Zwettler Markus (OIZ) wrote: > > I came up with the following query which should return any apply lag in= seconds. > > > > select coalesce(replay_delay, 0) replication_delay_in_sec from ( > > select datname, > > ( > > select case > > when received_lsn =3D latest_end_lsn then 0 > > else extract(epoch > > from now() - latest_end_time) > > end > > from pg_stat_wal_receiver > > ) replay_delay > > from pg_database > > where datname =3D current_database() > > ) xview; > > > > > > I would expect delays >0 in case SYNC or ASYNC replication is somehow > > behind. We will do a warning at 120 secs and critical at 300 secs. >=20 > pg_stat_wal_receiver is available only on the receiver, aka the standby s= o it would > not really be helpful on a primary. On top of that streaming replication= is system- > wide, so there is no actual point to look at databases either. >=20 > > Would this do the job or am I missing something here? >=20 > Here is a suggestion for Nagios: hot_standby_delay, as told in > https://github.com/bucardo/check_postgres/blob/master/check_postgres.pl > -- > Michael I don't want to use check_hot_standby_delay as I would have to configure ev= ery streaming replication configuration separately with nagios. I want a generic routine which I can load on any postgres server regardless= of streaming replication or database role. The query would return >0 if streaming replication falls behind and 0 in al= l other cases (replication or not). Checking streaming replication per database doesn't make any sense to me. Markus