Skip to content

Instantly share code, notes, and snippets.

@dimitri
Created August 20, 2013 20:14
Show Gist options
  • Save dimitri/6286677 to your computer and use it in GitHub Desktop.
Save dimitri/6286677 to your computer and use it in GitHub Desktop.
compute lag between two XLOG file names in terms of files and bytes
#! /usr/bin/env sbcl --script
;; compute lag between two XLOG file names in terms of files and bytes
(defparameter *xlog-seg-size* (* 16 1024 1024)
"Xlog segment size, as per PostgreSQL pg_config.h")
(defparameter *xlog-segments-per-xlog-id* 254
"How many Xlog segments we find per xlog ID (00 and FF are skipped)")
(defun xlog-from-filename (filename)
"Return the current XLOG position from given filename"
;; see PostgreSQL src/include/access/xlog_internal.h XLogFromFileName()
(let* ((position (parse-integer filename :radix 16))
(seg (ldb (byte 32 0) position)) ; first 8 hex digits
(log (ldb (byte 32 32) position)) ; next 8 hex digits
(tli (ldb (byte 32 64) position))) ; next 8 hex digits
(+ seg (* log *xlog-segments-per-xlog-id*))))
(defun pretty-print-bytes (bytes)
"Return a string to reprensent bytes in human readable format, with units"
(loop
for unit in '("TB" "GB" "MB" "kB")
for power in '(40 30 20 10 1)
for limit = (expt 2 power)
until (<= limit bytes)
finally (return (format nil "~4,1f ~a" (/ bytes limit) unit))))
(let* ((args (rest sb-ext:*posix-argv*))
(wal1 (first args))
(wal2 (second args))
(pos1 (xlog-from-filename wal1))
(pos2 (xlog-from-filename wal2))
(diff (if (< pos1 pos2) (- pos2 pos1) (- pos1 pos2)))
(bytes (* diff *xlog-seg-size*)))
(format t "XLog diff in WAL segments: ~d~%" diff)
(format t "Xlog diff in bytes: ~a~%" (pretty-print-bytes bytes)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment