def stat(self,in_time_id): if not super(TableName,self).stat(in_time_id): logger.error("Return from TN.stat False from super.stat()") return False idx_sets=self.get_dependants(False) if not idx_sets: logger.info("TN.stat no ids_sets returned. Maybe no indexes present. Tabe {0}".format(self.db_fields['tbl_name'])) return True else: for idx_set in idx_sets: idn=IndexName(self.db_conn,self.prod_dsn,idx_set) idn.set_prod_conn(self.prod_conn) if not idn.stat(in_time_id): logger.error("TN.stat False from idn.stat") ttn_id=self.get_toast_id() if ttn_id: ttn=TableToastName(self.db_conn,self.prod_dsn,ttn_id) ttn.set_prod_conn(self.prod_conn) if not ttn.stat(in_time_id): logger.error("TN.stat False from ttn.stat") tin_id=ttn.get_tindex_id() tin=IndexToastName(self.db_conn,self.prod_dsn,tin_id) tin.set_prod_conn(self.prod_conn) if not tin.stat(in_time_id): logger.error("TN.stat False from tin.stat") return True
for sn_id in dn.get_dependants(obs=True): sn=SchemaName(sn_id) sn.discover_tables(db_cursor) sn.discover_functions(db_cursor) for tbl_id in sn.get_tables(): tn=TableName(tbl_id) tn.stat(lt.id,db_cursor) tn.va_stat(lt.id,db_cursor) tn.discover_indexes(db_cursor) for ind_id in tn.get_dependants(): ind=IndexName(ind_id) ind.stat(lt.id,db_cursor) tn.discover_toast(db_cursor) toast_id=tn.get_toast_id() if toast_id: ttn=TableToastName(toast_id) ttn.stat(lt.id,db_cursor) ttn.discover_index(db_cursor) tin=IndexToastName(ttn.get_tindex_id()) tin.stat(lt.id,db_cursor) if hc.get_track_function(): for fnc_id in sn.get_functions(): func=FunctionName(fnc_id) func.stat(lt.id,db_cursor) if not db_cursor.closed: db_cursor.close() work_cursor.close() database.db_conn.close()
def discover_toast(self): p_cur=self._get_p_cursor() if not p_cur: logger.error("Returning from TN.discover_toast No p_cur obtained") return False try: p_cur.execute("""SELECT r.reltoastrelid,t.relname FROM pg_class r INNER JOIN pg_class t ON r.reltoastrelid=t.oid WHERE r.relkind='r' AND t.relkind='t' AND r.oid={0}""".format(self.db_fields['obj_oid'])) except Exception as e: logger.error("Canot execute toast discovery query on Prod: {0}".format(e.pgerror)) p_cur.close() return False prod_ttbl=p_cur.fetchone() p_cur.close() cur=self._get_cursor() if not cur: logger.error("Returning from TN.discover_toast No cur obtained") return False try: cur.execute("SELECT obj_oid,ttbl_name,id FROM table_toast_name WHERE tn_id={0} AND alive".format(self.id)) except Exception as e: logger.error("Canot execute toast discovery query on Local: {0}".format(e.pgerror)) cur.close() return False local_ttbl=cur.fetchone() cur.close() if local_ttbl and prod_ttbl: if not (local_ttbl[0] == prod_ttbl[0] and local_ttbl[1] == prod_ttbl[1]): logger.info("Retired TOAST table {0} for table {1}".format(local_ttbl[1],self.db_fields['tbl_name'])) old_ttbl=TableToastName(self.db_conn,self.prod_conn,local_ttbl[2]) if not old_ttbl.retire(): logger.error("TN.discover_toast Cannot retire old to create new") logger.info("Create new TOAST table {0} in table {1}".format(prod_ttbl[1],self.db_fields['tbl_name'])) new_ttbl=TableToastName(self.db_conn,self.prod_conn) new_ttbl.set_fields(tn_id=self.id,obj_oid=prod_ttbl[0],ttbl_name=prod_ttbl[1]) if not new_ttbl._create(): logger.error("TN.discover_toast Cannot create new after retire old") self.toast_id=local_ttbl[2] if not new_ttbl.discover_index(): logger.error("TTN.discover_toast False from discover_index for new after retire") elif local_ttbl and not prod_ttbl: self.toast_id=None logger.info("Retired TOAST table {0} for table {1}".format(local_ttbl[1],self.db_fields['tbl_name'])) old_ttbl=TableToastName(self.db_conn,self.prod_conn,local_ttbl[2]) if not old_ttbl.retire(): logger.error("TN.discover_toast Cannot retire old") elif not local_ttbl and prod_ttbl: logger.info("Create new TOAST table {0} in table {1}".format(prod_ttbl[1],self.db_fields['tbl_name'])) new_ttbl=TableToastName(self.db_conn,self.prod_conn) new_ttbl.set_fields(tn_id=self.id,obj_oid=prod_ttbl[0],ttbl_name=prod_ttbl[1]) if not new_ttbl._create(): logger.error("TN.discover_toast Cannot create new") self.toast_id=new_ttbl.get_id() if not new_ttbl.discover_index(): logger.error("TTN.discover_toast False from discover_index for new") return True
def discover_toast(self,prod_cursor): self.cursor.execute("SELECT obj_oid,ttbl_name,id FROM table_toast_name WHERE tn_id={0} AND alive".format(self.id)) local_ttbl=self.cursor.fetchone() try: prod_cursor.execute("""SELECT r.reltoastrelid,t.relname FROM pg_class r INNER JOIN pg_class t ON r.reltoastrelid=t.oid WHERE r.relkind='r' AND t.relkind='t' AND r.oid={0}""".format(self.db_fields['obj_oid'])) except Exception as e: logger.error("Cannot execute toast table discovery query: {0},{1}".format(e.pgcode,e.pgerror)) return prod_ttbl=prod_cursor.fetchone() if local_ttbl and prod_ttbl: if not (local_ttbl[0] == prod_ttbl[0] and local_ttbl[1] == prod_ttbl[1]): logger.info("Retired TOAST table {0} for table {1}".format(local_ttbl[1],self.db_fields['tbl_name'])) old_ttbl=TableToastName(local_ttbl[2]) old_ttbl.retire() logger.info("Create new TOAST table {0} in table {1}".format(prod_ttbl[1],self.db_fields['tbl_name'])) new_ttbl=TableToastName() new_ttbl.set_fields(tn_id=self.id,obj_oid=prod_ttbl[0],ttbl_name=prod_ttbl[1]) new_ttbl.create() self.toast_id=local_ttbl[2] new_ttbl.truncate() elif local_ttbl and not prod_ttbl: self.toast_id=None logger.info("Retired TOAST table {0} for table {1}".format(local_ttbl[1],self.db_fields['tbl_name'])) old_ttbl=TableToastName(local_ttbl[2]) old_ttbl.retire() elif not local_ttbl and prod_ttbl: logger.info("Create new TOAST table {0} in table {1}".format(prod_ttbl[1],self.db_fields['tbl_name'])) new_ttbl=TableToastName() new_ttbl.set_fields(tn_id=self.id,obj_oid=prod_ttbl[0],ttbl_name=prod_ttbl[1]) new_ttbl.create() self.toast_id=new_ttbl.get_id() new_ttbl.truncate()