1
-- Copyright 2011 Canonical Ltd. This software is licensed under the
2
-- GNU Affero General Public License version 3 (see the file LICENSE).
4
SET client_min_messages=ERROR;
6
CREATE OR REPLACE FUNCTION bugsummary_journal_ins(d bugsummary)
12
INSERT INTO BugSummaryJournal (
13
count, product, productseries, distribution,
14
distroseries, sourcepackagename, viewed_by, tag,
17
d.count, d.product, d.productseries, d.distribution,
18
d.distroseries, d.sourcepackagename, d.viewed_by, d.tag,
19
d.status, d.milestone);
24
COMMENT ON FUNCTION bugsummary_journal_ins(bugsummary) IS
25
'Add an entry into BugSummaryJournal';
28
CREATE OR REPLACE FUNCTION bugsummary_rollup_journal() RETURNS VOID
29
LANGUAGE plpgsql VOLATILE
30
SECURITY DEFINER SET search_path TO public AS
36
-- Lock so we don't content with other invokations of this
37
-- function. We can happily lock the BugSummary table for writes
38
-- as this function is the only thing that updates that table.
39
-- BugSummaryJournal remains unlocked so nothing should be blocked.
40
LOCK TABLE BugSummary IN ROW EXCLUSIVE MODE;
42
SELECT MAX(id) INTO max_id FROM BugSummaryJournal;
57
FROM BugSummaryJournal
60
product, productseries, distribution, distroseries,
61
sourcepackagename, viewed_by, tag, status, milestone
62
HAVING sum(count) <> 0
65
PERFORM bug_summary_dec(d);
66
ELSIF d.count > 0 THEN
67
PERFORM bug_summary_inc(d);
71
DELETE FROM BugSummaryJournal WHERE id <= max_id;
75
CREATE OR REPLACE FUNCTION bug_summary_dec(bugsummary) RETURNS VOID
78
-- We own the row reference, so in the absence of bugs this cannot
79
-- fail - just decrement the row.
80
UPDATE BugSummary SET count = count + $1.count
82
product IS NOT DISTINCT FROM $1.product
83
AND productseries IS NOT DISTINCT FROM $1.productseries
84
AND distribution IS NOT DISTINCT FROM $1.distribution
85
AND distroseries IS NOT DISTINCT FROM $1.distroseries
86
AND sourcepackagename IS NOT DISTINCT FROM $1.sourcepackagename
87
AND viewed_by IS NOT DISTINCT FROM $1.viewed_by
88
AND tag IS NOT DISTINCT FROM $1.tag
89
AND status IS NOT DISTINCT FROM $1.status
90
AND milestone IS NOT DISTINCT FROM $1.milestone;
91
-- gc the row (perhaps should be garbo but easy enough to add here:
92
DELETE FROM bugsummary
95
AND product IS NOT DISTINCT FROM $1.product
96
AND productseries IS NOT DISTINCT FROM $1.productseries
97
AND distribution IS NOT DISTINCT FROM $1.distribution
98
AND distroseries IS NOT DISTINCT FROM $1.distroseries
99
AND sourcepackagename IS NOT DISTINCT FROM $1.sourcepackagename
100
AND viewed_by IS NOT DISTINCT FROM $1.viewed_by
101
AND tag IS NOT DISTINCT FROM $1.tag
102
AND status IS NOT DISTINCT FROM $1.status
103
AND milestone IS NOT DISTINCT FROM $1.milestone;
104
-- If its not found then someone else also dec'd and won concurrently.
107
CREATE OR REPLACE FUNCTION bug_summary_inc(d bugsummary) RETURNS VOID
111
-- Shameless adaption from postgresql manual
113
-- first try to update the row
114
UPDATE BugSummary SET count = count + d.count
116
product IS NOT DISTINCT FROM d.product
117
AND productseries IS NOT DISTINCT FROM d.productseries
118
AND distribution IS NOT DISTINCT FROM d.distribution
119
AND distroseries IS NOT DISTINCT FROM d.distroseries
120
AND sourcepackagename IS NOT DISTINCT FROM d.sourcepackagename
121
AND viewed_by IS NOT DISTINCT FROM d.viewed_by
122
AND tag IS NOT DISTINCT FROM d.tag
123
AND status IS NOT DISTINCT FROM d.status
124
AND milestone IS NOT DISTINCT FROM d.milestone;
128
-- not there, so try to insert the key
129
-- if someone else inserts the same key concurrently,
130
-- we could get a unique-key failure
132
INSERT INTO BugSummary(
133
count, product, productseries, distribution,
134
distroseries, sourcepackagename, viewed_by, tag,
137
d.count, d.product, d.productseries, d.distribution,
138
d.distroseries, d.sourcepackagename, d.viewed_by, d.tag,
139
d.status, d.milestone);
141
EXCEPTION WHEN unique_violation THEN
142
-- do nothing, and loop to try the UPDATE again
148
COMMENT ON FUNCTION bugsummary_rollup_journal() IS
149
'Collate and migrate rows from BugSummaryJournal to BugSummary';
151
CREATE OR REPLACE FUNCTION bug_summary_flush_temp_journal() RETURNS VOID
152
LANGUAGE plpgsql VOLATILE AS
155
d bugsummary%ROWTYPE;
157
-- may get called even though no summaries were made (for simplicity in the
159
PERFORM ensure_bugsummary_temp_journal();
160
FOR d IN SELECT * FROM bugsummary_temp_journal LOOP
161
PERFORM bugsummary_journal_ins(d);
163
TRUNCATE bugsummary_temp_journal;
167
COMMENT ON FUNCTION bug_summary_flush_temp_journal() IS
168
'flush the temporary bugsummary journal into the bugsummaryjournal table';
171
INSERT INTO LaunchpadDatabaseRevision VALUES (2208, 63, 4);