From b54c32af1a32fa7b695db1ca76ccd2c6157c9237 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mar 19 2020 13:30:24 +0000 Subject: use real time for events NOW() is time of transaction start. For long transaction, there could be race and some other transactions which ended earlier can have newer events. clock_timestamp returns real time (changes during transaction), so it should be better for this case. Fixes: https://pagure.io/koji/issue/1747 --- diff --git a/docs/schema-upgrade-1.20-1.21.sql b/docs/schema-upgrade-1.20-1.21.sql new file mode 100644 index 0000000..5e09f71 --- /dev/null +++ b/docs/schema-upgrade-1.20-1.21.sql @@ -0,0 +1,16 @@ +-- upgrade script to migrate the Koji database schema +-- from version 1.20 to 1.21 + + +BEGIN; + +-- make better events +ALTER TABLE events ALTER COLUMN time SET NOT NULL; +ALTER TABLE events ALTER COLUMN time SET DEFAULT clock_timestamp(); + +CREATE OR REPLACE FUNCTION get_event() RETURNS INTEGER AS ' + INSERT INTO events (time) VALUES (clock_timestamp()); + SELECT currval(''events_id_seq'')::INTEGER; +' LANGUAGE SQL; + +COMMIT; diff --git a/docs/schema.sql b/docs/schema.sql index 4966015..5267646 100644 --- a/docs/schema.sql +++ b/docs/schema.sql @@ -7,12 +7,12 @@ BEGIN WORK; -- in the event that the system clock rolls back, event_ids will retain proper sequencing CREATE TABLE events ( id SERIAL NOT NULL PRIMARY KEY, - time TIMESTAMP NOT NULL DEFAULT NOW() + time TIMESTAMP NOT NULL DEFAULT clock_timestamp() ) WITHOUT OIDS; -- A function that creates an event and returns the id, used as DEFAULT value for versioned tables CREATE FUNCTION get_event() RETURNS INTEGER AS ' - INSERT INTO events (time) VALUES (''now''); + INSERT INTO events (time) VALUES (clock_timestamp()); SELECT currval(''events_id_seq'')::INTEGER; ' LANGUAGE SQL;