-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathflex-table.cpp
More file actions
422 lines (347 loc) · 12.3 KB
/
flex-table.cpp
File metadata and controls
422 lines (347 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-table.hpp"
#include "format.hpp"
#include "logging.hpp"
#include "pgsql-capabilities.hpp"
#include "pgsql-helper.hpp"
#include "util.hpp"
#include <algorithm>
#include <cassert>
#include <string>
char const *type_to_char(osmium::item_type type) noexcept
{
switch (type) {
case osmium::item_type::node:
return "N";
case osmium::item_type::way:
return "W";
case osmium::item_type::relation:
return "R";
default:
break;
}
return "X";
}
bool flex_table_t::has_multicolumn_id_index() const noexcept
{
return m_columns[0].type() == table_column_type::id_type;
}
std::string flex_table_t::id_column_names() const
{
std::string name;
if (!has_id_column()) {
return name;
}
name = m_columns[0].name();
if (has_multicolumn_id_index()) {
name += ',';
name += m_columns[1].name();
}
return name;
}
std::string flex_table_t::full_name() const
{
return qualified_name(schema(), name());
}
std::string flex_table_t::full_tmp_name() const
{
return qualified_name(schema(), name() + "_tmp");
}
bool flex_table_t::has_id_column() const noexcept
{
if (m_columns.empty()) {
return false;
}
return (m_columns[0].type() == table_column_type::id_type) ||
(m_columns[0].type() == table_column_type::id_num);
}
bool flex_table_t::matches_type(osmium::item_type type) const noexcept
{
// This table takes any type or has no ids -> okay
if (m_id_type == flex_table_index_type::any_object ||
m_id_type == flex_table_index_type::no_index) {
return true;
}
// Type and table type match -> okay
if ((type == osmium::item_type::node &&
m_id_type == flex_table_index_type::node) ||
(type == osmium::item_type::way &&
m_id_type == flex_table_index_type::way) ||
(type == osmium::item_type::relation &&
m_id_type == flex_table_index_type::relation)) {
return true;
}
// Relations can be written as linestrings into way tables -> okay
if (type == osmium::item_type::relation &&
m_id_type == flex_table_index_type::way) {
return true;
}
// Area tables can take ways or relations, but not nodes
return m_id_type == flex_table_index_type::area &&
type != osmium::item_type::node;
}
/// Map way/node/relation ID to id value used in database table column
osmid_t flex_table_t::map_id(osmium::item_type type, osmid_t id) const noexcept
{
if (m_id_type == flex_table_index_type::any_object) {
if (has_multicolumn_id_index()) {
return id;
}
switch (type) {
case osmium::item_type::node:
return id;
case osmium::item_type::way:
return -id;
case osmium::item_type::relation:
return -id - 100000000000000000LL;
default:
assert(false);
}
}
if (m_id_type != flex_table_index_type::relation &&
type == osmium::item_type::relation) {
return -id;
}
return id;
}
flex_table_column_t &flex_table_t::add_column(std::string const &name,
std::string const &type,
std::string const &sql_type)
{
// id_type (optional) and id_num must always be the first columns
assert(type != "id_type" || m_columns.empty());
assert(type != "id_num" || m_columns.empty() ||
(m_columns.size() == 1 &&
m_columns[0].type() == table_column_type::id_type));
auto &column = m_columns.emplace_back(name, type, sql_type);
if (column.is_geometry_column()) {
if (m_geom_column == std::numeric_limits<std::size_t>::max()) {
m_geom_column = m_columns.size() - 1;
} else {
m_has_multiple_geom_columns = true;
}
}
return column;
}
std::string flex_table_t::build_sql_prepare_get_wkb() const
{
util::string_joiner_t joiner{',', '"'};
for (auto const &column : m_columns) {
if (!column.expire_configs().empty()) {
joiner.add(column.name());
}
}
assert(!joiner.empty());
std::string const columns = joiner();
if (has_multicolumn_id_index()) {
return fmt::format(
R"(SELECT {} FROM {} WHERE "{}" = $1::char(1) AND "{}" = $2::bigint)",
columns, full_name(), m_columns[0].name(), m_columns[1].name());
}
return fmt::format(R"(SELECT {} FROM {} WHERE "{}" = $1::bigint)", columns,
full_name(), id_column_names());
}
std::string
flex_table_t::build_sql_create_table(table_type ttype,
std::string const &table_name) const
{
assert(!m_columns.empty());
std::string sql =
fmt::format("CREATE {} TABLE IF NOT EXISTS {} (",
ttype == table_type::interim ? "UNLOGGED" : "", table_name);
util::string_joiner_t joiner{','};
for (auto const &column : m_columns) {
// create_only columns are only created in permanent, not in the
// interim tables
if (ttype == table_type::permanent || !column.create_only()) {
joiner.add(column.sql_create());
}
}
sql += joiner();
sql += ')';
if (ttype == table_type::interim) {
sql += " WITH (autovacuum_enabled = off)";
}
sql += tablespace_clause(m_data_tablespace);
return sql;
}
std::string flex_table_t::build_sql_column_list() const
{
assert(!m_columns.empty());
util::string_joiner_t joiner{',', '"'};
for (auto const &column : m_columns) {
if (!column.create_only()) {
joiner.add(column.name());
}
}
return joiner();
}
std::string flex_table_t::build_sql_create_id_index() const
{
if (m_primary_key_index) {
auto ts = tablespace_clause(index_tablespace());
if (!ts.empty()) {
ts = " USING INDEX" + ts;
}
return fmt::format("ALTER TABLE {} ADD PRIMARY KEY ({}){}", full_name(),
id_column_names(), ts);
}
return fmt::format("CREATE {}INDEX ON {} USING BTREE ({}) {}",
m_build_unique_id_index ? "UNIQUE " : "", full_name(),
id_column_names(),
tablespace_clause(index_tablespace()));
}
flex_index_t &flex_table_t::add_index(std::string method)
{
return m_indexes.emplace_back(std::move(method));
}
bool flex_table_t::has_columns_with_expire() const noexcept
{
return std::any_of(m_columns.cbegin(), m_columns.cend(),
[](auto const &column) { return column.has_expire(); });
}
void flex_table_t::prepare(pg_conn_t const &db_connection) const
{
if (has_id_column() && has_columns_with_expire()) {
auto const stmt = fmt::format("get_wkb_{}", m_table_num);
db_connection.prepare(stmt, fmt::runtime(build_sql_prepare_get_wkb()));
}
}
void flex_table_t::analyze(pg_conn_t const &db_connection) const
{
analyze_table(db_connection, schema(), name());
}
namespace {
void enable_check_trigger(pg_conn_t const &db_connection,
flex_table_t const &table)
{
std::string checks;
for (auto const &column : table.columns()) {
if (column.is_geometry_column() && column.needs_isvalid()) {
checks.append(fmt::format(
R"((NEW."{0}" IS NULL OR ST_IsValid(NEW."{0}")) AND )",
column.name()));
}
}
if (checks.empty()) {
return;
}
// remove last " AND "
checks.resize(checks.size() - 5);
create_geom_check_trigger(db_connection, table.schema(), table.name(),
checks);
}
} // anonymous namespace
void table_connection_t::start(pg_conn_t const &db_connection,
bool append) const
{
if (!append) {
drop_table_if_exists(db_connection, table().schema(), table().name());
}
// These _tmp tables can be left behind if we run out of disk space.
drop_table_if_exists(db_connection, table().schema(),
table().name() + "_tmp");
if (!append) {
db_connection.exec(table().build_sql_create_table(
table().cluster_by_geom() ? flex_table_t::table_type::interim
: flex_table_t::table_type::permanent,
table().full_name()));
enable_check_trigger(db_connection, table());
}
table().prepare(db_connection);
}
void table_connection_t::stop(pg_conn_t const &db_connection, bool updateable,
bool append)
{
m_copy_mgr.sync();
if (append) {
return;
}
if (table().cluster_by_geom()) {
if (table().geom_column().needs_isvalid()) {
drop_geom_check_trigger(db_connection, table().schema(),
table().name());
}
log_info("Clustering table '{}' by geometry...", table().name());
db_connection.exec(table().build_sql_create_table(
flex_table_t::table_type::permanent, table().full_tmp_name()));
std::string const columns = table().build_sql_column_list();
auto const geom_column_name =
"\"" + table().geom_column().name() + "\"";
std::string const sql =
fmt::format("INSERT INTO {} ({}) SELECT {} FROM {} ORDER BY {}",
table().full_tmp_name(), columns, columns,
table().full_name(), geom_column_name);
db_connection.exec(sql);
db_connection.exec("DROP TABLE {}", table().full_name());
db_connection.exec(R"(ALTER TABLE {} RENAME TO "{}")",
table().full_tmp_name(), table().name());
m_id_index_created = false;
if (updateable) {
enable_check_trigger(db_connection, table());
}
}
if (table().indexes().empty()) {
log_info("No indexes to create on table '{}'.", table().name());
} else {
for (auto const &index : table().indexes()) {
log_info("Creating index on table '{}' {}...", table().name(),
index.columns());
auto const sql = index.create_index(
qualified_name(table().schema(), table().name()));
db_connection.exec(sql);
}
}
if ((table().always_build_id_index() || updateable) &&
table().has_id_column()) {
create_id_index(db_connection);
}
log_info("Analyzing table '{}'...", table().name());
table().analyze(db_connection);
}
void table_connection_t::create_id_index(pg_conn_t const &db_connection)
{
if (m_id_index_created) {
log_debug("Id index on table '{}' already created.", table().name());
} else {
log_info("Creating id index on table '{}'...", table().name());
db_connection.exec(table().build_sql_create_id_index());
m_id_index_created = true;
}
}
pg_result_t table_connection_t::get_geoms_by_id(pg_conn_t const &db_connection,
osmium::item_type type,
osmid_t id) const
{
assert(table().has_geom_column());
std::string const stmt = fmt::format("get_wkb_{}", table().num());
if (table().has_multicolumn_id_index()) {
return db_connection.exec_prepared_as_binary(stmt.c_str(),
type_to_char(type), id);
}
return db_connection.exec_prepared_as_binary(stmt.c_str(), id);
}
void table_connection_t::delete_rows_with(osmium::item_type type, osmid_t id)
{
m_copy_mgr.new_line(m_target);
if (!table().has_multicolumn_id_index()) {
type = osmium::item_type::undefined;
}
m_copy_mgr.delete_object(type_to_char(type)[0], id);
}
void table_connection_t::task_wait()
{
auto const run_time = m_task_result.wait();
log_info("All postprocessing on table '{}' done in {}.", table().name(),
util::human_readable_duration(run_time));
log_debug("Inserted {} rows into table '{}' ({} not inserted due to"
" NOT NULL constraints).",
m_count_insert, table().name(), m_count_not_null_error);
}