@@ -113,27 +113,13 @@ std::string build_sql(options_t const &options, std::string const &templ)
113
113
return sql_template.render ();
114
114
}
115
115
116
- std::vector<std::string> build_sql (options_t const &options,
117
- std::vector<std::string> const &templs)
118
- {
119
- std::vector<std::string> out;
120
- out.reserve (templs.size ());
121
-
122
- for (auto const &templ : templs) {
123
- out.push_back (build_sql (options, templ));
124
- }
125
-
126
- return out;
127
- }
128
-
129
116
} // anonymous namespace
130
117
131
118
middle_pgsql_t ::table_desc::table_desc(options_t const &options,
132
119
table_sql const &ts)
133
120
: m_copy_target(std::make_shared<db_target_descr_t >(
134
121
options.middle_dbschema, build_sql(options, ts.name), " id" ))
135
122
{
136
- m_create_fw_dep_indexes = build_sql (options, ts.create_fw_dep_indexes );
137
123
}
138
124
139
125
std::string middle_pgsql_t::render_template (std::string_view templ) const
@@ -165,23 +151,6 @@ void middle_pgsql_t::table_desc::drop_table(
165
151
util::human_readable_duration (timer.stop ()));
166
152
}
167
153
168
- void middle_pgsql_t::table_desc::build_index (
169
- connection_params_t const &connection_params) const
170
- {
171
- if (m_create_fw_dep_indexes.empty ()) {
172
- return ;
173
- }
174
-
175
- // Use a temporary connection here because we might run in a separate
176
- // thread context.
177
- pg_conn_t const db_connection{connection_params, " middle.index" };
178
-
179
- log_info (" Building index on table '{}'" , name ());
180
- for (auto const &query : m_create_fw_dep_indexes) {
181
- db_connection.exec (query);
182
- }
183
- }
184
-
185
154
void middle_pgsql_t::table_desc::init_max_id (pg_conn_t const &db_connection)
186
155
{
187
156
auto const qual_name = qualified_name (schema (), name ());
@@ -1169,11 +1138,55 @@ void middle_pgsql_t::stop()
1169
1138
table.drop_table (m_db_connection);
1170
1139
}
1171
1140
} else if (!m_options->append ) {
1172
- // Building the indexes takes time, so do it asynchronously.
1173
- for (auto &table : m_tables) {
1174
- table.task_set (thread_pool ().submit (
1175
- [&]() { table.build_index (m_options->connection_params ); }));
1176
- }
1141
+ dbexec (" CREATE OR REPLACE FUNCTION"
1142
+ " {schema}\" {prefix}_index_bucket\" (int8[])"
1143
+ " RETURNS int8[] AS $$"
1144
+ " SELECT ARRAY(SELECT DISTINCT"
1145
+ " unnest($1) >> {way_node_index_id_shift})"
1146
+ " $$ LANGUAGE SQL IMMUTABLE" );
1147
+
1148
+ auto const create_ways_index = render_template (
1149
+ " CREATE INDEX \" {prefix}_ways_nodes_bucket_idx\" "
1150
+ " ON {schema}\" {prefix}_ways\" "
1151
+ " USING GIN ({schema}\" {prefix}_index_bucket\" (nodes))"
1152
+ " WITH (fastupdate = off) {index_tablespace}" );
1153
+
1154
+ log_info (" Building index on middle ways table" );
1155
+ m_tables.ways ().task_set (thread_pool ().submit ([&, create_ways_index]() {
1156
+ pg_conn_t const db_connection{m_options->connection_params ,
1157
+ " middle.index.ways" };
1158
+ db_connection.exec (create_ways_index);
1159
+ }));
1160
+
1161
+ dbexec (" CREATE OR REPLACE FUNCTION"
1162
+ " {schema}\" {prefix}_member_ids\" (jsonb, char)"
1163
+ " RETURNS int8[] AS $$"
1164
+ " SELECT array_agg((el->>'ref')::int8)"
1165
+ " FROM jsonb_array_elements($1) AS el"
1166
+ " WHERE el->>'type' = $2"
1167
+ " $$ LANGUAGE SQL IMMUTABLE" );
1168
+
1169
+ auto const create_rels_index_node_members = render_template (
1170
+ " CREATE INDEX \" {prefix}_rels_node_members_idx\" "
1171
+ " ON {schema}\" {prefix}_rels\" USING GIN"
1172
+ " (({schema}\" {prefix}_member_ids\" (members, 'N'::char)))"
1173
+ " WITH (fastupdate = off) {index_tablespace}" );
1174
+
1175
+ auto const create_rels_index_way_members = render_template (
1176
+ " CREATE INDEX \" {prefix}_rels_way_members_idx\" "
1177
+ " ON {schema}\" {prefix}_rels\" USING GIN"
1178
+ " (({schema}\" {prefix}_member_ids\" (members, 'W'::char)))"
1179
+ " WITH (fastupdate = off) {index_tablespace}" );
1180
+
1181
+ log_info (" Building indexes on middle rels table" );
1182
+ m_tables.relations ().task_set (
1183
+ thread_pool ().submit ([&, create_rels_index_node_members,
1184
+ create_rels_index_way_members]() {
1185
+ pg_conn_t const db_connection{m_options->connection_params ,
1186
+ " middle.index.rels" };
1187
+ db_connection.exec (create_rels_index_node_members);
1188
+ db_connection.exec (create_rels_index_way_members);
1189
+ }));
1177
1190
}
1178
1191
}
1179
1192
@@ -1212,18 +1225,6 @@ table_sql sql_for_ways()
1212
1225
1213
1226
sql.name = " {prefix}_ways" ;
1214
1227
1215
- sql.create_fw_dep_indexes = {
1216
- " CREATE OR REPLACE FUNCTION"
1217
- " {schema}\" {prefix}_index_bucket\" (int8[])"
1218
- " RETURNS int8[] AS $$"
1219
- " SELECT ARRAY(SELECT DISTINCT"
1220
- " unnest($1) >> {way_node_index_id_shift})"
1221
- " $$ LANGUAGE SQL IMMUTABLE" ,
1222
- " CREATE INDEX \" {prefix}_ways_nodes_bucket_idx\" "
1223
- " ON {schema}\" {prefix}_ways\" "
1224
- " USING GIN ({schema}\" {prefix}_index_bucket\" (nodes))"
1225
- " WITH (fastupdate = off) {index_tablespace}" };
1226
-
1227
1228
return sql;
1228
1229
}
1229
1230
@@ -1233,23 +1234,6 @@ table_sql sql_for_relations()
1233
1234
1234
1235
sql.name = " {prefix}_rels" ;
1235
1236
1236
- sql.create_fw_dep_indexes = {
1237
- " CREATE OR REPLACE FUNCTION"
1238
- " {schema}\" {prefix}_member_ids\" (jsonb, char)"
1239
- " RETURNS int8[] AS $$"
1240
- " SELECT array_agg((el->>'ref')::int8)"
1241
- " FROM jsonb_array_elements($1) AS el"
1242
- " WHERE el->>'type' = $2"
1243
- " $$ LANGUAGE SQL IMMUTABLE" ,
1244
- " CREATE INDEX \" {prefix}_rels_node_members_idx\" "
1245
- " ON {schema}\" {prefix}_rels\" USING GIN"
1246
- " (({schema}\" {prefix}_member_ids\" (members, 'N'::char)))"
1247
- " WITH (fastupdate = off) {index_tablespace}" ,
1248
- " CREATE INDEX \" {prefix}_rels_way_members_idx\" "
1249
- " ON {schema}\" {prefix}_rels\" USING GIN"
1250
- " (({schema}\" {prefix}_member_ids\" (members, 'W'::char)))"
1251
- " WITH (fastupdate = off) {index_tablespace}" };
1252
-
1253
1237
return sql;
1254
1238
}
1255
1239
0 commit comments