以下SQL语句返回单行(JSON字符串)
Select jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT json_build_object(
'type', 'Feature',
'properties', json_build_object(
'cell', cell,
'value', value
),
'geometry', ST_AsGeoJSON(geom)::json
) as feature
from (
select dgg.*, trunc(random() * 9 + 1) as value
FROM mj_dgg_regions_BB($1, $2, $2, $3, $4) as dgg) row
) features
如果我将此语句放在函数中(见下文),则生成的JSON字符串不完整
CREATE OR REPLACE FUNCTION mj_dgg_regions_BB_geojson(res int, xmin float, ymin float, xmax float, ymax float)
RETURNS json AS $$
begin
return(
Select jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(feature)
)
FROM (
SELECT json_build_object(
'type', 'Feature',
'properties', json_build_object(
'cell', cell,
'value', value
),
'geometry', ST_AsGeoJSON(geom)::json
) as feature
from (
select dgg.*, trunc(random() * 9 + 1) as value
FROM mj_dgg_regions_BB($1, $2, $2, $3, $4) as dgg) row
) features
);
end;
$$ LANGUAGE plpgsql;
结果又是包含JSON字符串的单行。但是,此字符串不包含第一个语句(无函数)包含的所有条目。
由于没有错误信息,我有点迷失。
编辑1:
为了排除psycopg2和DBeaver的问题,我尝试使用psql
psql -U USER -d DATABASE -c "copy (select mj_dgg_regions_BB_geojson(4, 10, 10, 60, 60)) to stdout;"
返回相同的截断结果。单独调用语句会返回正确的(全长)JSON字符串,而调用该函数则不会。因此,我的猜测是该功能不正确。
编辑2:
mj_dgg_regions_bb()本质上是一个空间查询。
CREATE OR REPLACE FUNCTION public.mj_dgg_regions_bb(res integer, xmin double precision, xmax double precision, ymin double precision, ymax double precision)
RETURNS TABLE(cell bigint, geom geometry)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT dgg.cell, dgg.geom::geometry FROM regions as dgg
where dgg.res = $1
and
dgg.geom::geometry &&
-- && -- intersects
-- @ -- contained by
ST_MakeEnvelope (
$2, $3, -- bounding
$4, $5, -- box limits
4326);
END;
$function$
功能是否可能超时?空间查询可能需要几秒钟(设置空间索引)。