+def _get_middle_db_format(conn: Connection, tables: Set[str]) -> str:
+ """ Returns the version of the slim middle tables.
+ """
+ if 'osm2pgsql_properties' not in tables:
+ return '1'
+
+ with conn.cursor() as cur:
+ cur.execute("SELECT value FROM osm2pgsql_properties WHERE property = 'db_format'")
+ row = cur.fetchone()
+
+ return cast(str, row[0]) if row is not None else '1'
+
+
+def _setup_tablespace_sql(config: Configuration) -> Dict[str, str]:
+ """ Returns a dict with tablespace expressions for the different tablespace
+ kinds depending on whether a tablespace is configured or not.
+ """
+ out = {}
+ for subset in ('ADDRESS', 'SEARCH', 'AUX'):
+ for kind in ('DATA', 'INDEX'):
+ tspace = getattr(config, f'TABLESPACE_{subset}_{kind}')
+ if tspace:
+ tspace = f'TABLESPACE "{tspace}"'
+ out[f'{subset.lower()}_{kind.lower()}'] = tspace
+
+ return out
+
+
+def _setup_postgresql_features(conn: Connection) -> Dict[str, Any]:
+ """ Set up a dictionary with various optional Postgresql/Postgis features that
+ depend on the database version.
+ """
+ pg_version = conn.server_version_tuple()
+ postgis_version = conn.postgis_version_tuple()
+ pg11plus = pg_version >= (11, 0, 0)
+ ps3 = postgis_version >= (3, 0)
+ return {
+ 'has_index_non_key_column': pg11plus,
+ 'spgist_geom' : 'SPGIST' if pg11plus and ps3 else 'GIST'
+ }
+
+class SQLPreprocessor: