From: Drouvot, Bertrand <bdrouvot@amazon.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: [BUG] segfault during delete
Date: Tue, 23 Feb 2021 21:56:28 +0100
Message-ID: <39a71864-b120-5a5c-8cc5-c632b6f16761@amazon.com> (raw)
Hi hackers,
Here is a scenario that segfault during delete (with version >= 12):
create table parent (
col1 text primary key
);
create table child (
col1 text primary key,
FOREIGN KEY (col1) REFERENCES parent(col1) on delete cascade
);
CREATE or replace FUNCTION trigger_function()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS $$
BEGIN
raise notice 'trigger = %, old table = %',
TG_NAME,
(select string_agg(old_table::text, ', ' order by col1) from
old_table);
return NULL;
END;
$$
;
create trigger bdt_trigger after delete on child REFERENCING OLD TABLE
AS old_table for each statement EXECUTE function trigger_function();
alter table child add column col2 text not null default 'tutu';
insert into parent(col1) values ('1');
insert into child(col1) values ('1');
insert into parent(col1) values ('2');
insert into child(col1) values ('2');
delete from parent;
produces:
CREATE TABLE
CREATE TABLE
CREATE FUNCTION
CREATE TRIGGER
ALTER TABLE
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
psql:./segfault.repro.sql:35: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
psql:./segfault.repro.sql:35: fatal: connection to server was lost
The column being added to the child relation needs to have a default
value for the segfault to be triggered.
The stack is the following:
#0 0x000000000049aa59 in execute_attr_map_slot (attrMap=0x11736c8,
in_slot=0x1179c90, out_slot=0x1179da8) at tupconvert.c:193
#1 0x0000000000700ec0 in AfterTriggerSaveEvent (estate=0x1171820,
relinfo=0x1171cc8, event=1, row_trigger=true, oldslot=0x1179c90,
newslot=0x0, recheckIndexes=0x0, modifiedCols=0x0,
transition_capture=0x1172438) at trigger.c:5488
#2 0x00000000006fc6a8 in ExecARDeleteTriggers (estate=0x1171820,
relinfo=0x1171cc8, tupleid=0x7ffd2b7f2e40, fdw_trigtuple=0x0,
transition_capture=0x1172438) at trigger.c:2565
#3 0x0000000000770794 in ExecDelete (mtstate=0x1171a90,
resultRelInfo=0x1171cc8, tupleid=0x7ffd2b7f2e40, oldtuple=0x0,
planSlot=0x1173630, epqstate=0x1171b88, estate=0x1171820,
processReturning=true, canSetTag=true, changingPart=false,
tupleDeleted=0x0, epqreturnslot=0x0)
at nodeModifyTable.c:1128
#4 0x00000000007724cc in ExecModifyTable (pstate=0x1171a90) at
nodeModifyTable.c:2259
I had a look at it, and it looks to me that the slot being reused in
AfterTriggerSaveEvent() (when (map != NULL) and not (!storeslot)) has
previously been damaged by FreeExecutorState() in standard_ExecutorEnd().
Then I ended up with the enclosed patch proposal that does not make the
repro segfaulting and that is passing make check too.
Not sure that what is being done in the patch is the right/best approach
to solve the issue though.
Bertrand
PS: the same repro with a foreign key with update cascade, an update
trigger and an update on the col1 column would segfault too (but does
not with the enclosed patch proposal).
Attachments:v1-0001-segfault-after-trigger.patch (text/plain, 3.5K) download
src/backend/commands/trigger.c | 6 ++++++
src/backend/executor/execTuples.c | 44 +++++++++++++++++++++++++++++++++++++++
src/include/executor/tuptable.h | 2 ++
3 files changed, 52 insertions(+)
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.cindex 8908847c6c..a6c464b120 100644--- a/src/backend/commands/trigger.c+++ b/src/backend/commands/trigger.c@@ -5475,8 +5475,11 @@ AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
if (map != NULL)
{
TupleTableSlot *storeslot;
+ MemoryContext oldcontext;+ oldcontext = MemoryContextSwitchTo(TopTransactionContext);
storeslot = transition_capture->tcs_private->storeslot;
+
if (!storeslot)
{
storeslot = ExecAllocTableSlot(&estate->es_tupleTable,
@@ -5484,7 +5487,10 @@ AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
&TTSOpsVirtual);
transition_capture->tcs_private->storeslot = storeslot;
}
+ else+ InitTupleTableSlot(&estate->es_tupleTable, map->outdesc, &TTSOpsVirtual, storeslot);+ MemoryContextSwitchTo(oldcontext);
execute_attr_map_slot(map->attrMap, oldslot, storeslot);
tuplestore_puttupleslot(old_tuplestore, storeslot);
}
diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.cindex 73c35df9c9..5f31287529 100644--- a/src/backend/executor/execTuples.c+++ b/src/backend/executor/execTuples.c@@ -1131,6 +1131,50 @@ MakeTupleTableSlot(TupleDesc tupleDesc,
return slot;
}
+/* --------------------------------+ * InitTupleTableSlot+ *+ * Basic routine to make an empty TupleTableSlot for an existing slot of given+ * TupleTableSlotType. If tupleDesc is specified the slot's descriptor is+ * fixed for its lifetime, gaining some efficiency. If that's+ * undesirable, pass NULL.+ * --------------------------------+ */+void+InitTupleTableSlot(List** tupleTable, TupleDesc tupleDesc,+ const TupleTableSlotOps *tts_ops, TupleTableSlot *slot)+{+ Size basesz;++ basesz = tts_ops->base_slot_size;++ /* const for optimization purposes, OK to modify at allocation time */+ *((const TupleTableSlotOps **) &slot->tts_ops) = tts_ops;+ slot->type = T_TupleTableSlot;+ slot->tts_flags |= TTS_FLAG_EMPTY;+ if (tupleDesc != NULL)+ slot->tts_flags |= TTS_FLAG_FIXED;+ slot->tts_tupleDescriptor = tupleDesc;+ slot->tts_mcxt = CurrentMemoryContext;+ slot->tts_nvalid = 0;++ if (tupleDesc != NULL)+ {+ slot->tts_values = (Datum *)+ (((char *) slot)+ + MAXALIGN(basesz));+ slot->tts_isnull = (bool *)+ (((char *) slot)+ + MAXALIGN(basesz)+ + MAXALIGN(tupleDesc->natts * sizeof(Datum)));+ }++ /*+ * And allow slot type specific initialization.+ */+ slot->tts_ops->init(slot);+}+
/* --------------------------------
* ExecAllocTableSlot
*
diff --git a/src/include/executor/tuptable.h b/src/include/executor/tuptable.hindex 679e57fbdd..6c82883877 100644--- a/src/include/executor/tuptable.h+++ b/src/include/executor/tuptable.h@@ -295,6 +295,8 @@ typedef struct MinimalTupleTableSlot
/* in executor/execTuples.c */
extern TupleTableSlot *MakeTupleTableSlot(TupleDesc tupleDesc,
const TupleTableSlotOps *tts_ops);
+extern void InitTupleTableSlot(List **tupleTable, TupleDesc tupleDesc,+ const TupleTableSlotOps *tts_ops, TupleTableSlot *slot);
extern TupleTableSlot *ExecAllocTableSlot(List **tupleTable, TupleDesc desc,
const TupleTableSlotOps *tts_ops);
extern void ExecResetTupleTable(List *tupleTable, bool shouldFree);
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-hackers@postgresql.org
Cc: bdrouvot@amazon.com
Subject: Re: [BUG] segfault during delete
In-Reply-To: <39a71864-b120-5a5c-8cc5-c632b6f16761@amazon.com>
References: <39a71864-b120-5a5c-8cc5-c632b6f16761@amazon.com>
* 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