Skip to content

Commit

Permalink
Fix & optimize incorrect function declarations (#918)
Browse files Browse the repository at this point in the history
* All functions that access database must be declared as `STABLE`, not `IMMUTABLE` -- because database can change at any moment, e.g. during an update
* there are a few functions that could be made `STRICT` -- passing `NULL` as a parameter will always result in a `NULL`, but for some reason that causes a significant decrease in perf.
* tagged one function as parallel safe

NOTE: somehow `ST_AsMVT()` method of tile generation is showing 70-90% slowdown with this patch. I am not sure of why this is happening. If the reason is the `IMMUTABLE` -> `STABLE` change, we may have to dig deeper into PG optimization
  • Loading branch information
nyurik committed Jun 17, 2020
1 parent ad8bd41 commit 2b95d1c
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 18 deletions.
3 changes: 2 additions & 1 deletion layers/aerodrome_label/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ SELECT
FROM osm_aerodrome_label_point
WHERE geometry && bbox
AND zoom_level >= 10;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/aeroway/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ FROM (
WHERE zoom_level >= 14
) AS zoom_levels
WHERE geometry && bbox;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
8 changes: 6 additions & 2 deletions layers/boundary/boundary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ SELECT CASE
THEN replace(SUBSTRING(name, POSITION(' at ' IN name) + 4), ' ', '')
ELSE replace(replace(name, ' ', ''), 'Extentof', '')
END;
$$ LANGUAGE SQL IMMUTABLE;
$$ LANGUAGE SQL IMMUTABLE
-- STRICT
PARALLEL SAFE
;


-- etldoc: ne_110m_admin_0_boundary_lines_land -> boundary_z0
Expand Down Expand Up @@ -625,5 +628,6 @@ FROM (
WHERE geometry && bbox
AND zoom_level >= 13
) AS zoom_levels;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
5 changes: 4 additions & 1 deletion layers/building/building.sql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ FROM (
AND geometry && bbox
) AS zoom_levels
ORDER BY render_height ASC, ST_YMin(geometry) DESC;
$$ LANGUAGE SQL IMMUTABLE;
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE
;

-- not handled: where a building outline covers building parts
3 changes: 2 additions & 1 deletion layers/housenumber/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ SELECT
FROM osm_housenumber_point
WHERE zoom_level >= 14
AND geometry && bbox;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
4 changes: 3 additions & 1 deletion layers/landcover/landcover.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SELECT CASE
%%FIELD_MAPPING: class %%
END;
$$ LANGUAGE SQL IMMUTABLE
-- STRICT
PARALLEL SAFE;

-- etldoc: ne_110m_glaciated_areas -> landcover_z0
Expand Down Expand Up @@ -185,5 +186,6 @@ FROM (
WHERE zoom_level >= 14
AND geometry && bbox
) AS zoom_levels;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/landuse/landuse.sql
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,6 @@ FROM (
WHERE zoom_level >= 14
) AS zoom_levels
WHERE geometry && bbox;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/mountain_peak/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ WHERE zoom_level >= 7
AND (rank <= 5 OR zoom_level >= 14)
ORDER BY "rank" ASC;

$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
PARALLEL SAFE;
-- TODO: Check if the above can be made STRICT -- i.e. if pixel_width could be NULL
3 changes: 2 additions & 1 deletion layers/park/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,6 @@ FROM (
AND geometry_point && bbox
) AS park_point
) AS park_all;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
PARALLEL SAFE;
-- TODO: Check if the above can be made STRICT -- i.e. if pixel_width could be NULL
3 changes: 2 additions & 1 deletion layers/place/city.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ FROM (
OR (zoom_level BETWEEN 11 AND 12 AND (gridrank <= 14 OR "rank" IS NOT NULL))
OR (zoom_level >= 13)
) AS city_all;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/place/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ FROM (
FROM layer_city(bbox, zoom_level, pixel_width)
ORDER BY "rank" ASC
) AS place_all
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
PARALLEL SAFE;
-- TODO: Check if the above can be made STRICT -- i.e. if pixel_width could be NULL
3 changes: 2 additions & 1 deletion layers/poi/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ FROM (
AND zoom_level >= 14
) AS poi_union
ORDER BY "rank"
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
PARALLEL SAFE;
-- TODO: Check if the above can be made STRICT -- i.e. if pixel_width could be NULL
3 changes: 2 additions & 1 deletion layers/transportation/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -698,5 +698,6 @@ FROM (
) AS zoom_levels
WHERE geometry && bbox
ORDER BY z_order ASC;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/transportation_name/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,6 @@ FROM (
) AS zoom_levels
WHERE geometry && bbox
ORDER BY z_order ASC;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/water/water.sql
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,6 @@ FROM (
WHERE zoom_level >= 14
) AS zoom_levels
WHERE geometry && bbox;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/water_name/layer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ WHERE geometry && bbox
OR (zoom_level >= "rank" AND "rank" IS NOT NULL)
OR (zoom_level >= 8)
);
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;
3 changes: 2 additions & 1 deletion layers/waterway/waterway.sql
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,6 @@ FROM (
WHERE zoom_level >= 14
) AS zoom_levels
WHERE geometry && bbox;
$$ LANGUAGE SQL IMMUTABLE
$$ LANGUAGE SQL STABLE
-- STRICT
PARALLEL SAFE;

0 comments on commit 2b95d1c

Please sign in to comment.