Header And Logo

PostgreSQL
| The world's most advanced open source database.

trigger.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * trigger.h
00004  *    Declarations for trigger handling.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * src/include/commands/trigger.h
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef TRIGGER_H
00014 #define TRIGGER_H
00015 
00016 #include "nodes/execnodes.h"
00017 #include "nodes/parsenodes.h"
00018 
00019 /*
00020  * TriggerData is the node type that is passed as fmgr "context" info
00021  * when a function is called by the trigger manager.
00022  */
00023 
00024 #define CALLED_AS_TRIGGER(fcinfo) \
00025     ((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))
00026 
00027 typedef uint32 TriggerEvent;
00028 
00029 typedef struct TriggerData
00030 {
00031     NodeTag     type;
00032     TriggerEvent tg_event;
00033     Relation    tg_relation;
00034     HeapTuple   tg_trigtuple;
00035     HeapTuple   tg_newtuple;
00036     Trigger    *tg_trigger;
00037     Buffer      tg_trigtuplebuf;
00038     Buffer      tg_newtuplebuf;
00039 } TriggerData;
00040 
00041 /*
00042  * TriggerEvent bit flags
00043  *
00044  * Note that we assume different event types (INSERT/DELETE/UPDATE/TRUNCATE)
00045  * can't be OR'd together in a single TriggerEvent.  This is unlike the
00046  * situation for pg_trigger rows, so pg_trigger.tgtype uses a different
00047  * representation!
00048  */
00049 #define TRIGGER_EVENT_INSERT            0x00000000
00050 #define TRIGGER_EVENT_DELETE            0x00000001
00051 #define TRIGGER_EVENT_UPDATE            0x00000002
00052 #define TRIGGER_EVENT_TRUNCATE          0x00000003
00053 #define TRIGGER_EVENT_OPMASK            0x00000003
00054 
00055 #define TRIGGER_EVENT_ROW               0x00000004
00056 
00057 #define TRIGGER_EVENT_BEFORE            0x00000008
00058 #define TRIGGER_EVENT_AFTER             0x00000000
00059 #define TRIGGER_EVENT_INSTEAD           0x00000010
00060 #define TRIGGER_EVENT_TIMINGMASK        0x00000018
00061 
00062 /* More TriggerEvent flags, used only within trigger.c */
00063 
00064 #define AFTER_TRIGGER_DEFERRABLE        0x00000020
00065 #define AFTER_TRIGGER_INITDEFERRED      0x00000040
00066 
00067 #define TRIGGER_FIRED_BY_INSERT(event) \
00068     (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_INSERT)
00069 
00070 #define TRIGGER_FIRED_BY_DELETE(event) \
00071     (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_DELETE)
00072 
00073 #define TRIGGER_FIRED_BY_UPDATE(event) \
00074     (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_UPDATE)
00075 
00076 #define TRIGGER_FIRED_BY_TRUNCATE(event) \
00077     (((event) & TRIGGER_EVENT_OPMASK) == TRIGGER_EVENT_TRUNCATE)
00078 
00079 #define TRIGGER_FIRED_FOR_ROW(event) \
00080     ((event) & TRIGGER_EVENT_ROW)
00081 
00082 #define TRIGGER_FIRED_FOR_STATEMENT(event) \
00083     (!TRIGGER_FIRED_FOR_ROW(event))
00084 
00085 #define TRIGGER_FIRED_BEFORE(event) \
00086     (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_BEFORE)
00087 
00088 #define TRIGGER_FIRED_AFTER(event) \
00089     (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_AFTER)
00090 
00091 #define TRIGGER_FIRED_INSTEAD(event) \
00092     (((event) & TRIGGER_EVENT_TIMINGMASK) == TRIGGER_EVENT_INSTEAD)
00093 
00094 /*
00095  * Definitions for replication role based firing.
00096  */
00097 #define SESSION_REPLICATION_ROLE_ORIGIN     0
00098 #define SESSION_REPLICATION_ROLE_REPLICA    1
00099 #define SESSION_REPLICATION_ROLE_LOCAL      2
00100 extern PGDLLIMPORT int SessionReplicationRole;
00101 
00102 /*
00103  * States at which a trigger can be fired. These are the
00104  * possible values for pg_trigger.tgenabled.
00105  */
00106 #define TRIGGER_FIRES_ON_ORIGIN             'O'
00107 #define TRIGGER_FIRES_ALWAYS                'A'
00108 #define TRIGGER_FIRES_ON_REPLICA            'R'
00109 #define TRIGGER_DISABLED                    'D'
00110 
00111 extern Oid CreateTrigger(CreateTrigStmt *stmt, const char *queryString,
00112               Oid constraintOid, Oid indexOid,
00113               bool isInternal);
00114 
00115 extern void RemoveTriggerById(Oid trigOid);
00116 extern Oid  get_trigger_oid(Oid relid, const char *name, bool missing_ok);
00117 
00118 extern Oid renametrig(RenameStmt *stmt);
00119 
00120 extern void EnableDisableTrigger(Relation rel, const char *tgname,
00121                      char fires_when, bool skip_system);
00122 
00123 extern void RelationBuildTriggers(Relation relation);
00124 
00125 extern TriggerDesc *CopyTriggerDesc(TriggerDesc *trigdesc);
00126 
00127 extern void FreeTriggerDesc(TriggerDesc *trigdesc);
00128 
00129 extern void ExecBSInsertTriggers(EState *estate,
00130                      ResultRelInfo *relinfo);
00131 extern void ExecASInsertTriggers(EState *estate,
00132                      ResultRelInfo *relinfo);
00133 extern TupleTableSlot *ExecBRInsertTriggers(EState *estate,
00134                      ResultRelInfo *relinfo,
00135                      TupleTableSlot *slot);
00136 extern void ExecARInsertTriggers(EState *estate,
00137                      ResultRelInfo *relinfo,
00138                      HeapTuple trigtuple,
00139                      List *recheckIndexes);
00140 extern TupleTableSlot *ExecIRInsertTriggers(EState *estate,
00141                      ResultRelInfo *relinfo,
00142                      TupleTableSlot *slot);
00143 extern void ExecBSDeleteTriggers(EState *estate,
00144                      ResultRelInfo *relinfo);
00145 extern void ExecASDeleteTriggers(EState *estate,
00146                      ResultRelInfo *relinfo);
00147 extern bool ExecBRDeleteTriggers(EState *estate,
00148                      EPQState *epqstate,
00149                      ResultRelInfo *relinfo,
00150                      ItemPointer tupleid);
00151 extern void ExecARDeleteTriggers(EState *estate,
00152                      ResultRelInfo *relinfo,
00153                      ItemPointer tupleid);
00154 extern bool ExecIRDeleteTriggers(EState *estate,
00155                      ResultRelInfo *relinfo,
00156                      HeapTuple trigtuple);
00157 extern void ExecBSUpdateTriggers(EState *estate,
00158                      ResultRelInfo *relinfo);
00159 extern void ExecASUpdateTriggers(EState *estate,
00160                      ResultRelInfo *relinfo);
00161 extern TupleTableSlot *ExecBRUpdateTriggers(EState *estate,
00162                      EPQState *epqstate,
00163                      ResultRelInfo *relinfo,
00164                      ItemPointer tupleid,
00165                      TupleTableSlot *slot);
00166 extern void ExecARUpdateTriggers(EState *estate,
00167                      ResultRelInfo *relinfo,
00168                      ItemPointer tupleid,
00169                      HeapTuple newtuple,
00170                      List *recheckIndexes);
00171 extern TupleTableSlot *ExecIRUpdateTriggers(EState *estate,
00172                      ResultRelInfo *relinfo,
00173                      HeapTuple trigtuple,
00174                      TupleTableSlot *slot);
00175 extern void ExecBSTruncateTriggers(EState *estate,
00176                        ResultRelInfo *relinfo);
00177 extern void ExecASTruncateTriggers(EState *estate,
00178                        ResultRelInfo *relinfo);
00179 
00180 extern void AfterTriggerBeginXact(void);
00181 extern void AfterTriggerBeginQuery(void);
00182 extern void AfterTriggerEndQuery(EState *estate);
00183 extern void AfterTriggerFireDeferred(void);
00184 extern void AfterTriggerEndXact(bool isCommit);
00185 extern void AfterTriggerBeginSubXact(void);
00186 extern void AfterTriggerEndSubXact(bool isCommit);
00187 extern void AfterTriggerSetState(ConstraintsSetStmt *stmt);
00188 extern bool AfterTriggerPendingOnRel(Oid relid);
00189 
00190 
00191 /*
00192  * in utils/adt/ri_triggers.c
00193  */
00194 extern bool RI_FKey_pk_upd_check_required(Trigger *trigger, Relation pk_rel,
00195                               HeapTuple old_row, HeapTuple new_row);
00196 extern bool RI_FKey_fk_upd_check_required(Trigger *trigger, Relation fk_rel,
00197                               HeapTuple old_row, HeapTuple new_row);
00198 extern bool RI_Initial_Check(Trigger *trigger,
00199                  Relation fk_rel, Relation pk_rel);
00200 
00201 /* result values for RI_FKey_trigger_type: */
00202 #define RI_TRIGGER_PK   1       /* is a trigger on the PK relation */
00203 #define RI_TRIGGER_FK   2       /* is a trigger on the FK relation */
00204 #define RI_TRIGGER_NONE 0       /* is not an RI trigger function */
00205 
00206 extern int  RI_FKey_trigger_type(Oid tgfoid);
00207 
00208 extern Datum pg_trigger_depth(PG_FUNCTION_ARGS);
00209 
00210 #endif   /* TRIGGER_H */