Skip to content

Import changes combined (tracking PR) - #58

Open
cldellow wants to merge 8 commits into
masterfrom
better-imports
Open

cldellow wants to merge 8 commits into
masterfrom
better-imports

Conversation

@cldellow

@cldellow cldellow commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes #34, fixes #45, fixes #46, fixes #47, fixes #48, fixes #49, fixes #50.

What this is

better-imports combines every import change, cherry-picked onto upstream/master in this order. These changes fix how update_database --keep-attic stores a full-history file (.osh), so that databases with history can be built for testing. They are kept apart from all-changes, which is for performance work. Each change also has its own PR for review. This PR tracks their combined effect, and is updated when a change is added or revised.

Commit Change Own PR
Keep new attic skeletons when importing into an empty database with attic A fresh --keep-attic import no longer clears the node and way history that older way and relation versions need #51
Treat visible="false" as a deletion when importing history files Deleted versions in .osh files are deletions, not live objects #52
Use the last known position of a deleted node in a way's geometry A way version using an already deleted node no longer breaks all older versions #53
Read stored attic nodes and ways when importing in several batches Imports spanning several flushes read the history written by earlier batches #54
Flush only between elements, not between versions of the same element No batch boundary inside an element's versions (fixes duplicated tags) #55
Skip stored attic lookups for elements whose history is in memory Avoids reading back history that is already in memory #55 (second commit)
Keep one coordinate per nd when a way's node has no known position Nodes that have only a deletion in the extract no longer break a way's older versions #56
Report each missing relation member once per run Members outside the extract are logged once, not tens of millions of times #57

None of the changes alters the database format or the query API. osm3s_query doesn't link any changed file, so query performance (the benchmark queries of all-changes) is unaffected.

How to build a database with history

osmium cat extract.osh.pbf -f osh -o - | BETTER_IMPORTS/install/bin/update_database --db-dir=$PWD/db/ --keep-attic

Use -f osh: -f osm drops the visible="false" markers of deleted versions.

Correctness: upstream/master vs better-imports

Two Geofabrik full-history extracts (licensed, not shareable; aggregate numbers only):

  • Prince Edward Island: 3.1 M node, 291 k way and 28 k relation versions, 2006 to 2026-11-15
  • Nova Scotia: 20.3 M node, 2.1 M way and 131 k relation versions, 2006 to 2026-11-15

For each date, all objects in the world box were compared with osmium time-filter <file> <date>:

[date:"2018-01-01T00:00:00Z"][maxsize:4000000000][timeout:3600];
(node(-90,-180,90,180);way(-90,-180,90,180);rel(-90,-180,90,180););
out meta;

Objects whose latest version at or before the date is a deletion were queried by id, in chunks of up to 2,000, from a random sample:

[date:"2018-01-01T00:00:00Z"][timeout:3600];node(id:<up to 2000 ids of nodes deleted at that date>);out ids;

likewise with way(id:<ids>) and rel(id:<ids>).

Extract, date Ways missing, upstream/master Ways missing, better-imports Deleted objects returned by id, upstream/master better-imports
PEI 2010-01-01 842 of 2,094 0 not checked not checked
PEI 2018-01-01 7,469 of 73,186 0 27,103 of 27,103 checked 0 of 6,055 checked
PEI 2026-11-16 0 of 172,463 0 37,545 of 37,545 checked 0 of 6,324 checked
NS 2010-01-01 not imported 13 of 10,751 not imported 0
NS 2014-01-01 not imported 0 of 366,163 not imported 0
NS 2018-01-01 not imported 0 of 570,804 not imported 0
NS 2022-01-01 not imported 0 of 758,016 not imported 0
NS 2026-11-16 not imported 0 of 883,224 not imported 0

The upstream/master Nova Scotia import wasn't run here; #34 has its numbers from an earlier session.

Import time and memory

4-core, 15 GB cloud container, idle, stderr written to a file:

Extract upstream/master better-imports
PEI wall time 2:39 (one run) 1:09 and 1:14 (two runs)
PEI peak RSS 1.46 GB 1.88 GB
NS wall time not run 7:01 (one run)
NS peak RSS not run 5.65 GB
NS database size not run 27 GB

Generated by Claude Code

@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 21, 2026 — with Claude
@cldellow cldellow added the import-fix Fixes to update_database / history imports; collected on better-imports, not all-changes label Nov 21, 2026 — with Claude
@cldellow cldellow removed the query-perf Query performance change; collected on all-changes label Nov 21, 2026
@cldellow cldellow added the bug Fixes wrong results (in addition to the kind label) label Nov 21, 2026 — with Claude
…ttic

