Fix composite SRF (RETURNS TABLE) to correctly handle array returns#16
Merged
JerrySievert merged 1 commit intoplv8:mainfrom Mar 29, 2026
Merged
Conversation
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
0c02287 to
ba8e3cb
Compare
Member
|
thanks for the report and the PR! I was able to reproduce. |
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a crash (segfault) and incorrect behavior when a pljs function with
RETURNS TABLEorRETURNS SETOF compositereturns a JavaScript array of objects.Three bugs fixed in
call_srf_function(src/pljs.c):argv[0]instead ofret— The composite case read input arguments instead of the JS return value, causing all column values to be NULL.[{id:1, name:'a'}, {id:2, name:'b'}]were not iterated; only a single object was processed, leading to segfault.pfree(values)— Whenpljs_jsvalue_to_datums()returns NULL,pfree(NULL)crashes.Also fixed a minor memory leak: added
JS_FreeValuefor array elements in the non-composite scalar array case.Reproduction
Test plan
sql/function.sqlandexpected/function.out:RETURNS TABLEwith array of objectsRETURNS TABLEwith single object returnRETURNS SETOF recwith array returnDockerfile.testfor easy build & test:docker build -f Dockerfile.test -t pljs-test . && docker run --rm pljs-test