rustprooflabs / pgosm-flex

PgOSM Flex provides high quality OpenStreetMap datasets in PostGIS (Postgres) using the osm2pgsql Flex output.
MIT License
100 stars 20 forks source link

Fails when appending data from a second region #397

Closed jhon100 closed 1 month ago

jhon100 commented 1 month ago

Hi,

I installed the latest version of pgosm-flex on Docker Desktop yesterday, and it works correctly when importing data from one region, but it always fails when trying to add data from a second region.

The commands I used and part of the logs are below. As you can see, when executing command (2), it indicated that the --force argument was necessary. In command (3), when using the --force command, it warned that the command was unnecessary. But the bigger problem is that it's giving a foreign key error. I tested several times, with the database empty and also by reversing the order of the regions, and the problem always occurs with the second region.


(1) python3 docker/pgosm_flex.py --ram=2 --input-file=south_australia.osm.pbf --update create --skip-nested

Result: [...] ...:INFO:pgosm-flex:pgosm_flex:Skipping calculating nested polygons ...:INFO:pgosm-flex:pgosm_flex:Skipping pg_dump ...:INFO:pgosm-flex:pgosm_flex:PgOSM Flex complete!

(2) python3 docker/pgosm_flex.py --ram=2 --input-file=northern_territory.osm.pbf --update append --skip-nested

Result: [...] ...:ERROR:pgosm-flex:helpers:Prior data exists in the osm schema and --force was not used.

(3) python3 docker/pgosm_flex.py --ram=2 --input-file=northern_territory.osm.pbf --force --update append --skip-nested

Result: [...] ...:WARNING:pgosm-flex:pgosm_flex:Using --force with the built-in database is unnecessary. The pgosm database is always dropped and recreated when running on localhost (in Docker). ...:INFO:pgosm-flex:db:Database already existed. ...:INFO:pgosm-flex:db:Preparing database schema: osm [...] ...:INFO:pgosm-flex:helpers:2024-07-28 22:40:40 ERROR: DB copy thread failed: Database error: ERROR: update or delete on table "place_polygon" violates foreign key constraint "fk_place_polygon_nested" on table "place_polygon_nested" ...:INFO:pgosm-flex:helpers:DETAIL: Key (osm_id)=(-2316596) is still referenced from table "place_polygon_nested". [...]

LOG ...STATEMENT: DELETE FROM "osm"."place_polygon" WHERE osm_id IN (-3676,-3918,-3919,-8896,-9487,-153172,[...],-17848463)

rustprooflabs commented 1 month ago

Thank you for reporting this @jhon100! I haven't done much with these options, seems like there's a bug lurking surrounding how I added --force into the mix. My thought is your 2nd command should work w/out --force and we just need to add an appropriate check for --update mode. Does that match your expectations?

That logic should fit in to ImportMode.okay_to_run() (here-ish) with a check like this (untested):

        if self.update == 'append':
            return True

It will likely be a few days before I can test this. If you want to make a PR in the meantime I'd be happy to review/merge that. If not, I'll get there before too long!

jhon100 commented 1 month ago

Hi, thanks for your response.

I tested the change you suggested in the helpers.py file, but the foreign key error persists.

I also noticed that, as in the log I posted earlier, the error mentions the place_polygon_nested table. Is it correct for the script to manipulate '..._nested' tables when I'm using the --skip-nested argument?

Below, I am sending the complete log of the command with --update append.


python3 docker/pgosm_flex.py --ram=2 --input-file=northern_territory.osm.pbf --update append --skip-nested

