# queries run once per cluster (via the 'postgres' database)

- name: "checkpoints"
  help: "requested/timed checkpoints and checkpoint buffers"
  scope: cluster
  values:
    - "timed"
    - "requested"
    - "buffers"
  query: >-
          SELECT
          pg_stat_get_bgwriter_timed_checkpoints() timed,
          pg_stat_get_bgwriter_requested_checkpoints() requested,
          pg_stat_get_bgwriter_buf_written_checkpoints() buffers

- name: "LastCheckpointDistance"
  help: "distance to the last checkpoint"
  scope: cluster
  version: 9.6
  values:
    - "distance"
  query: SELECT pg_xlog_location_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END, checkpoint_location) "distance" FROM pg_control_checkpoint()

- name: "LastCheckpointDistance"
  help: "distance to the last checkpoint"
  scope: cluster
  min_version: 10
  values:
    - "distance"
  query: SELECT pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END, checkpoint_lsn) "distance" FROM pg_control_checkpoint()

- name: "waldistance"
  help: "amount of WAL written"
  scope: cluster
  min_version: 9.2
  max_version: 9.6
  values:
    - "location"
  query: SELECT pg_xlog_location_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END, '0/0') "location"

- name: "waldistance"
  help: "amount of WAL written"
  scope: cluster
  min_version: 10
  values:
    - "location"
  query: SELECT pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END, '0/0') "location"
