PostgreSQL Source Code  git master
trigfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "commands/trigger.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for trigfuncs.c:

Go to the source code of this file.

Functions

Datum suppress_redundant_updates_trigger (PG_FUNCTION_ARGS)
 

Function Documentation

◆ suppress_redundant_updates_trigger()

Datum suppress_redundant_updates_trigger ( PG_FUNCTION_ARGS  )

Definition at line 29 of file trigfuncs.c.

30 {
31  TriggerData *trigdata = (TriggerData *) fcinfo->context;
32  HeapTuple newtuple,
33  oldtuple,
34  rettuple;
35  HeapTupleHeader newheader,
36  oldheader;
37 
38  /* make sure it's called as a trigger */
39  if (!CALLED_AS_TRIGGER(fcinfo))
40  ereport(ERROR,
41  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
42  errmsg("suppress_redundant_updates_trigger: must be called as trigger")));
43 
44  /* and that it's called on update */
45  if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
46  ereport(ERROR,
47  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
48  errmsg("suppress_redundant_updates_trigger: must be called on update")));
49 
50  /* and that it's called before update */
51  if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
52  ereport(ERROR,
53  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
54  errmsg("suppress_redundant_updates_trigger: must be called before update")));
55 
56  /* and that it's called for each row */
57  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
58  ereport(ERROR,
59  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
60  errmsg("suppress_redundant_updates_trigger: must be called for each row")));
61 
62  /* get tuple data, set default result */
63  rettuple = newtuple = trigdata->tg_newtuple;
64  oldtuple = trigdata->tg_trigtuple;
65 
66  newheader = newtuple->t_data;
67  oldheader = oldtuple->t_data;
68 
69  /* if the tuple payload is the same ... */
70  if (newtuple->t_len == oldtuple->t_len &&
71  newheader->t_hoff == oldheader->t_hoff &&
72  (HeapTupleHeaderGetNatts(newheader) ==
73  HeapTupleHeaderGetNatts(oldheader)) &&
74  ((newheader->t_infomask & ~HEAP_XACT_MASK) ==
75  (oldheader->t_infomask & ~HEAP_XACT_MASK)) &&
76  memcmp(((char *) newheader) + SizeofHeapTupleHeader,
77  ((char *) oldheader) + SizeofHeapTupleHeader,
78  newtuple->t_len - SizeofHeapTupleHeader) == 0)
79  {
80  /* ... then suppress the update */
81  rettuple = NULL;
82  }
83 
84  return PointerGetDatum(rettuple);
85 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:529
#define SizeofHeapTupleHeader
Definition: htup_details.h:185
#define HEAP_XACT_MASK
Definition: htup_details.h:215
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
HeapTuple tg_trigtuple
Definition: trigger.h:36
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116

References CALLED_AS_TRIGGER, ereport, errcode(), errmsg(), ERROR, HEAP_XACT_MASK, HeapTupleHeaderGetNatts, if(), PointerGetDatum(), SizeofHeapTupleHeader, HeapTupleData::t_data, HeapTupleData::t_len, TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_trigtuple, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_UPDATE, and TRIGGER_FIRED_FOR_ROW.