Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evkdY-00058b-8s for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 14:10:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evkdX-0006xm-AR for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 14:10:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evkdW-0006xb-VE for pgsql-general@lists.postgresql.org; Tue, 13 Mar 2018 14:10:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evkdU-0001ke-K9 for pgsql-general@postgresql.org; Tue, 13 Mar 2018 14:10:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w2DEAcxu020511; Tue, 13 Mar 2018 10:10:38 -0400 From: Tom Lane To: Melvin Davidson cc: Steven Roth , "pgsql-general@postgresql.org >> PG-General Mailing List" Subject: Re: UPSERT on a view In-reply-to: References: Comments: In-reply-to Melvin Davidson message dated "Tue, 13 Mar 2018 09:47:14 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20509.1520950238.1@sss.pgh.pa.us> Date: Tue, 13 Mar 2018 10:10:38 -0400 Message-ID: <20510.1520950238@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Melvin Davidson writes: > On Mon, Mar 12, 2018 at 2:26 PM, Steven Roth wrote: >> Why does the following code raise an error? >> >> CREATE TABLE ttest (x integer); >> CREATE VIEW vtest AS SELECT x FROM ttest; >> CREATE FUNCTION vtest_insert() RETURNS trigger LANGUAGE plpgsql AS $$ >> RAISE 'foo' USING ERRCODE='unique_violation'; >> END $$; >> CREATE TRIGGER vtest_insert INSTEAD OF INSERT ON vtest >> FOR EACH ROW EXECUTE PROCEDURE vtest_insert(); >> INSERT INTO vtest VALUES (1) ON CONFLICT DO NOTHING; >> >> This code raises the error 'foo', even though the insert says DO NOTHING >> and the error type is unique_violation. Why? > *Your problem is that A. TRIGGERS are meant for TABLES, not views* > *and * > *B. You CANNOT insert into a VIEW.* No, instead-of triggers are supposed to do exactly that. The OP's problem is that he imagines that ON CONFLICT works by catching arbitrary errors and checking to see if they are labeled unique_violation. Doesn't work like that; if it did, it'd be the wrong thing, because we would not know whether or how to roll back any other side effects the trigger had had. >> More generally: how can one write trigger functions for a view (that is >> not automatically updatable), such that INSERT ... ON CONFLICT DO UPDATE >> will work with the correct semantics? Don't think it's possible unfortunately. The trigger would have to take care of the situation, but it doesn't have any way to know that the calling query has an ON CONFLICT rule. regards, tom lane