Repository navigation
Description
Osm2pgsql can handle any number of input files. The current code will just read the files one after the other. I think this isn't actually that useful and I propose to change it.
Use case 1: Create mode
Importing several OSM files into a database can be useful, for instance when you want to import two extracts with the data from two different countries.
Unfortunately the current code only allows you to do this if there is no overlap between the two files. If there is any object in both files, osm2pgsql will try to insert it twice into the database and will fail.
To make this work you need a tool like Osmium or Osmosis to merge the input files first.
Use case 2: Reading several change files in append mode
The other case where this could be useful is when reading multiple change files appending all changes to a database. This could be useful if, say, you want to update a database that is several days old with the change files from all of those days. Unfortunately this doesn't work, because the replication diffs you can download from planet.osm.org can contain multiple versions of the same object which will either not work at all or lead to extra work because you are updating the database more often than needed.
Currently you need a tool like Osmium or Osmosis to first merge all the change files and simplify them.
Proposed change
To make this feature more useful I propose to change the functionality. Instead of reading the input files sequentially, we can read them all at once. While reading, any object version but the newest is simply discarded and the newest is only processed once. So it doesn't matter any more if there are overlaps in the input files or if there are multiple changes to the same object. Both use cases above will just work.
The change has one drawback: The input files must all be sorted. Otherwise we can't do this merging of the input files on the fly. This isn't a huge problem though. Most OSM data and OSM change files normal users will encounter are sorted. And for those who have unsorted files due to some special circumstances, they can still use tools such as Osmium or Osmosis to sort their files first.
So we'd be removing some burden from the "normal" user while adding a little burden to users with special cases. I think this is a good tradeoff.
The change has additional benefits:
- Checking that the data coming in is sorted is cheap and easy and we can give the user a good error message.
- If we can rely on sorted data, some data structures inside osm2pgsql might become simpler and faster.
- Processing multiple and/or unsorted files is currently not tested well and it isn't at all clear whether it actually works reliably.
Note that this is related to #1097, because with negative ids, we'll have to define exactly how to sort objects with negative ids.
Activity
mmd-osm commentedon May 14, 2020
The Wiki page on the OSM XML format (https://wiki.openstreetmap.org/wiki/OSM_XML) explicitly states that blocks of a type don't have to be sorted (section "Certainties and Uncertainties"). You could argue that the Wiki isn't a reliable source of reference anyway, and simply ignore this page. However, there's some chance that Introducing stricter requirements will break some people's workflow.
Just to name one example, Overpass queries using
out qt; would produce an XML result that can no longer be processed by osm2pgsql. There may be other extract services that might be affected in a smilar way (probbaly needs a bit of a research to identify those).It’s clear that processing multiple files would largely benefit from sorted input files. However, I don’t see why this use case should have an impact on processing a single file. Can you separate those two use cases somehow?
Check order of input files and warn if they are out of order
Check order of input files and warn if they are out of order
Check order of input files and warn if they are out of order
Improve handling of multiple input files
joto commentedon Dec 2, 2020
What I proposed here is now mostly working. Diffs added in --append mode still have to be simplified beforehand, ie. they can't have multiple versions of the same object in them. So it probably still makes more sense to simplify and merge them before giving them to osm2pgsql. This might or might not be something we want to add later. But for the time being I'll close this here as done.