Home > Software engineering >  Move table to different schema in postgres
Move table to different schema in postgres

Time:01-25

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!

CodePudding user response:

  • 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)

  •  Tags:  
  • Related