I am trying to return a custom type from a PostgreSQL function as follows:
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);
I have two problems with this.
one). My results are returned as a single column like "(" 1753-01-01 12:00:00 "," 2009-11-11 03:45:00 "), where I need them, return to the two columns.
Solved! - Stupid mistake ... It must be SELECT * FROM GaugeSummary_GetDateRangeForGauge (123)
2) The results are the maximum and minimum values ββfrom the entire table - the WHERE constraint is not used.
Example:
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"
But calling the function gives me: "1753-01-01 12:00:00", "2009-11-11 03:45:00"
Thanks!
Answer for 2:
This same query seems to be running inside the "LANGUAGE" SQL "STABLE"; function works fine:
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;
However, it would be nice to know why the plpgsql function does not work correctly ....
source share