错误:函数 unnest(integer[]) 在 postgresql 中不存在 [英] ERROR: function unnest(integer[]) does not exist in postgresql

查看:207
本文介绍了错误:函数 unnest(integer[]) 在 postgresql 中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT UNNEST(ARRAY[1,2,3,4])

在执行上述查询时,我收到如下错误:

While executing the above query I got the error like this:

ERROR: function unnest(integer[]) does not exist in postgresql.

我使用的是 PostgreSQL 8.3 并且我已经在我的数据库中安装了 _int.sql 包用于整数数组操作.

I am using PostgreSQL 8.3 and I have installed the _int.sql package in my db for integer array operation.

如何解决这个错误?

推荐答案

unnest() 不是模块 intarray 的一部分,而是标准 PostgreSQL 的一部分.但是,您需要8.4版本或以后.

unnest() is not part of the module intarray, but of standard PostgreSQL. However, you need version 8.4 or later for that.

因此,您可以通过升级到更新的版本(最好是当前版本 9.1)来解决此问题.请参阅PostgreSQL 项目的版本控制政策.

So you can resolve this by upgrading to a more recent version, preferably the current version 9.1. See the versioning policy of the PostgreSQL project.

如果您应该使用 Heroku 的共享数据库,目前使用的是 8.3 版,他们也在考虑升级.Heroku Labs 已经提供 9.1.

If you should be using Heroku's shared database, which currently uses version 8.3, they are looking into upgrading, too. Heroku Labs already offers 9.1.

正如@Abdul 所评论的,您可以自己在 PostgreSQL 8.4 之前的版本中实现穷人的 unnest():

As @Abdul commented, you can implement a poor man's unnest() in versions before PostgreSQL 8.4 yourself:

CREATE OR REPLACE FUNCTION unnest(anyarray)
  RETURNS SETOF anyelement AS
$BODY$
   SELECT $1[i] FROM generate_series(array_lower($1,1), array_upper($1,1)) i;
$BODY$ LANGUAGE sql IMMUTABLE;

但是,请注意这仅适用于一维数组.(与 PostgreSQL 的 unnest() 不同,后者采用多维数组):

However, be aware that this only works for one-dimensional arrays. (As opposed to PostgreSQL's unnest() which takes arrays with multiple dimensions):

SELECT unnest('{1,2,3,4}'::int[])  -- works
SELECT unnest('{{1,2},{3,4},{5,6}}'::int[])  -- fails! (returns all NULLs)

可以为 n 维数组实现更多函数:

You could implement more functions for n-dimensional arrays:

CREATE OR REPLACE FUNCTION unnest2(anyarray) -- for 2-dimensional arrays
  RETURNS SETOF anyelement AS
$BODY$
SELECT $1[i][j]
FROM  (
    SELECT i, generate_series(array_lower($1,2), array_upper($1,2)) j
    FROM  (
        SELECT generate_series(array_lower($1,1), array_upper($1,1)) i
        ) x
    ) y;
$BODY$ LANGUAGE sql IMMUTABLE;

调用:

SELECT unnest2('{{1,2},{3,4},{5,6}}'::int[])  -- works!

您还可以编写一个处理多个维度的 PL/pgSQL 函数......

You could also write a PL/pgSQL function that deals with multiple dimensions ...

这篇关于错误:函数 unnest(integer[]) 在 postgresql 中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