Esempio n. 1
0
def impl(context, contents):
    contents = contents.split(',')
    for content in contents:
        with closing(
                dbconn.connect(dbconn.DbURL(dbname='template1'),
                               unsetSearchPath=False)) as conn:
            for role in [ROLE_PRIMARY, ROLE_MIRROR]:
                actual_datadir = dbconn.querySingleton(
                    conn,
                    "SELECT datadir FROM gp_segment_configuration WHERE preferred_role='{}' AND "
                    "content = {};".format(role, content))
                expected_datadir = context.original_seg_info["{}_{}".format(
                    content, role)].getSegmentDataDirectory()
                if not expected_datadir == actual_datadir:
                    raise Exception(
                        "Expected datadir {}, got {} for content {}".format(
                            expected_datadir, actual_datadir, content))
Esempio n. 2
0
def impl(context, storage_type, tablename, col_name_list, col_type_list,
         schemaname):
    schemaname_no_quote = schemaname
    if '"' in schemaname:
        schemaname_no_quote = schemaname[1:-1]
    if not check_schema_exists(context, schemaname_no_quote, context.dbname):
        raise Exception("Schema %s does not exist in database %s" %
                        (schemaname_no_quote, context.dbname))
    drop_table_if_exists(context, '.'.join([schemaname, tablename]),
                         context.dbname)
    with closing(dbconn.connect(dbconn.DbURL(dbname=context.dbname))) as conn:
        create_table_with_column_list(conn, storage_type, schemaname,
                                      tablename, col_name_list, col_type_list)
    check_table_exists(context,
                       context.dbname,
                       '.'.join([schemaname, tablename]),
                       table_type=storage_type)
Esempio n. 3
0
 def _analyze(self, restore_db, restore_tables, master_port):
     conn = None
     try:
         dburl = dbconn.DbURL(port=master_port, dbname=restore_db)
         conn = dbconn.connect(dburl)
         for table in restore_tables:
             logger.info('Commencing analyze of %s in %s database, please wait...' % (table, restore_db))
             try:
                 execSQL(conn, 'analyze %s' % table)
                 conn.commit()
             except Exception, e:
                 logger.warn('Issue with analyze of %s table, check log file for details' % table)
             else:
                 logger.info('Analyze of %s table completed without error' % table)
     finally:
         if conn is not None:
             conn.close()
Esempio n. 4
0
def check_db_exists(dbname, host=None, port=0, user=None):
    LIST_DATABASE_SQL = 'SELECT datname FROM pg_database'

    results = []
    with closing(
            dbconn.connect(dbconn.DbURL(hostname=host,
                                        username=user,
                                        port=port,
                                        dbname='template1'),
                           unsetSearchPath=False)) as conn:
        curs = dbconn.query(conn, LIST_DATABASE_SQL)
        results = curs.fetchall()
    for result in results:
        if result[0] == dbname:
            return True

    return False
Esempio n. 5
0
def stop_primary(context, content_id):
    get_psegment_sql = 'select datadir, hostname from gp_segment_configuration where content=%i and role=\'p\';' % content_id
    with closing(
            dbconn.connect(dbconn.DbURL(dbname='template1'),
                           unsetSearchPath=False)) as conn:
        cur = dbconn.query(conn, get_psegment_sql)
        rows = cur.fetchall()
        seg_data_dir = rows[0][0]
        seg_host = rows[0][1]

    # For demo_cluster tests that run on the CI gives the error 'bash: pg_ctl: command not found'
    # Thus, need to add pg_ctl to the path when ssh'ing to a demo cluster.
    subprocess.check_call([
        'ssh', seg_host,
        'source %s/greenplum_path.sh && pg_ctl stop -m fast -D %s' %
        (pipes.quote(os.environ.get("GPHOME")), pipes.quote(seg_data_dir))
    ])
