How to LOCK TABLE for PostgreSQL

18 views
Skip to first unread message

jens.t...@gmail.com

unread,
Feb 23, 2023, 10:44:01 PM2/23/23
to sqlalchemy
Hello,

In order to use the PostgreSQL LOCK with SQLAlchemy, is there a better way than

session.execute(sa.text(f"LOCK TABLE {Foo.__tablename__} IN SHARE MODE"))

Poking through the dialect docs or SQLA at large, I didn’t find much. This post is a little dated, and this comment seems to recommend the above approach (using a connection).

Also, do I need to flush() to push the LOCK to the db?

Much thanks!
Jens

Mike Bayer

unread,
Feb 24, 2023, 12:10:28 AM2/24/23
to noreply-spamdigest via sqlalchemy


On Thu, Feb 23, 2023, at 10:44 PM, jens.t...@gmail.com wrote:
Hello,

In order to use the PostgreSQL LOCK with SQLAlchemy, is there a better way than

session.execute(sa.text(f"LOCK TABLE {Foo.__tablename__} IN SHARE MODE"))

I would probably use session.connection().exec_driver_sql() for more succinctness. 

if you want to build a custom SQL construct you can check out @compiles at https://docs.sqlalchemy.org/en/20/core/compiler.html



Also, do I need to flush() to push the LOCK to the db?

not at all, execute() as well as connection().execute(), connection().exec_driver_sql() push the given SQL right out, which you can see by adding echo=True to your create_engine().




Much thanks!
Jens


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages