1 /*-------------------------------------------------------------------------
4 * Builtin functions for useful trigger support.
7 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/utils/adt/trigfuncs.c
12 *-------------------------------------------------------------------------
16 #include "access/htup_details.h"
17 #include "commands/trigger.h"
18 #include "utils/builtins.h"
19 #include "utils/rel.h"
23 * suppress_redundant_updates_trigger
25 * This trigger function will inhibit an update from being done
26 * if the OLD and NEW records are identical.
29 suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
31 TriggerData *trigdata = (TriggerData *) fcinfo->context;
35 HeapTupleHeader newheader,
38 /* make sure it's called as a trigger */
39 if (!CALLED_AS_TRIGGER(fcinfo))
41 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
42 errmsg("suppress_redundant_updates_trigger: must be called as trigger")));
44 /* and that it's called on update */
45 if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
47 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
48 errmsg("suppress_redundant_updates_trigger: must be called on update")));
50 /* and that it's called before update */
51 if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
53 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
54 errmsg("suppress_redundant_updates_trigger: must be called before update")));
56 /* and that it's called for each row */
57 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
59 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
60 errmsg("suppress_redundant_updates_trigger: must be called for each row")));
62 /* get tuple data, set default result */
63 rettuple = newtuple = trigdata->tg_newtuple;
64 oldtuple = trigdata->tg_trigtuple;
66 newheader = newtuple->t_data;
67 oldheader = oldtuple->t_data;
70 * We are called before the OID, if any, has been transcribed from the old
71 * tuple to the new (in heap_update). To avoid a bogus compare failure,
72 * copy the OID now. But check that someone didn't already put another
73 * OID value into newtuple. (That's not actually possible at present, but
76 if (trigdata->tg_relation->rd_rel->relhasoids &&
77 !OidIsValid(HeapTupleHeaderGetOid(newheader)))
78 HeapTupleHeaderSetOid(newheader, HeapTupleHeaderGetOid(oldheader));
80 /* if the tuple payload is the same ... */
81 if (newtuple->t_len == oldtuple->t_len &&
82 newheader->t_hoff == oldheader->t_hoff &&
83 (HeapTupleHeaderGetNatts(newheader) ==
84 HeapTupleHeaderGetNatts(oldheader)) &&
85 ((newheader->t_infomask & ~HEAP_XACT_MASK) ==
86 (oldheader->t_infomask & ~HEAP_XACT_MASK)) &&
87 memcmp(((char *) newheader) + SizeofHeapTupleHeader,
88 ((char *) oldheader) + SizeofHeapTupleHeader,
89 newtuple->t_len - SizeofHeapTupleHeader) == 0)
91 /* ... then suppress the update */
95 return PointerGetDatum(rettuple);