source: other-projects/tipple-android/osmosis/script/pgsnapshot_schema_0.6_upgrade_5-6.sql@ 26899

Last change on this file since 26899 was 26899, checked in by davidb, 11 years ago

Tipple reborn after Chris's Summer of Code 2013

File size: 6.0 KB
Line 
1-- Database script for the simple PostgreSQL schema. This script moves all tags into hstore columns.
2
3-- Create functions for building hstore data.
4CREATE OR REPLACE FUNCTION build_node_tags() RETURNS void AS $$
5DECLARE
6 previousId nodes.id%TYPE;
7 currentId nodes.id%TYPE;
8 result hstore;
9 tagRow node_tags%ROWTYPE;
10BEGIN
11 SET enable_seqscan = false;
12 SET enable_mergejoin = false;
13 SET enable_hashjoin = false;
14
15 FOR tagRow IN SELECT * FROM node_tags ORDER BY node_id LOOP
16 currentId := tagRow.node_id;
17
18 IF currentId <> previousId THEN
19 IF previousId IS NOT NULL THEN
20 IF result IS NOT NULL THEN
21 UPDATE nodes SET tags = result WHERE id = previousId;
22 IF ((currentId / 100000) <> (previousId / 100000)) THEN
23 RAISE INFO 'node id: %', previousId;
24 END IF;
25 result := NULL;
26 END IF;
27 END IF;
28 END IF;
29
30 IF result IS NULL THEN
31 result := tagRow.k => tagRow.v;
32 ELSE
33 result := result || (tagRow.k => tagRow.v);
34 END IF;
35
36 previousId := currentId;
37 END LOOP;
38
39 IF previousId IS NOT NULL THEN
40 IF result IS NOT NULL THEN
41 UPDATE nodes SET tags = result WHERE id = previousId;
42 result := NULL;
43 END IF;
44 END IF;
45END;
46$$ LANGUAGE plpgsql;
47
48CREATE OR REPLACE FUNCTION build_way_tags() RETURNS void AS $$
49DECLARE
50 previousId ways.id%TYPE;
51 currentId ways.id%TYPE;
52 result hstore;
53 tagRow way_tags%ROWTYPE;
54BEGIN
55 SET enable_seqscan = false;
56 SET enable_mergejoin = false;
57 SET enable_hashjoin = false;
58
59 FOR tagRow IN SELECT * FROM way_tags ORDER BY way_id LOOP
60 currentId := tagRow.way_id;
61
62 IF currentId <> previousId THEN
63 IF previousId IS NOT NULL THEN
64 IF result IS NOT NULL THEN
65 UPDATE ways SET tags = result WHERE id = previousId;
66 IF ((currentId / 100000) <> (previousId / 100000)) THEN
67 RAISE INFO 'way id: %', previousId;
68 END IF;
69 result := NULL;
70 END IF;
71 END IF;
72 END IF;
73
74 IF result IS NULL THEN
75 result := tagRow.k => tagRow.v;
76 ELSE
77 result := result || (tagRow.k => tagRow.v);
78 END IF;
79
80 previousId := currentId;
81 END LOOP;
82
83 IF previousId IS NOT NULL THEN
84 IF result IS NOT NULL THEN
85 UPDATE ways SET tags = result WHERE id = previousId;
86 result := NULL;
87 END IF;
88 END IF;
89END;
90$$ LANGUAGE plpgsql;
91
92CREATE OR REPLACE FUNCTION build_relation_tags() RETURNS void AS $$
93DECLARE
94 previousId relations.id%TYPE;
95 currentId relations.id%TYPE;
96 result hstore;
97 tagRow relation_tags%ROWTYPE;
98BEGIN
99 SET enable_seqscan = false;
100 SET enable_mergejoin = false;
101 SET enable_hashjoin = false;
102
103 FOR tagRow IN SELECT * FROM relation_tags ORDER BY relation_id LOOP
104 currentId := tagRow.relation_id;
105
106 IF currentId <> previousId THEN
107 IF previousId IS NOT NULL THEN
108 IF result IS NOT NULL THEN
109 UPDATE relations SET tags = result WHERE id = previousId;
110 IF ((currentId / 100000) <> (previousId / 100000)) THEN
111 RAISE INFO 'relation id: %', previousId;
112 END IF;
113 result := NULL;
114 END IF;
115 END IF;
116 END IF;
117
118 IF result IS NULL THEN
119 result := tagRow.k => tagRow.v;
120 ELSE
121 result := result || (tagRow.k => tagRow.v);
122 END IF;
123
124 previousId := currentId;
125 END LOOP;
126
127 IF previousId IS NOT NULL THEN
128 IF result IS NOT NULL THEN
129 UPDATE relations SET tags = result WHERE id = previousId;
130 result := NULL;
131 END IF;
132 END IF;
133END;
134$$ LANGUAGE plpgsql;
135
136CREATE OR REPLACE FUNCTION build_way_nodes() RETURNS void AS $$
137DECLARE
138 previousId ways.id%TYPE;
139 currentId ways.id%TYPE;
140 result bigint[];
141 wayNodeRow way_nodes%ROWTYPE;
142BEGIN
143 SET enable_seqscan = false;
144 SET enable_mergejoin = false;
145 SET enable_hashjoin = false;
146
147 FOR wayNodeRow IN SELECT * FROM way_nodes ORDER BY way_id, sequence_id LOOP
148 currentId := wayNodeRow.way_id;
149
150 IF currentId <> previousId THEN
151 IF previousId IS NOT NULL THEN
152 IF result IS NOT NULL THEN
153 UPDATE ways SET nodes = result WHERE id = previousId;
154 IF ((currentId / 100000) <> (previousId / 100000)) THEN
155 RAISE INFO 'way id: %', previousId;
156 END IF;
157 result := NULL;
158 END IF;
159 END IF;
160 END IF;
161
162 IF result IS NULL THEN
163 result = ARRAY[wayNodeRow.node_id];
164 ELSE
165 result = array_append(result, wayNodeRow.node_id);
166 END IF;
167
168 previousId := currentId;
169 END LOOP;
170
171 IF previousId IS NOT NULL THEN
172 IF result IS NOT NULL THEN
173 UPDATE ways SET nodes = result WHERE id = previousId;
174 result := NULL;
175 END IF;
176 END IF;
177END;
178$$ LANGUAGE plpgsql;
179
180-- Add hstore columns to entity tables.
181ALTER TABLE nodes ADD COLUMN tags hstore;
182ALTER TABLE ways ADD COLUMN tags hstore;
183ALTER TABLE relations ADD COLUMN tags hstore;
184
185-- Populate the hstore columns.
186SELECT build_node_tags();
187SELECT build_way_tags();
188SELECT build_relation_tags();
189
190-- Remove the hstore functions.
191DROP FUNCTION build_node_tags();
192DROP FUNCTION build_way_tags();
193DROP FUNCTION build_relation_tags();
194
195-- Drop the now redundant tag tables.
196DROP TABLE node_tags;
197DROP TABLE way_tags;
198DROP TABLE relation_tags;
199
200-- Add an index allowing relation_members to be queried by member id and type.
201CREATE INDEX idx_relation_members_member_id_and_type ON relation_members USING btree (member_id, member_type);
202
203-- Add the nodes column to the ways table.
204ALTER TABLE ways ADD COLUMN nodes bigint[];
205
206-- Populate the new nodes column on the ways table.
207SELECT build_way_nodes();
208--UPDATE ways w SET nodes = ARRAY(SELECT wn.node_id FROM way_nodes wn WHERE w.id = wn.way_id ORDER BY sequence_id);
209
210-- Remove the way nodes function.
211DROP FUNCTION build_way_nodes();
212
213-- Organise data according to geographical location.
214CLUSTER nodes USING idx_nodes_geom;
215CLUSTER ways USING idx_ways_linestring;
216
217-- Create the function that provides "unnest" functionality while remaining compatible with 8.3.
218CREATE OR REPLACE FUNCTION unnest_bbox_way_nodes() RETURNS void AS $$
219DECLARE
220 previousId ways.id%TYPE;
221 currentId ways.id%TYPE;
222 result bigint[];
223 wayNodeRow way_nodes%ROWTYPE;
224 wayNodes ways.nodes%TYPE;
225BEGIN
226 FOR wayNodes IN SELECT bw.nodes FROM bbox_ways bw LOOP
227 FOR i IN 1 .. array_upper(wayNodes, 1) LOOP
228 INSERT INTO bbox_way_nodes (id) VALUES (wayNodes[i]);
229 END LOOP;
230 END LOOP;
231END;
232$$ LANGUAGE plpgsql;
233
234
235-- Update the schema version.
236UPDATE schema_info SET version = 6;
237
238VACUUM ANALYZE;
Note: See TracBrowser for help on using the repository browser.