add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Help with an interesting Postgres question: Why isn't an Index Only Scan used on a partition accessed via the parent table?.
CREATE OR REPLACE FUNCTION select_by_txt(z int, x int, y int, VARIADIC cols text[] = NULL, OUT res text)
LANGUAGE plpgsql AS
$func$
BEGIN
-- EXECUTE format(
SELECT format( -- show statement instead of executing
$$
SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom%s
FROM table1 t
JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
) mvtgeom
$$, (SELECT ', ' || string_agg(quote_ident (col), ', ') FROM unnest(cols) col)
)
INTO res
-- USING z, x, y
;
END
$func$;
CREATE FUNCTION
-- your example:
SELECT select_by_txt(10,32,33,'col1', 'col2');
select_by_txt

SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
   SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom, col1, col2
   FROM table1 t
   JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
          ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
   ) mvtgeom
SELECT 1
-- alternative syntax:
SELECT select_by_txt(10,32,33, VARIADIC '{col1,col2}');
select_by_txt

SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
   SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom, col1, col2
   FROM table1 t
   JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
          ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
   ) mvtgeom
SELECT 1
-- futile attempt on SQL injection:
SELECT select_by_txt(10,32,33,'col1', 'col2', $$col3'); DROP TABLE table1;--$$);
select_by_txt

SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
   SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom, col1, col2, "col3'); DROP TABLE table1;--"
   FROM table1 t
   JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
          ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
   ) mvtgeom
SELECT 1
-- single additional column
SELECT select_by_txt(10,32,33, 'col1');
select_by_txt

SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
   SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom, col1
   FROM table1 t
   JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
          ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
   ) mvtgeom
SELECT 1
-- no additional columns
SELECT select_by_txt(10,32,33);
select_by_txt

SELECT ST_AsMVT(mvtgeom, 'public.select_by_txt')
FROM (
   SELECT ST_AsMVTGeom(ST_Transform(t.geom, 3857), bounds.geom) AS geom
   FROM table1 t
   JOIN (SELECT ST_TileEnvelope($1, $2, $3)) AS bounds(geom)
          ON ST_Intersects(t.geom, ST_Transform(bounds.geom, 4326))
   ) mvtgeom
SELECT 1