def impl(context, seg_type, content): if seg_type == "primary": preferred_role = 'p' elif seg_type == "mirror": preferred_role = 'm' else: raise Exception( "Invalid segment type %s (options are primary and mirror)" % seg_type) with closing( dbconn.connect(dbconn.DbURL(dbname="template1"), unsetSearchPath=False)) as conn: dbid, hostname = dbconn.queryRow( conn, "SELECT dbid, hostname FROM gp_segment_configuration WHERE content = %s AND preferred_role = '%s'" % (content, preferred_role)) if not hasattr(context, 'old_hostnames'): context.old_hostnames = {} context.old_hostnames[(content, preferred_role)] = hostname change_hostname(content, preferred_role, 'invalid_host') if not hasattr(context, 'down_segment_dbids'): context.down_segment_dbids = [] context.down_segment_dbids.append(dbid) wait_for_unblocked_transactions(context)
def check_table_exists(context, dbname, table_name, table_type=None, host=None, port=0, user=None): with closing( dbconn.connect(dbconn.DbURL(hostname=host, port=port, username=user, dbname=dbname), unsetSearchPath=False)) as conn: if '.' in table_name: schemaname, tablename = table_name.split('.') SQL_format = """ SELECT c.oid, c.relkind, c.relstorage, c.reloptions FROM pg_class c, pg_namespace n WHERE c.relname = '%s' AND n.nspname = '%s' AND c.relnamespace = n.oid; """ SQL = SQL_format % (escape_string( tablename, conn=conn), escape_string(schemaname, conn=conn)) else: SQL_format = """ SELECT oid, relkind, relstorage, reloptions \ FROM pg_class \ WHERE relname = E'%s';\ """ SQL = SQL_format % (escape_string(table_name, conn=conn)) table_row = None try: table_row = dbconn.queryRow(conn, SQL) except Exception as e: context.exception = e return False if table_type is None: return True if table_row[2] == 'a': original_table_type = 'ao' elif table_row[2] == 'c': original_table_type = 'co' elif table_row[2] == 'h': original_table_type = 'heap' elif table_row[2] == 'x': original_table_type = 'external' elif table_row[2] == 'v': original_table_type = 'view' else: raise Exception('Unknown table type %s' % table_row[2]) if original_table_type != table_type.strip(): return False return True
def __init__(self, coordinatorDataDir, readFromCoordinatorCatalog, timeout=None, retries=None, verbose=True): """ coordinatorDataDir: if None then we try to find it from the system environment readFromCoordinatorCatalog: if True then we will connect to the coordinator in utility mode and fetch some more data from there (like collation settings) """ if coordinatorDataDir is None: self.__coordinatorDataDir = gp.get_coordinatordatadir() else: self.__coordinatorDataDir = coordinatorDataDir logger.debug( "Obtaining coordinator's port from coordinator data directory") pgconf_dict = pgconf.readfile(self.__coordinatorDataDir + "/postgresql.conf") self.__coordinatorPort = pgconf_dict.int('port') logger.debug("Read from postgresql.conf port=%s" % self.__coordinatorPort) self.__coordinatorMaxConnections = pgconf_dict.int('max_connections') logger.debug("Read from postgresql.conf max_connections=%s" % self.__coordinatorMaxConnections) self.__gpHome = gp.get_gphome() self.__gpVersion = gp.GpVersion.local( 'local GP software version check', self.__gpHome) if verbose: logger.info("local Greenplum Version: '%s'" % self.__gpVersion) # read collation settings from coordinator if readFromCoordinatorCatalog: dbUrl = dbconn.DbURL(port=self.__coordinatorPort, dbname='template1', timeout=timeout, retries=retries) conn = dbconn.connect(dbUrl, utility=True) # MPP-13807, read/show the coordinator's database version too self.__pgVersion = dbconn.queryRow(conn, "select version();")[0] logger.info("coordinator Greenplum Version: '%s'" % self.__pgVersion) conn.close() else: self.__pgVersion = None
def getRow(dbname, exec_sql): with closing( dbconn.connect(dbconn.DbURL(dbname=dbname), unsetSearchPath=False)) as conn: result = dbconn.queryRow(conn, exec_sql) return result