77 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88 * Portions Copyright (c) 1994, Regents of the University of California
99 *
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 $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
14-
15-
16-
1714#include "postgres.h"
18- #include "commands/trigger.h"
15+
1916#include "access/htup.h"
17+ #include "commands/trigger.h"
18+ #include "utils/builtins.h"
19+
2020
2121/*
2222 * suppress_redundant_updates_trigger
2323 *
2424 * This trigger function will inhibit an update from being done
2525 * if the OLD and NEW records are identical.
26- *
2726 */
28-
2927Datum
3028suppress_redundant_updates_trigger (PG_FUNCTION_ARGS )
3129{
@@ -35,41 +33,47 @@ suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
3533
3634/* make sure it's called as a trigger */
3735if (!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" )));
4039
4140/* and that it's called on update */
4241if (!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" )));
4545
4646/* and that it's called before update */
4747if (!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" )));
5051
5152/* and that it's called for each row */
5253if (!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" )));
5557
56- /* get tuple data, set defaultreturn */
58+ /* get tuple data, set defaultresult */
5759rettuple = newtuple = trigdata -> tg_newtuple ;
5860oldtuple = trigdata -> tg_trigtuple ;
5961
6062newheader = newtuple -> t_data ;
6163oldheader = oldtuple -> t_data ;
6264
65+ /* if the tuple payload is the same ... */
6366if (newtuple -> t_len == oldtuple -> t_len &&
6467newheader -> t_hoff == oldheader -> t_hoff &&
6568(HeapTupleHeaderGetNatts (newheader )==
66- HeapTupleHeaderGetNatts (oldheader ) )&&
69+ HeapTupleHeaderGetNatts (oldheader ))&&
6770((newheader -> t_infomask & ~HEAP_XACT_MASK )==
68- (oldheader -> t_infomask & ~HEAP_XACT_MASK ) ) &&
71+ (oldheader -> t_infomask & ~HEAP_XACT_MASK )) &&
6972memcmp (((char * )newheader )+ offsetof(HeapTupleHeaderData ,t_bits ),
7073 ((char * )oldheader )+ offsetof(HeapTupleHeaderData ,t_bits ),
7174newtuple -> t_len - offsetof(HeapTupleHeaderData ,t_bits ))== 0 )
7275{
76+ /* ... then suppress the update */
7377rettuple = NULL ;
7478}
7579