...:INFO:pgosm-flex:pgosm_flex:PgOSM Flex starting... ...:INFO:pgosm-flex:db:Checking for Postgres service to be available ...:INFO:pgosm-flex:db:Connecting to Postgres using role "postgres" on host "localhost:5432" in database "pgosm" ...:INFO:pgosm-flex:db:Postgres instance ready ...:INFO:pgosm-flex:db:Database already existed. ...:INFO:pgosm-flex:db:Preparing database schema: osm ...:INFO:pgosm-flex:db:Deploying /app/db/deploy/osm.sql ...:INFO:pgosm-flex:db:Deploying /app/db/deploy/osm_pgosm_flex.sql ...:INFO:pgosm-flex:db:Deploying /app/db/deploy/pgosm_road.sql ...:INFO:pgosm-flex:db:Deploying /app/db/deploy/replication_functions.sql ...:INFO:pgosm-flex:db:Loading QGIS styles ...:INFO:pgosm-flex:qgis_styles:Load QGIS styles to database pgosm... ...:INFO:pgosm-flex:qgis_styles:QGIS Style table populated ...:INFO:pgosm-flex:db:Deploying /app/db/data/roads-us.sql ...:INFO:pgosm-flex:helpers:PgOSM Flex version: 1.0.1-0c009a5 ...:INFO:pgosm-flex:pgosm_flex:Started import id 2 ...:INFO:pgosm-flex:pgosm_flex:Running osm2pgsql ...:INFO:pgosm-flex:pgosm_flex:Running osm2pgsql ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 osm2pgsql version 1.11.0 ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 Database version: 16.3 (Debian 16.3-1.pgdg110+1) ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 PostGIS version: 3.4 ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 Loading properties from table '"public"."osm2pgsql_properties"'. ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 Not using flat node file (same as on import). ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 Using prefix 'planet_osm' (same as on import). ...:INFO:pgosm-flex:helpers:2024-07-29 18:42:30 Using style file '/app/flex-config/./run.lua' (same as on import). ...:INFO:pgosm-flex:helpers:Using default layerset INI path ...:INFO:pgosm-flex:helpers:Loading config: layerset/default.ini ...:INFO:pgosm-flex:helpers:Including amenity ...:INFO:pgosm-flex:helpers:Default SRID: 3857 ...:INFO:pgosm-flex:helpers:Explicit Date: 2024-07-29 ...:INFO:pgosm-flex:helpers:INFO - Default language not set. Using OSM Wiki priority for name. Set PGOSM_LANGUAGE to customize. ...:INFO:pgosm-flex:helpers:Including building ...:INFO:pgosm-flex:helpers:Including indoor ...:INFO:pgosm-flex:helpers:Including infrastructure ...:INFO:pgosm-flex:helpers:Including landuse ...:INFO:pgosm-flex:helpers:Including leisure ...:INFO:pgosm-flex:helpers:Including natural ...:INFO:pgosm-flex:helpers:Including place ...:INFO:pgosm-flex:helpers:Including poi ...:INFO:pgosm-flex:helpers:Including public_transport ...:INFO:pgosm-flex:helpers:Including road ...:INFO:pgosm-flex:helpers:Including shop ...:INFO:pgosm-flex:helpers:Including tags ...:INFO:pgosm-flex:helpers:Including traffic ...:INFO:pgosm-flex:helpers:Including water Processing: Node(2207k 11.1k/s) Way(126k 1.03k/s) Relation(6760 169.0/s)0k/s) Way(0k 0.00k/s) Relation(0 0.0/s) ...:INFO:pgosm-flex:helpers:2024-07-29 18:48:35 ERROR: DB copy thread failed: Database error: ERROR: update or delete on table "place_polygon" violates foreign key constraint "fk_place_polygon_nested" on table "place_polygon_nested" ...:INFO:pgosm-flex:helpers:DETAIL: Key (osm_id)=(-2316596) is still referenced from table "place_polygon_nested". ...:INFO:pgosm-flex:helpers: ...:ERROR:pgosm-flex:pgosm_flex:Failed to run osm2pgsql. Return code: 2 Failed to run osm2pgsql. Return code: 2 - Check the log output for details

rustprooflabs commented 1 month ago

Aha, no. It shouldn't have been doing that, there was a function call in a SQL file that always ran, loading data into that table. What --skip-nested was doing was skipping the expensive calculations but still had been loading data to the table.

The problem ran a bit deeper around that functionality and I think I have the path forward figured out in #398. There's a bit more work to be done on that, but I think the current WIP addresses the specific uses cases outlined here.

jhon100 commented 1 month ago

Thanks for clearing that up. Appreciate the work you're putting into this. I'll keep an eye on #398 and test it when the updates are ready.

rustprooflabs commented 1 month ago

Hi @jhon100 -- I think what I did in #398 should resolve your problem. If you could verify if it's truly fixed that'd be fantastic. I merged that work into the dev branch so you can either check that out and build from there, or pull the image docker pull rustprooflabs/pgosm-flex:dev.

jhon100 commented 1 month ago

Hi, I just tested the dev branch and the import of both regions worked without errors. Thank you!!!

rustprooflabs commented 1 month ago

Thank you for confirming! Merged into main and pushed a new latest branch. I'll try to get the new version tagged either today or this weekend.