Fix AfterTriggerExecute() to pass tg_trigtuple and tg_newtuple as NULLs

rather than pointers to garbage, when calling AFTER STATEMENT triggers.
Michael Fuhr
This commit is contained in:
Tom Lane 2006-08-03 16:04:41 +00:00
parent bc8ac3ce40
commit f5b78fa51f
1 changed files with 19 additions and 26 deletions

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.205 2006/07/31 20:09:00 tgl Exp $ * $PostgreSQL: pgsql/src/backend/commands/trigger.c,v 1.206 2006/08/03 16:04:41 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -2090,11 +2090,18 @@ AfterTriggerExecute(AfterTriggerEvent event,
/* /*
* Fetch the required OLD and NEW tuples. * Fetch the required OLD and NEW tuples.
*/ */
LocTriggerData.tg_trigtuple = NULL;
LocTriggerData.tg_newtuple = NULL;
LocTriggerData.tg_trigtuplebuf = InvalidBuffer;
LocTriggerData.tg_newtuplebuf = InvalidBuffer;
if (ItemPointerIsValid(&(event->ate_oldctid))) if (ItemPointerIsValid(&(event->ate_oldctid)))
{ {
ItemPointerCopy(&(event->ate_oldctid), &(oldtuple.t_self)); ItemPointerCopy(&(event->ate_oldctid), &(oldtuple.t_self));
if (!heap_fetch(rel, SnapshotAny, &oldtuple, &oldbuffer, false, NULL)) if (!heap_fetch(rel, SnapshotAny, &oldtuple, &oldbuffer, false, NULL))
elog(ERROR, "failed to fetch old tuple for AFTER trigger"); elog(ERROR, "failed to fetch old tuple for AFTER trigger");
LocTriggerData.tg_trigtuple = &oldtuple;
LocTriggerData.tg_trigtuplebuf = oldbuffer;
} }
if (ItemPointerIsValid(&(event->ate_newctid))) if (ItemPointerIsValid(&(event->ate_newctid)))
@ -2102,40 +2109,26 @@ AfterTriggerExecute(AfterTriggerEvent event,
ItemPointerCopy(&(event->ate_newctid), &(newtuple.t_self)); ItemPointerCopy(&(event->ate_newctid), &(newtuple.t_self));
if (!heap_fetch(rel, SnapshotAny, &newtuple, &newbuffer, false, NULL)) if (!heap_fetch(rel, SnapshotAny, &newtuple, &newbuffer, false, NULL))
elog(ERROR, "failed to fetch new tuple for AFTER trigger"); elog(ERROR, "failed to fetch new tuple for AFTER trigger");
if (LocTriggerData.tg_trigtuple != NULL)
{
LocTriggerData.tg_newtuple = &newtuple;
LocTriggerData.tg_newtuplebuf = newbuffer;
}
else
{
LocTriggerData.tg_trigtuple = &newtuple;
LocTriggerData.tg_trigtuplebuf = newbuffer;
}
} }
/* /*
* Setup the trigger information * Setup the remaining trigger information
*/ */
LocTriggerData.type = T_TriggerData; LocTriggerData.type = T_TriggerData;
LocTriggerData.tg_event = LocTriggerData.tg_event =
event->ate_event & (TRIGGER_EVENT_OPMASK | TRIGGER_EVENT_ROW); event->ate_event & (TRIGGER_EVENT_OPMASK | TRIGGER_EVENT_ROW);
LocTriggerData.tg_relation = rel; LocTriggerData.tg_relation = rel;
switch (event->ate_event & TRIGGER_EVENT_OPMASK)
{
case TRIGGER_EVENT_INSERT:
LocTriggerData.tg_trigtuple = &newtuple;
LocTriggerData.tg_newtuple = NULL;
LocTriggerData.tg_trigtuplebuf = newbuffer;
LocTriggerData.tg_newtuplebuf = InvalidBuffer;
break;
case TRIGGER_EVENT_UPDATE:
LocTriggerData.tg_trigtuple = &oldtuple;
LocTriggerData.tg_newtuple = &newtuple;
LocTriggerData.tg_trigtuplebuf = oldbuffer;
LocTriggerData.tg_newtuplebuf = newbuffer;
break;
case TRIGGER_EVENT_DELETE:
LocTriggerData.tg_trigtuple = &oldtuple;
LocTriggerData.tg_newtuple = NULL;
LocTriggerData.tg_trigtuplebuf = oldbuffer;
LocTriggerData.tg_newtuplebuf = InvalidBuffer;
break;
}
MemoryContextReset(per_tuple_context); MemoryContextReset(per_tuple_context);
/* /*