· 8 years ago · Sep 01, 2018, 01:54 AM
1Pager usage is off.
2Timing is on.
3SET
4Time: 0.633 ms
5SET
6Time: 0.261 ms
7DO $$
8DECLARE
9 tbl text := quote_ident('google_cloud_message_registration_records');
10 bigint_col text := quote_ident('user_id');
11 int_col text := quote_ident('old_user_id');
12 tmp_col text := quote_ident('tmp_user_id');
13
14BEGIN
15 -- If the target column does not exist, fail.
16
17 IF NOT EXISTS (
18 SELECT 1 FROM information_schema.columns
19 WHERE table_name = tbl
20 AND column_name = bigint_col
21 ) THEN
22 RAISE EXCEPTION 'bigint column % does not exist', bigint_col;
23 END IF;
24
25 -- If the int target column exists, but is not of type integer, fail.
26
27 IF NOT EXISTS (
28 SELECT 1 FROM information_schema.columns
29 WHERE table_name = tbl
30 AND column_name = int_col
31 AND data_type = 'integer'
32 ) THEN
33 RAISE EXCEPTION 'int column % does not have type integer', int_col;
34 END IF;
35
36 -- If the int target column exists, but is not of type integer, fail.
37
38 IF NOT EXISTS (
39 SELECT 1 FROM information_schema.columns
40 WHERE table_name = tbl
41 AND column_name = bigint_col
42 AND data_type = 'bigint'
43 ) THEN
44 RAISE EXCEPTION 'bigint column % does not have type bigint', bigint_col;
45 END IF;
46
47 -- If the tmp column already exists, fail.
48
49 IF EXISTS (
50 SELECT 1 FROM information_schema.columns
51 WHERE table_name = tbl
52 AND column_name = tmp_col
53 ) THEN
54 RAISE EXCEPTION
55 'Temp column % already exists! To continue, confirm that this is in fact a temp column, and remove it.',
56 new_col;
57 END IF;
58END
59$$;
60DO
61Time: 20.594 ms
62BEGIN;
63BEGIN
64Time: 0.190 ms
65SET LOCAL lock_timeout = '10s';
66SET
67Time: 0.204 ms
68DROP TRIGGER IF EXISTS fill_user_id_to_old_user_id_on_google_cloud_message_registratio ON google_cloud_message_registration_records;
69DROP TRIGGER
70Time: 1.863 ms
71DROP FUNCTION IF EXISTS copy_user_id_to_old_user_id_on_google_cloud_message_registratio CASCADE;
72DROP FUNCTION
73Time: 0.472 ms
74ALTER TABLE google_cloud_message_registration_records RENAME COLUMN user_id to bigint_user_id;
75ALTER TABLE
76Time: 0.319 ms
77ALTER TABLE google_cloud_message_registration_records RENAME COLUMN old_user_id to user_id;
78ALTER TABLE
79Time: 0.281 ms
80CREATE OR REPLACE FUNCTION copy_user_id_to_bigint_user_id_on_google_cloud_message_reg()
81 RETURNS TRIGGER AS
82 $copy_user_id_to_bigint_user_id_on_google_cloud_message_reg$
83 BEGIN
84 NEW.bigint_user_id := NEW.user_id;
85 RETURN NEW;
86 END;
87 $copy_user_id_to_bigint_user_id_on_google_cloud_message_reg$ LANGUAGE plpgsql;
88CREATE FUNCTION
89Time: 2.174 ms
90DROP TRIGGER IF EXISTS backfill_user_id_to_bigint_user_id_on_google_cloud_message ON google_cloud_message_registration_records;
91psql:things_go_wrong.psql:103: NOTICE: trigger "backfill_user_id_to_bigint_user_id_on_google_cloud_message" for relation "google_cloud_message_registration_records" does not exist, skipping
92DROP TRIGGER
93Time: 0.361 ms
94CREATE TRIGGER backfill_user_id_to_bigint_user_id_on_google_cloud_message BEFORE INSERT OR UPDATE ON google_cloud_message_registration_records FOR EACH ROW EXECUTE PROCEDURE copy_user_id_to_bigint_user_id_on_google_cloud_message_reg (user_id, bigint_user_id);
95CREATE TRIGGER
96Time: 1.205 ms
97END;
98COMMIT
99Time: 0.616 ms
100Done!