A Quick Look at PostgreSQL’s REGEXP_COUNT() Function

In PostgreSQL, the regexp_count() function returns the number of times a given POSIX regular expression pattern matches in a given string.

We pass the string and pattern as arguments. We can also pass an argument to specify where to start the search. Additionally, we also have the option of specifying a flag that changes the function’s behaviour.

Continue reading

Fix “source array too small” When Updating an Array in PostgreSQL

If you’re getting an error that reads “source array too small” when updating an array in PostgreSQL, it’s probably because you’re passing a slice that’s smaller than your specified range. For example, if you specify a range that covers four elements but provide an array that contains just three elements, then you’d get this error.

To fix the error, be sure to provide an an array that contains at least as many elements as are specified in the range.

Continue reading

Fix “function array_shuffle(integer, integer) does not exist” in PostgreSQL

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.

Continue reading

Difference Between ARRAY_POSITIONS() and ARRAY_POSITION() in PostgreSQL

PostgreSQL has a couple of functions that enable us to get the position or positions of a given value in an array. One of them – array_position() – returns the position of just the first occurrence (as an integer), while the other function – array_positions() – returns the position of all occurrences (as an array).

So these functions are aptly named – one is singular and the other is plural.

But this isn’t the only difference. The array_position() function allows us to specify a start position, while array_positions() doesn’t.

Continue reading

Fix Error “op ANY/ALL (array) requires array on right side” in PostgreSQL

If you’re getting a PostgreSQL error that reads “op ANY/ALL (array) requires array on right side” it’s probably because you’re passing a non array to the ANY() or ALL() construct.

These constructs require an array to be included in the parentheses, and so passing a non array returns an error.

To fix this issue, be sure to pass an array when using these functions.

Continue reading