CREATE OR REPLACE FUNCTION BBox(x integer, y integer, zoom integer) RETURNS geometry AS $BODY$ DECLARE max numeric := 6378137 * pi(); res numeric := max * 2 / 2^zoom; bbox geometry; BEGIN return ST_MakeEnvelope( -max + (x * res), max - (y * res), -max + (x * res) + res, max - (y * res) - res, 3857); END; $BODY$ LANGUAGE plpgsql IMMUTABLE;