How to Create an Empty Array When Using the ARRAY Constructor

In PostgreSQL we can use the ARRAY constructor to create an array. When we do this, we provide the array elements as a comma separated list, enclosed in square brackets. Postgres then works out the data type based on the array elements.

But what if we want to create an empty array?

Creating an empty array can cause issues if we don’t explicitly specify the type. We need to specify the type. Below is an example of creating an empty array in PostgreSQL using the ARRAY constructor.

Continue reading

Fix “function generate_subscripts(text[], integer, integer) does not exist” in PostgreSQL

If you’re getting a PostgreSQL error that reads something like “function generate_subscripts(text[], integer, integer) does not exist“, it’s probably because your third argument is of the wrong type when using the generate_subscripts() function.

The above error specifically implies that an integer was passed as the third argument, but it must be a boolean value.

The third argument of the generate_subscripts() function is optional, but if passed, it must be a boolean expression.

To fix this error, either pass a boolean value as the third argument, or eliminate the third argument altogether.

Continue reading

Using the <@ Operator in PostgreSQL

In PostgreSQL, the <@ operator checks to see whether the second array contains the first array. That is, whether or not the array on the right of the operator contains all elements in the array to the left.

The function returns a Boolean result: It returns true if the second array contains the first array, and false if it doesn’t. If the result is unknown, it returns NULL.

Continue reading

How the @> Operator Works in PostgreSQL

In PostgreSQL, the @> operator checks to see whether the first array contains the second array. That is, whether or not the array on the left of the operator contains all elements in the array to the right.

The function returns a Boolean result: It returns true if the first array contains the second, and false if it doesn’t. If the result is unknown, it returns NULL.

Continue reading

Fix Error “could not determine polymorphic type because input has type unknown” when using generate_subscripts() in PostgreSQL

If you’re getting an error that reads “could not determine polymorphic type because input has type unknown” when using the generate_subscripts() function in PostgreSQL, it’s probably because your first argument is of the wrong type.

The generate_subscripts() function requires an array as its first argument. Passing a non-array value will result in an error. The actual error can vary, depending on the argument you pass, but regardless, the error is usually due to a non-array argument being passed.

To fix this error, be sure to pass an array as the argument when calling generate_subscripts().

Continue reading

A Quick Look at the && Operator in PostgreSQL

We can use the && operator in PostgreSQL to check two arrays for any overlapping elements.

We include an array on each side of the operator to compare them, and the result is a Boolean value that indicates whether or not there’s any overlap. A result of True (or t) indicates that there’s an overlap, while False (or f) indicates there’s no overlap.

Continue reading