In today’s article, we will learn A PostgreSQL trigger is a database object that automatically executes a function in response to an event such as insert, update, delete, or truncate.
Triggers cannot be operated manually by users.
Triggers perform actions before or after the action is taken, according to the actions written in them.
Triggers cause performance loss in DML operations coming to the table in which they are located.
Triggers can cause locks on large data.
After Trigger: Triggers that come into play after the transaction takes place.
Before Trigger: It is the trigger that comes into play after the transaction takes place.
Instead Of Trigger: Used while the action is about to take place.
FOR EACH ROW: It is done for every transaction made. For example, if 1000 deletes were made, it writes them all.
FOR EACH STATEMENT: Performs a single transaction for each transaction made. For example, if 1000 deletes are made, only one will be written.
To define a trigger in PostgreSQL, we first need to create a function or procedure for the desired operation. After doing this, we need to specify the function or procedure created in the trigger.
Its general usage is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CREATE FUNCTION trigger_function() RETURNS TRIGGER LANGUAGE PLPGSQL AS $$ BEGIN -- trigger logic END; $$ CREATE TRIGGER trigger_ismi AFTER INSERT ON tablo_adi FOR EACH ROW EXECUTE PROCEDURE function_ismi(); |