why copy tuple in the end of trigger when nothing changed in NEW OLD record variable

From: "billy" <billywq(at)163(dot)com>
To: pghacker <pgsql-hackers(at)postgresql(dot)org>
Subject: why copy tuple in the end of trigger when nothing changed in NEW OLD record variable
Date: 2008-06-11 03:21:31
Message-ID: 484F44CE.0452E0.04501@m12-15.163.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi pghackers:

version 8.3.0 in function plpgsql_exec_trigger.

in a trigger, if NEW is returned as the result and we do nothing to NEW.

for example, we have a table like this:

create table test (a int);
insert into test values(1);

and a trigger like:

create or replace function test_trigger() returns trigger as $$
begin
return new;
end; $$language plpgsql;

create trigger before_update_test before update
on test for each row execute procedure test_trigger();

in this trigger, we don't change the value of NEW.

than execute:

update test set a = 3;

after execution:

/* Copy tuple to upper executor memory */
rettup = SPI_copytuple((HeapTuple) (estate.retval));

we come to function ExecUpdate():

HeapTuple newtuple;
newtuple = ExecBRUpdateTriggers(estate, resultRelInfo,
tupleid, tuple);

Since the trigger's return value is copied to another memory address, the newtuple is impossible equal to the oldtuple.

so the following condition:

if (newtuple != tuple) /* modified by Trigger(s) */
{

is FALSE for ever.

I think we can add some judgment conditions in function plpgsql_exec_trigger() to avoid this problem.

        billy
        billywq(at)163(dot)com
          2008-06-11

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2008-06-11 03:43:03 Re: why copy tuple in the end of trigger when nothing changed in NEW OLD record variable
Previous Message Andrew Dunstan 2008-06-11 01:41:47 Re: Proposal - improve eqsel estimates by including histogram bucket numdistinct statistics