Skip to content

Commit 73e4517

Browse files
committed
use bigint timestamp
1 parent 0a876b8 commit 73e4517

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

audit.sql

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
-- This file should be generic and not depend on application roles or structures,
55
-- as it's being listed here:
66
--
7-
-- https://wiki.postgresql.org/wiki/Audit_trigger_91plus
7+
-- https://wiki.postgresql.org/wiki/Audit_trigger_91plus
88
--
99
-- This trigger was originally based on
10-
-- http://wiki.postgresql.org/wiki/Audit_trigger
10+
-- http://wiki.postgresql.org/wiki/Audit_trigger
1111
-- but has been completely rewritten.
1212
--
1313
-- Should really be converted into a relocatable EXTENSION, with control and upgrade files.
@@ -42,9 +42,9 @@ CREATE TABLE audit.logged_actions (
4242
table_name text not null,
4343
relid oid not null,
4444
session_user_name text,
45-
action_tstamp_tx TIMESTAMP WITH TIME ZONE NOT NULL,
46-
action_tstamp_stm TIMESTAMP WITH TIME ZONE NOT NULL,
47-
action_tstamp_clk TIMESTAMP WITH TIME ZONE NOT NULL,
45+
action_tstamp_tx bigint NOT NULL,
46+
action_tstamp_stm bigint NOT NULL,
47+
action_tstamp_clk bigint NOT NULL,
4848
transaction_id bigint,
4949
application_name text,
5050
client_addr inet,
@@ -65,9 +65,9 @@ COMMENT ON COLUMN audit.logged_actions.schema_name IS 'Database schema audited t
6565
COMMENT ON COLUMN audit.logged_actions.table_name IS 'Non-schema-qualified table name of table event occured in';
6666
COMMENT ON COLUMN audit.logged_actions.relid IS 'Table OID. Changes with drop/create. Get with ''tablename''::regclass';
6767
COMMENT ON COLUMN audit.logged_actions.session_user_name IS 'Login / session user whose statement caused the audited event';
68-
COMMENT ON COLUMN audit.logged_actions.action_tstamp_tx IS 'Transaction start timestamp for tx in which audited event occurred';
69-
COMMENT ON COLUMN audit.logged_actions.action_tstamp_stm IS 'Statement start timestamp for tx in which audited event occurred';
70-
COMMENT ON COLUMN audit.logged_actions.action_tstamp_clk IS 'Wall clock time at which audited event''s trigger call occurred';
68+
COMMENT ON COLUMN audit.logged_actions.action_tstamp_tx IS 'Transaction start epoch for tx in which audited event occurred';
69+
COMMENT ON COLUMN audit.logged_actions.action_tstamp_stm IS 'Statement start epoch for tx in which audited event occurred';
70+
COMMENT ON COLUMN audit.logged_actions.action_tstamp_clk IS 'Wall clock time epoch at which audited event''s trigger call occurred';
7171
COMMENT ON COLUMN audit.logged_actions.transaction_id IS 'Identifier of transaction that made the change. May wrap, but unique paired with action_tstamp_tx.';
7272
COMMENT ON COLUMN audit.logged_actions.client_addr IS 'IP address of client that issued query. Null for unix ___domain socket.';
7373
COMMENT ON COLUMN audit.logged_actions.client_port IS 'Remote peer IP port address of client that issued query. Undefined for unix socket.';
@@ -90,30 +90,32 @@ DECLARE
9090
h_old hstore;
9191
h_new hstore;
9292
excluded_cols text[] = ARRAY[]::text[];
93+
precision int = 1000000;
9394
BEGIN
9495
IF TG_WHEN <> 'AFTER' THEN
9596
RAISE EXCEPTION 'audit.if_modified_func() may only run as an AFTER trigger';
9697
END IF;
9798

9899
audit_row = ROW(
99-
nextval('audit.logged_actions_event_id_seq'), -- event_id
100-
TG_TABLE_SCHEMA::text, -- schema_name
101-
TG_TABLE_NAME::text, -- table_name
102-
TG_RELID, -- relation OID for much quicker searches
103-
session_user::text, -- session_user_name
104-
current_timestamp, -- action_tstamp_tx
105-
statement_timestamp(), -- action_tstamp_stm
106-
clock_timestamp(), -- action_tstamp_clk
107-
txid_current(), -- transaction ID
108-
current_setting('application_name'), -- client application
109-
inet_client_addr(), -- client_addr
110-
inet_client_port(), -- client_port
111-
current_query(), -- top-level query or queries (if multistatement) from client
112-
substring(TG_OP,1,1), -- action
113-
NULL, NULL, -- row_data, changed_fields
114-
'f', -- statement_only
115-
NULL -- fields to store abritary info
116-
);
100+
nextval('audit.logged_actions_event_id_seq'), -- event_id
101+
TG_TABLE_SCHEMA::text, -- schema_name
102+
TG_TABLE_NAME::text, -- table_name
103+
TG_RELID, -- relation OID for much quicker searches
104+
session_user::text, -- session_user_name
105+
EXTRACT(EPOCH FROM current_timestamp) * precision, -- action_tstamp_tx
106+
EXTRACT(EPOCH FROM statement_timestamp()) * precision, -- action_tstamp_stm
107+
EXTRACT(EPOCH FROM clock_timestamp()) * precision, -- action_tstamp_clk
108+
txid_current(), -- transaction ID
109+
current_setting('application_name'), -- client application
110+
inet_client_addr(), -- client_addr
111+
inet_client_port(), -- client_port
112+
current_query(), -- top-level query or queries (if multistatement) from client
113+
substring(TG_OP,1,1), -- action
114+
NULL, -- row_data
115+
NULL, -- changed_fields
116+
'f', -- statement_only
117+
NULL -- fields to store abritary info
118+
);
117119

118120
IF NOT TG_ARGV[0]::boolean IS DISTINCT FROM 'f'::boolean THEN
119121
audit_row.client_query = NULL;

0 commit comments

Comments
 (0)