diff --git a/postgres/dataset_full/employee.sql b/postgres/dataset_full/employee.sql index a179700..443ce04 100644 --- a/postgres/dataset_full/employee.sql +++ b/postgres/dataset_full/employee.sql @@ -31,7 +31,8 @@ DROP TABLE IF EXISTS dept_emp, title, salary, employee, - department CASCADE; + department, + audit CASCADE; CREATE TABLE employee ( emp_no SERIAL NOT NULL, @@ -123,8 +124,9 @@ BEGIN END; $$ LANGUAGE plpgsql; +-- only log update and delete, otherwise, it will cause too much change. CREATE TRIGGER salary_log_trigger -AFTER INSERT OR UPDATE OR DELETE ON salary +AFTER UPDATE OR DELETE ON salary FOR EACH ROW EXECUTE FUNCTION log_dml_operations(); @@ -166,4 +168,4 @@ FROM \echo 'LOADING salary' \i load_salary1.sql \i load_salary2.sql -\i load_salary3.sql +\i load_salary3.sql \ No newline at end of file diff --git a/postgres/dataset_large/employee.sql b/postgres/dataset_large/employee.sql index 2a63b73..99e1e70 100644 --- a/postgres/dataset_large/employee.sql +++ b/postgres/dataset_large/employee.sql @@ -31,7 +31,8 @@ DROP TABLE IF EXISTS dept_emp, title, salary, employee, - department CASCADE; + department, + audit CASCADE; CREATE TABLE employee ( emp_no SERIAL NOT NULL, @@ -43,6 +44,8 @@ CREATE TABLE employee ( PRIMARY KEY (emp_no) ); +CREATE INDEX idx_employee_hire_date ON employee (hire_date); + CREATE TABLE department ( dept_no TEXT NOT NULL, dept_name TEXT NOT NULL, @@ -88,6 +91,45 @@ CREATE TABLE salary ( PRIMARY KEY (emp_no, from_date) ); +CREATE INDEX idx_salary_amount ON salary (amount); + +CREATE TABLE audit ( + id SERIAL PRIMARY KEY, + operation TEXT NOT NULL, + query TEXT, + user_name TEXT NOT NULL, + changed_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX idx_audit_operation ON audit (operation); +CREATE INDEX idx_audit_username ON audit (user_name); +CREATE INDEX idx_audit_changed_at ON audit (changed_at); + +CREATE OR REPLACE FUNCTION log_dml_operations() RETURNS TRIGGER AS $$ +BEGIN + IF (TG_OP = 'INSERT') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('INSERT', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'UPDATE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('UPDATE', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'DELETE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('DELETE', current_query(), current_user); + RETURN OLD; + END IF; + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +-- only log update and delete, otherwise, it will cause too much change. +CREATE TRIGGER salary_log_trigger +AFTER UPDATE OR DELETE ON salary +FOR EACH ROW +EXECUTE FUNCTION log_dml_operations(); + CREATE OR REPLACE VIEW dept_emp_latest_date AS SELECT emp_no, @@ -125,3 +167,5 @@ FROM \i load_title.sql \echo 'LOADING salary' \i load_salary1.sql +\i load_salary2.sql +\i load_salary3.sql diff --git a/postgres/dataset_small/employee.sql b/postgres/dataset_small/employee.sql index 2a63b73..9ef71e8 100644 --- a/postgres/dataset_small/employee.sql +++ b/postgres/dataset_small/employee.sql @@ -31,7 +31,8 @@ DROP TABLE IF EXISTS dept_emp, title, salary, employee, - department CASCADE; + department, + audit CASCADE; CREATE TABLE employee ( emp_no SERIAL NOT NULL, @@ -43,6 +44,8 @@ CREATE TABLE employee ( PRIMARY KEY (emp_no) ); +CREATE INDEX idx_employee_hire_date ON employee (hire_date); + CREATE TABLE department ( dept_no TEXT NOT NULL, dept_name TEXT NOT NULL, @@ -88,6 +91,50 @@ CREATE TABLE salary ( PRIMARY KEY (emp_no, from_date) ); +CREATE INDEX idx_salary_amount ON salary (amount); + +CREATE TABLE audit ( + id SERIAL PRIMARY KEY, + operation TEXT NOT NULL, + query TEXT, + user_name TEXT NOT NULL, + changed_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP +); + +CREATE INDEX idx_audit_operation ON audit (operation); +CREATE INDEX idx_audit_username ON audit (user_name); +CREATE INDEX idx_audit_changed_at ON audit (changed_at); + +CREATE OR REPLACE FUNCTION log_dml_operations() RETURNS TRIGGER AS $$ +BEGIN + IF (TG_OP = 'INSERT') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('INSERT', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'UPDATE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('UPDATE', current_query(), current_user); + RETURN NEW; + ELSIF (TG_OP = 'DELETE') THEN + INSERT INTO audit (operation, query, user_name) + VALUES ('DELETE', current_query(), current_user); + RETURN OLD; + END IF; + RETURN NULL; +END; +$$ LANGUAGE plpgsql; + +-- only log update and delete, otherwise, it will cause too much change. +CREATE TRIGGER salary_log_trigger +AFTER UPDATE OR DELETE ON salary +FOR EACH ROW +EXECUTE FUNCTION log_dml_operations(); + +CREATE TRIGGER salary_log_trigger +AFTER INSERT OR UPDATE OR DELETE ON salary +FOR EACH ROW +EXECUTE FUNCTION log_dml_operations(); + CREATE OR REPLACE VIEW dept_emp_latest_date AS SELECT emp_no, @@ -125,3 +172,5 @@ FROM \i load_title.sql \echo 'LOADING salary' \i load_salary1.sql +\i load_salary2.sql +\i load_salary3.sql