create function tg_audit_dist_shard() returns trigger
language plpgsql
as
$$
DECLARE
audit_entry int := nextval('audit_entry_seq'::regclass);
BEGIN
IF (TG_OP = 'INSERT') THEN
INSERT INTO dist_shard_audit
VALUES (now(), audit_entry, TG_OP::audit_action, TG_RELID::regclass, NEW.*);
RETURN NEW;
ELSIF (TG_OP = 'UPDATE') THEN
INSERT INTO dist_shard_audit
VALUES (now(), audit_entry, 'UPDATE_OLD'::audit_action, TG_RELID::regclass, OLD.*),
(now(), audit_entry, 'UPDATE_NEW'::audit_action, TG_RELID::regclass, NEW.*);
ELSIF (TG_OP = 'DELETE') THEN
INSERT INTO dist_shard_audit
VALUES (now(), audit_entry, TG_OP::audit_action, TG_RELID::regclass, OLD.*);
RETURN OLD;
END IF;
RETURN NULL;
END;
$$;