44.7. Функции событийных триггеров в PL/Tcl
На PL/Tcl можно написать функции событийных триггеров. PostgreSQL требует, чтобы функция, которая будет вызываться как событийный триггер, была объявлена как функция без аргументов и возвращала тип event_trigger.
Информация от менеджера триггеров передаётся в тело функции в следующих переменных:
$TG_eventИмя события, при котором срабатывает этот триггер.
$TG_tagТег команды, для которой срабатывает этот триггер.
Возвращаемое значение триггерной функции игнорируется.
В этом примере мини-функция событийного триггера просто выдаёт замечание (NOTICE) при каждом выполнении поддерживаемой команды:
CREATE OR REPLACE FUNCTION tclsnitch() RETURNS event_trigger AS $$ elog NOTICE "tclsnitch: $TG_event $TG_tag" $$ LANGUAGE pltcl; CREATE EVENT TRIGGER tcl_a_snitch ON ddl_command_start EXECUTE FUNCTION tclsnitch();
44.7. Event Trigger Functions in PL/Tcl
Event trigger functions can be written in PL/Tcl. PostgreSQL requires that a function that is to be called as an event trigger must be declared as a function with no arguments and a return type of event_trigger.
The information from the trigger manager is passed to the function body in the following variables:
$TG_eventThe name of the event the trigger is fired for.
$TG_tagThe command tag for which the trigger is fired.
The return value of the trigger function is ignored.
Here's a little example event trigger function that simply raises a NOTICE message each time a supported command is executed:
CREATE OR REPLACE FUNCTION tclsnitch() RETURNS event_trigger AS $$ elog NOTICE "tclsnitch: $TG_event $TG_tag" $$ LANGUAGE pltcl; CREATE EVENT TRIGGER tcl_a_snitch ON ddl_command_start EXECUTE FUNCTION tclsnitch();