forked from lix-project/hydra
452c8e36d1
The NrBuilds table tracks the value of ‘select count(*) from Builds where finished = 0’, keeping it up to date via a trigger. This is necessary to make the /all page fast, since otherwise it needs to do a sequential scan on the Builds table.
23 lines
848 B
PL/PgSQL
23 lines
848 B
PL/PgSQL
create table NrBuilds (
|
|
what text primary key not null,
|
|
count integer not null
|
|
);
|
|
|
|
create function modifyNrBuildsFinished() returns trigger as $$
|
|
begin
|
|
if ((tg_op = 'INSERT' and new.finished = 1) or
|
|
(tg_op = 'UPDATE' and old.finished = 0 and new.finished = 1)) then
|
|
update NrBuilds set count = count + 1 where what = 'finished';
|
|
elsif ((tg_op = 'DELETE' and old.finished = 1) or
|
|
(tg_op = 'UPDATE' and old.finished = 1 and new.finished = 0)) then
|
|
update NrBuilds set count = count - 1 where what = 'finished';
|
|
end if;
|
|
return null;
|
|
end;
|
|
$$ language plpgsql;
|
|
|
|
create trigger NrBuildsFinished after insert or update or delete on Builds
|
|
for each row
|
|
execute procedure modifyNrBuildsFinished();
|
|
|
|
insert into NrBuilds(what, count) select 'finished', count(*) from Builds where finished = 1;
|