Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_agg with distinct works in postgres 9.4 but not in postgres 9.6

I have a query that use array_agg with distinct as an argument and is not accepted on postgres 9.6.

I created this sample to illustrate the issue:

create table numbers (id integer primary key, name varchar(10));
insert into numbers values(1,'one');
insert into numbers values(2,'two');

postgres 9.4

select array_agg(distinct(id)) from numbers;
 array_agg 
-----------
 {1,2}

postgres 9.6

ERROR:  function array_agg(integer) is not unique
LINE 1: select array_agg(distinct(id)) from numbers;
               ^
HINT:  Could not choose a best candidate function. 
You might need to add explicit type casts.

What do I need to change in order to get this result on postgres 9.6?

Thanks.

This is what I get checking the functions:

nspname | proname | proargtypes 
------------+-----------+--------------------- 
pg_catalog | array_agg | [0:0]={anyarray} 
public | array_agg | [0:0]={anyelement} 
pg_catalog | array_agg | [0:0]={anynonarray

Now, I found the issue thanks to the comment by pozs. I remove the public definition of the aggregated function and it worked.

The issue was just on the database that I was working on, as I found some people saying that the sample worked for them I created a new database an run the example. And then the only change there was the aggregate function definitions.

like image 579
Browser_80 Avatar asked Apr 20 '17 15:04

Browser_80


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.

Can we use aggregate function in where clause PostgreSQL?

Aggregate functions are not allowed because the WHERE clause is used for filtering data before aggregation. So while WHERE isn't for aggregation, it has other uses. To filter data based on an aggregate function result, you must use the HAVING clause.

What is Json_agg in PostgreSQL?

Now, we understand the types and usage of Creation & Processing Functions available in PostgreSQL. Let us also learn about Aggregate Function. json_agg() function: gathers all input values, including null values, into a JSON array. According to to_json, values are transformed to JSON. Syntax: json_agg ( anyelement )

What is unbounded preceding in PostgreSQL?

If frame_end is omitted, the end defaults to CURRENT ROW . A frame_start of UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly a frame_end of UNBOUNDED FOLLOWING means that the frame ends with the last row of the partition.


1 Answers

Now, I found the issue thanks to the comment by pozs. I remove the public definition of the aggregated function and it worked.

The issue was just on the database that I was working on, as I found some people saying that the sample worked for them I created a new database an run the example. And then the only change there was the aggregate function definitions.

So I drop the function public | array_agg | [0:0]={anyelement} and it worked.

Thanks a lot.

like image 71
Browser_80 Avatar answered Sep 16 '22 22:09

Browser_80