PostgreSQL does not allow altering a view (i.e. adding column, changing column orders, 开发者_如何学编程adding criterie etc.) if it has dependent objects. This is really getting annoying since you have to write a script to:
- Drop all the dependent objects,
- Alter the view,
- Recreate all the dependent objects back again.
I understand that postgreSQL developers have very reasonable concerns to prevent altering views. But do you guys have any scripts/shot-cuts to do all those manual stuff in a single run?
Adding new columns isn't a problem, changing datatypes or changing the order of the columns, that's where you get problems.
Don't change the order, it's not that important anyway, just change your query:
SELECT a, b FROM view_name;
SELECT b, a FROM view_name;
When you have to change a datatype of a column, you have to check the depend objects as well. These might have problems with this new datatype. Just get the definition of this object and recreate after the changes. The information_schema and pg_catalog help you out.
- Make all changes within a single transaction.
If I place a addtional "drop view xyz; commit;" before the "create or replace view xyz as ..." statement, at least in many cases I resolve the blocking problem described above.
精彩评论