Skip to content

Commit

Permalink
docs: improve sample pg
Browse files Browse the repository at this point in the history
  • Loading branch information
tianzhou committed Aug 4, 2024
1 parent 5555fad commit 51250c3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
8 changes: 5 additions & 3 deletions postgres/dataset_full/employee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -166,4 +168,4 @@ FROM
\echo 'LOADING salary'
\i load_salary1.sql
\i load_salary2.sql
\i load_salary3.sql
\i load_salary3.sql
46 changes: 45 additions & 1 deletion postgres/dataset_large/employee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -125,3 +167,5 @@ FROM
\i load_title.sql
\echo 'LOADING salary'
\i load_salary1.sql
\i load_salary2.sql
\i load_salary3.sql
51 changes: 50 additions & 1 deletion postgres/dataset_small/employee.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -125,3 +172,5 @@ FROM
\i load_title.sql
\echo 'LOADING salary'
\i load_salary1.sql
\i load_salary2.sql
\i load_salary3.sql

0 comments on commit 51250c3

Please sign in to comment.