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 1evkkp-0005cF-KT for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 14:18:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evkkn-0000gj-PY for pgsql-general@arkaria.postgresql.org; Tue, 13 Mar 2018 14:18:13 +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 1evkkn-0000ga-E5 for pgsql-general@lists.postgresql.org; Tue, 13 Mar 2018 14:18:13 +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 1evkkj-0001yZ-Uy for pgsql-general@lists.postgresql.org; Tue, 13 Mar 2018 14:18:12 +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 w2DEI8eI021003; Tue, 13 Mar 2018 10:18:08 -0400 From: Tom Lane To: Steven Roth cc: Melvin Davidson , pgsql-general@lists.postgresql.org Subject: Re: UPSERT on a view In-reply-to: <20510.1520950238@sss.pgh.pa.us> References: <20510.1520950238@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 13 Mar 2018 10:10:38 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21001.1520950688.1@sss.pgh.pa.us> Date: Tue, 13 Mar 2018 10:18:08 -0400 Message-ID: <21002.1520950688@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote: >> On Mon, Mar 12, 2018 at 2:26 PM, Steven Roth wrote: >>> 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. BTW, I don't suppose it solves your problem, but ON CONFLICT does work with auto-updatable views: regression=# CREATE TABLE ttest (x integer primary key); CREATE TABLE regression=# CREATE VIEW vtest AS SELECT x FROM ttest; CREATE VIEW regression=# INSERT INTO vtest VALUES (1); INSERT 0 1 regression=# INSERT INTO vtest VALUES (1); ERROR: duplicate key value violates unique constraint "ttest_pkey" DETAIL: Key (x)=(1) already exists. regression=# INSERT INTO vtest VALUES (1) ON CONFLICT DO NOTHING; INSERT 0 0 The query rewriter knows how to translate the ON CONFLICT to apply to the underlying table along with the rest of the command. regards, tom lane