Обсуждение: create a procedure based on column data change in postgrersql

Поиск
Список
Период
Сортировка

create a procedure based on column data change in postgrersql

От
Jian He
Дата:

This is very useful and interesting.  I can see it's very useful. I am not sure if it's costly or not? 
begin;
create temp table account_details(email text primary key, username text, password text);
insert into account_details(email, username, password) values('a.com','b','c'),('b.com','d','e');
commit;

CREATE TRIGGER trigger_update_account_details
AFTER UPDATE ON account_details
FOR EACH ROW
WHEN (OLD.email IS DISTINCT FROM NEW.email
OR OLD.username IS DISTINCT FROM NEW.username
OR OLD.password IS DISTINCT FROM NEW.password)
EXECUTE PROCEDURE notify_insert_account_details();
________________________________________________________________________
What possible  PROCEDURE notify_insert_account_details() can be. 
The following is my immature test code. 
CREATE OR REPLACE FUNCTION notify_insert_account_details()
RETURNS trigger
LANGUAGE plpgsql AS
$$
BEGIN
RAISE NOTICE 'hello world';
END
$$;
_______________________________________________________
when I update then error occurs. the update clause didn't execute. the function did fired. 
NOTICE: hello world
ERROR: control reached end of trigger procedure without RETURN
CONTEXT: PL/pgSQL function notify_insert_account_details()
______________________________________________
I am wondering how sophisticated this procedure/function notify_insert_account_details() can become, let's say linked within 3 table.
Can anyone showcase an example/demo?

I also asked same question on stackoverflow. Since it's more easy to search. (https://stackoverflow.com/questions/69631945/create-a-procedure-based-on-column-data-change-in-postgrersql),

Re: create a procedure based on column data change in postgrersql

От
"David G. Johnston"
Дата:
On Tue, Oct 19, 2021 at 6:46 AM Jian He <hejian.mark@gmail.com> wrote:

This is very useful and interesting.  I can see it's very useful. I am not sure if it's costly or not? 

when I update then error occurs. the update clause didn't execute. the function did fired. 
NOTICE: hello world
ERROR: control reached end of trigger procedure without RETURN
CONTEXT: PL/pgSQL function notify_insert_account_details()
______________________________________________
I am wondering how sophisticated this procedure/function notify_insert_account_details() can become, let's say linked within 3 table.
Can anyone showcase an example/demo?

I also asked same question on stackoverflow. Since it's more easy to search. (https://stackoverflow.com/questions/69631945/create-a-procedure-based-on-column-data-change-in-postgrersql),


When you discover some new feature that you've never seen before on StackOverflow (or elsewhere) I suggest you take some time to read the documentation, which often contains examples.

In this case you seem to have discovered:
user-defined functions
pl/pgsql language functions
triggers

There is lots of excellent material and examples on how these things all work in the documentation.

David J.