Beispiel #1
0
def obtain_transaction_lock(session: Session, lock_space: Optional[str],
                            lock_value: int) -> None:
    """Obtain a transaction-level advisory lock from PostgreSQL.

    The lock_space arg must be either None or the name of one of the members of the
    LockSpaces enum (case-insensitive). Contention for a lock will only occur when both
    lock_space and lock_value have the same values.
    """
    if lock_space:
        try:
            lock_space_value = LockSpaces[lock_space.upper()].value
        except KeyError:
            raise ValueError("Invalid lock space: %s" % lock_space)

        session.query(func.pg_advisory_xact_lock(lock_space_value,
                                                 lock_value)).one()
    else:
        session.query(func.pg_advisory_xact_lock(lock_value)).one()
Beispiel #2
0
def lock_table(connection: Connection, target_table: Table):
    """
    Lock a table using a PostgreSQL advisory lock

    The OID of the table in the pg_class relation is used as lock id.
    :param connection: DB connection
    :param target_table: Table object
    """
    logger.debug('Locking table "%s"', target_table.name)
    oid = connection.execute(
        select([column("oid")]).select_from(table("pg_class")).where(
            (column("relname") == target_table.name))).scalar()
    connection.execute(select([func.pg_advisory_xact_lock(oid)])).scalar()
Beispiel #3
0
Datei: db.py Projekt: agdsn/hades
def lock_table(connection: Connection, target_table: Table):
    """
    Lock a table using a PostgreSQL advisory lock

    The OID of the table in the pg_class relation is used as lock id.
    :param connection: DB connection
    :param target_table: Table object
    """
    logger.debug('Locking table "%s"', target_table.name)
    oid = connection.execute(select([column("oid")])
                             .select_from(table("pg_class"))
                             .where((column("relname") == target_table.name))
                             ).scalar()
    connection.execute(select([func.pg_advisory_xact_lock(oid)])).scalar()