47.6. Триггерные функции
Когда функция используется как триггер, словарь TD
содержит значения, связанные с работой триггера:
TD["event"]
содержит название события в виде строки:
INSERT
,UPDATE
,DELETE
илиTRUNCATE
.TD["when"]
содержит одну из строк:
BEFORE
,AFTER
илиINSTEAD OF
.TD["level"]
содержит
ROW
илиSTATEMENT
.TD["new"]
TD["old"]
Для триггера уровня строки одно или оба этих поля содержат соответствующие строки триггера, в зависимости от события триггера.
TD["name"]
содержит имя триггера.
TD["table_name"]
содержит имя таблицы, для которой сработал триггер.
TD["table_schema"]
содержит схему таблицы, для которой сработал триггер.
TD["relid"]
содержит OID таблицы, для которой сработал триггер.
TD["args"]
Если в команде
CREATE TRIGGER
задавались аргументы, их можно получить как элементы массива сTD["args"][0]
поTD["args"][
.n
-1]
Если в TD["when"]
передано BEFORE
или INSTEAD OF
, а в TD["level"]
— ROW
, вы можете вернуть значение None
или "OK"
из функции Python, чтобы показать, что строка не была изменена, значение "SKIP"
, чтобы прервать событие, либо, если в TD["event"]
передана команда INSERT
или UPDATE
, вы можете вернуть "MODIFY"
, чтобы показать, что новая строка была изменена. Во всех других случаях возвращаемое значение игнорируется.
47.6. Trigger Functions
When a function is used as a trigger, the dictionary TD
contains trigger-related values:
TD["event"]
contains the event as a string:
INSERT
,UPDATE
,DELETE
, orTRUNCATE
.TD["when"]
contains one of
BEFORE
,AFTER
, orINSTEAD OF
.TD["level"]
contains
ROW
orSTATEMENT
.TD["new"]
TD["old"]
For a row-level trigger, one or both of these fields contain the respective trigger rows, depending on the trigger event.
TD["name"]
contains the trigger name.
TD["table_name"]
contains the name of the table on which the trigger occurred.
TD["table_schema"]
contains the schema of the table on which the trigger occurred.
TD["relid"]
contains the OID of the table on which the trigger occurred.
TD["args"]
If the
CREATE TRIGGER
command included arguments, they are available inTD["args"][0]
toTD["args"][
.n
-1]
If TD["when"]
is BEFORE
or INSTEAD OF
and TD["level"]
is ROW
, you can return None
or "OK"
from the Python function to indicate the row is unmodified, "SKIP"
to abort the event, or if TD["event"]
is INSERT
or UPDATE
you can return "MODIFY"
to indicate you've modified the new row. Otherwise the return value is ignored.