Re: UPSERT wiki page, and SQL MERGE syntax

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: Kevin Grittner <kgrittn(at)ymail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>, Andres Freund <andres(at)2ndquadrant(dot)com>
Subject: Re: UPSERT wiki page, and SQL MERGE syntax
Date: 2014-10-09 01:56:54
Message-ID: CABRT9RBX_QvpvLnWcJj9k6=wS+HgJHK=uMk7365Gu4qMj1+uPA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Oct 9, 2014 at 4:25 AM, Peter Geoghegan <pg(at)heroku(dot)com> wrote:
> On Wed, Oct 8, 2014 at 6:12 PM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
>> Skipping
>> BEFORE UPDATE entirely seems to violate POLA.

> Good thing that the patch doesn't do that, then. I clearly documented
> this in a few places, including:
> http://postgres-benchmarks.s3-website-us-east-1.amazonaws.com/on-conflict-docs/trigger-definition.html

I tried to understand the docs and found them somewhat confusing, so I
turned to testing the implementation you posted on 2014-10-07. It
shows no signs of running UPDATE triggers in the following case. So is
this just a bug or a missing feature?

create table evt_type (id serial primary key, name text unique,
evt_count int, update_count int default 0);
prepare upsert(text) as
INSERT into evt_type (name, evt_count) values ($1, 1)
on conflict within evt_type_name_key
UPDATE set evt_count=conflicting(evt_count)+1;
create or replace function ins() returns trigger language plpgsql as
$$begin raise notice 'trigger % %', TG_WHEN, TG_OP; return new; end$$;
create or replace function upd() returns trigger language plpgsql as
$$begin raise notice 'trigger % %', TG_WHEN, TG_OP;
new.update_count=new.update_count+1; return new; end$$;
create trigger ev1 before insert on evt_type execute procedure ins();
create trigger ev2 before update on evt_type execute procedure upd();
create trigger ev3 after insert on evt_type execute procedure ins();
create trigger ev4 after update on evt_type execute procedure upd();

db=# execute upsert('foo');
NOTICE: trigger BEFORE INSERT
NOTICE: trigger AFTER INSERT
INSERT 0 1
db=# execute upsert('foo');
NOTICE: trigger BEFORE INSERT
NOTICE: trigger AFTER INSERT
INSERT 0 0
marti=# table evt_type;
id | name | evt_count | update_count
----+------+-----------+--------------
1 | foo | 2 | 0

>> MySQL gets away with lots of things, they have several other caveats
> No true Scotsman.

Eh? I'm just saying there may be good reasons not to imitate MySQL here.

Regards,
Marti

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Marti Raudsepp 2014-10-09 02:04:56 Re: UPSERT wiki page, and SQL MERGE syntax
Previous Message Peter Geoghegan 2014-10-09 01:25:02 Re: UPSERT wiki page, and SQL MERGE syntax