How to make a view not updatable in postgresql
I am using PostgreSQL to store business data in large amount for data warehousing purpose. I am writing views to customize the materialized tables.
I am wondering whether it is possible to set a view not updatable.
I have checked th开发者_运维百科e postgresql manual, but found no specific answer.
Many thanks in advance.
PostgreSQL doesn't have updatable views, you have to create RULE's to make this possible. Without these extra rules, you can't do an update on a view.
From the manual:
What happens if a view is named as the target relation for an INSERT, UPDATE, or DELETE? After doing the substitutions described above, we will have a query tree in which the result relation points at a subquery range-table entry. This will not work, so the rewriter throws an error if it sees it has produced such a thing.
Views in PostreSQL aren't updatable.
The accepted answer is out-of-date - PostgreSQL currently supports updatable views so the question is actually relevant:
https://www.postgresql.org/docs/14/rules-views.html#RULES-VIEWS-UPDATE
In order to make a view non-updatable, one simply needs to avoid granting the INSERT/UPDATE/DELETE privileges on any role for the view (or revoke such privileges if they are already granted).
Credit for the answer goes to RhodiumToad (https://twitter.com/rhodiumtoad, https://github.com/RhodiumToad)
精彩评论