Skip to content

Commit 960ef0c

Browse files
committed
Add option to always build id index
If the user always needs the id index, they can force the index build even in non-slim (or slim+drop) mode by setting the `create_index` option of the `ids` setting in the define_table() Lua command to `always`. The default is `autpo` which means: Only build the index in slim mode. See #1854 The test runner (steps_db.py) has been extended so that the table_exists() check also checks for views, so that we can access the data in the pg_catalog.pg_indexes view.
1 parent 38b1432 commit 960ef0c

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

src/flex-table.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ void table_connection_t::stop(bool updateable, bool append)
359359
}
360360
}
361361

362-
if (updateable && table().has_id_column()) {
362+
if (table().always_build_id_index() ||
363+
(updateable && table().has_id_column())) {
363364
create_id_index();
364365
}
365366

src/flex-table.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ class flex_table_t
155155

156156
flex_index_t &add_index(std::string method);
157157

158+
void set_always_build_id_index() noexcept
159+
{
160+
m_always_build_id_index = true;
161+
}
162+
163+
bool always_build_id_index() const noexcept
164+
{
165+
return m_always_build_id_index;
166+
}
167+
158168
private:
159169
/// The name of the table
160170
std::string m_name;
@@ -200,6 +210,9 @@ class flex_table_t
200210
/// Does this table have more than one geometry column?
201211
bool m_has_multiple_geom_columns = false;
202212

213+
/// Always build the id index, not only when it is needed for updates?
214+
bool m_always_build_id_index = false;
215+
203216
}; // class flex_table_t
204217

205218
class table_connection_t

src/output-flex.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ void output_flex_t::setup_id_columns(flex_table_t *table)
566566
lua_pop(lua_state(), 1); // "id_column"
567567
check_identifier(name, "column names");
568568

569+
std::string const create_index = luaX_get_table_string(
570+
lua_state(), "create_index", -1, "The ids field", "auto");
571+
lua_pop(lua_state(), 1); // "create_index"
572+
if (create_index == "always") {
573+
table->set_always_build_id_index();
574+
} else if (create_index != "auto") {
575+
throw fmt_error("Unknown value '{}' for 'create_index' field of ids",
576+
create_index);
577+
}
578+
569579
auto &column = table->add_column(name, "id_num", "");
570580
column.set_not_null();
571581
lua_pop(lua_state(), 1); // "ids"

tests/bdd/flex/lua-index-definitions.feature

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,64 @@ Feature: Index definitions in Lua file
476476
Then SELECT schemaname, tablename FROM pg_catalog.pg_indexes WHERE tablename = 'mytable' AND indexdef LIKE '%USING btree (name)%' AND indexdef LIKE '%WHERE (name = lower(name))%'
477477
| schemaname | tablename |
478478
| public | mytable |
479+
480+
Scenario: Don't create id index if the configuration doesn't mention it
481+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
482+
And the lua style
483+
"""
484+
local t = osm2pgsql.define_table({
485+
name = 'mytable',
486+
ids = { type = 'node', id_column = 'node_id' },
487+
columns = {
488+
{ column = 'name', type = 'text' },
489+
{ column = 'tags', type = 'jsonb' },
490+
{ column = 'geom', type = 'geometry' },
491+
}
492+
})
493+
"""
494+
When running osm2pgsql flex
495+
Then table pg_catalog.pg_indexes has 0 rows with condition
496+
"""
497+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
498+
"""
499+
500+
Scenario: Don't create id index if the configuration doesn't says so
501+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
502+
And the lua style
503+
"""
504+
local t = osm2pgsql.define_table({
505+
name = 'mytable',
506+
ids = { type = 'node', id_column = 'node_id', create_index = 'auto' },
507+
columns = {
508+
{ column = 'name', type = 'text' },
509+
{ column = 'tags', type = 'jsonb' },
510+
{ column = 'geom', type = 'geometry' },
511+
}
512+
})
513+
"""
514+
When running osm2pgsql flex
515+
Then table pg_catalog.pg_indexes has 0 rows with condition
516+
"""
517+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
518+
"""
519+
520+
Scenario: Always create id index if the configuration says so
521+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
522+
And the lua style
523+
"""
524+
local t = osm2pgsql.define_table({
525+
name = 'mytable',
526+
ids = { type = 'node', id_column = 'node_id', create_index = 'always' },
527+
columns = {
528+
{ column = 'name', type = 'text' },
529+
{ column = 'tags', type = 'jsonb' },
530+
{ column = 'geom', type = 'geometry' },
531+
}
532+
})
533+
"""
534+
When running osm2pgsql flex
535+
Then table pg_catalog.pg_indexes has 1 rows with condition
536+
"""
537+
schemaname = 'public' AND tablename = 'mytable' AND indexname LIKE '%node_id%'
538+
"""
539+

tests/bdd/steps/steps_db.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ def table_exists(conn, table):
121121
num = scalar(conn, """SELECT count(*) FROM pg_tables
122122
WHERE tablename = %s AND schemaname = %s""",
123123
(tablename, schema))
124+
if num == 1:
125+
return True
126+
127+
num = scalar(conn, """SELECT count(*) FROM pg_views
128+
WHERE viewname = %s AND schemaname = %s""",
129+
(tablename, schema))
124130
return num == 1
125131

126132

0 commit comments

Comments
 (0)