PostgreSQL Source Code
git master
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-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
28
suppress_redundant_updates_trigger
(
PG_FUNCTION_ARGS
)
29
{
30
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
if
(!
CALLED_AS_TRIGGER
(fcinfo))
39
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 */
44
if
(!
TRIGGER_FIRED_BY_UPDATE
(trigdata->
tg_event
))
45
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 */
50
if
(!
TRIGGER_FIRED_BEFORE
(trigdata->
tg_event
))
51
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 */
56
if
(!
TRIGGER_FIRED_FOR_ROW
(trigdata->
tg_event
))
57
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 */
62
rettuple = newtuple = trigdata->
tg_newtuple
;
63
oldtuple = trigdata->
tg_trigtuple
;
64
65
newheader = newtuple->
t_data
;
66
oldheader = oldtuple->t_data;
67
68
/* if the tuple payload is the same ... */
69
if
(newtuple->
t_len
== oldtuple->t_len &&
70
newheader->t_hoff == oldheader->t_hoff &&
71
(
HeapTupleHeaderGetNatts
(newheader) ==
72
HeapTupleHeaderGetNatts
(oldheader)) &&
73
((newheader->t_infomask & ~
HEAP_XACT_MASK
) ==
74
(oldheader->t_infomask & ~
HEAP_XACT_MASK
)) &&
75
memcmp(((
char
*) newheader) +
SizeofHeapTupleHeader
,
76
((
char
*) oldheader) +
SizeofHeapTupleHeader
,
77
newtuple->
t_len
-
SizeofHeapTupleHeader
) == 0)
78
{
79
/* ... then suppress the update */
80
rettuple = NULL;
81
}
82
83
return
PointerGetDatum
(rettuple);
84
}
errcode
int errcode(int sqlerrcode)
Definition:
elog.c:853
errmsg
int errmsg(const char *fmt,...)
Definition:
elog.c:1070
ERROR
#define ERROR
Definition:
elog.h:39
ereport
#define ereport(elevel,...)
Definition:
elog.h:149
PG_FUNCTION_ARGS
#define PG_FUNCTION_ARGS
Definition:
fmgr.h:193
htup_details.h
HeapTupleHeaderGetNatts
#define HeapTupleHeaderGetNatts(tup)
Definition:
htup_details.h:529
SizeofHeapTupleHeader
#define SizeofHeapTupleHeader
Definition:
htup_details.h:185
HEAP_XACT_MASK
#define HEAP_XACT_MASK
Definition:
htup_details.h:215
if
if(TABLE==NULL||TABLE_index==NULL)
Definition:
isn.c:77
postgres.h
PointerGetDatum
static Datum PointerGetDatum(const void *X)
Definition:
postgres.h:322
Datum
uintptr_t Datum
Definition:
postgres.h:64
HeapTupleData
Definition:
htup.h:63
HeapTupleData::t_len
uint32 t_len
Definition:
htup.h:64
HeapTupleData::t_data
HeapTupleHeader t_data
Definition:
htup.h:68
HeapTupleHeaderData
Definition:
htup_details.h:154
TriggerData
Definition:
trigger.h:32
TriggerData::tg_event
TriggerEvent tg_event
Definition:
trigger.h:34
TriggerData::tg_newtuple
HeapTuple tg_newtuple
Definition:
trigger.h:37
TriggerData::tg_trigtuple
HeapTuple tg_trigtuple
Definition:
trigger.h:36
suppress_redundant_updates_trigger
Datum suppress_redundant_updates_trigger(PG_FUNCTION_ARGS)
Definition:
trigfuncs.c:28
trigger.h
TRIGGER_FIRED_BEFORE
#define TRIGGER_FIRED_BEFORE(event)
Definition:
trigger.h:128
CALLED_AS_TRIGGER
#define CALLED_AS_TRIGGER(fcinfo)
Definition:
trigger.h:26
TRIGGER_FIRED_FOR_ROW
#define TRIGGER_FIRED_FOR_ROW(event)
Definition:
trigger.h:122
TRIGGER_FIRED_BY_UPDATE
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition:
trigger.h:116
src
backend
utils
adt
trigfuncs.c
Generated on Mon Sep 16 2024 18:13:21 for PostgreSQL Source Code by
1.9.1