2008-11-03 21:17:21 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* trigfuncs.c
|
2009-06-11 16:49:15 +02:00
|
|
|
* Builtin functions for useful trigger support.
|
2008-11-03 21:17:21 +01:00
|
|
|
*
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2008-11-03 21:17:21 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/adt/trigfuncs.c
|
2008-11-03 21:17:21 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2008-11-04 01:29:39 +01:00
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2008-11-04 01:29:39 +01:00
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "utils/builtins.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.h"
|
2008-11-04 01:29:39 +01:00
|
|
|
|
2008-11-03 21:17:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* suppress_redundant_updates_trigger
|
|
|
|
*
|
|
|
|
* This trigger function will inhibit an update from being done
|
|
|
|
* if the OLD and NEW records are identical.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
TriggerData *trigdata = (TriggerData *) fcinfo->context;
|
|
|
|
HeapTuple newtuple,
|
|
|
|
oldtuple,
|
|
|
|
rettuple;
|
|
|
|
HeapTupleHeader newheader,
|
|
|
|
oldheader;
|
2008-11-03 21:17:21 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/* make sure it's called as a trigger */
|
|
|
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
|
|
|
ereport(ERROR,
|
2008-11-04 01:29:39 +01:00
|
|
|
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
|
|
|
|
errmsg("suppress_redundant_updates_trigger: must be called as trigger")));
|
2009-06-11 16:49:15 +02:00
|
|
|
|
|
|
|
/* and that it's called on update */
|
|
|
|
if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
|
|
|
|
ereport(ERROR,
|
2008-11-04 01:29:39 +01:00
|
|
|
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
|
|
|
|
errmsg("suppress_redundant_updates_trigger: must be called on update")));
|
2008-11-03 21:17:21 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/* and that it's called before update */
|
|
|
|
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
|
|
|
|
ereport(ERROR,
|
2008-11-04 01:29:39 +01:00
|
|
|
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
|
|
|
|
errmsg("suppress_redundant_updates_trigger: must be called before update")));
|
2008-11-03 21:17:21 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/* and that it's called for each row */
|
|
|
|
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
|
|
|
|
ereport(ERROR,
|
2008-11-04 01:29:39 +01:00
|
|
|
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
|
|
|
|
errmsg("suppress_redundant_updates_trigger: must be called for each row")));
|
2008-11-03 21:17:21 +01:00
|
|
|
|
2008-11-04 01:29:39 +01:00
|
|
|
/* get tuple data, set default result */
|
2009-06-11 16:49:15 +02:00
|
|
|
rettuple = newtuple = trigdata->tg_newtuple;
|
2008-11-03 21:17:21 +01:00
|
|
|
oldtuple = trigdata->tg_trigtuple;
|
|
|
|
|
|
|
|
newheader = newtuple->t_data;
|
|
|
|
oldheader = oldtuple->t_data;
|
|
|
|
|
2008-11-04 01:29:39 +01:00
|
|
|
/* if the tuple payload is the same ... */
|
2009-06-11 16:49:15 +02:00
|
|
|
if (newtuple->t_len == oldtuple->t_len &&
|
2008-11-03 21:17:21 +01:00
|
|
|
newheader->t_hoff == oldheader->t_hoff &&
|
2009-06-11 16:49:15 +02:00
|
|
|
(HeapTupleHeaderGetNatts(newheader) ==
|
2008-11-04 01:29:39 +01:00
|
|
|
HeapTupleHeaderGetNatts(oldheader)) &&
|
2009-06-11 16:49:15 +02:00
|
|
|
((newheader->t_infomask & ~HEAP_XACT_MASK) ==
|
2008-11-04 01:29:39 +01:00
|
|
|
(oldheader->t_infomask & ~HEAP_XACT_MASK)) &&
|
2015-02-21 21:13:06 +01:00
|
|
|
memcmp(((char *) newheader) + SizeofHeapTupleHeader,
|
|
|
|
((char *) oldheader) + SizeofHeapTupleHeader,
|
|
|
|
newtuple->t_len - SizeofHeapTupleHeader) == 0)
|
2008-11-03 21:17:21 +01:00
|
|
|
{
|
2008-11-04 01:29:39 +01:00
|
|
|
/* ... then suppress the update */
|
2008-11-03 21:17:21 +01:00
|
|
|
rettuple = NULL;
|
|
|
|
}
|
2008-11-05 21:17:18 +01:00
|
|
|
|
|
|
|
return PointerGetDatum(rettuple);
|
2008-11-03 21:17:21 +01:00
|
|
|
}
|