Skip to content

Commit 6bbeca5

Browse files
committed
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.
1 parent 43ec304 commit 6bbeca5

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

flex-config/indexes.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ tables.roads = osm2pgsql.define_way_table('roads', {
6262
}})
6363

6464
-- Instead of on a column (or columns) you can define an index on an expression.
65+
-- Indexes can be named (the default name is the one that PostgreSQL creates).
6566
tables.postboxes = osm2pgsql.define_node_table('postboxes', {
6667
{ column = 'operator', type = 'text' },
6768
{ column = 'geom', type = 'point', not_null = true },
6869
}, { indexes = {
69-
{ expression = 'lower(operator)', method = 'btree' },
70+
{ expression = 'lower(operator)',
71+
method = 'btree',
72+
name = 'postbox_operator_idx' },
7073
}})
7174

7275
-- Helper function that looks at the tags and decides if this is possibly

src/debug-output.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ void write_table_list_to_debug_log(std::vector<flex_table_t> const &tables)
6161
log_debug(" - cluster={}", table.cluster_by_geom());
6262
for (auto const &index : table.indexes()) {
6363
log_debug(" - INDEX USING {}", index.method());
64+
if (index.name().empty()) {
65+
log_debug(" - name=(default name)");
66+
} else {
67+
log_debug(" - name={}", index.name());
68+
}
6469
log_debug(" - column={}", index.columns());
6570
log_debug(" - expression={}", index.expression());
6671
log_debug(" - include={}", index.include_columns());

src/flex-index.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ flex_index_t::create_index(std::string const &qualified_table_name) const
3030
joiner.add("UNIQUE");
3131
}
3232

33-
joiner.add("INDEX ON");
33+
joiner.add("INDEX");
34+
35+
if (!m_name.empty()) {
36+
joiner.add(fmt::format(R"("{}")", m_name));
37+
}
38+
39+
joiner.add("ON");
3440
joiner.add(qualified_table_name);
3541

3642
joiner.add("USING");

src/flex-index.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class flex_index_t
4848
m_include_columns = columns;
4949
}
5050

51+
std::string const &name() const noexcept { return m_name; }
52+
53+
void set_name(std::string name)
54+
{
55+
m_name = std::move(name);
56+
}
57+
5158
std::string const &expression() const noexcept { return m_expression; }
5259

5360
void set_expression(std::string expression)
@@ -89,6 +96,7 @@ class flex_index_t
8996
private:
9097
std::vector<std::string> m_columns;
9198
std::vector<std::string> m_include_columns;
99+
std::string m_name;
92100
std::string m_method;
93101
std::string m_expression;
94102
std::string m_tablespace;

src/flex-lua-index.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
7878
}
7979
lua_pop(lua_state, 1);
8080

81+
// get name
82+
std::string const name =
83+
luaX_get_table_string(lua_state, "name", -1, "Index definition", "");
84+
lua_pop(lua_state, 1);
85+
index.set_name(name);
86+
8187
// get expression
8288
std::string const expression = luaX_get_table_string(
8389
lua_state, "expression", -1, "Index definition", "");

0 commit comments

Comments
 (0)