From 61d622dd852681e66bdac9c67234713795091dcc Mon Sep 17 00:00:00 2001 From: Rafael de Lima Prado <rlp09@inf.ufpr.br> Date: Thu, 23 Jan 2014 14:02:50 -0200 Subject: [PATCH] database: Add creation of tablespaces. Add script to migrate data and indexes to the respective tablespaces Signed-off-by: Rafael de Lima Prado <rlp09@inf.ufpr.br> --- database/create_tablespaces.sql | 6 ++++ database/migracao_tablespace.py | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 database/create_tablespaces.sql create mode 100644 database/migracao_tablespace.py diff --git a/database/create_tablespaces.sql b/database/create_tablespaces.sql new file mode 100644 index 0000000..7ce550a --- /dev/null +++ b/database/create_tablespaces.sql @@ -0,0 +1,6 @@ +CREATE TABLESPACE dw_index_ts LOCATION '/home/index/dw_tablespace'; +CREATE TABLESPACE aggr_index_ts LOCATION '/home/index/aggr_tablespace'; +CREATE TABLESPACE aggr_table_ts LOCATION '/home/banco/aggr_tablespace'; +CREATE TABLESPACE dw_table_ts LOCATION '/home/banco/dw_tablespace'; +CREATE TABLESPACE sa_ts LOCATION '/home/banco/sa_tablespace'; +CREATE TABLESPACE le_save_conf_tablespace location '/home/banco/le_save_conf'; diff --git a/database/migracao_tablespace.py b/database/migracao_tablespace.py new file mode 100644 index 0000000..7ef7dff --- /dev/null +++ b/database/migracao_tablespace.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import psycopg2 +import sys + +dbName = "proinfodata" +tablespace_table_dw = "dw_table_ts" +tablespace_index_dw = "dw_index_ts" + +try: + + con = psycopg2.connect(user = 'postgres', database = dbName, port = '5432') + db = con.cursor() + + db.execute( "alter table %s.%s set tablespace %s" % ( 'public', 'aggr_availability', 'aggr_table_ts' ) ) + db.execute("select * from pg_indexes where schemaname = '%s' and tablename = '%s' order by indexname" % ( 'public', 'aggr_availability')) + rows = db.fetchall() + for index in rows: + db.execute( "alter index %s.%s set tablespace %s" % ( 'public', index[2], 'aggr_index_ts' ) ) + + db.execute("alter table %s.%s set tablespace %s" % ('public', 'proinfo_inventory', 'sa_ts')) + db.execute( "select * from pg_indexes where schemaname = '%s' and tablename = '%s' order by indexname" % ( 'public', 'proinfo_inventory')) + rows = db.fetchall() + for index in rows: + db.execute( "alter index %s.%s set tablespace %s" % ( 'public', index[2], 'sa_ts' ) ) + + db.execute( "alter table %s.%s set tablespace %s" % ( 'public', 'proinfo_net_usage', 'sa_ts' ) ) + db.execute( "select * from pg_indexes where schemaname = '%s' and tablename = '%s' order by indexname" % ( 'public', 'proinfo_net_usage')) + rows = db.fetchall() + for index in rows: + db.execute( "alter index %s.%s set tablespace %s" % ( 'public', index[2], 'sa_ts' ) ) + + db.execute("select * from pg_tables where tablename != 'aggr_availability' and tablename != 'proinfo_inventory' and tablename != 'proinfo_net_usage' order by tablename") + rows_tables = db.fetchall() + for table in rows_tables: + schemaName = table[0] + if schemaName == "public": + tableName = table[1] + db.execute( "alter table %s.%s set tablespace %s" % ( schemaName, tableName, tablespace_table_dw ) ) + db.execute( "select * from pg_indexes where schemaname = '%s' and tablename = '%s' order by indexname" % ( schemaName, tableName )) + rows_indexes = db.fetchall() + for index in rows_indexes: + db.execute( "alter index %s.%s set tablespace %s" % ( schemaName, index[2], tablespace_index_dw ) ) + + con.commit() + +except psycopg2.DatabaseError, e: + if con: + con.rollback() + + print 'Error %s' % e + sys.exit(1) + +finally: + if con: + con.close() -- GitLab