forked from mozilla-services/socorro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruncate_56.py
102 lines (73 loc) · 2.11 KB
/
truncate_56.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
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
96
97
98
99
100
101
102
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import sys
import psycopg2, psycopg2.extensions
# this script truncates the database down to 56 to 62 days of data
# for use in staging and/or dev environments
conn = psycopg2.connect("dbname=breakpad user=postgres")
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
cur.execute("""
SET work_mem = '128 MB'
""")
cur.execute("""
SET maintenance_work_mem = '256 MB'
""")
cur.execute("""
SET temp_buffers = '128 MB'
""")
# get a list of reports partitions older than 62 days
cur.execute("""
select relname from pg_stat_user_tables
where relname like 'reports_20%%'
and relname < 'reports_' || to_char(current_date - 62, 'YYYYMMDD')
order by relname
""" )
# drop all of the old partitions
# use cascade so it takes out frames, extensions, etc. too
partitions = [ x for ( x, ) in cur.fetchall() ]
for partition in partitions:
cur.execute("DROP TABLE %s CASCADE" % ( partition, ))
print "%s dropped." % partition
# delete data from top crashers
cur.execute("""
DELETE FROM top_crashes_by_url_signature
USING top_crashes_by_url
WHERE top_crashes_by_url_id = top_crashes_by_url.id
AND window_end < ( now() - interval '60 days')
""")
cur.execute("""
VACUUM FULL top_crashes_by_url_signature
""")
cur.execute("""
DELETE FROM top_crashes_by_url
WHERE window_end < ( now() - interval '60 days')
""")
cur.execute("""
VACUUM FULL top_crashes_by_url
""")
print "top crashes by url truncated"
cur.execute("""
DELETE FROM top_crashes_by_signature
WHERE window_end < ( now() - interval '60 days')
""")
cur.execute("""
VACUUM FULL top_crashes_by_signature
""")
print "top_crashes_by_signature truncated"
# truncate raw_adu
cur.execute("""
DELETE FROM raw_adu
WHERE "date" < ( now() - interval '60 days')
""")
cur.execute("""
VACUUM FULL raw_adu
""")
print "raw_adu truncated"
# analyze
cur.execute("""
ANALYZE
""")
print "done truncating"