Function Returning SETOF RECORD: Trouble With Char Type

From: "Angus B(dot) Atkins-Trimnell" <trimnell(at)uic(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Function Returning SETOF RECORD: Trouble With Char Type
Date: 2008-03-12 17:43:58
Message-ID: 47D8165E.2010403@uic.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I am having trouble with a function designed to return all column
constraints on a table. The problem is not in the SQL, which works fine
on its own. The problem comes when the function is invoked using SELECT
* FROM function_name(arg_name) AS temp_table(.....). When the function
does not contain either of the two char columns, it returns perfectly
well, but when either char column is added, it returns "ERROR: wrong
record type supplied in RETURN NEXT". I'm hoping this is something
obvious or know that I am missing. Working and non-working code are
below. Please pardon the complex query.

Thanks.

--Angus Atkins-Trimnell

Begin Working Code:
**********************************

CREATE OR REPLACE FUNCTION sql_get_schema_full(select_table text)
RETURNS SETOF RECORD AS $$
DECLARE
sql_result record;
BEGIN
FOR sql_result in EXECUTE
'SELECT t.relname, a.attname, c.conname, c.consrc, ft.relname,
fa.attname FROM (((pg_class AS t LEFT JOIN
(SELECT attname, attnum, attrelid FROM pg_attribute WHERE attnum>0)
AS a ON t.oid=a.attrelid) LEFT JOIN
pg_constraint AS c ON t.oid=c.conrelid AND a.attnum=ANY(c.conkey))
LEFT JOIN
pg_class AS ft ON ft.oid=c.confrelid) LEFT JOIN
(SELECT attname, attnum, attrelid FROM pg_attribute WHERE attnum>0)
AS fa ON ft.oid=fa.attrelid AND fa.attnum=ANY(c.confkey)
WHERE t.relname=''' || $1 || '''' LOOP
RETURN NEXT sql_result;
END LOOP;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

SELECT * FROM sql_get_schema_full('temp_visit') AS temp_schema(table1
name, column1 name, constname name, consttext text, table2 name, column2
name);

This works fine.
************************************
End Working Code

Begin Failing Code
************************************

CREATE OR REPLACE FUNCTION sql_get_schema_full(select_table text)
RETURNS SETOF RECORD AS $$
DECLARE
sql_result record;
BEGIN
FOR sql_result in EXECUTE
'SELECT t.relname, a.attname, c.conname, c.contype, c.consrc,
ft.relname, fa.attname FROM (((pg_class AS t LEFT JOIN (SELECT attname,
attnum, attrelid FROM pg_attribute WHERE attnum>0) AS a ON
t.oid=a.attrelid) LEFT JOIN
pg_constraint AS c ON t.oid=c.conrelid AND a.attnum=ANY(c.conkey))
LEFT JOIN
pg_class AS ft ON ft.oid=c.confrelid) LEFT JOIN
(SELECT attname, attnum, attrelid FROM pg_attribute WHERE attnum>0)
AS fa ON ft.oid=fa.attrelid AND fa.attnum=ANY(c.confkey)
WHERE t.relname=''' || $1 || '''' LOOP
RETURN NEXT sql_result;
END LOOP;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

SELECT * FROM sql_get_schema_full('temp_visit') AS temp_schema(table1
name, column1 name, constname name,
consttype char, consttext text, table2 name, column2 name);

ERROR: wrong record type supplied in RETURN NEXT
CONTEXT: PL/pgSQL function "sql_get_schema_full" line 11 at return next

**********************************
End Failing Code

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lee Hachadoorian 2008-03-12 17:49:59 PostgreSQL won't start
Previous Message Ivan Sergio Borgonovo 2008-03-12 17:31:26 Re: postgre vs MySQL