Breaking the ingress speed limit with Apache Arrow and ADBC
PostGIS Day
https://dewey.dunnington.ca/post/2024/wrangling-and-joining-130m-points-with-duckdb–the-open-source-spatial-stack/
import geopandas as gpd
import pyarrow.parquet as pq
from geoalchemy2 import Geometry
table = pq.read_table("fixture.parquet")
gdf = table.to_pandas()
gdf["geometry"] = gpd.GeoSeries.from_wkb(gdf["geometry"])
gdf = gpd.GeoDataFrame(gdf, geometry="geometry")
gdf.to_postgis(
"destination", engine, if_exists="replace", index=False,
dtype={"geometry": Geometry(spatial_index=False)},
)Good: intuitive! Tricky: materialize the entire table up front, slowest method
Good: one liner, flexible input Tricky: must remember to ingest without spatial index and with copy, need to find a build with Arrow for Parquet
INSTALL postgres; LOAD postgres;
INSTALL spatial; LOAD spatial;
CREATE SECRET postgis_ingest (TYPE postgres, ...);
ATTACH '' AS postgis (TYPE postgres, SECRET postgis_ingest);
DROP TABLE IF EXISTS postgis.destination;
CREATE TABLE postgis.destination AS
SELECT * FROM read_parquet('fixture.parquet');Good: great if you’re in the DuckDB ecosystem! Fastest over the network. Tricky: No Z value support
import adbc_driver_postgresql.dbapi as dbapi
import pyarrow as pa
import pyarrow.parquet as pq
parquet = pq.ParquetFile("fixture.parquet")
reader = pa.RecordBatchReader.from_batches(
parquet.schema_arrow, parquet.iter_batches()
)
with dbapi.connect(uri) as conn, conn.cursor() as cur:
cur.adbc_ingest("destination", reader, mode="replace")
cur.execute("""
ALTER TABLE destination ALTER COLUMN geometry TYPE geometry
USING ST_GeomFromWKB(geometry)
""")Good: Flexible and low dependency (any Arrow stream in!) Tricky: Requires an ALTER TABLE without a workaround
ns-water elevation (11 million points), ns-water water_poly (700k polygons)
ns-water elevation (11 million points), ns-water water_poly (700k polygons)
microsoft-buildings_point (130 million points)
microsoft-buildings_point (130 million points)
Insert + bind (geopandas, ogr2ogr without PG_USECOPY=1):
COPY (DuckDB, ogr2ogr with PG_USECOPY=1, ADBC):
COPY (DuckDB, DuckDB-copy):
But some holes to fill in the ecosystem:
Parquet is fast enough to saturate the network; ADBC and DuckDB are consistently twice as fast as ogr2ogr
Use multiple connections explicitly may be faster (but check first since it’s slightly more complicated)
Avoid GeoPandas over a million rows