Showing posts with label triggers. Show all posts
Showing posts with label triggers. Show all posts

Thursday, 13 March 2025

Returning Data From Modified Rows In PostgreSQL || Best PostgreSQL Tutor...


In this video, we explore how to return data from modified rows using the RETURNING clause in PostgreSQL. PostgreSQL offers a powerful RETURNING clause with INSERT, UPDATE, DELETE, and MERGE commands that allows you to fetch data directly from affected rows without running an additional SELECT query.

The RETURNING clause is especially useful when working with auto-generated IDs (serial columns), computed columns, or trigger-influenced data, and helps in optimizing database interactions.

We will start by demonstrating INSERT statements with RETURNING, including how to fetch serial IDs and entire rows. Then, we cover UPDATE with RETURNING to get updated values in real-time, and DELETE with RETURNING to capture deleted rows for audit or logging purposes.

Moreover, you will learn how to combine MERGE with RETURNING for upsert operations, and how triggers interact with RETURNING to retrieve rows modified by triggers before final insertion.

Key Highlights of this video:

  • Syntax and usage of RETURNING clause.
  • Efficient way to fetch modified rows after DML operations.
  • Practical examples with INSERT, UPDATE, DELETE, MERGE.
  • How RETURNING works with trigger-modified data.
  • Real-time data retrieval without additional SELECT queries.

If you are working on PostgreSQL databases and want to optimize your data modification workflows, this video is for you! Watch till the end to fully master the RETURNING clause!

👉 Next Video: Table Expressions In PostgreSQL - The FROM Clause

🔔 Subscribe to our channel for more practical PostgreSQL tutorials!

Sunday, 20 August 2023

How To Call A Stored Procedure/Function From A Trigger Function In Postg...


In this tutorial, you will learn how to call a stored procedure or function from a trigger function in PostgreSQL. This video is designed for developers and database administrators who want to automate tasks or enforce business rules by leveraging the power of triggers in PostgreSQL. We'll begin by explaining what triggers are and how they work within a PostgreSQL database. Next, we'll walk you through the process of creating a trigger function that can invoke a stored procedure or function whenever a specific event occurs in your database, such as an insert, update, or delete operation. You'll see practical examples and gain insights into how this powerful feature can enhance your database applications. By the end of this tutorial, you'll be confident in your ability to implement triggers that call stored procedures or functions, adding a new level of automation and functionality to your PostgreSQL projects.

PostgreSQL triggers, stored procedure PostgreSQL, function PostgreSQL, call stored procedure from trigger, PostgreSQL tutorial, trigger function PostgreSQL, PostgreSQL automation, database triggers, SQL tutorial, PostgreSQL functions, PostgreSQL stored procedures, PostgreSQL 16, SQL triggers, PostgreSQL trigger examples, database management, PostgreSQL development, SQL server, trigger stored procedure, PostgreSQL database, PostgreSQL trigger function tutorial

Please Like, Comment, and Subscribe to my channel. ❤

Working with triggers is fun and learning. There are endless possibilities that can be achieved through Triggers.

This time, I have explained the usage of Triggers to call other stored procedures or functions in PostgreSQL Trigger Functions.


#trigger #procedure #function #postgresql #database


CREATE TABLE students
(
    roll numeric(10,0),
    name character varying(30),
    course character varying(30)
);


CREATE OR REPLACE PROCEDURE 
trigger_proc(in_value1 IN numeric, in_value2 IN numeric, out_result OUT numeric)
LANGUAGE 'plpgsql'
AS $BODY$
Declare
lv_msg CHARACTER varying(100);
Begin
  out_result := in_value1 + in_value2;
exception
when others then
lv_msg := 'Error : '||sqlerrm;
raise notice '%',lv_msg;
end;
$BODY$;


CREATE OR REPLACE FUNCTION 
trigger_func(in_value1 IN numeric, in_value2 IN numeric) returns numeric
LANGUAGE 'plpgsql'
AS $BODY$
Declare
lv_msg CHARACTER varying(100);
out_result numeric (10);
Begin
  out_result := in_value1 + in_value2;
return out_result;
exception
when others then
lv_msg := 'Error :'||sqlerrm;
raise notice '%',lv_msg;
end;
$BODY$;


CREATE OR REPLACE FUNCTION student_logs_trg_func()
    RETURNS TRIGGER
    LANGUAGE 'plpgsql'
AS $BODY$
declare
lv_out_proc numeric(10);
lv_out_func numeric(10);
begin
call trigger_proc(50, 50, lv_out_proc);
lv_out_func := trigger_func(100, 100);
raise notice 'Proc Output: %',lv_out_proc;
raise notice 'Func Output: %',lv_out_func;
return new;
end;
$BODY$;


