PostgreSQL documents it's Set Returning Functions with C extensions like this,

C-language functions have two options for returning sets (multiple rows).

  • In one method, called ValuePerCall mode, a set-returning function is called repeatedly (passing the same arguments each time) and it returns one new row on each call, until it has no more rows to return and signals that by returning NULL. The set-returning function (SRF) must therefore save enough state across calls to remember what it was doing and return the correct next item on each call.
  • In the other method, called Materialize mode, an SRF fills and returns a tuplestore object containing its entire result; then only one call occurs for the whole result, and no inter-call state is needed.

If you're optimizing for execution speed, which is faster ValuePerCall or Materialize mode? Obviously Materialize mode will take more memory but it seems like it should faster if your goal is to return a whole set, I don't see that documented anywhere though.

These seem to be documented in the code as Set Returning Function Mode SFRM_ValuePerCall and SFRM_Materialize.

1 Answer 1


I benchmarked this pretty thoroughly with my projects. Here is what I found,

  • For resultsets fits in work_mem Materialize in a FROM clause.
  • For large sets it is better to use ValuePerCell in a SELECT list, and for sets with a LIMIT.

You can see my benchmarks in my repo pg-srf-repeat-benchmark

There is also SFRM_Materialize_Preferred which allows you to write a function that accepts a hint from the caller and does both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.