Esempio n. 6
0
    def _get_gpdb_host_list(self):
        """
        TODO: AK: Get rid of this. Program logic should not be driving host list building .
        
            This method gets the host names 
            of all hosts in the gpdb array.
            It sets the following variables 
                GpPkgProgram.master_host to master
                GpPkgProgram.standby_host to standby
                GpPkgProgram.segment_host_list to segment hosts
        """

        logger.debug('_get_gpdb_host_list')

        #Get host list
        gparr = GpArray.initFromCatalog(dbconn.DbURL(port=self.master_port),
                                        utility=True)
        master_host = None
        standby_host = None
        segment_host_list = []

        segs = gparr.getDbList()

        for seg in segs:
            if seg.isSegmentMaster(current_role=True):
                master_host = seg.getSegmentHostName()
            elif seg.isSegmentStandby(current_role=True):
                standby_host = seg.getSegmentHostName()
            else:
                segment_host_list.append(seg.getSegmentHostName())

        #Deduplicate the hosts so that we
        #dont install multiple times on the same host
        segment_host_list = list(set(segment_host_list))

        #Segments might exist on the master host. Since we store the
        #master host separately in self.master_host, storing the master_host
        #in the segment_host_list is redundant.
        for host in segment_host_list:
            if host == master_host or host == standby_host:
                segment_host_list.remove(host)

        self.master_host = master_host
        self.standby_host = standby_host
        self.segment_host_list = segment_host_list
