Fix postgres replication_slot chart on standby (#11455)
From: https://github.com/OPMDG/check_pgactivity/pull/246
Adrien nayrat committed
Aug 25, 2021 at 14:26 UTC
54018f5ffbcdc308d732cb84474c36806d3726ce
1 file changed
+28
-4
collectors/python.d.plugin/postgres/postgres.chart.py
+28
-4
@@ -576,8 +576,20 @@ FROM
576
slot_type,
577
COALESCE (
578
floor(
579
- (pg_wal_lsn_diff(pg_current_wal_lsn (),slot.restart_lsn)
580
- - (pg_walfile_name_offset (restart_lsn)).file_offset) / (s.val)
579
+ CASE WHEN pg_is_in_recovery()
580
+ THEN (
581
+ pg_wal_lsn_diff(pg_last_wal_receive_lsn(), slot.restart_lsn)
582
+ -- this is needed to account for whole WAL retention and
583
+ -- not only size retention
584
+ + (pg_wal_lsn_diff(restart_lsn, '0/0') %% s.val)
585
+ ) / s.val
586
+ ELSE (
587
+ pg_wal_lsn_diff(pg_current_wal_lsn(), slot.restart_lsn)
588
+ -- this is needed to account for whole WAL retention and
589
+ -- not only size retention
590
+ + (pg_walfile_name_offset(restart_lsn)).file_offset
591
+ ) / s.val
592
+ END
593
),0) AS replslot_wal_keep
594
FROM pg_replication_slots slot
595
LEFT JOIN (
@@ -615,8 +627,20 @@ FROM
627
slot_type,
628
COALESCE (
629
floor(
618
- (pg_wal_lsn_diff(pg_current_wal_lsn (),slot.restart_lsn)
619
- - (pg_walfile_name_offset (restart_lsn)).file_offset) / (s.val)
630
+ CASE WHEN pg_is_in_recovery()
631
+ THEN (
632
+ pg_wal_lsn_diff(pg_last_wal_receive_lsn(), slot.restart_lsn)
633
+ -- this is needed to account for whole WAL retention and
634
+ -- not only size retention
635
+ + (pg_wal_lsn_diff(restart_lsn, '0/0') %% s.val)
636
+ ) / s.val
637
+ ELSE (
638
+ pg_wal_lsn_diff(pg_current_wal_lsn(), slot.restart_lsn)
639
+ -- this is needed to account for whole WAL retention and
640
+ -- not only size retention
641
+ + (pg_walfile_name_offset(restart_lsn)).file_offset
642
+ ) / s.val
643
+ END
644
),0) AS replslot_wal_keep
645
FROM pg_replication_slots slot
646
LEFT JOIN (