If you’re getting an error that reads “function array_sample(numeric, integer) does not exist” when using the array_sample()
function in PostgreSQL, it’s probably because your first argument is a numeric type instead of an array.
The first argument for this function must be an array.
To fix this error, be sure that your first argument to array_sample()
is an array, not a numeric value.
Example of Error
Here’s an example of code that produces the error:
SELECT array_sample( 1.5, 2 );
Result:
ERROR: function array_sample(numeric, integer) does not exist
LINE 1: SELECT array_sample( 1.5, 2 );
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Here I passed a numeric value as the first argument to array_sample()
but it doesn’t accept numeric values for the first argument. The first argument must be an array of any type.
Solution
We can fix the problem by ensuring that the first argument is an array:
SELECT array_sample( ARRAY[ 1.5, 2.5, 3.5 ], 2 );
Result:
array_sample
--------------
{2.5,1.5}
This time the function worked without error.