Esempio n. 7
0
def check_table_exists(context, dbname, table_name, table_type=None, host=None, port=0, user=None):
    with dbconn.connect(dbconn.DbURL(hostname=host, port=port, username=user, dbname=dbname)) 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.execSQLForSingletonRow(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
Esempio n. 8
0
    def test_TableMainColumn_tablenames_exist(self):
        gpcheckcat_file = os.path.abspath(
            os.path.dirname(__file__) + "/../../../gpcheckcat")
        subject = imp.load_source('gpcheckcat', gpcheckcat_file)

        dburl = dbconn.DbURL(hostname=os.getenv('HOSTNAME', 'localhost'),
                             port=os.getenv('PGPORT', 5432),
                             dbname=os.getenv('PGDATABASE', 'postgres'))
        conn = dbconn.connect(dburl)
        table_query = "select count(*) from pg_class where relname='{table_name}'"

        # 5.json has an incomplete list of catalog tables
        # src/backend/catalog has .h files for some catalog tables
        # gpdb-doc/dita/ref_guide/system_catalogs/ has .xml files for almost all catalog tables
        for key in subject.TableMainColumn.keys():
            cursor = dbconn.execSQL(conn, table_query.format(table_name=key))
            self.assertTrue(cursor.rowcount == 1,
                            "%s not found in catalog dir" % key)
Esempio n. 9
0
def impl(context, dbname):

    SLICE_QUERY = """
select count(*) from (select * from
(select * from 
(select * from (select a from t2 group by a) SUBA, (select a from t1 group by a) SUBB) PARTA,
(select * from (select b from t2 group by b) SUBC, (select b from t1 group by b) SUBD) PARTB) SOUPA,
(select * from 
(select * from (select a from t2 group by a) SUBA, (select a from t1 group by a) SUBB) PARTA,
(select * from (select b from t2 group by b) SUBC, (select b from t1 group by b) SUBD) PARTB) SOUPB) FINALA;
    """

    try:
        with dbconn.connect(dbconn.DbURL(dbname=dbname)) as conn:
            context.slice_query_result = dbconn.execSQLForSingleton(
                conn, SLICE_QUERY)
    except Exception, e:
        context.dispatch_exception = e
Esempio n. 10
0
 def validate_columns(self, schema_name, table_name, sort_column_list):
     columns = []
     with dbconn.connect(dbconn.DbURL(dbname=self.database, port=self.port)) as conn:
         res = execSQL(conn,
                       """SELECT attname
                          FROM pg_attribute
                          WHERE attrelid = (SELECT pg_class.oid
                                            FROM pg_class, pg_namespace
                                            WHERE pg_class.relname = '{table}' AND pg_namespace.nspname = '{schema}'
                                            AND pg_class.relnamespace = pg_namespace.oid
                                            AND pg_class.relkind != 'v')"""
                              .format(table=table_name, schema=schema_name))
         for cols in res.fetchall():
             columns.append(cols[0].strip())
         for c in sort_column_list:
             if c[0] not in columns:
                 raise ExceptionNoStackTraceNeeded('Table {schema}.{table} does not have column {col}'
                                                    .format(schema=schema_name, table=table_name, col=c[0]))
Esempio n. 11
0
    def start(self):
        """
        Start the standby postmaster.  The options to pg_ctl needs to be
        determined by gppylib logic.
        """

        dburl = dbconn.DbURL()
        gparray = GpArray.initFromCatalog(dburl, utility=True)
        numcontent = gparray.getNumSegmentContents()
        standby = gparray.standbyMaster
        master = gp.MasterStart("Starting Master Standby",
                                self.datadir, self.port, standby.dbid,
                                0, numcontent, None, None, None)
        # -w option would wait forever.
        master.cmdStr = master.cmdStr.replace(' -w', '')
        master.run(validateAfter=True)

        return master.get_results()
    def prepare(self):
        sql = '''
            DROP TABLE IF EXISTS {tablename};

            CREATE TABLE {tablename} (
                c1 INT,
                c2 INT
            ) DISTRIBUTED BY (c1);
        '''.format(tablename=self.tablename)

        conn = dbconn.connect(dbconn.DbURL(dbname=self.dbname),
                              unsetSearchPath=False)
        dbconn.execSQL(conn, sql)

        self.prepare_extra(conn)

        conn.commit()
        conn.close()
Esempio n. 13
0
    def execute(self): 
        """ TODO: Improve with grouping by host and ParallelOperation dispatch. """
        gparray = GpArray.initFromCatalog(dbconn.DbURL(port=self.master_port), utility=True)   
        primaries = [seg for seg in gparray.getDbList() if seg.isSegmentPrimary(current_role=True)]
        dump_count = 0
        for seg in primaries:
            if seg.isSegmentDown():
                """ Why must every Segment function have the word Segment in it ?! """
                raise ExceptionNoStackTraceNeeded("Host %s dir %s dbid %d marked as invalid" % (seg.getSegmentHostName(), seg.getSegmentDataDirectory(), seg.getSegmentDbId()))

            path = os.path.join(seg.getSegmentDataDirectory(), DUMP_DIR, self.restore_timestamp[0:8])
            host = seg.getSegmentHostName()
            path = os.path.join(path, "%s0_%d_%s" % (DBDUMP_PREFIX, seg.getSegmentDbId(), self.restore_timestamp))
            if self.compress:
                path += ".gz"
            exists = CheckRemoteFile(path, host).run()
            if not exists:
                raise ExceptionNoStackTraceNeeded("No dump file on %s at %s" % (seg.getSegmentHostName(), path))
Esempio n. 14
0
 def _set_allowconn_template0(self, flag):
     """
     Enable connections to template0 on master and all segments.
     """
     if flag:
         logger.info("Enabling connections to template0")
     else:
         logger.info("Disabling connections to template0")
     for seg in self.gparray.getDbList(True):
         if seg.role == 'p':
             seg_url = dbconn.DbURL(hostname=seg.hostname, port=seg.port)
             with dbconn.connect(seg_url,
                                 utility=True,
                                 allowSystemTableMods='dml') as conn:
                 dbconn.execSQL(
                     conn, "update pg_database set datallowconn=%s "
                     "where datname='template0'" % flag)
                 conn.commit()
Esempio n. 15
0
    def corrupt_ct_logfile(self, write_string='*', corruption_offset=1, start_of_file=True):
        # Force flush CT file to disk
        PSQL.run_sql_command("CHECKPOINT", flags = '-q -t', dbname= 'postgres')

        gparray = GpArray.initFromCatalog(dbconn.DbURL())
        primary_segs = [seg for seg in gparray.getDbList() if seg.isSegmentPrimary()]
        for seg in primary_segs:
            file = '%s/pg_changetracking/CT_LOG_FULL' % seg.datadir
            try:
                with open(file, "r+b") as f:
                    if start_of_file:
                        f.seek(corruption_offset, 0)
                    else:
                        f.seek(corruption_offset, 2)
                    f.write(write_string)
                    f.close()
            except Exception, e:
                pass
Esempio n. 16
0
 def validate_table(self, schema_name, table_name):
     conn = dbconn.connect(
         dbconn.DbURL(dbname=self.database, port=self.port))
     try:
         c = dbconn.querySingleton(
             conn, """SELECT count(*)
                                    FROM pg_class, pg_namespace
                                    WHERE pg_namespace.nspname = '{schema}'
                                    AND pg_class.relname = '{table}'
                                    AND pg_class.relnamespace = pg_namespace.oid
                                    AND pg_class.relkind != 'v'""".format(
                 schema=schema_name, table=table_name))
         if not c:
             raise ExceptionNoStackTraceNeeded(
                 'Table {schema}.{table} does not exist'.format(
                     schema=schema_name, table=table_name))
     finally:
         conn.close()
Esempio n. 17
0
def drop_table(context, table_name, dbname, host=None, port=0, user=None):
    SQL = 'drop table %s' % table_name
    with dbconn.connect(
            dbconn.DbURL(hostname=host,
                         username=user,
                         port=port,
                         dbname=dbname)) as conn:
        dbconn.execSQL(conn, SQL)
        conn.commit()

    if check_table_exists(context,
                          table_name=table_name,
                          dbname=dbname,
                          host=host,
                          port=port,
                          user=user):
        raise Exception('Unable to successfully drop the table %s' %
                        table_name)
Esempio n. 18
0
    def _get_gpdb_host_list(self):
        """
        TODO: Perhaps the host list should be produced by gparray instead of here.

            This method gets the host names
            of all hosts in the gpdb array.
            It sets the following variables
                GpPkgProgram.coordinator_host to coordinator
                GpPkgProgram.standby_host to standby
                GpPkgProgram.segment_host_list to segment hosts
        """

        logger.debug('_get_gpdb_host_list')

        gparr = GpArray.initFromCatalog(
            dbconn.DbURL(port=self.coordinator_port), utility=True)
        coordinator_host = None
        standby_host = None
        segment_host_list = []

        segs = gparr.getDbList()

        for seg in segs:
            if seg.isSegmentCoordinator(current_role=True):
                coordinator_host = seg.getSegmentHostName()
            elif seg.isSegmentStandby(current_role=True):
                standby_host = seg.getSegmentHostName()
            else:
                segment_host_list.append(seg.getSegmentHostName())

        # Deduplicate the hosts so that we
        # dont install multiple times on the same host
        segment_host_list = list(set(segment_host_list))

        # Segments might exist on the coordinator host. Since we store the
        # coordinator host separately in self.coordinator_host, storing the coordinator_host
        # in the segment_host_list is redundant.
        for host in segment_host_list:
            if host == coordinator_host or host == standby_host:
                segment_host_list.remove(host)

        self.coordinator_host = coordinator_host
        self.standby_host = standby_host
        self.segment_host_list = segment_host_list
Esempio n. 19
0
    def validate_mid_level_partitions(self, schema_name, table_name):
        partition_level, max_level = None, None
        conn = dbconn.connect(
            dbconn.DbURL(dbname=self.database, port=self.port))
        try:
            parent_schema, parent_table = self.parent_partition_map[(
                schema_name, table_name)]
            if (parent_schema, parent_table) == (schema_name, table_name):
                return
            try:
                max_level = dbconn.querySingleton(
                    conn, """SELECT max(partitionlevel)
                                                      FROM pg_partitions
                                                      WHERE tablename='%s'
                                                      AND schemaname='%s'
                                                   """ %
                    (parent_table, parent_schema))
            except Exception as e:
                logger.debug(
                    'Unable to get the maximum partition level for table %s: (%s)'
                    % (table_name, str(e)))

            try:
                partition_level = dbconn.querySingleton(
                    conn, """SELECT partitionlevel
                                                            FROM pg_partitions
                                                            WHERE partitiontablename='%s'
                                                            AND partitionschemaname='%s'
                                                         """ %
                    (table_name, schema_name))
            except Exception as e:
                logger.debug(
                    'Unable to get the partition level for table %s: (%s)' %
                    (table_name, str(e)))

            if partition_level != max_level:
                logger.error(
                    'Partition level of the table = %s, Max partition level = %s'
                    % (partition_level, max_level))
                raise Exception(
                    'Mid Level partition %s.%s is not supported by gpreload. Please specify only leaf partitions or parent table name'
                    % (schema_name, table_name))
        finally:
            conn.close()
Esempio n. 20
0
    def verify(self, hostname=None, port=0):
        """
        Verify tablespace functionality by ensuring the tablespace can be
        written to, read from, and the initial data is still correctly
        distributed.
        """
        url = dbconn.DbURL(hostname=hostname, port=port, dbname=self.dbname)
        with dbconn.connect(url, unsetSearchPath=False) as conn:
            db = pg.DB(conn)
            data = db.query("SELECT gp_segment_id, i FROM tbl").getresult()

            # verify that we can still write to the tablespace
            self.table_counter += 1
            db.query("CREATE TABLE tbl_%s (i int) DISTRIBUTED RANDOMLY" % self.table_counter)
            db.query("INSERT INTO tbl_%s VALUES (GENERATE_SERIES(0, 25))" % self.table_counter)

        if sorted(data) != sorted(self.initial_data):
            raise Exception("Tablespace data is not identically distributed. Expected:\n%r\n but found:\n%r" % (
                sorted(self.initial_data), sorted(data)))
Esempio n. 21
0
 def check_indexes(self, schema_name, table_name):
     with closing(
             dbconn.connect(
                 dbconn.DbURL(dbname=self.database,
                              port=self.port))) as conn:
         c = dbconn.querySingleton(
             conn, """SELECT count(*)
                                          FROM pg_index
                                          WHERE indrelid = (SELECT pg_class.oid
                                                            FROM pg_class, pg_namespace
                                                            WHERE pg_class.relname='{table}' AND pg_namespace.nspname='{schema}' AND pg_class.relnamespace = pg_namespace.oid)"""
             .format(table=table_name, schema=schema_name))
         if c != 0:
             if self.interactive:
                 return ask_yesno(
                     None,
                     'Table {schema}.{table} has indexes. This might slow down table reload. Do you still want to continue ?'
                     .format(schema=schema_name, table=table_name), 'N')
     return True
    def test_start_gpdb_with_high_transaction_id(self):
        """
        
        @description GPDB hang after high transaction id
        @created 2013-04-18 00:00:00
        @modified 2013-04-18 00:00:00
        @tags transaction MPP-17302 MPP-17323 MPP-17325 MPP-18462 MPP-18463 schedule_transaction 
        
        @note This requires that both primary and mirror to reset xlog.

        Repro step from Hitoshi:
        gpstop -a
        pg_resetxlog -x 100000000 /data/haradh1/gpdata/d/gpseg0
        dd if=/dev/zero of=/data/haradh1/gpdata/d/gpseg0/pg_clog/0017 oflag=append conv=notrunc bs=1048576 count=1
        cp /data/haradh1/gpdata/d/gpseg0/pg_clog/0017 /data/haradh1/gpdata/d/gpseg0/pg_distributed/02FA
        gpstart -a
        """

        # @note: need a class to get GPDB configuration, need to get primary/mirror segment location
        sqlcmd = "select fselocation from gp_segment_configuration, pg_filespace_entry where dbid=fsedbid and content=0"
        with dbconn.connect(dbconn.DbURL()) as conn:
            segments = dbconn.execSQL(conn, sqlcmd)

        # @note: Issue with self.run_gpstop, hard-coded remoteHost to mdw
        # @note: upgrade model uses a series of gpstop and gpstart command, need helper classes
        cmd = GpStop("gpstop")
        cmd.run(validateAfter=True)

        for segment in segments:
            cmd = Command(name="reset xlog",
                          cmdStr="pg_resetxlog -x 100000000 %s" % segment[0])
            cmd.run()

            xlogfile = local_path('xlog_file')
            # @todo: able to copy the xlogfile remotely
            shutil.copyfile(xlogfile, "%s/pg_clog/0017" % segment[0])
            shutil.copyfile(xlogfile, "%s/pg_distributedlog/02FA" % segment[0])

        # @note: workaround the issue with tinc and 4.1 gppylib
        cmd = Command(name='run gpstop',
                      cmdStr='source %s/greenplum_path.sh;\
            gpstart -a' % os.environ['GPHOME'])
        cmd.run(validateAfter=True)
    def __init__(self, masterDataDir, readFromMasterCatalog, timeout=None, retries=None, verbose=True):
        """
        masterDataDir: if None then we try to find it from the system environment
        readFromMasterCatalog: if True then we will connect to the master in utility mode and fetch some more
                               data from there (like collation settings)

        """
        if masterDataDir is None:
            self.__masterDataDir = gp.get_masterdatadir()
        else: self.__masterDataDir = masterDataDir

        logger.debug("Obtaining master's port from master data directory")
        pgconf_dict = pgconf.readfile(self.__masterDataDir + "/postgresql.conf")
        self.__masterPort = pgconf_dict.int('port')
        logger.debug("Read from postgresql.conf port=%s" % self.__masterPort)
        self.__masterMaxConnections = pgconf_dict.int('max_connections')
        logger.debug("Read from postgresql.conf max_connections=%s" % self.__masterMaxConnections)

        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 master
        if readFromMasterCatalog:
            dbUrl = dbconn.DbURL(port=self.__masterPort, dbname='template1', timeout=timeout, retries=retries)
            conn = dbconn.connect(dbUrl, utility=True)
            (self.__lcCollate, self.__lcMonetary, self.__lcNumeric) = catalog.getCollationSettings(conn)

            # MPP-13807, read/show the master's database version too
            self.__pgVersion = dbconn.execSQLForSingletonRow(conn, "select version();")[0]
            logger.info("master Greenplum Version: '%s'" % self.__pgVersion)
            conn.close()

            checkNotNone("lc_collate", self.__lcCollate)
            checkNotNone("lc_monetary", self.__lcMonetary)
            checkNotNone("lc_numeric", self.__lcNumeric)
        else:
            self.__lcCollate = None
            self.__lcMonetary = None
            self.__lcNumeric = None
            self.__pgVersion = None
Esempio n. 24
0
def impl(context, seg):
    gparray = GpArray.initFromCatalog(dbconn.DbURL())

    if seg == "primary":
        primary_segs = [
            seg for seg in gparray.getDbList() if seg.isSegmentPrimary()
        ]
        context.pseg = primary_segs[0]
        context.pseg_data_dir = context.pseg.getSegmentDataDirectory()
        context.pseg_hostname = context.pseg.getSegmentHostName()
        context.pseg_dbid = context.pseg.getSegmentDbId()
    elif seg == "mirror":
        mirror_segs = [
            seg for seg in gparray.getDbList() if seg.isSegmentMirror()
        ]
        context.mseg = mirror_segs[0]
        context.mseg_hostname = context.mseg.getSegmentHostName()
        context.mseg_dbid = context.mseg.getSegmentDbId()
        context.mseg_data_dir = context.mseg.getSegmentDataDirectory()
Esempio n. 25
0
    def execute(self):
        existing_tables = []
        table_counts = []
        conn = None
        try:
            dburl = dbconn.DbURL(port=self.master_port, dbname=self.restore_db)
            conn = dbconn.connect(dburl)
            for restore_table in self.restore_tables:
                if '.' not in restore_table:
                    logger.warn(
                        "No schema name supplied for %s, removing from list of tables to restore"
                        % restore_table)
                    continue

                schema, table = restore_table.split('.')
                count = execSQLForSingleton(
                    conn,
                    "select count(*) from pg_class, pg_namespace where pg_class.relname = '%s' and pg_class.relnamespace = pg_namespace.oid and pg_namespace.nspname = '%s'"
                    % (table, schema))
                if count == 0:
                    logger.warn(
                        "Table %s does not exist in database %s, removing from list of tables to restore"
                        % (table, self.restore_db))
                    continue

                count = execSQLForSingleton(
                    conn, "select count(*) from %s.%s" % (schema, table))
                if count > 0:
                    logger.warn('Table %s has %d records %s' %
                                (restore_table, count, WARN_MARK))
                existing_tables.append(restore_table)
                table_counts.append((restore_table, count))
        finally:
            if conn is not None:
                conn.close()

        if len(existing_tables) == 0:
            raise ExceptionNoStackTraceNeeded("Have no tables to restore")
        logger.info("Have %d tables to restore, will continue" %
                    len(existing_tables))

        return (existing_tables, table_counts)
Esempio n. 26
0
    def test_pg_inherits(self):
        """
        Change order of children in pg_inherits on segments.  Alter should not
        cause inconsistent OIDs.

        """
        # Create paritioned table.
        sql = local_path("create_part_table.sql")
        out = local_path("create_part_table.out")
        ans = local_path("create_part_table.ans")
        PSQL.run_sql_file(sql, out)
        assert Gpdiff.are_files_equal(out, ans)

        # Change order of children in pg_inherits on segments but not
        # on master.
        sql = local_path("reorder_pg_inherits.sql")
        out = local_path("reorder_pg_inherits.out")
        ans = local_path("reorder_pg_inherits.ans")
        segments = [
            seg for seg in self.gparray.getSegDbList() if seg.role == "p"
        ]
        assert len(segments) > 0, "No primary segments found."
        primary = segments[0]
        PSQL.run_sql_file(sql,
                          out,
                          host=primary.hostname,
                          port=primary.port,
                          PGOPTIONS=("-c allow_system_table_mods=dml "
                                     "-c gp_session_role=utility"))
        assert Gpdiff.are_files_equal(out, ans)

        # Alter the partitioned table so that it's rewritten.
        with dbconn.connect(dbconn.DbURL()) as conn:
            dbconn.execSQL(conn, "ALTER TABLE co1 ALTER COLUMN c2 TYPE int8")
            conn.commit()

        # Run gpcheckcat
        result = GpdbVerify().gpcheckcat(testname="inconsistent")
        # Test return code
        if result[0] != 0:
            logger.error(result[2])  # log output
            self.fail("gpcheckcat 'inconsistent' test failed")
Esempio n. 27
0
    def run_pg_rewind(self, rewindInfo):
        """
        Run pg_rewind for incremental recovery.
        """

        rewindFailedSegments = []
        # Run pg_rewind on all the targets
        for targetSegment, sourceHostName, sourcePort in rewindInfo:
            # Do CHECKPOINT on source to force TimeLineID to be updated in pg_control.
            # pg_rewind wants that to make incremental recovery successful finally.
            self.__logger.debug('Do CHECKPOINT on %s (port: %d) before running pg_rewind.' % (sourceHostName, sourcePort))
            dburl = dbconn.DbURL(hostname=sourceHostName,
                                 port=sourcePort,
                                 dbname='template1')
            conn = dbconn.connect(dburl, utility=True)
            dbconn.execSQL(conn, "CHECKPOINT")

            # If the postmaster.pid still exists and another process
            # is actively using that pid, pg_rewind will fail when it
            # tries to start the failed segment in single-user
            # mode. It should be safe to remove the postmaster.pid
            # file since we do not expect the failed segment to be up.
            self.remove_postmaster_pid_from_remotehost(
                targetSegment.getSegmentHostName(),
                targetSegment.getSegmentDataDirectory())

            # Run pg_rewind to do incremental recovery.
            cmd = gp.SegmentRewind('segment rewind',
                                   targetSegment.getSegmentHostName(),
                                   targetSegment.getSegmentDataDirectory(),
                                   sourceHostName,
                                   sourcePort,
                                   verbose=gplog.logging_is_verbose())
            try:
                cmd.run(True)
                self.__logger.debug('pg_rewind results: %s' % cmd.results)
            except base.ExecutionError as e:
                self.__logger.debug("pg_rewind failed for target directory %s." % targetSegment.getSegmentDataDirectory())
                self.__logger.warning("Incremental recovery failed for dbid %s. You must use gprecoverseg -F to recover the segment." % targetSegment.getSegmentDbId())
                rewindFailedSegments.append(targetSegment)

        return rewindFailedSegments
Esempio n. 28
0
def wait_for_unblocked_transactions(context, num_retries=150):
    """
    Tries once a second to successfully commit a transaction to the database
    running on PGHOST/PGPORT. Raises an Exception after failing <num_retries>
    times.
    """
    attempt = 0
    while attempt < num_retries:
        try:
            with dbconn.connect(dbconn.DbURL()) as conn:
                # Cursor.execute() will issue an implicit BEGIN for us.
                conn.cursor().execute('COMMIT')
                break
        except Exception as e:
            attempt += 1
            pass
        time.sleep(1)

    if attempt == num_retries:
        raise Exception('Unable to establish a connection to database !!!')
Esempio n. 29
0
def populate_partition(tablename,
                       start_date,
                       dbname,
                       data_offset,
                       rowcount=1094,
                       host=None,
                       port=0,
                       user=None):
    insert_sql_str = "insert into %s select i+%d, 'backup', i + date '%s' from generate_series(0,%d) as i" % (
        tablename, data_offset, start_date, rowcount)
    insert_sql_str += "; insert into %s select i+%d, 'restore', i + date '%s' from generate_series(0,%d) as i" % (
        tablename, data_offset, start_date, rowcount)

    with dbconn.connect(
            dbconn.DbURL(hostname=host,
                         port=port,
                         username=user,
                         dbname=dbname)) as conn:
        dbconn.execSQL(conn, insert_sql_str)
        conn.commit()
Esempio n. 30
0
 def testReadPostmasterTempFile(self):
     logger.info("testReadPostmasterTempFile")
     url = dbconn.DbURL()
     gpdb = GpArray.initFromCatalog(url)
     
     logger.info("Search for valid master port: %s" % gpdb.master.port)
     cmd = pg.ReadPostmasterTempFile.local('test pg tempfile read',gpdb.master.port)        
     (exists,PID,datadir)=cmd.getResults()
     logger.info("exists:=%s PID=%d datadir='%s'" % (exists,PID,datadir))                
     self.assertTrue(exists)
     self.assertTrue(PID > 0)
     self.assertEquals(datadir,gpdb.master.datadir)
     
     
     gpdb.master.port=4000
     logger.info("Search for bogus master port: %s" % gpdb.master.port)        
     cmd = pg.ReadPostmasterTempFile.local('test pg tempfile read',gpdb.master.port)        
     (exists,PID,datadir)=cmd.getResults()
     logger.info("exists:=%s PID=%d datadir='%s'" % (exists,PID,datadir))        
     self.assertFalse(exists)