From b54c32af1a32fa7b695db1ca76ccd2c6157c9237 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Mon, 9 Mar 2020 09:48:45 +0100 Subject: [PATCH] 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 --- docs/schema-upgrade-1.20-1.21.sql | 16 ++++++++++++++++ docs/schema.sql | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 docs/schema-upgrade-1.20-1.21.sql 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 00000000..5e09f715 --- /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 49660157..52676464 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;