On 01/25/2018 03:14 PM, Steven Winfield wrote: There have been comments on- and off-list about rules generally being slower than rules, which seemed counterintuitive (for my case at least) so I’ve done some
timings. (Also note that I haven’t done any partitioning here, new- or old-style - a few people have mentioned RULEs in relation to partitioning). These compare the INSERT and UPDATE rules against equivalent INSTEAD OF triggers. Best of three runs each time, times are in seconds. rows 10^4 10^5 10^6 insert rule 0.9 15.0 179 insert trigger 1.3 19.7 224 delete rule 1.8 22.8 282 delete trigger 2.3 28.0 331 …so the rules are about 20% faster than the triggers. Significant, but not game-changing. Note that this is on quite close to a “real life” table too - there is the maintenance of the primary key index and the gist index that supports the exclude
constraint in all those timings, so a table without those would have shown a bigger disparity between the two methods. This makes sense - the RULEs just have one query to rewrite before it is planned and executed, whereas the TRIGGERs have to be re-executed for each row. Back to my original attempt at writing an UPDATE rule… CREATE RULE rule_test_update AS ON UPDATE TO rule_test_view DO INSTEAD ( UPDATE rule_test SET tt = tstzrange(lower(tt), CURRENT_TIMESTAMP, '[)') WHERE id = OLD.id; INSERT INTO rule_test (tt, foo, bar) VALUES (tstzrange(CURRENT_TIMESTAMP, NULL, '[)'), NEW.foo, NEW.bar) RETURNING *; ); …I wondered whether the pseudo relations NEW and OLD were somehow being modified by the first command (the UPDATE), such that the second command (INSERT) could
not function properly. That would fit with what I observe, but I’m not sure how I go about proving or fixing it. Best, Steven. |