Sha256: aa1cca91478cfa384a320c1fc0ae1a83ccce10eee5a33f8cbe97f7cc0c0a85e0
Contents?: true
Size: 881 Bytes
Versions: 26
Compression:
Stored size: 881 Bytes
Contents
CREATE OR REPLACE FUNCTION hstore_logger() RETURNS TRIGGER AS $body$ DECLARE changes_h jsonb; size integer; buffer jsonb; BEGIN size := jsonb_array_length(NEW.log); changes_h := hstore_to_jsonb_loose( hstore(NEW.*) - hstore(OLD.*) ); NEW.log := jsonb_set( NEW.log, ARRAY[size::text], jsonb_build_object( 'ts', extract(epoch from now())::int, 'i', (NEW.log#>>ARRAY[(size - 1)::text, 'i'])::int + 1, 'd', changes_h ), true ); return NEW; END; $body$ LANGUAGE plpgsql; ALTER TABLE pgbench_accounts ADD COLUMN log jsonb DEFAULT '[]' NOT NULL; UPDATE pgbench_accounts SET log = to_jsonb(ARRAY[json_build_object('i', 0)])::jsonb; CREATE TRIGGER hstore_log_accounts BEFORE UPDATE ON pgbench_accounts FOR EACH ROW EXECUTE PROCEDURE hstore_logger();
Version data entries
26 entries across 26 versions & 1 rubygems