-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_wal_dump.py
executable file
·65 lines (49 loc) · 1.72 KB
/
parse_wal_dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import os
import re
import sys
import argparse
import psycopg2
parser = argparse.ArgumentParser(description='Parse WAL segments and insert them into a database.')
parser.add_argument('connstr', help='connection string for the target database')
args = parser.parse_args()
#
waldump_line_pattern = re.compile(r'rmgr: (?P<rmgr>[A-Za-z]+).+len \(rec\/tot\)\: *(?P<len_rec>\d+) *\/ *(?P<len_tot>\d+),' +\
'.+lsn\: (?P<lsn>[0-9A-F]+\/[0-9A-F]+),' +\
'.+rel (?P<tablespace>\d+)/(?P<database>\d+)/(?P<relation>\d+) ')
conn = psycopg2.connect(args.connstr)
conn.autocommit = True
#
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS wal_usage(
lsn text primary key,
rmgr text,
len_rec int,
len_tot int,
tablespace int,
database int,
relation int
)
""")
#
lines_accepted = 0
lines_rejected = 0
for line in sys.stdin:
parsed = waldump_line_pattern.match(line)
if not parsed:
lines_rejected += 1
continue
lines_accepted += 1
c.execute("""INSERT INTO wal_usage(rmgr, len_rec, len_tot, lsn, tablespace, database, relation)
VALUES(%(rmgr)s, %(len_rec)s, %(len_tot)s, %(lsn)s, %(tablespace)s, %(database)s, %(relation)s)""",
{ 'rmgr': parsed.group('rmgr'),
'len_rec': int(parsed.group('len_rec')),
'len_tot': int(parsed.group('len_tot')),
'lsn': parsed.group('lsn'),
'tablespace': int(parsed.group('tablespace')),
'database': int(parsed.group('database')),
'relation': int(parsed.group('relation')),
}
)
print(f"{lines_accepted} lines successfully parsed, {lines_rejected} lines rejected")