28
28
suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
30
TriggerData *trigdata = (TriggerData *) fcinfo->context;
31
HeapTuple newtuple, oldtuple, rettuple;
32
HeapTupleHeader newheader, oldheader;
30
TriggerData *trigdata = (TriggerData *) fcinfo->context;
34
HeapTupleHeader newheader,
34
/* make sure it's called as a trigger */
35
if (!CALLED_AS_TRIGGER(fcinfo))
37
/* make sure it's called as a trigger */
38
if (!CALLED_AS_TRIGGER(fcinfo))
37
40
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
38
41
errmsg("suppress_redundant_updates_trigger: must be called as trigger")));
40
/* and that it's called on update */
41
if (! TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
43
/* and that it's called on update */
44
if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
43
46
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
44
47
errmsg("suppress_redundant_updates_trigger: must be called on update")));
46
/* and that it's called before update */
47
if (! TRIGGER_FIRED_BEFORE(trigdata->tg_event))
49
/* and that it's called before update */
50
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
49
52
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
50
53
errmsg("suppress_redundant_updates_trigger: must be called before update")));
52
/* and that it's called for each row */
53
if (! TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
55
/* and that it's called for each row */
56
if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
55
58
(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
56
59
errmsg("suppress_redundant_updates_trigger: must be called for each row")));
58
61
/* get tuple data, set default result */
59
rettuple = newtuple = trigdata->tg_newtuple;
62
rettuple = newtuple = trigdata->tg_newtuple;
60
63
oldtuple = trigdata->tg_trigtuple;
62
65
newheader = newtuple->t_data;
63
66
oldheader = oldtuple->t_data;
66
* We are called before the OID, if any, has been transcribed from the
67
* old tuple to the new (in heap_update). To avoid a bogus compare
68
* failure, copy the OID now. But check that someone didn't already put
69
* another OID value into newtuple. (That's not actually possible at
70
* present, but maybe someday.)
69
* We are called before the OID, if any, has been transcribed from the old
70
* tuple to the new (in heap_update). To avoid a bogus compare failure,
71
* copy the OID now. But check that someone didn't already put another
72
* OID value into newtuple. (That's not actually possible at present, but
72
if (trigdata->tg_relation->rd_rel->relhasoids &&
75
if (trigdata->tg_relation->rd_rel->relhasoids &&
73
76
!OidIsValid(HeapTupleHeaderGetOid(newheader)))
74
77
HeapTupleHeaderSetOid(newheader, HeapTupleHeaderGetOid(oldheader));
76
79
/* if the tuple payload is the same ... */
77
if (newtuple->t_len == oldtuple->t_len &&
80
if (newtuple->t_len == oldtuple->t_len &&
78
81
newheader->t_hoff == oldheader->t_hoff &&
79
(HeapTupleHeaderGetNatts(newheader) ==
82
(HeapTupleHeaderGetNatts(newheader) ==
80
83
HeapTupleHeaderGetNatts(oldheader)) &&
81
((newheader->t_infomask & ~HEAP_XACT_MASK) ==
84
((newheader->t_infomask & ~HEAP_XACT_MASK) ==
82
85
(oldheader->t_infomask & ~HEAP_XACT_MASK)) &&
83
memcmp(((char *)newheader) + offsetof(HeapTupleHeaderData, t_bits),
84
((char *)oldheader) + offsetof(HeapTupleHeaderData, t_bits),
86
memcmp(((char *) newheader) + offsetof(HeapTupleHeaderData, t_bits),
87
((char *) oldheader) + offsetof(HeapTupleHeaderData, t_bits),
85
88
newtuple->t_len - offsetof(HeapTupleHeaderData, t_bits)) == 0)
87
90
/* ... then suppress the update */