array_agg with distinct在Postgres 9.4中有效,但在Postgres 9.6中无效。

6

我有一个查询,使用了带有distinct参数的array_agg函数,在Postgres 9.6中无法被接受。

我创建了这个示例来说明这个问题:

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.

我需要在Postgres 9.6上做哪些改变才能获得这个结果?谢谢。

以下是检查函数时所得到的结果:

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

现在,我感谢pozs的评论,找到了问题所在。我删除了聚合函数的公共定义,问题得以解决。

问题只出现在我正在使用的数据库上。我发现有些人说示例对他们有效,于是我创建了一个新的数据库并运行了示例。然后唯一的变化就是聚合函数的定义。


2
无关的,但是:distinct 不是一个函数。distinct iddistinct (id) 是一样的。 - user330315
1
你的示例在我的9.6.2版本中可以运行:请参见此处:https://i.imgur.com/PgaAkRD.png - user330315
它在基于Linux的安装中也对我有效:https://i.imgur.com/C7Td0OV.png - user330315
1
也许是UDF干扰了?您可以通过列出所有array_agg命名函数来检查,例如使用此链接:http://rextester.com/MLHSEL68822。 - pozs
这是我在检查函数时得到的内容: nspname | proname | proargtypes
------------+-----------+--------------------- pg_catalog | array_agg | [0:0]={anyarray} public | array_agg | [0:0]={anyelement} pg_catalog | array_agg | [0:0]={anynonarray}
- Browser_80
显示剩余2条评论
2个回答

7
现在,我感谢pozs的评论,找到了问题所在。我删除了聚合函数的公共定义,然后它就起作用了。
问题只出现在我正在处理的数据库上,因为我发现有些人说样本对他们有效,所以我创建了一个新的数据库并运行了示例。然后唯一的变化就是聚合函数的定义。
所以我删除了函数public | array_agg | [0:0] = {anyelement},然后它就起作用了。
非常感谢。

我遇到了同样的问题。 你能解释一下这个函数是做什么的吗? 另外,你怎么删除这个函数? - Dvir Itzko


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接