Skip to content

Fix composite SRF (RETURNS TABLE) to correctly handle array returns#16

Merged
JerrySievert merged 1 commit intoplv8:mainfrom
wenerme:fix/composite-srf-array-return
Mar 29, 2026
Merged

Fix composite SRF (RETURNS TABLE) to correctly handle array returns#16
JerrySievert merged 1 commit intoplv8:mainfrom
wenerme:fix/composite-srf-array-return

Conversation

@wenerme
Copy link
Copy Markdown
Contributor

@wenerme wenerme commented Mar 4, 2026

Summary

Fixes a crash (segfault) and incorrect behavior when a pljs function with RETURNS TABLE or RETURNS SETOF composite returns a JavaScript array of objects.

Three bugs fixed in call_srf_function (src/pljs.c):

  1. Used argv[0] instead of ret — The composite case read input arguments instead of the JS return value, causing all column values to be NULL.
  2. No array-of-objects handling — Functions returning arrays like [{id:1, name:'a'}, {id:2, name:'b'}] were not iterated; only a single object was processed, leading to segfault.
  3. Missing NULL check before pfree(values) — When pljs_jsvalue_to_datums() returns NULL, pfree(NULL) crashes.

Also fixed a minor memory leak: added JS_FreeValue for array elements in the non-composite scalar array case.

Reproduction

-- This crashes PostgreSQL (segfault) on current main:
CREATE FUNCTION test_srf() RETURNS TABLE(id int, name text) AS $$
  return [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
$$ LANGUAGE pljs;
SELECT * FROM test_srf();

Test plan

  • Added 3 regression tests to sql/function.sql and expected/function.out:
    • RETURNS TABLE with array of objects
    • RETURNS TABLE with single object return
    • RETURNS SETOF rec with array return
  • All 20 existing regression tests continue to pass
  • Added Dockerfile.test for easy build & test: docker build -f Dockerfile.test -t pljs-test . && docker run --rm pljs-test

The composite case in call_srf_function had three bugs:

1. Used argv[0] (input argument) instead of ret (JS return value),
   causing all column values to be NULL.
2. Did not handle array-of-objects return for RETURNS TABLE/SETOF,
   only processing a single object. JS functions returning arrays
   like [{id:1, name:'a'}, {id:2, name:'b'}] would crash (segfault).
3. Missing NULL check on pfree(values) when pljs_jsvalue_to_datums
   returns NULL.

Also fixed a minor memory leak: added JS_FreeValue for array elements
in the non-composite scalar array case.

Added regression tests for:
- RETURNS TABLE with array of objects
- RETURNS TABLE with single object return
- RETURNS SETOF composite with array return
@wenerme wenerme force-pushed the fix/composite-srf-array-return branch from 0c02287 to ba8e3cb Compare March 4, 2026 10:27
@JerrySievert
Copy link
Copy Markdown
Member

thanks for the report and the PR!

I was able to reproduce.

@JerrySievert JerrySievert merged commit 2410d90 into plv8:main Mar 29, 2026
5 checks passed
JerrySievert pushed a commit that referenced this pull request Mar 29, 2026
)

The composite case in call_srf_function had three bugs:

1. Used argv[0] (input argument) instead of ret (JS return value),
   causing all column values to be NULL.
2. Did not handle array-of-objects return for RETURNS TABLE/SETOF,
   only processing a single object. JS functions returning arrays
   like [{id:1, name:'a'}, {id:2, name:'b'}] would crash (segfault).
3. Missing NULL check on pfree(values) when pljs_jsvalue_to_datums
   returns NULL.

Also fixed a minor memory leak: added JS_FreeValue for array elements
in the non-composite scalar array case.

Added regression tests for:
- RETURNS TABLE with array of objects
- RETURNS TABLE with single object return
- RETURNS SETOF composite with array return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants