7
7
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8
8
* Portions Copyright (c) 1994, Regents of the University of California
9
9
*
10
- * $PostgreSQL: pgsql/src/backend/utils/adt/trigfuncs.c,v 1.1 2008/11/03 20:17:20 adunstan Exp $
10
+ * $PostgreSQL: pgsql/src/backend/utils/adt/trigfuncs.c,v 1.2 2008/11/04 00:29:39 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
14
-
15
-
16
-
17
14
#include "postgres.h"
18
- #include "commands/trigger.h"
15
+
19
16
#include "access/htup.h"
17
+ #include "commands/trigger.h"
18
+ #include "utils/builtins.h"
19
+
20
20
21
21
/*
22
22
* suppress_redundant_updates_trigger
23
23
*
24
24
* This trigger function will inhibit an update from being done
25
25
* if the OLD and NEW records are identical.
26
- *
27
26
*/
28
-
29
27
Datum
30
28
suppress_redundant_updates_trigger (PG_FUNCTION_ARGS )
31
29
{
@@ -35,41 +33,47 @@ suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
35
33
36
34
/* make sure it's called as a trigger */
37
35
if (!CALLED_AS_TRIGGER (fcinfo ))
38
- elog (ERROR , (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
39
- errmsg ("suppress_redundant_updates_trigger: must be called as trigger" )));
36
+ ereport (ERROR ,
37
+ (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
38
+ errmsg ("suppress_redundant_updates_trigger: must be called as trigger" )));
40
39
41
40
/* and that it's called on update */
42
41
if (! TRIGGER_FIRED_BY_UPDATE (trigdata -> tg_event ))
43
- ereport (ERROR , (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
44
- errmsg ( "suppress_redundant_updates_trigger: may only be called on update" )));
42
+ ereport (ERROR ,
43
+ (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
44
+ errmsg ("suppress_redundant_updates_trigger: must be called on update" )));
45
45
46
46
/* and that it's called before update */
47
47
if (! TRIGGER_FIRED_BEFORE (trigdata -> tg_event ))
48
- ereport (ERROR , (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
49
- errmsg ( "suppress_redundant_updates_trigger: may only be called before update" )));
48
+ ereport (ERROR ,
49
+ (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
50
+ errmsg ("suppress_redundant_updates_trigger: must be called before update" )));
50
51
51
52
/* and that it's called for each row */
52
53
if (! TRIGGER_FIRED_FOR_ROW (trigdata -> tg_event ))
53
- ereport (ERROR , (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
54
- errmsg ( "suppress_redundant_updates_trigger: may only be called for each row" )));
54
+ ereport (ERROR ,
55
+ (errcode (ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED ),
56
+ errmsg ("suppress_redundant_updates_trigger: must be called for each row" )));
55
57
56
- /* get tuple data, set default return */
58
+ /* get tuple data, set default result */
57
59
rettuple = newtuple = trigdata -> tg_newtuple ;
58
60
oldtuple = trigdata -> tg_trigtuple ;
59
61
60
62
newheader = newtuple -> t_data ;
61
63
oldheader = oldtuple -> t_data ;
62
64
65
+ /* if the tuple payload is the same ... */
63
66
if (newtuple -> t_len == oldtuple -> t_len &&
64
67
newheader -> t_hoff == oldheader -> t_hoff &&
65
68
(HeapTupleHeaderGetNatts (newheader ) ==
66
- HeapTupleHeaderGetNatts (oldheader ) ) &&
69
+ HeapTupleHeaderGetNatts (oldheader )) &&
67
70
((newheader -> t_infomask & ~HEAP_XACT_MASK ) ==
68
- (oldheader -> t_infomask & ~HEAP_XACT_MASK ) ) &&
71
+ (oldheader -> t_infomask & ~HEAP_XACT_MASK )) &&
69
72
memcmp (((char * )newheader ) + offsetof(HeapTupleHeaderData , t_bits ),
70
73
((char * )oldheader ) + offsetof(HeapTupleHeaderData , t_bits ),
71
74
newtuple -> t_len - offsetof(HeapTupleHeaderData , t_bits )) == 0 )
72
75
{
76
+ /* ... then suppress the update */
73
77
rettuple = NULL ;
74
78
}
75
79
0 commit comments