The plpgsql function:
CREATE OR REPLACE FUNCTION testarray (int[]) returns int as $$
DECLARE
len int;
BEGIN
len := array_upper($1);
return len;
END
$$ language plpgsql;
The node-postgres query + test array:
var ta = [1,2,3,4,5];
client.query('SELECT testarray($1)', [ta], function(err, result) {
console.log('err: ' + err);
console.log('result: ' + result);
});
Output from node server:
err: error: array value must start with "{" or dimension information
result: undefined
I also tried cast the parameter in the client query like testarray($1::int[])
which returned the same error.
I changed the function argument to (anyarray int)
and the output error changed:
err: error: invalid input syntax for integer: "1,2,3,4,5"
result: undefined
As well as a couple other variations.
I seek the variation that produces no error and returns 5.
I read about the Postgres parse-array issue and this stackoverflow question on parameterised arrays in node-postgres:
But the answer didn't seem to be there.
The parameter has to be in one of these forms:
Also, you can simplify your function:
Or a simple SQL function:
Or just use
array_length($1, 1)
directly.array_upper()
is the wrong function. Arrays can have arbitrary subscripts.array_length()
does what you are looking for. Related question:Both
array_length()
andarray_upper()
require two parameters. The second is the array dimension -1
in your case.thanks to the responses from PinnyM and Erwin. I reviewed the options and reread related answers.
the array formats described by Erwin work in node-postgres literally as follows:
the tl:dr of javascript quoting
to parameterize them in node-postgres: (based on this answer)
Based on the answer you posted, this might work for you: