I have a postgres db that runs on an AWS RDS instance. It contains a somewhat complicated materialized view (created by four left joins). I would like to drop it but the query never completes (no error message, it seems to hang). I am using Postico to execute my SQL queries.
I don’t need this mv anymore, so I really only need to figure out how to drop it.
Any suggestions? Thank you!
2
Answers
Maybe there are running queries preventing to drop the object.
Try to rename it first.
ALTER MATERIALIZED VIEW name
RENAME TO new_name
This way, new queries will fail.
Or try to disable new connections as explained here
PostgreSQL: Temporarily disable connections
The system views
pg_stat_activity
can tell you what’s everyone doing, andpg_locks
who’s holding what. You can once again re-experience your discomfort byfirst one’s enough to get her locked (up).
drop view daughter;
, pretending this means you’re casually trying to drop daughter off to school – it’ll hang and you’ll have to wait while she’s still hanging out with that punk from session B. Luckily, until the situation’s resolved, it would now also hang if someone tried toalter view daughter rename to their_girlfriend;
, so, there’s some silver lining.The post-mortem you shared confirms that’s pretty much what was going down. If you’re very important, with little regard for whoever stands in your way, you can
pg_cancel_backend()
If on top of being that important and ruthless, you’re also in a big rush or Liam Neeson, you can go ahead and
pg_terminate_backend()
(you brute):The first option is like finding the guy who’s talking to your
daughter
and telling them to hang up. The second one is like finding them and ending the conversation by smacking them across the face hard enough that you break both their phone and potentially their jaw.Note that in both cases above, you’re actually doing that to anyone doing anything about any
daughter
, not just yours. But one: you get the point, two: that would actually send a good message.If you want to be surgical about this, you can refer to lock monitoring at PostgreSQL wiki. Also,
pg_stat_activity
can tell you exactly you who, since when, using what application, from what ip is trying to do what. With all that noted that down you easily track them down, can sue them into oblivion, get a restraining order, get their parole officer involved.The default behaviour of putting you on hold and telling you to wait is actually a reasonable middle ground between strictness and good manners: it’s waiting for everyone to finish what they had started doing, while already blocking newcomers. A third session trying to access would wait for the first one to complete, then the second one would be allowed to finish the drop, then the third one would be told
daughter
doesn’t exist. Also, everyone involved can use all that wait time to cool off their temper.