pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedSegmentation fault during update inside ExecBRUpdateTriggers
8+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2019-08-16 1ced082b95 03813a50ef aed967d697 1fe8d209ed cb0c79ae62 afa71d9152 60886965a2 Prevent possible double-free when update trigger returns old tuple.
* Segmentation fault during update inside ExecBRUpdateTriggers
@ 2019-08-15 21:33 Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 8+ messages in thread
From: Piotr Gabriel Kosinski @ 2019-08-15 21:33 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org
Hello,
The following code causes a segmentation fault (confirmed in versions
11.4 on Debian Buster, 11.5 on Debian Buster and Arch Linux 64-bit):
CREATE TABLE foo (id SERIAL NOT NULL PRIMARY KEY, bar INTEGER, baz
INTEGER, ud TIMESTAMPTZ, ud2 TIMESTAMPTZ);
CREATE OR REPLACE FUNCTION udu() RETURNS TRIGGER AS $$
BEGIN
NEW.ud := current_timestamp;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION ud2u() RETURNS TRIGGER AS $$
BEGIN
IF row(NEW.bar) IS DISTINCT FROM row(OLD.bar) THEN
NEW.ud2 := current_timestamp;
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER udt
BEFORE UPDATE ON foo
FOR EACH ROW EXECUTE PROCEDURE udu();
CREATE TRIGGER ud2t
BEFORE UPDATE ON foo
FOR EACH ROW EXECUTE PROCEDURE ud2u();
INSERT INTO foo (bar, baz) VALUES (1, 2);
UPDATE foo SET baz = 5 WHERE id = 1;
Backtrace on Debian Buster:
#0 0x000055c9e358b0c0 in ?? ()
#1 0x000055c9e133d144 in ExecBRUpdateTriggers
(estate=estate@entry=0x55c9e3583190,
epqstate=epqstate@entry=0x55c9e35845c0,
relinfo=relinfo@entry=0x55c9e3583420,
tupleid=tupleid@entry=0x7fff0e1565da,
fdw_trigtuple=fdw_trigtuple@entry=0x0, slot=0x55c9e3589688) at
./build/../src/backend/commands/trigger.c:3065
#2 0x000055c9e138258e in ExecUpdate
(mtstate=mtstate@entry=0x55c9e3584500, tupleid=0x7fff0e1565da,
oldtuple=0x0, slot=<optimized out>, planSlot=0x55c9e3584dd0,
epqstate=epqstate@entry=0x55c9e35845c0,
estate=0x55c9e3583190, canSetTag=true) at
./build/../src/backend/executor/nodeModifyTable.c:974
#3 0x000055c9e1382f72 in ExecModifyTable (pstate=0x55c9e3584500) at
./build/../src/backend/executor/nodeModifyTable.c:2166
#4 0x000055c9e135df3b in ExecProcNode (node=0x55c9e3584500) at
./build/../src/include/executor/executor.h:247
#5 ExecutePlan (execute_once=<optimized out>, dest=0x55c9e357ede0,
direction=<optimized out>, numberTuples=0, sendTuples=<optimized out>,
operation=CMD_UPDATE, use_parallel_mode=<optimized out>,
planstate=0x55c9e3584500, estate=0x55c9e3583190) at
./build/../src/backend/executor/execMain.c:1723
#6 standard_ExecutorRun (queryDesc=0x55c9e35780c0,
direction=<optimized out>, count=0, execute_once=<optimized out>) at
./build/../src/backend/executor/execMain.c:364
#7 0x000055c9e14b7fc7 in ProcessQuery (plan=<optimized out>,
sourceText=0x55c9e348c180 "UPDATE foo SET baz = 5 WHERE id = 1;",
params=0x0, queryEnv=0x0, dest=0x55c9e357ede0,
completionTag=0x7fff0e156920 "") at ./build/../src/backend/tcop/pquery.c:161
#8 0x000055c9e14b820b in PortalRunMulti
(portal=portal@entry=0x55c9e3525c60, isTopLevel=isTopLevel@entry=true,
setHoldSnapshot=setHoldSnapshot@entry=false,
dest=dest@entry=0x55c9e357ede0,
altdest=altdest@entry=0x55c9e357ede0,
completionTag=completionTag@entry=0x7fff0e156920 "") at
./build/../src/backend/tcop/pquery.c:1286
#9 0x000055c9e14b8e0f in PortalRun
(portal=portal@entry=0x55c9e3525c60,
count=count@entry=9223372036854775807,
isTopLevel=isTopLevel@entry=true, run_once=run_once@entry=true,
dest=dest@entry=0x55c9e357ede0,
altdest=altdest@entry=0x55c9e357ede0, completionTag=0x7fff0e156920
"") at ./build/../src/backend/tcop/pquery.c:799
#10 0x000055c9e14b4cce in exec_simple_query
(query_string=0x55c9e348c180 "UPDATE foo SET baz = 5 WHERE id = 1;")
at ./build/../src/backend/tcop/postgres.c:1145
#11 0x000055c9e14b6527 in PostgresMain (argc=<optimized out>,
argv=argv@entry=0x55c9e34ec2c8, dbname=<optimized out>,
username=<optimized out>) at
./build/../src/backend/tcop/postgres.c:4182
#12 0x000055c9e14402d2 in BackendRun (port=0x55c9e34e3a80) at
./build/../src/backend/postmaster/postmaster.c:4358
#13 BackendStartup (port=0x55c9e34e3a80) at
./build/../src/backend/postmaster/postmaster.c:4030
#14 ServerLoop () at ./build/../src/backend/postmaster/postmaster.c:1707
#15 0x000055c9e1441176 in PostmasterMain (argc=5, argv=0x55c9e3486c30)
at ./build/../src/backend/postmaster/postmaster.c:1380
#16 0x000055c9e11bddc9 in main (argc=5, argv=0x55c9e3486c30) at
./build/../src/backend/main/main.c:228
Regards,
Piotr Kosinski
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
@ 2019-08-15 22:23 ` Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Thomas Munro @ 2019-08-15 22:23 UTC (permalink / raw)
To: Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; +Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
On Fri, Aug 16, 2019 at 9:55 AM Piotr Gabriel Kosinski
<pg.kosinski@gmail.com> wrote:
> Backtrace on Debian Buster:
>
> #0 0x000055c9e358b0c0 in ?? ()
> #1 0x000055c9e133d144 in ExecBRUpdateTriggers
> (estate=estate@entry=0x55c9e3583190,
> epqstate=epqstate@entry=0x55c9e35845c0,
> relinfo=relinfo@entry=0x55c9e3583420,
> tupleid=tupleid@entry=0x7fff0e1565da,
> fdw_trigtuple=fdw_trigtuple@entry=0x0, slot=0x55c9e3589688) at
> ./build/../src/backend/commands/trigger.c:3065
Hi,
Right, this happens on REL_11_STABLE but not on master (which rewrote
the relevant code quite a bit in the "slotification" project). It's a
double free, here:
for (i = 0; i < trigdesc->numtriggers; i++)
{
...
if (oldtuple != newtuple && oldtuple != slottuple)
heap_freetuple(oldtuple);
...
}
if (trigtuple != fdw_trigtuple && trigtuple != newtuple)
heap_freetuple(trigtuple);
In a very quick test, the following change fixes the problem and
passes regression tests, but I'm not sure if it's the right fix.
- if (oldtuple != newtuple && oldtuple != slottuple)
+ if (oldtuple != newtuple && oldtuple != slottuple &&
oldtuple != trigtuple)
--
Thomas Munro
https://enterprisedb.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
@ 2019-08-15 22:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:09 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Andres Freund <andres@anarazel.de>
2019-08-15 23:26 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 8+ messages in thread
From: Tom Lane @ 2019-08-15 22:39 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Thomas Munro <thomas.munro@gmail.com> writes:
> Right, this happens on REL_11_STABLE but not on master (which rewrote
> the relevant code quite a bit in the "slotification" project).
We should probably trace back why it doesn't happen before v11.
I have a vague memory of having touched this code a year or two
back, so likely this is my fault, but I wonder why it doesn't
fail before.
> In a very quick test, the following change fixes the problem and
> passes regression tests, but I'm not sure if it's the right fix.
> - if (oldtuple != newtuple && oldtuple != slottuple)
> + if (oldtuple != newtuple && oldtuple != slottuple &&
> oldtuple != trigtuple)
My thoughts were headed in the same direction. It looks like the
issue is that the first trigger returns OLD, ie "trigtuple",
which gets assigned to "newtuple", and then the second iteration
of the loop fails to deal with the aliasing.
[ thinks some more... ] Actually, I'm beginning to recall that
we made changes here because v11 plpgsql is capable of actually
returning "trigtuple" where before it would always have made a copy.
If that's accurate, then very likely the bug exists further back
but requires some other PL than plpgsql to manifest.
I'd be inclined to put a test case exercising this into all branches,
even ones not currently showing the bug, because it's clearly a
fragile area.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-08-15 23:09 ` Andres Freund <andres@anarazel.de>
2019-08-15 23:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 8+ messages in thread
From: Andres Freund @ 2019-08-15 23:09 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
On 2019-08-15 18:39:24 -0400, Tom Lane wrote:
> Thomas Munro <thomas.munro@gmail.com> writes:
> > Right, this happens on REL_11_STABLE but not on master (which rewrote
> > the relevant code quite a bit in the "slotification" project).
>
> We should probably trace back why it doesn't happen before v11.
> I have a vague memory of having touched this code a year or two
> back, so likely this is my fault, but I wonder why it doesn't
> fail before.
There's
commit 25b692568f429436f89ff203c1413e9670d0ad67
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: 2018-02-27 13:27:38 -0500
Prevent dangling-pointer access when update trigger returns old tuple.
But that shouldn't itself have caused it. But the referenced 4b93f5799
might have.
Greetings,
Andres Freund
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:09 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Andres Freund <andres@anarazel.de>
@ 2019-08-15 23:39 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 8+ messages in thread
From: Tom Lane @ 2019-08-15 23:39 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Andres Freund <andres@anarazel.de> writes:
> There's
> commit 25b692568f429436f89ff203c1413e9670d0ad67
> But that shouldn't itself have caused it. But the referenced 4b93f5799
> might have.
Yeah, the problem is that that fix didn't fully close the hole.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-08-15 23:26 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:35 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
1 sibling, 1 reply; 8+ messages in thread
From: Tom Lane @ 2019-08-15 23:26 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
I wrote:
> [ thinks some more... ] Actually, I'm beginning to recall that
> we made changes here because v11 plpgsql is capable of actually
> returning "trigtuple" where before it would always have made a copy.
> If that's accurate, then very likely the bug exists further back
> but requires some other PL than plpgsql to manifest.
As I suspected ... the attached test case crashes 9.4 through 11.
We already had some problems in this area, which is why a suitable
trigger is already at hand in regress.c.
regards, tom lane
Attachments:
test-for-trigger-troubles.patch (text/x-diff, 3.1K) download
diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out
index c64151b..1e4053c 100644
--- a/src/test/regress/expected/triggers.out
+++ b/src/test/regress/expected/triggers.out
@@ -144,6 +144,76 @@ select * from trigtest;
----+----
(0 rows)
+-- Also check what happens when such a trigger runs before or after others
+create function f1_times_10() returns trigger as
+$$ begin new.f1 := new.f1 * 10; return new; end $$ language plpgsql;
+create trigger trigger_alpha
+ before insert or update on trigtest
+ for each row execute procedure f1_times_10();
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+ f1 | f2
+----+-----
+ 10 | foo
+(1 row)
+
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+ f1 | f2
+----+-----
+ 10 | foo
+(1 row)
+
+delete from trigtest;
+select * from trigtest;
+ f1 | f2
+----+----
+(0 rows)
+
+create trigger trigger_zed
+ before insert or update on trigtest
+ for each row execute procedure f1_times_10();
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+ f1 | f2
+-----+-----
+ 100 | foo
+(1 row)
+
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+ f1 | f2
+------+-----
+ 1000 | foo
+(1 row)
+
+delete from trigtest;
+select * from trigtest;
+ f1 | f2
+----+----
+(0 rows)
+
+drop trigger trigger_alpha on trigtest;
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+ f1 | f2
+----+-----
+ 10 | foo
+(1 row)
+
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+ f1 | f2
+-----+-----
+ 100 | foo
+(1 row)
+
+delete from trigtest;
+select * from trigtest;
+ f1 | f2
+----+----
+(0 rows)
+
drop table trigtest;
create sequence ttdummy_seq increment 10 start 0 minvalue 0;
create table tttest (
diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql
index 4534dc9..c21b6c1 100644
--- a/src/test/regress/sql/triggers.sql
+++ b/src/test/regress/sql/triggers.sql
@@ -117,6 +117,41 @@ select * from trigtest;
delete from trigtest;
select * from trigtest;
+-- Also check what happens when such a trigger runs before or after others
+create function f1_times_10() returns trigger as
+$$ begin new.f1 := new.f1 * 10; return new; end $$ language plpgsql;
+
+create trigger trigger_alpha
+ before insert or update on trigtest
+ for each row execute procedure f1_times_10();
+
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+delete from trigtest;
+select * from trigtest;
+
+create trigger trigger_zed
+ before insert or update on trigtest
+ for each row execute procedure f1_times_10();
+
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+delete from trigtest;
+select * from trigtest;
+
+drop trigger trigger_alpha on trigtest;
+
+insert into trigtest values(1, 'foo');
+select * from trigtest;
+update trigtest set f2 = f2 || 'bar';
+select * from trigtest;
+delete from trigtest;
+select * from trigtest;
+
drop table trigtest;
create sequence ttdummy_seq increment 10 start 0 minvalue 0;
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:26 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-08-15 23:35 ` Thomas Munro <thomas.munro@gmail.com>
2019-08-16 00:05 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 8+ messages in thread
From: Thomas Munro @ 2019-08-15 23:35 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
On Fri, Aug 16, 2019 at 11:26 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I wrote:
> > [ thinks some more... ] Actually, I'm beginning to recall that
> > we made changes here because v11 plpgsql is capable of actually
> > returning "trigtuple" where before it would always have made a copy.
> > If that's accurate, then very likely the bug exists further back
> > but requires some other PL than plpgsql to manifest.
>
> As I suspected ... the attached test case crashes 9.4 through 11.
> We already had some problems in this area, which is why a suitable
> trigger is already at hand in regress.c.
Ah, I see. I had written a test patch that uses plpgsql (attached for
posterity) but yours is better because it crashes more releases. I
will now get out of your way :-)
--
Thomas Munro
https://enterprisedb.com
Attachments:
0001-Fix-double-free-when-one-trigger-passes-OLD-to-anoth.patch (application/octet-stream, 3.7K) download
From 7666e8b806bcf87e7627152c9dcefc92f427e6f0 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 16 Aug 2019 11:25:02 +1200
Subject: [PATCH] Fix double-free when one trigger passes OLD to another
trigger.
In PLs that are capable of returning the old tuple directly (rather than
making a copy), if one tuple returns OLD directly (without copying),
ExecBRUpdateTriggers() would double-free it when processing the next
trigger. This doesn't affect 12 and later, which rewrote the relevant
code. Repair in back branches.
Reported-by: Piotr Kosinski
Discussion: https://postgr.es/m/CAFMLSdP0rd7LqC3j-H6Fh51FYSt5A10DDh-3%3DW4PPc4LLUQ8YQ%40mail.gmail.com
---
src/backend/commands/trigger.c | 4 +++-
src/test/regress/expected/triggers.out | 25 ++++++++++++++++++++++
src/test/regress/sql/triggers.sql | 29 ++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 1 deletion(-)
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index a716827c77..97ab357a24 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -3052,7 +3052,9 @@ ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
relinfo->ri_TrigFunctions,
relinfo->ri_TrigInstrument,
GetPerTupleMemoryContext(estate));
- if (oldtuple != newtuple && oldtuple != slottuple)
+ if (oldtuple != newtuple &&
+ oldtuple != slottuple &&
+ oldtuple != trigtuple)
heap_freetuple(oldtuple);
if (newtuple == NULL)
{
diff --git a/src/test/regress/expected/triggers.out b/src/test/regress/expected/triggers.out
index 5be0009f00..e67a6a6a2d 100644
--- a/src/test/regress/expected/triggers.out
+++ b/src/test/regress/expected/triggers.out
@@ -283,6 +283,31 @@ select * from tttest where price_on <= 35 and price_off > 35 and price_id = 5;
drop table tttest;
drop sequence ttdummy_seq;
--
+-- test correct handling of trigtuple lifetime when one trigger returns old
+-- tuple and it becomes new tuple for the next trigger
+--
+create table tttest (id int, bar int);
+create or replace function tf1() returns trigger language plpgsql as $$
+begin
+ return old;
+end;
+$$;
+create or replace function tf2() returns trigger language plpgsql as $$
+begin
+ new.bar := 42;
+ return new;
+end;
+$$;
+create trigger t1
+before update on tttest
+for each row execute procedure tf1();
+create trigger t2
+before update on tttest
+for each row execute procedure tf2();
+insert into tttest values (1, 1);
+update tttest set bar = bar where id = 1;
+drop table tttest;
+--
-- tests for per-statement triggers
--
CREATE TABLE log_table (tstamp timestamp default timeofday()::timestamp);
diff --git a/src/test/regress/sql/triggers.sql b/src/test/regress/sql/triggers.sql
index 6376046c5d..300e79ee5f 100644
--- a/src/test/regress/sql/triggers.sql
+++ b/src/test/regress/sql/triggers.sql
@@ -190,6 +190,35 @@ select * from tttest where price_on <= 35 and price_off > 35 and price_id = 5;
drop table tttest;
drop sequence ttdummy_seq;
+--
+-- test correct handling of trigtuple lifetime when one trigger returns old
+-- tuple and it becomes new tuple for the next trigger
+--
+
+create table tttest (id int, bar int);
+create or replace function tf1() returns trigger language plpgsql as $$
+begin
+ return old;
+end;
+$$;
+create or replace function tf2() returns trigger language plpgsql as $$
+begin
+ new.bar := 42;
+ return new;
+end;
+$$;
+create trigger t1
+before update on tttest
+for each row execute procedure tf1();
+create trigger t2
+before update on tttest
+for each row execute procedure tf2();
+insert into tttest values (1, 1);
+
+update tttest set bar = bar where id = 1;
+
+drop table tttest;
+
--
-- tests for per-statement triggers
--
--
2.22.0
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: Segmentation fault during update inside ExecBRUpdateTriggers
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:26 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:35 ` Re: Segmentation fault during update inside ExecBRUpdateTriggers Thomas Munro <thomas.munro@gmail.com>
@ 2019-08-16 00:05 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 8+ messages in thread
From: Tom Lane @ 2019-08-16 00:05 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Piotr Gabriel Kosinski <pg.kosinski@gmail.com>; PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Thomas Munro <thomas.munro@gmail.com> writes:
> Ah, I see. I had written a test patch that uses plpgsql (attached for
> posterity) but yours is better because it crashes more releases. I
> will now get out of your way :-)
OK, pushed.
regards, tom lane
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2019-08-16 00:05 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-15 21:33 Segmentation fault during update inside ExecBRUpdateTriggers Piotr Gabriel Kosinski <pg.kosinski@gmail.com>
2019-08-15 22:23 ` Thomas Munro <thomas.munro@gmail.com>
2019-08-15 22:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:09 ` Andres Freund <andres@anarazel.de>
2019-08-15 23:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:26 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:35 ` Thomas Munro <thomas.munro@gmail.com>
2019-08-16 00:05 ` Tom Lane <tgl@sss.pgh.pa.us>
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