PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
trigfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * trigfuncs.c
4  * Builtin functions for useful trigger support.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/backend/utils/adt/trigfuncs.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15 
16 #include "access/htup_details.h"
17 #include "commands/trigger.h"
18 #include "utils/builtins.h"
19 #include "utils/rel.h"
20 
21 
22 /*
23  * suppress_redundant_updates_trigger
24  *
25  * This trigger function will inhibit an update from being done
26  * if the OLD and NEW records are identical.
27  */
28 Datum
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  /*
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
74  * maybe someday.)
75  */
76  if (trigdata->tg_relation->rd_rel->relhasoids &&
77  !OidIsValid(HeapTupleHeaderGetOid(newheader)))
78  HeapTupleHeaderSetOid(newheader, HeapTupleHeaderGetOid(oldheader));
79 
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)
90  {
91  /* ... then suppress the update */
92  rettuple = NULL;
93  }
94 
95  return PointerGetDatum(rettuple);
96 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
Datum suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
Definition: trigfuncs.c:29
#define PointerGetDatum(X)
Definition: postgres.h:562
int errcode(int sqlerrcode)
Definition: elog.c:575
Form_pg_class rd_rel
Definition: rel.h:114
#define OidIsValid(objectId)
Definition: c.h:538
HeapTuple tg_trigtuple
Definition: trigger.h:35
HeapTupleHeader t_data
Definition: htup.h:67
#define ERROR
Definition: elog.h:43
#define HeapTupleHeaderSetOid(tup, oid)
Definition: htup_details.h:473
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:532
uint32 t_len
Definition: htup.h:64
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
HeapTuple tg_newtuple
Definition: trigger.h:36
#define NULL
Definition: c.h:229
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
TriggerEvent tg_event
Definition: trigger.h:33
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:134
#define HeapTupleHeaderGetOid(tup)
Definition: htup_details.h:465
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define HEAP_XACT_MASK
Definition: htup_details.h:204
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
Relation tg_relation
Definition: trigger.h:34