On 06/08/2018 01:38 AM, Ryan Murphy wrote:
> Hello.
>
> I enjoy using VIEWs. Often my views are updatable, either automatically
> (due to being a simple 1-table view, or due to a TRIGGER). Sometimes
> they are meant to be just read-only.
>
> Is there any way to set a VIEW to be read-only -- specifically, can I do
> this for a view that is automatically updatable due to being simple?
Using INSTEAD OF trigger?:
create view ct_vw as select * from container;
insert into ct_vw (c_id, cdesc, cell_per, c_size, c_units) values
('test', 'test container', 1, 2, 4);
INSERT 1836533 1
CREATE OR REPLACE FUNCTION public.vw_ro()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Read only view';
RETURN NULL;
END;
$function$
CREATE TRIGGER ro_trg INSTEAD OF INSERT or UPDATE or DELETE ON ct_vw FOR
EACH ROW EXECUTE procedure vw_ro();
insert into ct_vw (c_id, cdesc, cell_per, c_size, c_units) values
('test', 'test container', 1, 2, 4);
NOTICE: Read only view
INSERT 0 0
update ct_vw set cell_per = 100 where c_id = '200PT';
NOTICE: Read only view
UPDATE 0
delete from ct_vw where c_id = '200PT';
NOTICE: Read only view
DELETE 0
>
> The reason I want this: It will help me encode into my schema the
> distinction between views that are supposed to behave like full-fledged
> "subtypes" of a larger relation and need to be updatable, vs those that
> are merely a report / literally just a "view".
>
> Thanks!
> Ryan
--
Adrian Klaver
adrian.klaver@aklaver.com