Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VH6HM-0002Bt-BE for pgsql-sql@arkaria.postgresql.org; Wed, 04 Sep 2013 06:09:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VH6HK-0005U4-G0 for pgsql-sql@arkaria.postgresql.org; Wed, 04 Sep 2013 06:09:22 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VH6HJ-0005Tx-DU for pgsql-sql@postgresql.org; Wed, 04 Sep 2013 06:09:21 +0000 Received: from woozle.rinet.ru ([195.54.192.68]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VH6HG-0001ub-LX for pgsql-sql@postgresql.org; Wed, 04 Sep 2013 06:09:20 +0000 Received: from localhost (localhost [127.0.0.1]) by woozle.rinet.ru (8.14.5/8.14.5) with ESMTP id r8469HVo098577 for ; Wed, 4 Sep 2013 10:09:17 +0400 (MSK) (envelope-from marck@rinet.ru) Date: Wed, 4 Sep 2013 10:09:17 +0400 (MSK) From: Dmitry Morozovsky To: pgsql-sql@postgresql.org Subject: Re: Trigger for updating view with join In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (BSF 1167 2008-08-23) X-NCC-RegID: ru.rinet X-OpenPGP-Key-ID: 6B691B03 MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (woozle.rinet.ru [0.0.0.0]); Wed, 04 Sep 2013 10:09:17 +0400 (MSK) X-Pg-Spam-Score: -3.7 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Heh, it's me spamming you again :) now -- asking for comments. > > create trigger fsl_update instead of insert or update on fsl ... > > > > but till now did not succeed in it. Quick googlink did not help either. > > Argh. My google-fu is definitely low in the night ;) > > for the record: it's right at > http://www.postgresql.org/docs/9.1/static/plpgsql-trigger.html > example 39-5 this example is only related, as the second one found at http://vibhork.blogspot.ru/2011/10/updateable-views-in-postgresql-91-using.html (the latter does not allow inserting one-to many records) after a bit if try'n'error, I'm with the following --- 8< --- create table machines ( mid serial not null primary key, mname text not null unique ); create table fs ( fsid serial not null primary key, mid int not null references machines, mpoint text not null ); create unique index fs_mp on fs(mid, mpoint); create view fsl as select fsid, mid, mname, mpoint from fs join machines using(mid); create or replace function update_fsl() returns trigger as $$ declare mmid bigint; begin if (TG_OP = 'DELETE') then -- only fs is deleted, not machine delete from fs where mpoint = OLD.mpoint and mid = (select mid from machines where mname=OLD.mname); if NOT FOUND then return NULL; else return OLD; end if; elsif (TG_OP = 'INSERT') then select mid into mmid from machines where mname=NEW.mname; if NOT FOUND then insert into machines (mname) values (NEW.mname); select mid into mmid from machines where mname=NEW.mname; end if; insert into fs (mid, mpoint) select mmid, NEW.mpoint; return NEW; elsif (TG_OP = 'UPDATE') then -- only mpoint is allowed to be changed update fs set mpoint = NEW.mpoint where mpoint = OLD.mpoint and mid = (select mid from machines where mname=OLD.mname); if NOT FOUND then return NULL; else return NEW; end if; end if; end; $$ language plpgsql; create trigger update_fsl instead of insert or update or delete on fsl for each row execute procedure update_fsl(); --- 8< --- I looks all requested operations are fine, but I'm a bit reluctant: maybe I missed something easy but important? Thanks! -- Sincerely, D.Marck [DM5020, MCK-RIPE, DM3-RIPN] [ FreeBSD committer: marck@FreeBSD.org ] ------------------------------------------------------------------------ *** Dmitry Morozovsky --- D.Marck --- Wild Woozle --- marck@rinet.ru *** ------------------------------------------------------------------------ -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql