Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * trigfuncs.c
4 : : * Builtin functions for useful trigger support.
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2024, 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/fmgrprotos.h"
19 : :
20 : :
21 : : /*
22 : : * suppress_redundant_updates_trigger
23 : : *
24 : : * This trigger function will inhibit an update from being done
25 : : * if the OLD and NEW records are identical.
26 : : */
27 : : Datum
5641 andrew@dunslane.net 28 :CBC 15 : suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
29 : : {
5421 bruce@momjian.us 30 : 15 : TriggerData *trigdata = (TriggerData *) fcinfo->context;
31 : : HeapTuple newtuple,
32 : : oldtuple,
33 : : rettuple;
34 : : HeapTupleHeader newheader,
35 : : oldheader;
36 : :
37 : : /* make sure it's called as a trigger */
38 [ + - - + ]: 15 : if (!CALLED_AS_TRIGGER(fcinfo))
5421 bruce@momjian.us 39 [ # # ]:UBC 0 : ereport(ERROR,
40 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
41 : : errmsg("suppress_redundant_updates_trigger: must be called as trigger")));
42 : :
43 : : /* and that it's called on update */
5421 bruce@momjian.us 44 [ - + ]:CBC 15 : if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
5421 bruce@momjian.us 45 [ # # ]:UBC 0 : ereport(ERROR,
46 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
47 : : errmsg("suppress_redundant_updates_trigger: must be called on update")));
48 : :
49 : : /* and that it's called before update */
5421 bruce@momjian.us 50 [ - + ]:CBC 15 : if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event))
5421 bruce@momjian.us 51 [ # # ]:UBC 0 : ereport(ERROR,
52 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
53 : : errmsg("suppress_redundant_updates_trigger: must be called before update")));
54 : :
55 : : /* and that it's called for each row */
5421 bruce@momjian.us 56 [ - + ]:CBC 15 : if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
5421 bruce@momjian.us 57 [ # # ]:UBC 0 : ereport(ERROR,
58 : : (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
59 : : errmsg("suppress_redundant_updates_trigger: must be called for each row")));
60 : :
61 : : /* get tuple data, set default result */
5421 bruce@momjian.us 62 :CBC 15 : rettuple = newtuple = trigdata->tg_newtuple;
5641 andrew@dunslane.net 63 : 15 : oldtuple = trigdata->tg_trigtuple;
64 : :
65 : 15 : newheader = newtuple->t_data;
66 : 15 : oldheader = oldtuple->t_data;
67 : :
68 : : /* if the tuple payload is the same ... */
5421 bruce@momjian.us 69 [ + + ]: 15 : if (newtuple->t_len == oldtuple->t_len &&
5641 andrew@dunslane.net 70 [ + - ]: 12 : newheader->t_hoff == oldheader->t_hoff &&
5421 bruce@momjian.us 71 : 12 : (HeapTupleHeaderGetNatts(newheader) ==
5640 tgl@sss.pgh.pa.us 72 [ + - ]: 12 : HeapTupleHeaderGetNatts(oldheader)) &&
5421 bruce@momjian.us 73 : 12 : ((newheader->t_infomask & ~HEAP_XACT_MASK) ==
5640 tgl@sss.pgh.pa.us 74 [ + - ]: 12 : (oldheader->t_infomask & ~HEAP_XACT_MASK)) &&
3340 75 : 12 : memcmp(((char *) newheader) + SizeofHeapTupleHeader,
76 : : ((char *) oldheader) + SizeofHeapTupleHeader,
77 [ + + ]: 12 : newtuple->t_len - SizeofHeapTupleHeader) == 0)
78 : : {
79 : : /* ... then suppress the update */
5641 andrew@dunslane.net 80 : 6 : rettuple = NULL;
81 : : }
82 : :
5639 tgl@sss.pgh.pa.us 83 : 15 : return PointerGetDatum(rettuple);
84 : : }
|