Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to lock a table for a specified time in PostgreSQL database by running psql commands via shell script or sql file?

If we run the LOCK TABLE command, when the script exits the lock will also be gone, so that is not sufficient.

share|improve this question
2  
Why do you want to lock a table for a longer time than the script? You are probably doing something wrong. – LtWorf 14 hours ago

1 Answer

Use pg_sleep for your specified time in conjunction with LOCK TABLE? Something like the script below should lock a table for 60 seconds (note this is untested):

BEGIN WORK;
LOCK TABLE MyTable IN ACCESS EXCLUSIVE MODE;
SELECT pg_sleep(60);
COMMIT WORK;
share|improve this answer
Thanks a lot. It is working as expected with this logic – user2485200 10 hours ago
If the suggested script works, then please mark this as the accepted answer and upvote when possible. :) – Talvalin 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.