-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind_bad_toast
executable file
·95 lines (67 loc) · 2.73 KB
/
find_bad_toast
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
from __future__ import print_function # You must use Python >= 2.6!
import argparse
import os
import psycopg2
def debug(*args):
if os.getenv('FIND_BAD_TOAST_DEBUG', False):
print(*args)
"""
find_bad_toast - *****************
Scan a PostgreSQL table for rows with corrupted TOAST records.
Arguments:
- connect_string is psycopg2 connection string.
- table is the name of the table to scan.
- pks is a sequence of column names. These are the columns that compose
the primary key of the column. Of course, there may be only one such column.
This is a translation, for performance reasons, from plpgsql to Python,
of the logic described at
*** http://www.databasesoup.com/2014/07/improved-toast-corruption-function.html
The performance reasons are a) to avoid keeping a transaction open for the
initial whole-table scan, which is painful for large tables, and b) to improve
upon the performance of plpgsql as a language.
"""
def find_bad_toast(connect_string, table, pks):
conn = psycopg2.connect(connect_string)
conn.autocommit = True
ids_cur = conn.cursor()
pk_cols = ', '.join(pks)
ids_query = 'SELECT {p} FROM {table}'.format(p=pk_cols, table=table)
debug('Selecting IDs using query "{ids_query}"'.format(ids_query=ids_query))
ids_cur.execute(ids_query)
count = 0
copy_cur = conn.cursor()
while True:
ids = ids_cur.fetchone()
if not ids:
break
debug('Examining row with IDs', ids)
count += 1
if count % 100000 == 0:
print('{count} rows inspected'.format(count=count))
try:
copy_query = 'COPY (SELECT * FROM {t} WHERE '.format(t=table)
where_clauses = []
for col in pks:
clause = col + ' = %s'
debug('Adding clause "{clause}"'.format(clause=clause))
where_clauses.append(clause)
debug('AND_clauses: ', where_clauses)
copy_query += ' AND '.join(where_clauses)
copy_query += ") TO '/tmp/testout'"
debug('COPY query:', copy_query)
copy_cur.execute(copy_query, ids)
except Exception as e:
print('Found corruption in the row with PK columns', ids,
"Exception: '{ex}'".format(ex=str(e)))
def main():
parser = argparse.ArgumentParser(
description='Find rows in a table that have corrupted TOAST values.'
)
parser.add_argument('-t', '--table', required=True)
parser.add_argument('-p', '--pk', required=True, action='append')
parser.add_argument('--connect-string', required=True)
args = parser.parse_args()
find_bad_toast(args.connect_string, args.table, args.pk)
if __name__ == '__main__':
main()