listtables.py
changeset 68 b0d972be2631
parent 38 d3593869d624
child 83 515fadd3d286
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/listtables.py	Tue Feb 26 14:40:39 2013 +0100
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3.2
+
+from pgtoolkit import pgbrowser, toolbase, prettysize
+
+
+class ListTablesTool(toolbase.SimpleTool):
+    def __init__(self):
+        toolbase.SimpleTool.__init__(self, name='listtables', desc='List tables in database.')
+        self.parser.add_argument('-o', dest='options', type=str, nargs='*', help='Filter by options (eg. -o autovacuum_enabled=false).')
+        self.init()
+
+    def main(self):
+        browser = pgbrowser.PgBrowser(self.pgm.get_conn('target'))
+
+        # scan all tables from all shemas, remember names and sizes
+        tables = []
+        for schema in browser.schemas.values():
+            for table in schema.tables.values():
+                for option in self.args.options:
+                    if option in table.options:
+                        tables.append(table)
+
+        # print result
+        if len(tables):
+            print('Found %d tables:' % len(tables))
+        else:
+            print('No table meets the conditions.')
+        for table in tables:
+            table_name = '%s.%s' % (table.schema.name, table.name)
+            print(' ', table_name)
+
+
+tool = ListTablesTool()
+tool.main()
+