pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Melvin Davidson <melvin6925@gmail.com>
Cc: Steven Roth <steve@rothskeller.net>
Cc: pgsql-general@postgresql.org >> PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: UPSERT on a view
Date: Tue, 13 Mar 2018 10:10:38 -0400
Message-ID: <20510.1520950238@sss.pgh.pa.us> (raw)
In-Reply-To: <CANu8FiznoYDtLONXpBZjNa75JbAHWetwzUVGr6pfuJdjRG_ZOg@mail.gmail.com>
References: <CAAnpqKH7B0aWnAPUsXTPQBi3_52tAD_PQ_mAf1x7NsdE8_fXdQ@mail.gmail.com>
	<CANu8FiznoYDtLONXpBZjNa75JbAHWetwzUVGr6pfuJdjRG_ZOg@mail.gmail.com>

Melvin Davidson <melvin6925@gmail.com> writes:
> On Mon, Mar 12, 2018 at 2:26 PM, Steven Roth <steve@rothskeller.net> 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




view thread (6+ messages)  latest in thread

Message-ID: <20510.1520950238@sss.pgh.pa.us>
Permalink:  ../20510.1520950238@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/20510.1520950238@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, melvin6925@gmail.com, steve@rothskeller.net
  Subject: Re: UPSERT on a view
  In-Reply-To: <20510.1520950238@sss.pgh.pa.us>
  References: <CAAnpqKH7B0aWnAPUsXTPQBi3_52tAD_PQ_mAf1x7NsdE8_fXdQ@mail.gmail.com> <CANu8FiznoYDtLONXpBZjNa75JbAHWetwzUVGr6pfuJdjRG_ZOg@mail.gmail.com> <20510.1520950238@sss.pgh.pa.us>

* 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