I have a table of images and need to use a C image processing library to extract the n most relevant images from it. I try to figure out what parameters will be passed by prototyping the function in SQL (dummy):
CREATE OR REPLACE FUNCTION clusterize(anyarray,integer)
RETURNS TABLE(image bytea) AS $$
SELECT * FROM $1
ORDER BY random()
LIMIT $2
$$ LANGUAGE SQL; -- will be C
WITH subreq AS (
SELECT decode(encode(thumbnail,'escape'), 'base64') "data",
otherfields
FROM images -- which actually is another WITH subrequest ...
)
SELECT clusterize(* FROM subreq,12)
generates a syntax error close to the $1. How to pass "subreq" results to the function ? In this case, does it mean that all images will be loaded in memory before calling the function, or is there an iterator mechanism ?
Is it simpler to decompose in two functions, an "addimage" function to call for each image and then a "clusterize(12)" that would return a table of images ?

