从Postgresql函数返回自定义类型

时间:2010-01-11 20:44:50

标签: postgresql types rowtype

我正在尝试从PostgreSQL函数返回一个Custom类型,如下所示:


DROP TYPE IF EXISTS GaugeSummary_GetDateRangeForGauge_Type CASCADE; -- Drop our previous type

CREATE TYPE GaugeSummary_GetDateRangeForGauge_Type AS   -- Recreate our type    
(
    Minimum timestamp without time zone,
    Maximum timestamp without time zone
);


CREATE OR REPLACE FUNCTION GaugeSummary_GetDateRangeForGauge
(
    GaugeID integer
)
RETURNS GaugeSummary_GetDateRangeForGauge_Type AS 
$$
DECLARE
    iGaugeID ALIAS FOR $1;
    oResult     GaugeSummary_GetDateRangeForGauge_Type%ROWTYPE;
BEGIN
    SELECT INTO oResult
        min(ArchivedMinimumTime) as Minimum, 
        max(TelemeteredMaximumTime) as Maximum 
    FROM GaugeSummary 
    WHERE GaugeID = $1;

    RETURN oResult;
END;
$$ LANGUAGE plpgsql;

SELECT GaugeSummary_GetDateRangeForGauge(2291308);

我遇到了两个问题。

1) - 我的结果以“(”1753-01-01 12:00:00“,”2009-11-11 03:45:00“)”的形式返回单列,我需要它们回到两栏。

解决! - 愚蠢的错误...... 它应该是SELECT * FROM GaugeSummary_GetDateRangeForGauge(123)

2)结果是整个表的最大值和最小值 - 未使用WHERE约束。

示例:

GaugeSummaryID  GaugeID   ArchivedMinimumTime   TelemeteredMaximumTime
80               4491   "2009-03-28 12:00:00"   "2009-06-27 12:00:00"
81               4491   "2009-03-28 12:00:00"   "2009-06-27 12:00:00"

但是对函数的调用给了我: “1753-01-01 12:00:00”,“2009-11-11 03:45:00”

谢谢!

回答2:

似乎在“LANGUAGE'SQL'STABLE;”中运行同一个查询功能正常:

CREATE OR REPLACE FUNCTION GaugeSummary_GetDateRangeForGauge
(
    GaugeID integer
)
RETURNS GaugeSummary_GetDateRangeForGauge_Type AS
$$
    SELECT min(ArchivedMinimumTime) as Minimum, 
        max(TelemeteredMaximumTime) as Maximum 
    FROM GaugeSummary WHERE GaugeID = $1;
$$ LANGUAGE 'SQL' STABLE;

但是,知道为什么plpgsql函数无法正常工作会很好....

1 个答案:

答案 0 :(得分:8)

我尝试了这个,在做

时我得到两列
SELECT * GaugeSummary_GetDateRangeForGauge(1);  

结果:

aadb=# select * from GaugeSummary_GetDateRangeForGauge(1);
      minimum               |          maximum
----------------------------+----------------------------
 2010-01-11 15:14:20.649786 | 2010-01-11 15:14:24.745783
(1 row)

我正在使用8.4并在psql中运行它。你能澄清一下如何得到你的结果吗?

对于#2,如果您只想要结果,则从查询中删除min()和max()聚合函数。删除这些将确保将在与您的ID匹配的行上返回这些列的结果。

更新:好的我不确定那是怎么回事。我只是将所有类似的东西放入我的测试数据库中,并按照我的预期进行工作。

自定义类型

create type custom_type as ( 
   minimum timestamp without time zone, 
   maximum timestamp without time zone);

表(测试)

aadb=# select * from test order by id;
 id |             a              |             b
----+----------------------------+----------------------------
  1 | 2010-01-11 17:09:52.329779 | 2010-01-11 17:09:52.329779
  1 | 2010-01-11 17:10:04.729776 | 2010-01-11 17:10:04.729776
  2 | 2010-01-11 17:09:55.049781 | 2010-01-11 17:10:21.753781
  2 | 2010-01-11 17:10:30.501781 | 2010-01-11 17:10:30.501781
  3 | 2010-01-11 17:09:58.289772 | 2010-01-11 17:09:58.289772
  3 | 2010-01-11 17:35:38.089853 | 2010-01-11 17:35:38.089853
(6 rows)

功能

create or replace function maxmin (pid integer) returns custom_type as $$ 
declare  
  oResult custom_type%rowtype; 
begin 
  select into oResult min(a) as minimum, max(b) as maximum 
  from test where id = pid; 

  return oResult; 
end; 
$$ language plpgsql;

结果

aadb=# select * from maxmin(2);
          minimum           |          maximum
----------------------------+----------------------------
 2010-01-11 17:09:55.049781 | 2010-01-11 17:10:30.501781
(1 row)