Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function array_agg(text) is not unique when connecting with pgAdmin III

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?

like image 569
Daniel Gadawski Avatar asked Dec 15 '17 14:12

Daniel Gadawski


People also ask

What is Array_agg in Postgres?

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.

How do I return an array in PostgreSQL?

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.

Which of the following will be used to search data in the array in PostgreSQL?

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.


2 Answers

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);
like image 137
Daniel Gadawski Avatar answered Sep 19 '22 12:09

Daniel Gadawski


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.

like image 44
buzz Avatar answered Sep 18 '22 12:09

buzz