Fix “sample size must be between…” Error when using array_sample() in PostgreSQL

If you’re getting an error that starts with “sample size must be between…” when using the array_sample() function in PostgreSQL, it’s probably because your second argument is either a negative value, or it’s larger than the number of elements in the array.

The second argument must be no less than zero, and it can’t be any larger than the number of elements in the array.

To fix this error, be sure that your second argument is greater than zero and less than or equal to the number of elements in the array at the first argument.

Continue reading