Skip to content

Commit

Permalink
Use ST_EstimatedExtent for quick bounds calc
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkAndshark authored and nyurik committed Mar 16, 2024
1 parent 3a53055 commit 44fbcbb
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions martin/src/pg/query_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,16 @@ pub async fn table_to_query(
BoundsCalcType::Skip => {}
BoundsCalcType::Calc => {
debug!("Computing {} table bounds for {id}", info.format_id());
info.bounds = calc_bounds(&pool, &schema, &table, &geometry_column, srid).await?;
info.bounds =
calc_bounds(&pool, &schema, &table, &geometry_column, srid, false).await?;
}
BoundsCalcType::Quick => {
debug!(
"Computing {} table bounds with {}s timeout for {id}",
info.format_id(),
DEFAULT_BOUNDS_TIMEOUT.as_secs()
);
let bounds = calc_bounds(&pool, &schema, &table, &geometry_column, srid);
let bounds = calc_bounds(&pool, &schema, &table, &geometry_column, srid, true);
pin_mut!(bounds);
if let Ok(bounds) = timeout(DEFAULT_BOUNDS_TIMEOUT, &mut bounds).await {
info.bounds = bounds?;
Expand Down Expand Up @@ -217,10 +218,12 @@ async fn calc_bounds(
table: &str,
geometry_column: &str,
srid: i32,
is_quick: bool,
) -> PgResult<Option<Bounds>> {
Ok(pool.get()
.await?
.query_one(&format!(
let sql = if is_quick {
format!("SELECT ST_EstimatedExtent('{schema}', '{table}', '{geometry_column}') as bounds")
} else {
format!(
r#"
WITH real_bounds AS (SELECT ST_SetSRID(ST_Extent({geometry_column}), {srid}) AS rb FROM {schema}.{table})
SELECT ST_Transform(
Expand All @@ -232,7 +235,14 @@ SELECT ST_Transform(
4326
) AS bounds
FROM {schema}.{table};
"#), &[])
"#
)
};

Ok(pool
.get()
.await?
.query_one(&sql, &[])
.await
.map_err(|e| PostgresError(e, "querying table bounds"))?
.get::<_, Option<ewkb::Polygon>>("bounds")
Expand Down

0 comments on commit 44fbcbb

Please sign in to comment.