Skip to content

Commit 34387bf

Browse files
committed
Refactor: Switch index creation to new template code in middle
1 parent 2d9568a commit 34387bf

File tree

2 files changed

+49
-70
lines changed

2 files changed

+49
-70
lines changed

Diff for: src/middle-pgsql.cpp

+49-65
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,13 @@ std::string build_sql(options_t const &options, std::string const &templ)
113113
return sql_template.render();
114114
}
115115

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-
129116
} // anonymous namespace
130117

131118
middle_pgsql_t::table_desc::table_desc(options_t const &options,
132119
table_sql const &ts)
133120
: m_copy_target(std::make_shared<db_target_descr_t>(
134121
options.middle_dbschema, build_sql(options, ts.name), "id"))
135122
{
136-
m_create_fw_dep_indexes = build_sql(options, ts.create_fw_dep_indexes);
137123
}
138124

139125
std::string middle_pgsql_t::render_template(std::string_view templ) const
@@ -165,23 +151,6 @@ void middle_pgsql_t::table_desc::drop_table(
165151
util::human_readable_duration(timer.stop()));
166152
}
167153

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-
185154
void middle_pgsql_t::table_desc::init_max_id(pg_conn_t const &db_connection)
186155
{
187156
auto const qual_name = qualified_name(schema(), name());
@@ -1169,11 +1138,55 @@ void middle_pgsql_t::stop()
11691138
table.drop_table(m_db_connection);
11701139
}
11711140
} 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+
}));
11771190
}
11781191
}
11791192

@@ -1212,18 +1225,6 @@ table_sql sql_for_ways()
12121225

12131226
sql.name = "{prefix}_ways";
12141227

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-
12271228
return sql;
12281229
}
12291230

@@ -1233,23 +1234,6 @@ table_sql sql_for_relations()
12331234

12341235
sql.name = "{prefix}_rels";
12351236

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-
12531237
return sql;
12541238
}
12551239

Diff for: src/middle-pgsql.hpp

-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class middle_query_pgsql_t : public middle_query_t
8989
struct table_sql
9090
{
9191
std::string name;
92-
std::vector<std::string> create_fw_dep_indexes;
9392
};
9493

9594
struct middle_pgsql_t : public middle_t
@@ -140,9 +139,6 @@ struct middle_pgsql_t : public middle_t
140139
///< Drop table from database using existing database connection.
141140
void drop_table(pg_conn_t const &db_connection) const;
142141

143-
///< Open a new database connection and build index on this table.
144-
void build_index(connection_params_t const &connection_params) const;
145-
146142
void task_set(std::future<std::chrono::microseconds> &&future)
147143
{
148144
m_task_result.set(std::move(future));
@@ -155,7 +151,6 @@ struct middle_pgsql_t : public middle_t
155151
osmid_t max_id() const noexcept { return m_max_id; }
156152

157153
private:
158-
std::vector<std::string> m_create_fw_dep_indexes;
159154
std::shared_ptr<db_target_descr_t> m_copy_target;
160155
task_result_t m_task_result;
161156

0 commit comments

Comments
 (0)