Merge ~cjwatson/launchpad:db-karmacache-id-bigint-1 into launchpad:db-devel

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 1dd50c5d289ee9f64bcaff79e3c96e984003ca12
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:db-karmacache-id-bigint-1
Merge into: launchpad:db-devel
Diff against target: 69 lines (+63/-0)
1 file modified
database/schema/patch-2210-39-0.sql (+63/-0)
Reviewer Review Type Date Requested Status
William Grant db Approve
Review via email: mp+414058@code.launchpad.net

Commit message

64-bit KarmaCache.id: new _id column and sync trigger

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

TIL SQL supports C-style block comments.

review: Approve (db)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/database/schema/patch-2210-39-0.sql b/database/schema/patch-2210-39-0.sql
2new file mode 100644
3index 0000000..d1a49df
4--- /dev/null
5+++ b/database/schema/patch-2210-39-0.sql
6@@ -0,0 +1,63 @@
7+-- Copyright 2022 Canonical Ltd. This software is licensed under the
8+-- GNU Affero General Public License version 3 (see the file LICENSE).
9+
10+SET client_min_messages=ERROR;
11+
12+-- STEP 1, COLD
13+-- Add the new wide column to KarmaCache.
14+ALTER TABLE KarmaCache ADD COLUMN _id bigint;
15+
16+-- KarmaCache needs an INSERT trigger, ensuring that new rows get a
17+-- KarmaCache._id matching KarmaCache.id.
18+CREATE FUNCTION karmacache_sync_id_t() RETURNS trigger LANGUAGE plpgsql AS $$
19+BEGIN
20+ NEW._id := NEW.id;
21+ RETURN NEW;
22+END;
23+$$;
24+
25+CREATE TRIGGER karmacache_sync_id_t
26+ BEFORE INSERT ON KarmaCache
27+ FOR EACH ROW EXECUTE PROCEDURE karmacache_sync_id_t();
28+
29+
30+-- Subsequent statements, to be executed live and in subsequent patches
31+-- after timing and optimization.
32+
33+/*
34+-- STEP 2, HOT
35+-- Backfill KarmaCache._id.
36+UPDATE KarmaCache SET _id=id WHERE _id IS NULL;
37+
38+
39+-- STEP 3, HOT
40+-- To be done CONCURRENTLY, create the UNIQUE index on KarmaCache._id.
41+CREATE UNIQUE INDEX karmacache_id_key ON KarmaCache(_id);
42+
43+
44+-- STEP 4, COLD
45+-- Constraints, swap into place, and the rest.
46+
47+-- Set KarmaCache._id to NOT NULL.
48+ALTER TABLE KarmaCache ALTER COLUMN _id SET NOT NULL;
49+
50+-- We no longer need the trigger.
51+DROP TRIGGER karmacache_sync_id_t ON KarmaCache;
52+DROP FUNCTION karmacache_sync_id_t();
53+
54+-- Fix the SEQUENCE owner, so it doesn't get removed when the old id column
55+-- is dropped.
56+ALTER SEQUENCE karmacache_id_seq OWNED BY KarmaCache._id;
57+
58+-- Swap in the wide column.
59+ALTER TABLE KarmaCache DROP COLUMN id;
60+ALTER TABLE KarmaCache RENAME _id TO id;
61+
62+-- Fix up the primary key.
63+ALTER INDEX karmacache_id_key RENAME TO karmacache_pkey;
64+ALTER TABLE KarmaCache
65+ ALTER COLUMN id SET DEFAULT nextval('karmacache_id_seq'),
66+ ADD CONSTRAINT karmacache_pkey PRIMARY KEY USING INDEX karmacache_pkey;
67+*/
68+
69+INSERT INTO LaunchpadDatabaseRevision VALUES (2210, 39, 0);

Subscribers

People subscribed via source and target branches

to status/vote changes: