UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
От:
jian he <jian.universality@gmail.com>
Дата:
Hi.
CREATE OR REPLACE FUNCTION trigger_info()
RETURNS TRIGGER LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'trigger name: %: TG_OP: % WHEN: % TG_LEVEL: %',
TG_NAME, TG_WHEN, TG_OP, TG_LEVEL;
RAISE NOTICE ' old: %', OLD;
RAISE NOTICE ' old: %', NEW;
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
END;
$$;
drop table if exists ts;
create table ts(a int4range, b int);
insert into ts values('[1,10)', 2), ('[1,10)', 3);
CREATE TRIGGER ts_trig1
BEFORE INSERT OR UPDATE OR DELETE ON ts
FOR EACH STATEMENT EXECUTE PROCEDURE trigger_info();
update ts for portion of a from 2 to 4 set b = 12;
The above UPDATE statement is triggering the BEFORE FOR EACH STATEMENT
action four times.
This appears to contradict the documentation mentioned below.
Am I missing something?
https://www.postgresql.org/docs/devel/sql-createtrigger.html
"""
In contrast, a trigger that is marked FOR EACH STATEMENT only executes
once for any given operation, regardless of how many rows it modifies
(in particular, an operation that modifies zero rows will still result
in the execution of any applicable FOR EACH STATEMENT triggers).
"""
--
jian
https://www.enterprisedb.com/
Re: UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
От:
Thom Brown <thom@linux.com>
Дата:
On Sun, 3 May 2026, 05:49 jian he, <jian.universality@gmail.com> wrote:
Hi.
CREATE OR REPLACE FUNCTION trigger_info()
RETURNS TRIGGER LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'trigger name: %: TG_OP: % WHEN: % TG_LEVEL: %',
TG_NAME, TG_WHEN, TG_OP, TG_LEVEL;
RAISE NOTICE ' old: %', OLD;
RAISE NOTICE ' old: %', NEW;
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
END;
$$;
drop table if exists ts;
create table ts(a int4range, b int);
insert into ts values('[1,10)', 2), ('[1,10)', 3);
CREATE TRIGGER ts_trig1
BEFORE INSERT OR UPDATE OR DELETE ON ts
FOR EACH STATEMENT EXECUTE PROCEDURE trigger_info();
update ts for portion of a from 2 to 4 set b = 12;
The above UPDATE statement is triggering the BEFORE FOR EACH STATEMENT
action four times.
This appears to contradict the documentation mentioned below.
Am I missing something?
https://www.postgresql.org/docs/devel/sql-createtrigger.html
"""
In contrast, a trigger that is marked FOR EACH STATEMENT only executes
once for any given operation, regardless of how many rows it modifies
(in particular, an operation that modifies zero rows will still result
in the execution of any applicable FOR EACH STATEMENT triggers).
"""
This appears to be triggering on the internal DML produced by the temporal tables functionality.
What if you debug, outputting pg_trigger_depth()? I would expect to see one at depth 0 and the other 3 to be at depth 1.
Thom
Re: UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
От:
Paul A Jungwirth <pj@illuminatedcomputing.com>
Дата:
On Sat, May 2, 2026 at 9:49 PM jian he wrote:
> drop table if exists ts;
> create table ts(a int4range, b int);
> insert into ts values('[1,10)', 2), ('[1,10)', 3);
> CREATE TRIGGER ts_trig1
> BEFORE INSERT OR UPDATE OR DELETE ON ts
> FOR EACH STATEMENT EXECUTE PROCEDURE trigger_info();
>
> update ts for portion of a from 2 to 4 set b = 12;
>
> The above UPDATE statement is triggering the BEFORE FOR EACH STATEMENT
> action four times.
> This appears to contradict the documentation mentioned below.
> Am I missing something?
In fact it fires once for the update, and four times for the inserts.
This behavior is correct and intended. The standard says that the
inserts for temporal leftovers are "statements", so we decided to fire
statement triggers on them. We talked about this a lot at the last
PGConf and in some emails afterwards. Here is a mailing list thread
with lots of details:
https://www.postgresql.org/message-id/ec498c3d-5f2b-48ec-b989-5561c8aa2024%40illuminatedcomputing.com
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com
Re: UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
От:
Thom Brown <thom@linux.com>
Дата:
On Sun, 3 May 2026 at 09:24, Thom Brown wrote:
>
> On Sun, 3 May 2026, 05:49 jian he, wrote:
>>
>> Hi.
>>
>> CREATE OR REPLACE FUNCTION trigger_info()
>> RETURNS TRIGGER LANGUAGE plpgsql AS
>> $$
>> BEGIN
>> RAISE NOTICE 'trigger name: %: TG_OP: % WHEN: % TG_LEVEL: %',
>> TG_NAME, TG_WHEN, TG_OP, TG_LEVEL;
>> RAISE NOTICE ' old: %', OLD;
>> RAISE NOTICE ' old: %', NEW;
>> IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
>> RETURN NEW;
>> ELSIF TG_OP = 'DELETE' THEN
>> RETURN OLD;
>> END IF;
>> END;
>> $$;
>>
>> drop table if exists ts;
>> create table ts(a int4range, b int);
>> insert into ts values('[1,10)', 2), ('[1,10)', 3);
>> CREATE TRIGGER ts_trig1
>> BEFORE INSERT OR UPDATE OR DELETE ON ts
>> FOR EACH STATEMENT EXECUTE PROCEDURE trigger_info();
>>
>> update ts for portion of a from 2 to 4 set b = 12;
>>
>> The above UPDATE statement is triggering the BEFORE FOR EACH STATEMENT
>> action four times.
>> This appears to contradict the documentation mentioned below.
>> Am I missing something?
>>
>> https://www.postgresql.org/docs/devel/sql-createtrigger.html
>> """
>> In contrast, a trigger that is marked FOR EACH STATEMENT only executes
>> once for any given operation, regardless of how many rows it modifies
>> (in particular, an operation that modifies zero rows will still result
>> in the execution of any applicable FOR EACH STATEMENT triggers).
>> """
>
>
> This appears to be triggering on the internal DML produced by the temporal tables functionality.
>
> What if you debug, outputting pg_trigger_depth()? I would expect to see one at depth 0 and the other 3 to be at depth 1.
Actually, I'm wrong here. It outputs all at depth 1. This would have
been a way to implement a barrier into the function so that you can
decide whether the trigger only fires for the original statement.
So might there be a scenario where a user would wish to distinguish
between an explicit INSERT from an INSERT generated by the temporal
table machinery? None of the TG_* variables expose anything that would
allow one to do this. Sure, the user could pattern match against
current_query() and see that it really is an INSERT, but it's somewhat
kludgy.
Thom
Re: UPDATE/DELETE FOR PORTION OF fire FOR EACH STATEMENT more than once
От:
Paul A Jungwirth <pj@illuminatedcomputing.com>
Дата:
On Sun, May 3, 2026 at 3:50 PM Thom Brown wrote:
>
> Actually, I'm wrong here. It outputs all at depth 1. This would have
> been a way to implement a barrier into the function so that you can
> decide whether the trigger only fires for the original statement.
>
> So might there be a scenario where a user would wish to distinguish
> between an explicit INSERT from an INSERT generated by the temporal
> table machinery? None of the TG_* variables expose anything that would
> allow one to do this. Sure, the user could pattern match against
> current_query() and see that it really is an INSERT, but it's somewhat
> kludgy.
That's one way, but I agree it's kludgy. I have some uncommitted
patches at [1] to (1) expose the FOR PORTION OF parameters to triggers
in the C struct (2) use that info ourselves to implement CASCADE/SET
NULL/SET DEFAULT against temporal foreign keys (3) expose the same
information in PL/pgSQL TG_* variables. But those would only be set
for UPDATE/DELETE triggers, not the INSERT trigger. But maybe we
should pass them to INSERT triggers too, if the insert is the result
of an UPDATE/DELETE FOR PORTION OF? It seems a little strange, but it
would be unambiguous, and it would give useful information to users
who want to bypass triggers in that case.
[1] https://commitfest.postgresql.org/patch/5836/
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com