Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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()