summaryrefslogtreecommitdiff
path: root/src/backend/commands/trigger.c
diff options
context:
space:
mode:
authorAlvaro Herrera2012-01-25 16:15:29 +0000
committerAlvaro Herrera2012-01-25 16:22:54 +0000
commit74ab96a45ef6259aa6a86a781580edea8488511a (patch)
tree92415eea1e32bb33c68b9fdbbdc5c57732d78c68 /src/backend/commands/trigger.c
parent6d5aae7afacc564ead2af88c76b13cfc55750556 (diff)
Add pg_trigger_depth() function
This reports the depth level of triggers currently in execution, or zero if not called from inside a trigger. No catversion bump in this patch, but you have to initdb if you want access to the new function. Author: Kevin Grittner
Diffstat (limited to 'src/backend/commands/trigger.c')
-rw-r--r--src/backend/commands/trigger.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index f5e12e5681d..2838b66e402 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -59,6 +59,8 @@
/* GUC variables */
int SessionReplicationRole = SESSION_REPLICATION_ROLE_ORIGIN;
+/* How many levels deep into trigger execution are we? */
+static int MyTriggerDepth = 0;
#define GetModifiedColumns(relinfo, estate) \
(rt_fetch((relinfo)->ri_RangeTableIndex, (estate)->es_range_table)->modifiedCols)
@@ -1838,7 +1840,18 @@ ExecCallTriggerFunc(TriggerData *trigdata,
pgstat_init_function_usage(&fcinfo, &fcusage);
- result = FunctionCallInvoke(&fcinfo);
+ MyTriggerDepth++;
+ PG_TRY();
+ {
+ result = FunctionCallInvoke(&fcinfo);
+ }
+ PG_CATCH();
+ {
+ MyTriggerDepth--;
+ PG_RE_THROW();
+ }
+ PG_END_TRY();
+ MyTriggerDepth--;
pgstat_end_function_usage(&fcusage, true);
@@ -4632,3 +4645,9 @@ AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo,
&new_event, &new_shared);
}
}
+
+Datum
+pg_trigger_depth(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_INT32(MyTriggerDepth);
+}