Skip to content

Postgres 9.6 compatibility #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions audit.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- This is based on 2ndQuadrant/audit-trigger.
--
-- Few changes from the original
-- 1. Requires postgres >= 10
-- 1. Requires postgres >= 9.6
-- 2. Row data is stored in jsonb

-- The following are comments preserved from the original file:
Expand Down Expand Up @@ -89,6 +89,33 @@ CREATE INDEX logged_actions_relid_idx ON audit.logged_actions(relid);
CREATE INDEX logged_actions_action_tstamp_tx_stm_idx ON audit.logged_actions(action_tstamp_stm);
CREATE INDEX logged_actions_action_idx ON audit.logged_actions(action);

--
-- Helper function to support postgres 9.6
CREATE OR REPLACE FUNCTION audit.exclude_keys(obj jsonb, ekeys text[]) RETURNS jsonb AS $$
DECLARE
dest jsonb;
BEGIN
SELECT json_object_agg(filtered.key, filtered.value) into dest
FROM (
SELECT * from jsonb_each(obj) where NOT (key = ANY (ekeys))
) as filtered;

RETURN dest;
END;
$$ LANGUAGE plpgsql
IMMUTABLE
RETURNS NULL ON NULL INPUT;

COMMENT ON FUNCTION audit.exclude_keys(jsonb, text[]) IS $body$
Compatibility Function for postgres 9.6.
The function excludes a specific set of keys given in the second parameter from the jsonb object provided in the first parameter.

param 0: jsonb, The object to be processed.

param 1: text[], columns to be excluded.

$body$;

CREATE OR REPLACE FUNCTION audit.if_modified_func() RETURNS TRIGGER AS $body$
DECLARE
audit_row audit.logged_actions;
Expand Down Expand Up @@ -131,18 +158,18 @@ BEGIN
IF (TG_OP = 'UPDATE' AND TG_LEVEL = 'ROW') THEN
old_r = to_jsonb(OLD);
new_r = to_jsonb(NEW);
audit_row.row_data = old_r - excluded_cols;
audit_row.row_data = exclude_keys(old_r, excluded_cols);
SELECT
jsonb_object_agg(new_t.key, new_t.value) - excluded_cols
exclude_keys(jsonb_object_agg(new_t.key, new_t.value), excluded_cols)
INTO
audit_row.changed_fields
FROM jsonb_each(old_r) as old_t
JOIN jsonb_each(new_r) as new_t
ON (old_t.key = new_t.key AND old_t.value <> new_t.value);
ELSIF (TG_OP = 'DELETE' AND TG_LEVEL = 'ROW') THEN
audit_row.row_data = to_jsonb(OLD) - excluded_cols;
audit_row.row_data = exclude_keys(to_jsonb(OLD), excluded_cols);
ELSIF (TG_OP = 'INSERT' AND TG_LEVEL = 'ROW') THEN
audit_row.row_data = to_jsonb(NEW) - excluded_cols;
audit_row.row_data = exclude_keys(to_jsonb(NEW), excluded_cols);
ELSIF (TG_LEVEL = 'STATEMENT' AND TG_OP IN ('INSERT','UPDATE','DELETE','TRUNCATE')) THEN
audit_row.statement_only = 't';
ELSE
Expand Down