pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: henry.hinze@gmail.com
Subject: BUG #16185: Trigger fires twice and with wrong TG_OP when updating partion key on partitioned table
Date: Fri, 03 Jan 2020 15:41:21 +0000
Message-ID: <16185-ad3eced5747aa2b8@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16185
Logged by:          Henry Hinze
Email address:      henry.hinze@gmail.com
PostgreSQL version: 12.1
Operating system:   Ubuntu 16.04.6 LTS
Description:        

If a trigger is defined on a partitioned table and the partition key is
updated that the row is moved to another partition, then the trigger is
fired twice first with TG_OP = 'DELETE' followed by TG_OP = 'INSERT'.  As a
user I would expect that the trigger fires only once with TG_OP =
'UPDATE'.

To reproduce:
****************
create table t (id int, p_key int) partition by list (p_key);

create table t1 partition of t for values in (1);
create table t2 partition of t for values in (2);

insert into t (id,p_key) values (1,1);

create or replace function f()
    RETURNS TRIGGER
    LANGUAGE plpgsql
AS
$function$
BEGIN
    RAISE NOTICE 'TG_OP: %', TG_OP;
    RETURN NULL;
END;
$function$;

CREATE TRIGGER f_trig
    AFTER INSERT or UPDATE or DELETE
    ON t
    FOR EACH ROW
EXECUTE PROCEDURE f();

postgres=# update t set p_key = 2 where id = 1;
NOTICE:  TG_OP: DELETE
NOTICE:  TG_OP: INSERT
UPDATE 1



Message-ID: <16185-ad3eced5747aa2b8@postgresql.org>
Permalink:  ../16185-ad3eced5747aa2b8@postgresql.org/
Also on:    postgresql.org/message-id/16185-ad3eced5747aa2b8@postgresql.org

 · 

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-bugs@postgresql.org
  Cc: noreply@postgresql.org, henry.hinze@gmail.com
  Subject: Re: BUG #16185: Trigger fires twice and with wrong TG_OP when updating partion key on partitioned table
  In-Reply-To: <16185-ad3eced5747aa2b8@postgresql.org>
  References: <16185-ad3eced5747aa2b8@postgresql.org>

* 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