/**
 * View to monitor a follower WAL status
 *
 * NOTE: This view is only working on a follower node, on a primary it will
 * throw an error
 *
 * @author: "Stefanie Janine Stölting"<stefanie@proopensource.eu>
 * @license: PostgreSQL https://opensource.org/licenses/postgresql
 */

CREATE OR REPLACE VIEW monitoring_follower_wal_status AS
SELECT statement_timestamp () AS timestamp_execution
	, pg_read_file('/etc/hostname') AS hostname
	, pg_is_in_recovery () AS in_recovery
	, pg_is_wal_replay_paused () AS wal_replay_paused
	, pg_last_wal_receive_lsn () AS last_wal_receive_lsn
	, pg_last_wal_replay_lsn () AS last_wal_replay_lsn
	, pg_last_xact_replay_timestamp () AS last_xact_replay_timestamp
;

COMMENT ON VIEW monitoring_follower_wal_status IS 'View to monitor a follower WAL status.';
COMMENT ON COLUMN monitoring_follower_wal_status.timestamp_execution IS 'Timestamp at exection of the statement.';
COMMENT ON COLUMN monitoring_follower_wal_status.hostname IS 'Does return the hostname on Linux servers.';
COMMENT ON COLUMN monitoring_follower_wal_status.in_recovery IS 'Whether the follower is in recovery state, or not.';
COMMENT ON COLUMN monitoring_follower_wal_status.wal_replay_paused IS 'Whether the WAL replay on the server has been paused.';
COMMENT ON COLUMN monitoring_follower_wal_status.last_wal_receive_lsn IS 'The last WAL segment, that has been received, type is [pg_lsn](https://www.postgresql.org/docs/current/datatype-pg-lsn.html).';
COMMENT ON COLUMN monitoring_follower_wal_status.last_wal_replay_lsn IS 'The last WAL segment, that has been replayed, type is [pg_lsn](https://www.postgresql.org/docs/current/datatype-pg-lsn.html).';
COMMENT ON COLUMN monitoring_follower_wal_status.last_xact_replay_timestamp IS 'Returns the last timestamp of a WAL segment replayed during recovery.';
