Postgres: build function or procedure to assist in column changes

Postgres is a lot more restrictive when it comes to adding/deleting columns to views, changing column names of a table, changing data types of a column, etc. If the column is used somewhere else Postgres will not allow it. In some cases, like if you try to delete a table, it will tell you which views prevent you from doing that.

You have to go find anywhere where the object is used, edit each view or drop all views, make your table change, then recreate the views. This is dangerous as if you have multiple nested views, or if you’re deep into a project and there are many views using a table, you risk losing your work, dropping and not creating in the same order, etc.

My idea here is that we at the very least come up with a script to query the database metadata to assist in this effort. At a minimum, you input a table or column and it outputs the views where it is used. The max ask here would be for the SP to execute the change for you - it would store the drop and create view scripts in temp variables, then execute the drop script, make the column change, execute the recreate script.

Please authenticate to join the conversation.

Upvoters
Status

In Review

Board

Syniti Knowledge Platform

Date

20 days ago

Author

jay.hornback@syniti.com

Subscribe to post

Get notified by email when there are changes.