Triggers in PostgreSQL are special procedures that are automatically executed or fired when certain events occur in a database table. Statement-level triggers are executed once per SQL statement, regardless of the number of rows affected by the statement.
Usage
Statement-level triggers are used to enforce business rules, validate input, or maintain audit logs when a statement affects any number of rows. They are defined to act before or after `INSERT`, `UPDATE`, or `DELETE` operations on a table.
sql
CREATETRIGGER trigger_name
{BEFORE|AFTER}{INSERT|UPDATE|DELETE}ON table_name
FOREACHSTATEMENTEXECUTEPROCEDUREfunction_name();Работает на
In this syntax, `FOR EACH STATEMENT` specifies that the trigger should fire once per statement, and `EXECUTE PROCEDURE` defines the function to be called. Note that statement-level triggers do not have access to the `OLD` and `NEW` row values, as they are not row-specific.
Examples
1. Basic Statement-level Trigger
sql
-- This function logs an entry into audit_log whenever an INSERT,UPDATE, or DELETE operation is performed on the employees table.CREATEFUNCTIONlog_total_changes()RETURNSTRIGGERAS $$
BEGININSERTINTOaudit_log(event_type, event_time)VALUES(TG_OP,now());RETURNNULL;END;
$$ LANGUAGE plpgsql;CREATETRIGGER audit_changes
AFTERINSERTORUPDATEORDELETEON employees
FOREACHSTATEMENTEXECUTEPROCEDURElog_total_changes();Работает на
2. Trigger for Insert Operations
sql
-- This function logs every INSERT operation on the employees table into the insert_log table with the current timestamp.CREATEFUNCTIONlog_inserts()RETURNSTRIGGERAS $$
BEGININSERTINTOinsert_log(table_name, insert_time)VALUES(TG_TABLE_NAME,now());RETURNNULL;END;
$$ LANGUAGE plpgsql;CREATETRIGGER log_employee_inserts
AFTERINSERTON employees
FOREACHSTATEMENTEXECUTEPROCEDURElog_inserts();Работает на
3. Conditional Trigger Execution
sql
-- This function conditionally calls a notification function only when a DELETE operation is performed on the orders table.CREATEFUNCTIONnotify_admin()RETURNSTRIGGERAS $$
BEGINIF(TG_OP='DELETE')THENPERFORMnotify_admin_function();ENDIF;RETURNNULL;END;
$$ LANGUAGE plpgsql;CREATETRIGGER admin_notification
AFTERDELETEON orders
FOREACHSTATEMENTEXECUTEPROCEDUREnotify_admin();Работает на
In these examples, `TG_OP` refers to the type of operation (`INSERT`, `UPDATE`, or `DELETE`), and `TG_TABLE_NAME` refers to the name of the table on which the trigger is defined.
Tips and Best Practices
Limit trigger complexity. Keep trigger logic simple to avoid unintended side effects and maintain performance.
Use triggers sparingly. Excessive use can lead to maintenance challenges and make debugging difficult.
Clearly document triggers. Always document the purpose and function of triggers within your database.
Test thoroughly. Ensure triggers are thoroughly tested to validate their behavior under various scenarios.
Consider performance impact. Be aware that triggers can affect performance, especially in high-transaction environments.
Consider using row-level triggers for row-specific logic. Statement-level triggers are not suitable for row-specific operations, so use row-level triggers as needed.
Understand trigger differences. Statement-level triggers execute once per statement without row-specific access, while row-level triggers execute for each affected row and can access `OLD` and `NEW` row values.
Triggers in PostgreSQL are special procedures that are automatically executed or fired when certain events occur in a database table. Statement-level triggers are executed once per SQL statement, regardless of the number of rows affected by the statement.
Usage
Statement-level triggers are used to enforce business rules, validate input, or maintain audit logs when a statement affects any number of rows. They are defined to act before or after `INSERT`, `UPDATE`, or `DELETE` operations on a table.
In this syntax, `FOR EACH STATEMENT` specifies that the trigger should fire once per statement, and `EXECUTE PROCEDURE` defines the function to be called. Note that statement-level triggers do not have access to the `OLD` and `NEW` row values, as they are not row-specific.
Examples
1. Basic Statement-level Trigger
2. Trigger for Insert Operations
3. Conditional Trigger Execution
In these examples, `TG_OP` refers to the type of operation (`INSERT`, `UPDATE`, or `DELETE`), and `TG_TABLE_NAME` refers to the name of the table on which the trigger is defined.
Tips and Best Practices