This is the how the function definition looks:
CREATE OR REPLACE FUNCTION my_function(foo NUMERIC)
RETURNS SETOF my_type AS $$
DECLARE rec record;
BEGIN
FOR rec IN SELECT * FROM my_table WHERE my_table.bar > foo
LOOP
RETURN QUERY SELECT
my_scalar_function(rec.bar1),
my_scalar_function(rec.bar2),
my_scalar_function(rec.bar3);
END LOOP;
END;
$$ LANGUAGE plpgsql;
On the client i am trying to use a QueryStream from the pg-query-stream package:
pool.connect((err, client, done) => {
if (err) throw err
const query = new QueryStream('SELECT * FROM my_function($1);', [123])
const stream = client.query(query)
stream.on('end', ()=>{
console.log('finished')
done()
process.exit(0)
})
stream.pipe(JSONStream.stringify()).pipe(process.stdout)
});
But still i am getting all rows at the end of the function and no intermediate rows. I would like to stream every row when it is calculated in the my_function.
const query = new QueryStream('SELECT * FROM generate_series(0, $1) num', [1000000])