Hello,
I'd like to know how can a Trigger Function (written in PL/pgSQL) obtain the name of fields where OLD content is different from NEW content. Actually, I'd like to stock these name into a list variable.
Example :
I've got this table :
CREATE TABLE public.machine (
id_machine int8 NOT NULL,
str_serial_number_machine varchar(100),
str_comment text,
str_name_machine varchar(20),
id_state_machine int8,
CONSTRAINT machine_pkey PRIMARY KEY (id_machine)
) WITH OIDS;
And i'd like to write a PL/pgSQL Trigger function which can know what are the fields that have been modified in order to obtain something like this :
CREATE FUNCTION public.trig_machine() RETURNS trigger AS 'DECLARE
req varchar(4000);
colonne RECORD;
BEGIN
-- Here's a SQL request which obtain the list of all column name of the modified table (TG_relname)
req:=''select attname from pg_attribute where attrelid=(select relfilenode from pg_class where relname='''''';
req:=req || TG_RELNAME;
req := req || '''''') and attnum>0'';
FOR colonne IN EXECUTE req LOOP
insert into machine_history (str_old_value,str_new_value) values (OLD.colonne.attnum, NEW.colonne.attnum);
END LOOP;
return NEW;
END;' LANGUAGE 'plpgsql' VOLATILE;
I'd like to know how can a Trigger Function (written in PL/pgSQL) obtain the name of fields where OLD content is different from NEW content. Actually, I'd like to stock these name into a list variable.
Example :
I've got this table :
CREATE TABLE public.machine (
id_machine int8 NOT NULL,
str_serial_number_machine varchar(100),
str_comment text,
str_name_machine varchar(20),
id_state_machine int8,
CONSTRAINT machine_pkey PRIMARY KEY (id_machine)
) WITH OIDS;
And i'd like to write a PL/pgSQL Trigger function which can know what are the fields that have been modified in order to obtain something like this :
CREATE FUNCTION public.trig_machine() RETURNS trigger AS 'DECLARE
req varchar(4000);
colonne RECORD;
BEGIN
-- Here's a SQL request which obtain the list of all column name of the modified table (TG_relname)
req:=''select attname from pg_attribute where attrelid=(select relfilenode from pg_class where relname='''''';
req:=req || TG_RELNAME;
req := req || '''''') and attnum>0'';
FOR colonne IN EXECUTE req LOOP
insert into machine_history (str_old_value,str_new_value) values (OLD.colonne.attnum, NEW.colonne.attnum);
END LOOP;
return NEW;
END;' LANGUAGE 'plpgsql' VOLATILE;