omniscale / imposm3

Imposm imports OpenStreetMap data into PostGIS
http://imposm.org/docs/imposm3/latest/
Apache License 2.0
710 stars 156 forks source link

Generalized table has not index on OSM ID #200

Closed HansonYip closed 5 years ago

HansonYip commented 5 years ago

Environment

When a table has a column with id field type for OSM ID, whose name is not id, for example osm_id, all generalized tables of it have not index on osm_id.

func createIndex(pg *PostGIS, tableName string, columns []ColumnSpec) error {
    foundIDCol := false
    for _, cs := range columns {
        if cs.Name == "id" {
            foundIDCol = true
        }
    }

    for _, col := range columns {
        if col.Type.Name() == "GEOMETRY" {
            sql := fmt.Sprintf(`CREATE INDEX "%s_geom" ON "%s"."%s" USING GIST ("%s")`,
                tableName, pg.Config.ImportSchema, tableName, col.Name)
            step := log.Step(fmt.Sprintf("Creating geometry index on %s", tableName))
            _, err := pg.Db.Exec(sql)
            step()
            if err != nil {
                return err
            }
        }
        if col.FieldType.Name == "id" && foundIDCol {
            // Create index for OSM ID required for diff updates, but only if
            // the table does have an `id` column.
            // The explicit `id` column prevented the creation of our composite
            // PRIMARY KEY index of id (serial) and OSM ID.
            sql := fmt.Sprintf(`CREATE INDEX "%s_%s_idx" ON "%s"."%s" USING BTREE ("%s")`,
                tableName, col.Name, pg.Config.ImportSchema, tableName, col.Name)
            step := log.Step(fmt.Sprintf("Creating OSM id index on %s", tableName))
            _, err := pg.Db.Exec(sql)
            step()
            if err != nil {
                return err
            }
        }
    }
    return nil
}

The source shown as below, foundIDCol is always false. This is because the generalized table has no column with name 'id', but its source table does.

olt commented 5 years ago

Thanks for reporting this issue! The foundIDCol check is only valid for regular columns, where 0.8.0 now automatically creates a composite PRIMARY KEY of osm_id and id (serial).

I fixed this for generalized tables with v0.8.1 (576c4b745d03e77294b80b0545acd42f2337a469).