简体   繁体   中英

Clear access share locks on postgres relations after select query

I understand that when we run a select query on a postgres table, it acquires a AccessShareLock . It gets created only on the tables I have run select queries but not on others. I am using sqlalchemy to interact with postgres and all I wanted is whether we can clear the AccessShareLock after the select statement is executed (below is an example copied from here

from sqlalchemy.orm import sessionmaker
from models import OneTable, get_engine

engine = get_engine(database="mydb")
session = sessionmaker(bind=engine)()

results = session.query(OneTable.company_name).all()

# need to remove "AccessShareLock" lock on OneTable
# without explicitly calling session.close() or engine.dispose()

session.close()

All I need is release the lock after querying the table. I am running some daemon services which queries the database at regular intervals (say once in an hour), but my DDL statements on this table are not executed as it needs AccessExclusiveLock . Any suggestions on how to get this done or how should I be doing to solve this problem

The only way to release a lock on a table is to terminate the transaction that took the lock, either with COMMIT or ROLLBACK . Locks are always held until the end of the transaction.

So the way to get what you need is to make sure that all your transactions are short. Apart from solving your problem, this will also make sure that autovacuum can remove old row versions effectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM