0

I would like to change schema of few tables in my Postgres DB. Problem is that all the time there are long running queries and as I understand schema change needs exclusive lock.

Question is how can I do it? Of course I can kill all existing queries and try to do schema rename (move table to different schema) but there is a huge chance that in the meantime new queries will appear.

Thanks for help!

1
  • "I understand schema change needs exclusive lock." - correct. You will have to block new connections until that change is through (which only takes a few seconds, as it is only a "rename" not a real physical move) Commented Jan 24, 2022 at 13:53

1 Answer 1

5
  • run SELECT pg_backend_pid() before running the ALTER TABLE

  • start the ALTER TABLE statement

  • in a second database session, run SELECT pg_blocking_pids(12345), where 12345 is the result from the first query

  • cancel all the blocking transactions found with the previous query with SELECT pg_cancel_backend(23456)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.