Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ozeA1-0006gk-7L for pgsql-general@arkaria.postgresql.org; Mon, 28 Nov 2022 13:27:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ozeA0-0002J3-4S for pgsql-general@arkaria.postgresql.org; Mon, 28 Nov 2022 13:27:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oze9z-0002It-JY for pgsql-general@lists.postgresql.org; Mon, 28 Nov 2022 13:26:59 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oze9r-0005Ba-Qu for pgsql-general@lists.postgresql.org; Mon, 28 Nov 2022 13:26:58 +0000 Received: from [192.168.0.104] (unknown [62.217.188.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id B334C21C24CD; Mon, 28 Nov 2022 16:26:49 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1669642009; bh=+kce/Y4UexdjMZoZ8Wl6ZRSu1zO+O5CXjiy1d85i3UY=; h=Date:Subject:To:References:From:In-Reply-To; b=E6TxeNltHEeDE6XRi8BcDF3isUucOAVzBMOt7FMBM637/ZZ3Q7Tx9fw8yXPwesa94 Ua/9Uh4wEjBWaNSoVUWwvK+D5F7RCnYYC/5ytIICFLkZ8zk/etiG4uKZusTYSadwRf eGDzCScQ1yTQzLlXHGCKqb+uGnRiHYGa8vPrVkio= Content-Type: multipart/alternative; boundary="------------0jQQYOl7F2H65Ju3myRLIVWh" Message-ID: <02375b28-3e55-f873-5a93-de7b965206f5@postgrespro.ru> Date: Mon, 28 Nov 2022 16:26:49 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: how to implement add using upsert and trigger? Content-Language: en-US To: "yin.zhb@163.com" , pgsql-general References: <202211282037367351626@163.com> From: Pavel Luzanov In-Reply-To: <202211282037367351626@163.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0jQQYOl7F2H65Ju3myRLIVWh Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, I think you need:     insert into stat_result(itemid,value,cnt) values(new.itemid, new.value, 1)     on conflict(itemid) do update     set value = stat_result.value + new.value, cnt = stat_result.cnt +1; excluded.value and new.value is the same value from inserted record, but your need a current value from stat_result. On 28.11.2022 15:37, yin.zhb@163.com wrote: > Hi, all: >   when I using upsert and trigger to update other table automatically: > create table stat_detail(itemid bigint not null, value bigint, captime > bigint); > create table stat_result(itemid bigint primary key, value bigint, cnt > bigint); > create or replace function inertfunc() returns trigger as $$ > begin > insert into stat_result(itemid,value,cnt) values(new.itemid, new.value, 1) > on conflict(itemid) do update > set value = excluded.value + new.value, cnt = excluded.cnt +1 where > excluded.itemid = new.itemid; > return new; > end; > $$ > language plpgsql; > create trigger tri_insert after insert on stat_detail for each row > execute function inertfunc(); > postgres=# insert into stat_detail values(100,1,1); > INSERT 0 1 > postgres=# select * from stat_result ; >  itemid | value | cnt > --------+-------+----- > 100 | 1 | 1 > (1 row) > postgres=# insert into stat_detail values(100,1,1); > INSERT 0 1 > postgres=# select * from stat_result ; >  itemid | value | cnt > --------+-------+----- > 100 | 2 | 2 > (1 row) > postgres=# insert into stat_detail values(100,1,1); > INSERT 0 1 > postgres=# select * from stat_result ; >  itemid | value | cnt > --------+-------+----- > 100 | 2 | 2 > (1 row) > > But  I want it is "100 3 3". So how I can do? > > ------------------------------------------------------------------------ > yin.zhb@163.com -- Pavel Luzanov Postgres Professional:https://postgrespro.com The Russian Postgres Company --------------0jQQYOl7F2H65Ju3myRLIVWh Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, I think you need:

=C2=A0=C2=A0=C2=A0 insert into stat_result(itemid,value,cnt) values(n= ew.itemid, new.value, 1)
=C2=A0=C2=A0=C2=A0 on conflict(itemid) do update
=C2=A0=C2=A0=C2=A0 set value =3D stat_result.value + new.value, cnt =3D= stat_result.cnt +1;

excluded.value and new.value is the same value from inserted record, but your need a current value from stat_result.

On 28.11.2022 15:37, yin.zhb@163.com wrote:
=
Hi, all=EF=BC=9A
=C2=A0 when I using upsert and trigger to update other table automatically=EF=BC=9A
cre= ate table stat_detail(itemi= d bigint not null, value bigint= , captime bigint);
create table stat_result(itemid bi= gint primary key, value bigint, cnt bigint);
create or replace function inertfunc() returns trigger as $$
begin
= =C2=A0 =C2=A0 insert into stat_result(itemid,value,cnt) values(n= ew.itemid, new.value, 1)
=C2=A0 =C2=A0 on conflict(itemid)= do update
=C2=A0 =C2=A0= set value =3D excluded.value + new.value, cnt =3D excluded.cnt +1 whe= re excluded.itemid =3D new.itemid;
=C2=A0 =C2=A0 return new;
end;
$$
language plpgsql;
create trigger tri_insert after insert on stat_detail for = each row execute function ine= rtfunc();
postgres=3D# insert into stat_detail values(100,1,1);=
INSERT 0 1
postgres=3D# select * from stat_result ;
=C2=A0itemid |= value | cnt
--------+-------+-----
=C2=A0 =C2=A0= 100 | =C2=A0 =C2=A0 1 | =C2=A0 1
(1 row)
postgres=3D# insert into stat_detail values(100,1,1);=
INSERT 0 1
postgres=3D# select * from stat_result ;
=C2=A0itemid |= value | cnt
--------+-------+-----
=C2=A0 =C2=A0= 100 | =C2=A0 =C2=A0 2 | =C2=A0 2
(1 row)
postgres=3D# insert into stat_detail values(100,1,1);=
INSERT 0 1
postgres=3D# select * from stat_result ;
=C2=A0itemid |= value | cnt
--------+-------+-----
=C2=A0 =C2=A0= 100 | =C2=A0 =C2=A0 2 | =C2=A0 2
(1 row)

But=C2=A0 I want it is "100 3 3". So how I can do?



--=20
Pavel Luzanov
Postgres Professional: https://postgrespro.com
The Russian Postgres Company
--------------0jQQYOl7F2H65Ju3myRLIVWh--