Skip to content

Instantly share code, notes, and snippets.

@dimitri
Created March 14, 2013 17:21
Show Gist options
  • Save dimitri/5163271 to your computer and use it in GitHub Desktop.
Save dimitri/5163271 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 python
import os, sys
# compute lag between two XLOG file names in terms of files and bytes
# given 000000010000DAD7000000C4 and 000000010000DB1E00000032 we have:
# (int('DB1E',16) - int('DAD7', 16)) * 254 - int('C4',16) + int('132', 16)
#
# in pgsql:src/include/access/xlog_internal.h we have
#
# XLogFromFileName(fname, tli, logSegNo)
# uint32 log;
# uint32 seg;
# sscanf(fname, "%08X%08X%08X", tli, &log, &seg);
# *logSegNo = (uint64) log * XLogSegmentsPerXLogId + seg;
#
# and the files 00 and FF are skipped
#
# and in pg_config.h we have XLOG_SEG_SIZE (16 * 1024 * 1024)
XLOG_SEG_SIZE = 16 * 1024 * 1024
XLogSegmentsPerXLogId = 254
KB = 1024.0
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
def xlog_from_filename(filename):
"Return the current XLOG position from given filename"
tli = int(filename[0:8], 16)
log = int(filename[8:16], 16)
seg = int(filename[16:24], 16)
return log * XLogSegmentsPerXLogId + seg
def pprint_bytes(n):
"Return a string to reprensent bytes in human readable format, with units"
if n >= TB:
return "%4.1f TB" % (n / TB)
elif n >= GB:
return "%4.1f GB" % (n / GB)
elif n >= MB:
return "%4.1f MB" % (n / MB)
elif n >= GB:
return "%4.1f KB" % (n / KB)
else:
return "%d B" % n
if __name__ == '__main__':
wal1 = sys.argv[1]
wal2 = sys.argv[2]
pos1 = xlog_from_filename(wal1)
pos2 = xlog_from_filename(wal2)
diff = pos2 - pos1
if diff < 0:
diff = pos1 - pos2
print "XLog diff in WAL segments: %d" % diff
print "Xlog diff in bytes: %s" % pprint_bytes(diff * XLOG_SEG_SIZE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment