PostgreSQL 7.4beta4 Documentation
Prev | Fast Backward |  | Fast Forward | Next

# Chapter 36. Triggers

This chapter describes how to write trigger functions. In particular, it describes the C-language interface for trigger functions. The trigger interfaces in most procedural languages work analogously. (Trigger functions cannot be written in SQL.)

A trigger function can execute before or after a `INSERT`, `UPDATE`, or `DELETE`, either once per modified row, or once per SQL statement.

# 36.1. Trigger Definition

If a trigger event occurs, the trigger manager is called by the executor. It sets up an information structure of type `TriggerData` (described below) and calls the trigger function to handle the event.

The trigger function must be defined before the trigger itself can be created. The trigger function must be declared as a function taking no arguments and returning type trigger. (The trigger function receives its input through a `TriggerData` structure, not in the form of ordinary function arguments.) If the function is written in C, it must use the "version 1" function manager interface.

The syntax for creating triggers is described in CREATE TRIGGER.

Trigger functions return a value of type `HeapTuple`, which represents a table row, to the calling executor. The return value is ignored for triggers fired after an operation, but a triggers fired before an operation has the following choices:

- It can return a `NULL` pointer to skip the operation for the current row (and so the row will not be inserted/updated/deleted).

- For `INSERT` and `UPDATE` triggers only, the returned row becomes the row that will be inserted or will replace the row being updated. This allows the trigger function to modify the row being inserted or updated.

A before trigger that does not intend to cause either of these behaviors must be careful to return the same row that was passed in as the new row (see below).

If more than one trigger is defined for the same event on the same relation, the triggers will be fired in alphabetical order by name. In the case of before triggers, the possibly-modified row returned by each trigger becomes the input to the next trigger. If any before trigger returns a `NULL` pointer, the operation is abandoned and subsequent triggers are not fired.

If a trigger function executes SQL commands (using SPI) then these commands may fire triggers again. This is known as cascading triggers. There is no direct limitation on the number of cascade levels. It is possible for cascades to cause a recursive invocation of the same trigger; for example, an `INSERT` trigger might execute a command that inserts an additional row into the same table, causing the `INSERT` trigger to be fired again. It is the trigger programmer's responsibility to avoid infinite recursion in such scenarios.

When a trigger is being defined, arguments can be specified for it. The purpose of including arguments in the trigger definition is to allow different triggers with similar requirements to call the same function. As an example, there could be a generalized trigger function that takes as its arguments two column names and puts the current user in one and the current time stamp in the other. Properly written, this trigger function would be independent of the specific table it is triggering on. So the same function could be used for `INSERT` events on any table with suitable columns, to automatically track creation of records in a transaction table for example. It could also be used to track last-update events if defined as an `UPDATE` trigger.

Prev | Home | Next
Rules versus Triggers | Up | Interaction with the Trigger Manager