src/dirbs/partition_utils.py
def repartition_blacklist(conn, *, num_physical_shards, src_filter_sql=None):
    """
    Function to repartition the blacklist table.

    Arguments:
        conn: dirbs db connection object
        num_physical_shards: number of shard to partition with
        src_filter_sql: custom filtration sql, default None
    """
    with conn.cursor() as cursor, utils.db_role_setter(conn, role_name='dirbs_core_listgen'):
        # Create parent partition
        cursor.execute(
            """CREATE TABLE blacklist_new (
                   LIKE blacklist INCLUDING DEFAULTS
                                  INCLUDING IDENTITY
                                  INCLUDING CONSTRAINTS
                                  INCLUDING STORAGE
                                  INCLUDING COMMENTS
               )
               PARTITION BY RANGE (virt_imei_shard)
            """
        )

        # Create child partitions
        create_imei_shard_partitions(conn, tbl_name='blacklist_new', num_physical_shards=num_physical_shards,
                                     perms_func=_grant_perms_list, fillfactor=80)

        # Insert data from original partition
        base_sql = sql.SQL("""INSERT INTO blacklist_new
                                   SELECT *
                                     FROM blacklist""")
        if src_filter_sql is not None:
            insert_sql = sql.SQL('{0} {1}').format(base_sql, sql.SQL(src_filter_sql))
        else:
            insert_sql = base_sql
        cursor.execute(insert_sql)

        # Add in indexes to each partition
        idx_metadata = [
            IndexMetadatum(idx_cols=cols, is_unique=is_uniq, partial_sql=partial)
            for cols, is_uniq, partial in [
                (['imei_norm'], True, 'WHERE end_run_id IS NULL'),
                (['end_run_id'], False, None),
                (['start_run_id'], False, None)
            ]
        ]
        add_indices(conn, tbl_name='blacklist_new', idx_metadata=idx_metadata)

        # Drop old table, rename tables, indexes and constraints
        cursor.execute('ALTER SEQUENCE blacklist_row_id_seq OWNED BY blacklist_new.row_id')
        cursor.execute('DROP TABLE blacklist CASCADE')
        rename_table_and_indices(conn, old_tbl_name='blacklist_new',
                                 new_tbl_name='blacklist', idx_metadata=idx_metadata)

    Function to repartition the blacklist table.

    Arguments:
        conn: dirbs db connection object
        num_physical_shards: number of shard to partition with
        src_filter_sql: custom filtration sql, default None
    
Function to repartition the blacklist table.
Function to repartition the blacklist table .
def repartition_blacklist(conn, *, num_physical_shards, src_filter_sql=None):
    with conn.cursor() as cursor, utils.db_role_setter(conn, role_name='dirbs_core_listgen'):
        cursor.execute(
            """CREATE TABLE blacklist_new (
                   LIKE blacklist INCLUDING DEFAULTS
                                  INCLUDING IDENTITY
                                  INCLUDING CONSTRAINTS
                                  INCLUDING STORAGE
                                  INCLUDING COMMENTS
               )
               PARTITION BY RANGE (virt_imei_shard)
            """
        )
        create_imei_shard_partitions(conn, tbl_name='blacklist_new', num_physical_shards=num_physical_shards,
                                     perms_func=_grant_perms_list, fillfactor=80)
        base_sql = sql.SQL("""INSERT INTO blacklist_new
                                   SELECT *
                                     FROM blacklist""")
        if src_filter_sql is not None:
            insert_sql = sql.SQL('{0} {1}').format(base_sql, sql.SQL(src_filter_sql))
        else:
            insert_sql = base_sql
        cursor.execute(insert_sql)
        idx_metadata = [
            IndexMetadatum(idx_cols=cols, is_unique=is_uniq, partial_sql=partial)
            for cols, is_uniq, partial in [
                (['imei_norm'], True, 'WHERE end_run_id IS NULL'),
                (['end_run_id'], False, None),
                (['start_run_id'], False, None)
            ]
        ]
        add_indices(conn, tbl_name='blacklist_new', idx_metadata=idx_metadata)
        cursor.execute('ALTER SEQUENCE blacklist_row_id_seq OWNED BY blacklist_new.row_id')
        cursor.execute('DROP TABLE blacklist CASCADE')
        rename_table_and_indices(conn, old_tbl_name='blacklist_new',
                                 new_tbl_name='blacklist', idx_metadata=idx_metadata)
def repartition_blacklist ( conn , * , num_physical_shards , src_filter_sql = None ) : with conn . cursor ( ) as cursor , utils . db_role_setter ( conn , role_name = 'dirbs_core_listgen' ) : cursor . execute ( """CREATE TABLE blacklist_new (
                   LIKE blacklist INCLUDING DEFAULTS
                                  INCLUDING IDENTITY
                                  INCLUDING CONSTRAINTS
                                  INCLUDING STORAGE
                                  INCLUDING COMMENTS
               )
               PARTITION BY RANGE (virt_imei_shard)
            """ ) create_imei_shard_partitions ( conn , tbl_name = 'blacklist_new' , num_physical_shards = num_physical_shards , perms_func = _grant_perms_list , fillfactor = 80 ) base_sql = sql . SQL ( """INSERT INTO blacklist_new
                                   SELECT *
                                     FROM blacklist""" ) if src_filter_sql is not None : insert_sql = sql . SQL ( '{0} {1}' ) . format ( base_sql , sql . SQL ( src_filter_sql ) ) else : insert_sql = base_sql cursor . execute ( insert_sql ) idx_metadata = [ IndexMetadatum ( idx_cols = cols , is_unique = is_uniq , partial_sql = partial ) for cols , is_uniq , partial in [ ( [ 'imei_norm' ] , True , 'WHERE end_run_id IS NULL' ) , ( [ 'end_run_id' ] , False , None ) , ( [ 'start_run_id' ] , False , None ) ] ] add_indices ( conn , tbl_name = 'blacklist_new' , idx_metadata = idx_metadata ) cursor . execute ( 'ALTER SEQUENCE blacklist_row_id_seq OWNED BY blacklist_new.row_id' ) cursor . execute ( 'DROP TABLE blacklist CASCADE' ) rename_table_and_indices ( conn , old_tbl_name = 'blacklist_new' , new_tbl_name = 'blacklist' , idx_metadata = idx_metadata )
Function to repartition the blacklist table.
Function to repartition the blacklist table .
"""
    Function to repartition the blacklist table.

    Arguments:
        conn: dirbs db connection object
        num_physical_shards: number of shard to partition with
        src_filter_sql: custom filtration sql, default None
    """ # Create parent partition # Create child partitions # Insert data from original partition # Add in indexes to each partition # Drop old table, rename tables, indexes and constraints
conn
dirbs db connection object
dirbs db connection object
num_physical_shards
number of shard to partition with
number of shard to partition with
src_filter_sql
custom filtration sql, default None
custom filtration sql default None