From 6bbeca533cf4bc28e6e16069e670c16ef26f01bb Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sun, 2 Jun 2024 11:21:22 +0200 Subject: [PATCH] Allow setting the index names in the flex output Usually the name chosen by PostgreSQL for an index is fine, but sometimes you want to use a better name, especially if you are using expression indexes. --- flex-config/indexes.lua | 5 ++++- src/debug-output.cpp | 5 +++++ src/flex-index.cpp | 8 +++++++- src/flex-index.hpp | 8 ++++++++ src/flex-lua-index.cpp | 6 ++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/flex-config/indexes.lua b/flex-config/indexes.lua index 942c6f066..238083a30 100644 --- a/flex-config/indexes.lua +++ b/flex-config/indexes.lua @@ -62,11 +62,14 @@ tables.roads = osm2pgsql.define_way_table('roads', { }}) -- Instead of on a column (or columns) you can define an index on an expression. +-- Indexes can be named (the default name is the one that PostgreSQL creates). tables.postboxes = osm2pgsql.define_node_table('postboxes', { { column = 'operator', type = 'text' }, { column = 'geom', type = 'point', not_null = true }, }, { indexes = { - { expression = 'lower(operator)', method = 'btree' }, + { expression = 'lower(operator)', + method = 'btree', + name = 'postbox_operator_idx' }, }}) -- Helper function that looks at the tags and decides if this is possibly diff --git a/src/debug-output.cpp b/src/debug-output.cpp index ea27e8cfd..3ee119d2c 100644 --- a/src/debug-output.cpp +++ b/src/debug-output.cpp @@ -61,6 +61,11 @@ void write_table_list_to_debug_log(std::vector const &tables) log_debug(" - cluster={}", table.cluster_by_geom()); for (auto const &index : table.indexes()) { log_debug(" - INDEX USING {}", index.method()); + if (index.name().empty()) { + log_debug(" - name=(default name)"); + } else { + log_debug(" - name={}", index.name()); + } log_debug(" - column={}", index.columns()); log_debug(" - expression={}", index.expression()); log_debug(" - include={}", index.include_columns()); diff --git a/src/flex-index.cpp b/src/flex-index.cpp index 99cc0b92a..0f6ef2928 100644 --- a/src/flex-index.cpp +++ b/src/flex-index.cpp @@ -30,7 +30,13 @@ flex_index_t::create_index(std::string const &qualified_table_name) const joiner.add("UNIQUE"); } - joiner.add("INDEX ON"); + joiner.add("INDEX"); + + if (!m_name.empty()) { + joiner.add(fmt::format(R"("{}")", m_name)); + } + + joiner.add("ON"); joiner.add(qualified_table_name); joiner.add("USING"); diff --git a/src/flex-index.hpp b/src/flex-index.hpp index ba38e2c27..b6ee96389 100644 --- a/src/flex-index.hpp +++ b/src/flex-index.hpp @@ -48,6 +48,13 @@ class flex_index_t m_include_columns = columns; } + std::string const &name() const noexcept { return m_name; } + + void set_name(std::string name) + { + m_name = std::move(name); + } + std::string const &expression() const noexcept { return m_expression; } void set_expression(std::string expression) @@ -89,6 +96,7 @@ class flex_index_t private: std::vector m_columns; std::vector m_include_columns; + std::string m_name; std::string m_method; std::string m_expression; std::string m_tablespace; diff --git a/src/flex-lua-index.cpp b/src/flex-lua-index.cpp index 33876fd37..c9168b3a7 100644 --- a/src/flex-lua-index.cpp +++ b/src/flex-lua-index.cpp @@ -78,6 +78,12 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table) } lua_pop(lua_state, 1); + // get name + std::string const name = + luaX_get_table_string(lua_state, "name", -1, "Index definition", ""); + lua_pop(lua_state, 1); + index.set_name(name); + // get expression std::string const expression = luaX_get_table_string( lua_state, "expression", -1, "Index definition", "");