I've created simple trigger
CREATE TABLE data ( id int, v1 text, v2 text ); CREATE TABLE events ( id serial primary key, v1 text ); CREATE OR REPLACE FUNCTION emit_event_for_v1() RETURNS trigger AS ' BEGIN IF OLD.v1 != NEW.v1 THEN INSERT INTO events (v1) VALUES (NEW.v1); END IF; RETURN NULL; END ' LANGUAGE 'plpgsql'; CREATE TRIGGER event_v1 AFTER INSERT OR UPDATE ON data FOR EACH ROW EXECUTE PROCEDURE emit_event_for_v1(); INSERT INTO data VALUES (1, '', '');
After that, I've launched many clients (25) doing many updates (500 each) on the data.
UPDATE data SET v1 = $1 WHERE id = 1
$WORKER_ID : $ITER_ID. In the end I've verified that last
inserted event (
SELECT id, v1 FROM events ORDER BY id DESC) matches current
state of data (
SELECT v1 FROM data WHERE id = 1).
I've also checked that
id of last event is equal to number of updates
25 * 500). And it is.
My question is, is this guaranteed to work? Are results of inserts in triggers always visible in same order as the original update that triggered the trigger? Or did I just get lucky during my testing (like 50 times in a row)?