Re: Can you make a simple view non-updatable?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Ryan Murphy <ryanfmurphy(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Can you make a simple view non-updatable?
Date: 2018-06-08 13:27:28
Message-ID: 1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2018-06-08 14:02:22 Re: Code of Conduct plan
Previous Message Geoff Winkless 2018-06-08 13:10:00 Re: manipulating NUMERIC values in C extension