CREATE TRIGGER student_trg
    BEFORE INSERT OR DELETE OR UPDATE 
    ON students
    FOR EACH ROW
    EXECUTE FUNCTION student_logs_trg_func();

Insert into students values (1, 'Akram', 'MCA');


How To Call A Stored Procedure/Function From A Trigger Function In PostgreSQL
How To Call A Stored Procedure From A Trigger Function
Trigger Function In PostgreSQL
How To Call Stored Function From Trigger Function
Trigger In PostgreSQL
Call Procedure From Trigger
Call Function From Trigger
Procedure Call From Trigger In PostgreSQL
Function Call From Trigger In PostgreSQL
PostgreSQL Triggers
Triggers
Trigger Example PostgreSQL
How To Triggers PostgreSQL

Sunday, 12 February 2023

How To Create Triggers In PostgreSQL || How To Create Trigger Function I...


In this comprehensive tutorial, we delve into the world of triggers in PostgreSQL, showing you how to create both triggers and trigger functions from scratch. Triggers are a powerful feature in PostgreSQL that allow you to automatically execute a function when certain events occur on a table, such as INSERT, UPDATE, or DELETE operations.

We start by explaining the concept of triggers and their importance in maintaining data integrity and automating routine tasks. You’ll learn how to define a trigger function, which contains the logic that will be executed when the trigger is fired. From there, we guide you through the process of creating a trigger that links this function to a specific table, ensuring that your database reacts automatically to changes.

The video includes detailed, step-by-step examples, making it easy for you to follow along and implement these techniques in your own PostgreSQL environment. We also cover best practices for managing and optimizing triggers, ensuring that they perform efficiently without negatively impacting your database.

Whether you're a database administrator, developer, or just starting with PostgreSQL, this tutorial will equip you with the knowledge needed to effectively use triggers to automate and secure your database operations.


PostgreSQL triggers, create triggers PostgreSQL, trigger functions PostgreSQL, SQL triggers, database automation, PostgreSQL tutorial, database management, trigger functions examples, PostgreSQL triggers step-by-step, SQL automation


-- Table: public.students

-- DROP TABLE IF EXISTS public.students;

CREATE TABLE IF NOT EXISTS public.students
(
    roll numeric(10,0),
    name character varying(30) COLLATE pg_catalog."default",
    course character varying(30) COLLATE pg_catalog."default"
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.students
    OWNER to postgres;

-- Table: public.students_logs

-- DROP TABLE IF EXISTS public.students_logs;

CREATE TABLE IF NOT EXISTS public.students_logs
(
    roll_old numeric(10,0),
    name_old character varying(30) COLLATE pg_catalog."default",
    course_old character varying(30) COLLATE pg_catalog."default"
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.students_logs
    OWNER to postgres;


-- FUNCTION: public.student_logs_trg_func()

-- DROP FUNCTION IF EXISTS public.student_logs_trg_func();

CREATE OR REPLACE FUNCTION public.student_logs_trg_func()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
declare
begin
insert into students_logs
(roll_old,name_old,course_old)
values(old.roll,old.name,old.course);
return new;
end;
$BODY$;

ALTER FUNCTION public.student_logs_trg_func()
    OWNER TO postgres;


-- Trigger: student_trg

-- DROP TRIGGER IF EXISTS student_trg ON public.students;

CREATE TRIGGER student_trg
    BEFORE DELETE OR UPDATE 
    ON public.students
    FOR EACH ROW
    EXECUTE FUNCTION public.student_logs_trg_func();

insert into students values (1,'Akram','MCA');

select * from students;
select * from students_logs;

update students set name = 'Akram Sohail' where roll = 1;

update students set name = 'Akram Sohail (Modified)' where roll = 1;

delete from students where roll = 1;


----------------------------------------------------------------------------------------------

Triggers are special types of functions that are called/invoked/executed/performed automatically as per the trigger event declaration. A trigger is usually set before or after updating, deleting, insert DML statements on a table. That means, whenever a record is to be inserted, updated, or deleted, based on the time of execution which can be before or after, the trigger executes. Generally, the execution of the Trigger is called “Trigger Fired”.

In PostgreSQL, to create a trigger and make it usable, the sequence of object creation will be:
•   Create a Table on which a trigger to be implemented
•   Create another table to keep logs, so we can see the Trigger work
•   Create a Trigger Function that holds the trigger
•   Create the Trigger Function and Define the Trigger Logic
•   Perform Insert/Update/Delete Operations on the Table