If you’re getting an error that reads “function array_shuffle(integer, integer) does not exist” when using the array_shuffle()
function in PostgreSQL, it’s probably because the argument you’re passing to the function is not an array.
Actually, the error message implies that you’re passing multiple integer values (two in this case, but it could be more), separated by a comma. Perhaps you’re trying to pass an array of integers, but you haven’t provided them in an array. Or perhaps you’re passing the wrong column or variable. Either way, the argument for this function must be an array.
To fix this error, replace the integer values with a single array value when calling the array_shuffle()
function.
Example of Error
Here’s an example of code that produces the error:
SELECT array_shuffle( 1,5 );
Result:
ERROR: function array_shuffle(integer, integer) does not exist
LINE 1: SELECT array_shuffle( 1,5 );
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Here I passed two integers to the array_shuffle()
function but it doesn’t accept such arguments. It only accepts a single array of any type.
We’d get a similar error if we passed more integers:
SELECT array_shuffle( 1,2,3,4,5 );
Result:
ERROR: function array_shuffle(integer, integer, integer, integer, integer) does not exist
LINE 1: SELECT array_shuffle( 1,2,3,4,5 );
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The only difference is the number of times the word integer
is repeated in the error message (i.e. it reflects how many integers we passed to the function).
The same error can occur even if not all arguments are integers. For example, the following example includes a numeric type:
SELECT array_shuffle( 1,2.5 );
Result:
ERROR: function array_shuffle(integer, numeric) does not exist
LINE 1: SELECT array_shuffle( 1,2.5 );
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Solution
To fix the problem, simply pass an array instead of an integer/numeric value:
SELECT array_shuffle( ARRAY[ 1,2,3,4,5 ] );
Sample result:
array_shuffle
---------------
{2,4,1,3,5}
This time the function worked without error.