Skip to content

Commit 4504921

Browse files
committed
Remove special case for old PostGIS versions when clustering
Ordering by geometry was done differently before PostGIS 2.4. This commit removes the old code and adds a check that PostGIS 2.5 is available which is the oldest version we are checking in our CI. Note that PostGIS 3 has been available for a long time now (it is in Debian Bullseye ("oldstable", released 2021) and Ubuntu 20.04), so we are way beyond where this should matter in real setups.
1 parent 8d7aa83 commit 4504921

File tree

4 files changed

+14
-48
lines changed

4 files changed

+14
-48
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ with other versions of those libraries (set the `EXTERNAL_*libname*` option to
6767
* [protozero](https://github.com/mapbox/protozero) (>= 1.6.3)
6868

6969
It also requires access to a database server running
70-
[PostgreSQL](https://www.postgresql.org/) 9.6+ and
71-
[PostGIS](https://www.postgis.net/) 2.2+.
70+
[PostgreSQL](https://www.postgresql.org/) (version 9.6+ works, 13+ strongly
71+
recommended) and [PostGIS](https://www.postgis.net/) (version 2.5+).
7272

7373
Make sure you have installed the development packages for the libraries
7474
mentioned in the requirements section and a C++ compiler which supports C++17.

src/flex-table.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -323,34 +323,14 @@ void table_connection_t::stop(pg_conn_t const &db_connection, bool updateable,
323323
flex_table_t::table_type::permanent, table().full_tmp_name()));
324324

325325
std::string const columns = table().build_sql_column_list();
326-
std::string sql = fmt::format("INSERT INTO {} ({}) SELECT {} FROM {}",
327-
table().full_tmp_name(), columns, columns,
328-
table().full_name());
329-
330-
auto const postgis_version = get_postgis_version();
331326

332327
auto const geom_column_name =
333328
"\"" + table().geom_column().name() + "\"";
334329

335-
sql += " ORDER BY ";
336-
if (postgis_version.major == 2 && postgis_version.minor < 4) {
337-
log_debug("Using GeoHash for clustering table '{}'",
338-
table().name());
339-
if (table().geom_column().srid() == 4326) {
340-
sql += fmt::format("ST_GeoHash({},10)", geom_column_name);
341-
} else {
342-
sql += fmt::format(
343-
"ST_GeoHash(ST_Transform(ST_Envelope({}),4326),10)",
344-
geom_column_name);
345-
}
346-
sql += " COLLATE \"C\"";
347-
} else {
348-
log_debug("Using native order for clustering table '{}'",
349-
table().name());
350-
// Since Postgis 2.4 the order function for geometries gives
351-
// useful results.
352-
sql += geom_column_name;
353-
}
330+
std::string const sql =
331+
fmt::format("INSERT INTO {} ({}) SELECT {} FROM {} ORDER BY {}",
332+
table().full_tmp_name(), columns, columns,
333+
table().full_name(), geom_column_name);
354334

355335
db_connection.exec(sql);
356336

src/osm2pgsql.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ static void check_db(options_t const &options)
8181

8282
init_database_capabilities(db_connection);
8383

84+
auto const pv = get_postgis_version();
85+
if (pv.major < 2 || (pv.major == 2 && pv.minor < 5)) {
86+
throw std::runtime_error{"Need at least PostGIS version 2.5"};
87+
}
88+
8489
check_schema(options.dbschema);
8590
check_schema(options.middle_dbschema);
8691
check_schema(options.output_dbschema);

src/table.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,9 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
195195

196196
log_info("Clustering table '{}' by geometry...", m_target->name());
197197

198-
std::string sql = fmt::format("CREATE TABLE {} {} AS SELECT * FROM {}",
199-
qual_tmp_name, m_table_space, qual_name);
200-
201-
auto const postgis_version = get_postgis_version();
202-
203-
sql += " ORDER BY ";
204-
if (postgis_version.major == 2 && postgis_version.minor < 4) {
205-
log_debug("Using GeoHash for clustering table '{}'",
206-
m_target->name());
207-
if (m_srid == "4326") {
208-
sql += "ST_GeoHash(way,10)";
209-
} else {
210-
sql += "ST_GeoHash(ST_Transform(ST_Envelope(way),4326),10)";
211-
}
212-
sql += " COLLATE \"C\"";
213-
} else {
214-
log_debug("Using native order for clustering table '{}'",
215-
m_target->name());
216-
// Since Postgis 2.4 the order function for geometries gives
217-
// useful results.
218-
sql += "way";
219-
}
198+
std::string const sql =
199+
fmt::format("CREATE TABLE {} {} AS SELECT * FROM {} ORDER BY way",
200+
qual_tmp_name, m_table_space, qual_name);
220201

221202
m_db_connection->exec(sql);
222203

0 commit comments

Comments
 (0)