pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ryan Murphy <ryanfmurphy@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Can you make a simple view non-updatable?
Date: Fri, 8 Jun 2018 06:27:28 -0700
Message-ID: <1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com> (raw)
In-Reply-To: <CAHeEsBdFBLNLYZ-paRNzNXZLJ-4VHXWu3PfBqWN0z1yV12ibpw@mail.gmail.com>
References: <CAHeEsBdFBLNLYZ-paRNzNXZLJ-4VHXWu3PfBqWN0z1yV12ibpw@mail.gmail.com>

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




view thread (7+ messages)  latest in thread

Message-ID: <1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com>
Permalink:  ../1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com/
Also on:    postgresql.org/message-id/1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, ryanfmurphy@gmail.com
  Subject: Re: Can you make a simple view non-updatable?
  In-Reply-To: <1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com>
  References: <CAHeEsBdFBLNLYZ-paRNzNXZLJ-4VHXWu3PfBqWN0z1yV12ibpw@mail.gmail.com> <1cc0d88c-52f5-e4a0-cf21-ab5e645a90e1@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox