@@ -64,23 +64,18 @@ static std::string build_sql(options_t const &options, char const *templ)
6464
6565middle_pgsql_t ::table_desc::table_desc(options_t const &options,
6666 table_sql const &ts)
67- : m_create (build_sql(options, ts.create_table)),
67+ : m_create_table (build_sql(options, ts.create_table)),
6868 m_prepare_query (build_sql(options, ts.prepare_query)),
6969 m_copy_target(std::make_shared<db_target_descr_t >())
7070{
7171 m_copy_target->name = build_sql (options, ts.name );
7272 m_copy_target->schema = options.middle_dbschema ;
7373 m_copy_target->id = " id" ; // XXX hardcoded column name
7474
75- // Gazetteer doesn't use mark-pending processing and consequently
76- // needs no way-node index.
77- // TODO Currently, set here to keep the impact on the code small.
78- // We actually should have the output plugins report their needs
79- // and pass that via the constructor to middle_t, so that middle_t
80- // itself doesn't need to know about details of the output.
81- if (options.output_backend != " gazetteer" ) {
82- m_prepare_intarray = build_sql (options, ts.prepare_mark );
83- m_array_indexes = build_sql (options, ts.create_index );
75+ if (options.with_forward_dependencies ) {
76+ m_prepare_fw_dep_lookups =
77+ build_sql (options, ts.prepare_fw_dep_lookups );
78+ m_create_fw_dep_indexes = build_sql (options, ts.create_fw_dep_indexes );
8479 }
8580}
8681
@@ -103,9 +98,9 @@ void middle_pgsql_t::table_desc::stop(std::string const &conninfo,
10398 auto const qual_name = qualified_name (
10499 m_copy_target->schema , m_copy_target->name );
105100 sql_conn.exec (" DROP TABLE IF EXISTS {}" _format (qual_name));
106- } else if (build_indexes && !m_array_indexes .empty ()) {
101+ } else if (build_indexes && !m_create_fw_dep_indexes .empty ()) {
107102 fmt::print (stderr, " Building index on table: {}\n " , name ());
108- sql_conn.exec (m_array_indexes );
103+ sql_conn.exec (m_create_fw_dep_indexes );
109104 }
110105
111106 fmt::print (stderr, " Stopped table: {} in {}s\n " , name (), timer.stop ());
@@ -570,8 +565,8 @@ void middle_pgsql_t::start()
570565
571566 // Prepare queries for updating dependent objects
572567 for (auto &table : m_tables) {
573- if (!table.m_prepare_intarray .empty ()) {
574- m_db_connection.exec (table.m_prepare_intarray );
568+ if (!table.m_prepare_fw_dep_lookups .empty ()) {
569+ m_db_connection.exec (table.m_prepare_fw_dep_lookups );
575570 }
576571 }
577572 } else {
@@ -583,7 +578,7 @@ void middle_pgsql_t::start()
583578 table.m_copy_target ->schema , table.m_copy_target ->name );
584579 m_db_connection.exec (
585580 " DROP TABLE IF EXISTS {} CASCADE" _format (qual_name));
586- m_db_connection.exec (table.m_create );
581+ m_db_connection.exec (table.m_create_table );
587582 }
588583
589584 // The extra query connection is only needed in append mode, so close.
@@ -666,33 +661,34 @@ static table_sql sql_for_ways(bool has_bucket_index,
666661 " WHERE id = ANY($1::int8[]);\n " ;
667662
668663 if (has_bucket_index) {
669- sql.prepare_mark =
664+ sql.prepare_fw_dep_lookups =
670665 " PREPARE mark_ways_by_node(int8) AS"
671666 " SELECT id FROM {schema}{prefix}_ways w"
672667 " WHERE $1 = ANY(nodes)"
673668 " AND {schema}{prefix}_index_bucket(w.nodes)"
674669 " && {schema}{prefix}_index_bucket(ARRAY[$1]);\n " ;
675670 } else {
676- sql.prepare_mark = " PREPARE mark_ways_by_node(int8) AS"
677- " SELECT id FROM {schema}{prefix}_ways"
678- " WHERE nodes && ARRAY[$1];\n " ;
671+ sql.prepare_fw_dep_lookups = " PREPARE mark_ways_by_node(int8) AS"
672+ " SELECT id FROM {schema}{prefix}_ways"
673+ " WHERE nodes && ARRAY[$1];\n " ;
679674 }
680675
681676 if (way_node_index_id_shift == 0 ) {
682- sql.create_index =
677+ sql.create_fw_dep_indexes =
683678 " CREATE INDEX ON {schema}{prefix}_ways USING GIN (nodes)"
684679 " WITH (fastupdate = off) {index_tablespace};\n " ;
685680 } else {
686- sql.create_index = " CREATE OR REPLACE FUNCTION"
687- " {schema}{prefix}_index_bucket(int8[])"
688- " RETURNS int8[] AS $$\n "
689- " SELECT ARRAY(SELECT DISTINCT"
690- " unnest($1) >> {way_node_index_id_shift})\n "
691- " $$ LANGUAGE SQL IMMUTABLE;\n "
692- " CREATE INDEX {schema}{prefix}_ways_nodes_bucket_idx"
693- " ON {schema}{prefix}_ways"
694- " USING GIN ({schema}{prefix}_index_bucket(nodes))"
695- " WITH (fastupdate = off) {index_tablespace};\n " ;
681+ sql.create_fw_dep_indexes =
682+ " CREATE OR REPLACE FUNCTION"
683+ " {schema}{prefix}_index_bucket(int8[])"
684+ " RETURNS int8[] AS $$\n "
685+ " SELECT ARRAY(SELECT DISTINCT"
686+ " unnest($1) >> {way_node_index_id_shift})\n "
687+ " $$ LANGUAGE SQL IMMUTABLE;\n "
688+ " CREATE INDEX {schema}{prefix}_ways_nodes_bucket_idx"
689+ " ON {schema}{prefix}_ways"
690+ " USING GIN ({schema}{prefix}_index_bucket(nodes))"
691+ " WITH (fastupdate = off) {index_tablespace};\n " ;
696692 }
697693
698694 return sql;
@@ -717,17 +713,19 @@ static table_sql sql_for_relations() noexcept
717713 " SELECT members, tags"
718714 " FROM {schema}{prefix}_rels WHERE id = $1;\n " ;
719715
720- sql.prepare_mark = " PREPARE mark_rels_by_node(int8) AS"
721- " SELECT id FROM {schema}{prefix}_rels"
722- " WHERE parts && ARRAY[$1]"
723- " AND parts[1:way_off] && ARRAY[$1];\n "
724- " PREPARE mark_rels_by_way(int8) AS"
725- " SELECT id FROM {schema}{prefix}_rels"
726- " WHERE parts && ARRAY[$1]"
727- " AND parts[way_off+1:rel_off] && ARRAY[$1];\n " ;
728-
729- sql.create_index = " CREATE INDEX ON {schema}{prefix}_rels USING GIN (parts)"
730- " WITH (fastupdate = off) {index_tablespace};\n " ;
716+ sql.prepare_fw_dep_lookups =
717+ " PREPARE mark_rels_by_node(int8) AS"
718+ " SELECT id FROM {schema}{prefix}_rels"
719+ " WHERE parts && ARRAY[$1]"
720+ " AND parts[1:way_off] && ARRAY[$1];\n "
721+ " PREPARE mark_rels_by_way(int8) AS"
722+ " SELECT id FROM {schema}{prefix}_rels"
723+ " WHERE parts && ARRAY[$1]"
724+ " AND parts[way_off+1:rel_off] && ARRAY[$1];\n " ;
725+
726+ sql.create_fw_dep_indexes =
727+ " CREATE INDEX ON {schema}{prefix}_rels USING GIN (parts)"
728+ " WITH (fastupdate = off) {index_tablespace};\n " ;
731729
732730 return sql;
733731}
0 commit comments