Node_Updater and Way_Updater initialise partial_possible to false when keeping
attic data, but the constructor body then overwrites it with "the database
files don't exist yet". So a fresh `update_database --keep-attic` import runs in
partial mode, and partial mode clears new_skeletons, attic_skeletons and
new_attic_skeletons after each node or way update.

The way updater needs the new attic node skeletons to compute the geometry and
index of older way versions, and the relation updater needs the new attic way
skeletons likewise. Without them, a way version that is older than the latest
move of one of its nodes gets an empty geometry and the invalid index, so
[date:] queries by bounding box don't find it.

Keep the initialiser's intent: partial updates only without attic data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
A full-history file (.osh, e.g. from `osmium cat x.osh.pbf -f osh`) marks deleted
versions with visible="false" instead of wrapping them in <delete>. The parser
ignored the attribute, so update_database stored each deleted version as a live
object: a node at the out-of-range placeholder position (lat 100, lon 200), a way
without nodes, a relation without members. Such objects were then returned by id
queries, with or without [date:], as if never deleted.

Pass elements with visible="false" to set_id_deleted, as for <delete>.

This depends on keeping the new attic skeletons during a fresh attic import:
without that, ways referring to a node that is deleted later lose that node's
coordinates in their older versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
History data has way versions that refer to nodes deleted before them. In
the extract this was found with, a way's nodes were deleted ten seconds before
the way itself. compute_idx_and_geometry then skipped those nodes, so the
stored way had fewer coordinates than nds. Overpass stores older way versions
as deltas from the newer ones, so expanding any older version through that
state fails with "Bad geometry for way", and [date:] queries lose the way for
every date before it, not only for the ten seconds.

Use the node's last known position instead, keeping one coordinate per nd. Do
the same for node and way members when computing a relation's index.

Before deletions in history files were honoured, such nodes stayed alive at the
placeholder position, which hid this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
update_database flushes every --flush-size elements. The way updater computes
the geometry of older way versions from the attic node versions of the current
batch plus the current node positions, and the relation updater does the same
with nodes and ways. When a history file's nodes span several batches (with the
default flush size, any extract with more than a few million node versions), the
attic versions written by earlier batches are not seen: older way versions get
too few coordinates or the placeholder index, and the import can abort with
"Bad geometry for way".

When importing without dispatcher (Way_Updater and Relation_Updater constructed
with a db_dir), also read the stored attic versions of all nodes, respectively
nodes and ways, referenced by the batch, and merge them into the attic
skeletons used for the computation. Updates via the dispatcher are unchanged.

Memory: the added map holds the attic versions of the nodes (and ways) referred
to by one batch of ways (relations), so it is bounded by the batch size times
the number of versions of those elements. It is not counted towards any limit,
like the other structures of the updater.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
update_database flushes once --flush-size elements have been read. In a
history file the versions of an element are consecutive, so a flush could fall
between two versions of one element. With the versions split over two batches,
attic tags came out duplicated: in the extract this was found with, a relation
whose versions were split showed every tag twice at later dates.

Defer the flush until the next element has a different id. A batch then grows
by at most the versions of one element. osmChange files are unaffected, since
they rarely contain the same element twice in a row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
Since update_database flushes only between elements, an element with attic
versions in the current batch has all its versions of this import in memory.
Only the referenced elements without such versions need their stored attic
versions read. This saves reading back, in a single-batch import, the whole
attic history that was just written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
compute_idx_and_geometry skipped nodes it found neither in the attic nor in
the current data. In a regional history extract that happens to nodes whose
only version in the extract is their deletion, e.g. because they were
outside the region while visible. The stored way version then had fewer
coordinates than nds, and since older versions are stored as deltas from
newer ones, expanding any older version through it failed with "Bad geometry
for way", so [date:] queries lost the way for every earlier date.

Do as compute_geometry already does for current versions: push a zero
Quad_Coord for the unknown node, keeping the geometry aligned with the nds,
and compute the index only from the nodes with a known position. Queries
print such a node as a reference without coordinates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
Relations in a regional extract often have members outside it, and the
relation updater computes a relation's index for each version and each
intermediate state, printing a line per missing member each time. Importing
a full-history extract of Prince Edward Island printed 66 million such lines
for 85 thousand distinct missing members. std::cerr is unbuffered, so this
took more than half of the import time.

Print each missing member once per run. The set of reported members is
bounded by the number of distinct missing members.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJV9ygtU1g57EBR2Ufo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment