10637.3.7
by Guilherme Salgado
merge devel |
1 |
#!/usr/bin/python -S
|
8687.15.9
by Karl Fogel
Add the copyright header block to more files (everything under database/). |
2 |
#
|
3 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
|
4 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
5 |
|
6 |
"""Disable autovacuum on all tables in the database and kill off
|
|
7 |
any autovacuum processes.
|
|
8 |
||
8201.1.4
by Stuart Bishop
Review feedback |
9 |
We run this script on databases we require to be totally inactive with
|
10 |
no open connections, such as the template databases we clone. If not
|
|
11 |
disabled, autovacuum processes sometimes run and break our scripts.
|
|
12 |
||
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
13 |
Don't run this on any production systems.
|
14 |
"""
|
|
15 |
||
16 |
__metaclass__ = type |
|
17 |
__all__ = [] |
|
18 |
||
8201.1.4
by Stuart Bishop
Review feedback |
19 |
# pylint: disable-msg=W0403
|
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
20 |
import _pythonpath |
21 |
||
7675.357.1
by Stuart Bishop
Developer database setup working with PG 8.4 |
22 |
from distutils.version import LooseVersion |
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
23 |
from optparse import OptionParser |
24 |
import sys |
|
25 |
import time |
|
26 |
||
27 |
from canonical.database.sqlbase import connect |
|
28 |
from canonical.launchpad.scripts import logger_options, db_options, logger |
|
29 |
||
30 |
||
31 |
def main(): |
|
32 |
parser = OptionParser() |
|
33 |
logger_options(parser) |
|
34 |
db_options(parser) |
|
35 |
||
36 |
options, args = parser.parse_args() |
|
37 |
||
38 |
if len(args) > 0: |
|
39 |
parser.error("Too many arguments.") |
|
40 |
||
41 |
log = logger(options) |
|
42 |
||
43 |
log.debug("Connecting") |
|
44 |
con = connect(options.dbuser) |
|
45 |
con.set_isolation_level(0) # Autocommit |
|
46 |
cur = con.cursor() |
|
47 |
||
7675.357.1
by Stuart Bishop
Developer database setup working with PG 8.4 |
48 |
cur.execute('show server_version') |
49 |
pg_version = LooseVersion(cur.fetchone()[0]) |
|
50 |
||
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
51 |
log.debug("Disabling autovacuum on all tables in the database.") |
7675.357.1
by Stuart Bishop
Developer database setup working with PG 8.4 |
52 |
if pg_version < LooseVersion('8.4.0'): |
53 |
cur.execute(""" |
|
54 |
INSERT INTO pg_autovacuum
|
|
55 |
SELECT pg_class.oid, FALSE, -1,-1,-1,-1,-1,-1,-1,-1
|
|
56 |
FROM pg_class
|
|
57 |
WHERE relkind in ('r','t')
|
|
58 |
AND pg_class.oid NOT IN (SELECT vacrelid FROM pg_autovacuum)
|
|
59 |
""") |
|
60 |
else: |
|
61 |
cur.execute(""" |
|
62 |
SELECT nspname,relname
|
|
63 |
FROM pg_namespace, pg_class
|
|
64 |
WHERE relnamespace = pg_namespace.oid
|
|
65 |
AND relkind = 'r' AND nspname <> 'pg_catalog'
|
|
66 |
""") |
|
67 |
for namespace, table in list(cur.fetchall()): |
|
68 |
cur.execute(""" |
|
69 |
ALTER TABLE ONLY "%s"."%s" SET ( |
|
70 |
autovacuum_enabled=false,
|
|
71 |
toast.autovacuum_enabled=false)
|
|
72 |
""" % (namespace, table)) |
|
8201.1.2
by Stuart Bishop
Disable autovacuum on our development template databases |
73 |
|
74 |
log.debug("Killing existing autovacuum processes") |
|
75 |
num_autovacuums = -1 |
|
76 |
while num_autovacuums != 0: |
|
77 |
# Sleep long enough for pg_stat_activity to be updated.
|
|
78 |
time.sleep(0.6) |
|
79 |
cur.execute(""" |
|
80 |
SELECT procpid FROM pg_stat_activity
|
|
81 |
WHERE
|
|
82 |
datname=current_database()
|
|
83 |
AND current_query LIKE 'autovacuum: %'
|
|
84 |
""") |
|
85 |
autovacuums = [row[0] for row in cur.fetchall()] |
|
86 |
num_autovacuums = len(autovacuums) |
|
87 |
for procpid in autovacuums: |
|
88 |
log.debug("Cancelling %d" % procpid) |
|
89 |
cur.execute("SELECT pg_cancel_backend(%d)" % procpid) |
|
90 |
||
91 |
||
92 |
if __name__ == '__main__': |
|
93 |
sys.exit(main()) |