pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Steven Roth <steve@rothskeller.net>
Cc: Melvin Davidson <melvin6925@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: UPSERT on a view
Date: Tue, 13 Mar 2018 10:18:08 -0400
Message-ID: <21002.1520950688@sss.pgh.pa.us> (raw)
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>

I wrote:
>> On Mon, Mar 12, 2018 at 2:26 PM, Steven Roth <steve@rothskeller.net> 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




view thread (6+ messages)

Message-ID: <21002.1520950688@sss.pgh.pa.us>
Permalink:  ../21002.1520950688@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21002.1520950688@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, steve@rothskeller.net, melvin6925@gmail.com
  Subject: Re: UPSERT on a view
  In-Reply-To: <21002.1520950688@sss.pgh.pa.us>
  References: <CAAnpqKH7B0aWnAPUsXTPQBi3_52tAD_PQ_mAf1x7NsdE8_fXdQ@mail.gmail.com> <CANu8FiznoYDtLONXpBZjNa75JbAHWetwzUVGr6pfuJdjRG_ZOg@mail.gmail.com> <20510.1520950238@sss.pgh.pa.us> <21002.1520950688@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