openstreetmap-cgimap icon indicating copy to clipboard operation
openstreetmap-cgimap copied to clipboard

pqxx::stream_to

Open mmd-osm opened this issue 1 year ago • 3 comments
trafficstars

Follow up for https://github.com/OpenHistoricalMap/issues/issues/783

One of the suggestions in the OHM issue (besides fixing PostgreSQL parameter settings) was to use COPY to load table contents. No doubt, COPY will be faster than the current approach. Library support for COPY was rather limited 5 years ago, stream_to was only added in libpqxx 7.x.x (IIRC). Since we cannot switch to a more recent libpqxx right away, this would introduce a bit of code duplication to support both 6.4.x and 7.8.x.

I tested this idea on a more recent libpqxx 7.8.1 with 1 million table entries (3 bigint columns). Performance improvement was about 20%.

auto cn = pqxx::stream_to::raw_table(work, "current_way_nodes", "way_id, node_id, sequence_id");
...
cn.write_values(way_id, node_id, sequence_id);
...
cn.complete();

Relevant methods:

  • ApiDB_Node_Updater

    • insert_new_current_node_tags
  • ApiDB_Way_Updater

    • insert_new_current_way_tags
    • insert_new_current_way_nodes
  • ApiDB_Relation_Updater

    • insert_new_current_relation_tags
    • insert_new_current_relation_members

mmd-osm avatar Jun 11 '24 20:06 mmd-osm

Further tests with https://github.com/mmd-osm/openstreetmap-cgimap/tree/feature/stream_to have shown no significant improvement. We'll keep the issue open for the time being, but likely won't use this approach.

mmd-osm avatar Jun 14 '24 18:06 mmd-osm

@Woazboat : maybe you could review or try out this change, if you can see any significant performance difference. Maybe also check out the recommendation given in https://github.com/OpenHistoricalMap/issues/issues/783#issuecomment-2148074348

As a side note, it would be interesting to try this out on the osm dev server. As the server is still running on Ubuntu 22.04, a 7.8.x libpqxx version isn't available at the moment without additional manual install effort.

mmd-osm avatar Jun 26 '24 21:06 mmd-osm

Sure, I can take a look. I'm not too familiar with SQL optimization but maybe I can find something.

Woazboat avatar Jun 28 '24 11:06 Woazboat

So in the end I merged the PR, after it became clear that some other (unrelated) query was dominating the overall runtime. Unfortunately, there's no zero effort way to test this code on the dev instance, as long as the server is still running on Ubuntu 22.04.

mmd-osm avatar Jul 27 '24 14:07 mmd-osm