简体   繁体   中英

user creation dumping using trigger and fucntions

As PostgreSQL doesn't dump object creation date so I want to manually dump user creation date by using trigger and functions. I have created trigger and functions but it's not working.

CREATE TABLE user_audits (
   usesysid INT GENERATED ALWAYS AS IDENTITY,
   usename varchar NOT NULL,
   created_on TIMESTAMP(6) NOT NULL
);

============================
CREATE OR REPLACE FUNCTION user_creation()
  RETURNS TRIGGER 
  LANGUAGE PLPGSQL
  AS
$$
BEGIN
    IF NEW.usename <> OLD.usename THEN
         INSERT INTO user_audits(usesysid,usename,created_on)
         VALUES(usesysid,usename,now());
    END IF;

    RETURN NEW;
END;
$$
=================================
CREATE TRIGGER user_creation
  BEFORE UPDATE
  ON user
  FOR EACH ROW
  EXECUTE PROCEDURE user_creation();

This is important for audit purpose, for now I am using log file to check creation date but it will rotate after sometime. Please suggest the better way to dump user creation date in table so that I can retrieve the information anytime. Thanks

I created a similar excercise with the following tables:

  1. The user_tbl table having only a identity column usersysid and the username
CREATE TABLE user_tbl (
   usersysid INT GENERATED ALWAYS AS IDENTITY,
   username varchar NOT NULL
);
  1. The user_audits table, slightly modified version of yours: where i added an id identity field. I removed the identity from the usersysid field (since it'll be populated with the one coming from user_tbl )
CREATE TABLE user_audits (
   id INT GENERATED ALWAYS AS IDENTITY,
   usersysid INT,
   username varchar NOT NULL,
   created_on TIMESTAMP(6) NOT NULL
);

Now the function, I check if the OLD.username is null, this means that is an insert, if NEW.username <> OLD.username then is an update.

CREATE OR REPLACE FUNCTION user_creation()
  RETURNS TRIGGER 
  LANGUAGE PLPGSQL
  AS
$$
BEGIN
    IF OLD.username is null OR NEW.username <> OLD.username THEN
         INSERT INTO user_audits(usersysid,username,created_on)
         VALUES(NEW.usersysid,NEW.username,now());
    END IF;

    RETURN NEW;
END;
$$
;

And finally the trigger, which is fired both on INSERT or UPDATE

CREATE TRIGGER user_creation
BEFORE INSERT OR UPDATE
ON user_tbl
FOR EACH ROW
EXECUTE PROCEDURE user_creation();

Now if I create two new rows and update one with the following

insert into user_tbl (username) values('Carlo');
insert into user_tbl (username) values('Gianni');
update user_tbl set username='Giorgio' where usersysid=1;

I end up with the user_tbl containing the 2 expected rows

defaultdb=> select * from user_tbl;
 usersysid | username 
-----------+----------
         2 | Gianni
         1 | Giorgio
(2 rows)

and the user_audits tables containing 3 rows (2 for the insert + 1 for the update)

defaultdb=> select * from user_audits;
 id | usersysid | username |         created_on         
----+-----------+----------+----------------------------
  1 |         1 | Carlo    | 2021-06-04 13:57:44.810889
  2 |         2 | Gianni   | 2021-06-04 13:58:14.680878
  3 |         1 | Giorgio  | 2021-06-04 13:58:44.702364
(3 rows)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM