When I connect to PostgreSQL database with pgAdmin III (1.22.2), I get this error:
ERROR: function array_agg(text) is not unique
LINE 5: (SELECT array_agg(label) FROM pg_shseclabel sl1 WHERE sl1.ob...
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.
The database server is PostgreSQL 9.6.
Same instance of pgAdmin works fine with databases running PostgreSQL 9.0 versions.
How can I fix this problem?
PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array.
CREATE OR REPLACE FUNCTION contact_countries_array(INT) RETURNS ANYARRAY AS ' SELECT ARRAY[contacts_primarycountry, contacts_othercountry] FROM contacts WHERE contacts_id = $1' LANGUAGE SQL; The data type of contacts_primarycountry and contacts_othercountry is integer. contacts_id is unique and integer.
The elements of the array can be retrieved using the SELECT statement. The values of the array column can be enclosed within square brackets [] or curly braces {}. We can search for array column values using the ANY() function.
I found that problem is caused by migration from 9.0 to 9.6.
Log in to the database using psql and run \df array_agg
. This is how it should look in 9.0:
db=# \df array_agg
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+-----------+------------------+---------------------+------
pg_catalog | array_agg | anyarray | anyelement | agg
(1 row)
and in 9.6:
db=# \df array_agg
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+-----------+------------------+---------------------+------
pg_catalog | array_agg | anyarray | anyarray | agg
pg_catalog | array_agg | anyarray | anynonarray | agg
(2 rows)
However, when I ran \df array_agg
on my server, I get:
db=> \df array_agg
Lista funkcji
Schemat | Nazwa | Typ danych wyniku | Typy danych argumentów | Typ
------------+-----------+-------------------+------------------------+-------
pg_catalog | array_agg | anyarray | anyarray | agreg
pg_catalog | array_agg | anyarray | anynonarray | agreg
public | array_agg | anyarray | anyelement | agreg
(3 rows)
As mentioned here, it can be fixed by:
DROP AGGREGATE public.array_agg(anyelement);
In my case, explicitly typecasting the argument to either TEXT
or VARCHAR
works.
E.g.,
SELECT array_agg('Group1');
Results in:
ERROR: function array_agg(unknown) is not unique LINE 1: SELECT array_agg('Group1'); ^ HINT: Could not choose a best candidate function. You might need to add explicit type casts. SQL state: 42725 Character: 8
However, both of
SELECT array_agg('Group1'::VARCHAR);
and
SELECT array_agg('Group1'::TEXT);
work as expected and return the respective TEXT
or VARCHAR
array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With