title: Postgresql 9.x: return custom type from a function
body: <p>I'm trying to return a Custom type from a PostgreSQL function as follows:</p>
  
  <pre><code>CREATE TYPE myType as 
  ( 
      "Name" character varying
      ,"Surname" Text
   );
  
  CREATE OR REPLACE FUNCTION public.xml_to_type(inputXml xml)
    RETURNS SETOF myType AS
  $BODY$
  DECLARE
      xml_content myType;
  BEGIN
  
      FOR xml_content in 
          SELECT 
          (xpath('//Name/text()',testColumn))[1]::text::character varying as "Name"
          ,(xpath('//Surname/text()',testColumn))[1]::text::text as "Surname"
          FROM unnest(xpath('//myNodes/node',inputXml)) AS t(testColumn)
      LOOP
  
      RETURN NEXT xml_content;
      END LOOP;
  
  END;
  $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  </code></pre>
  
  <p>From PostgreSQL query tool it works:</p>
  
  <pre><code>SELECT * FROM xml_to_type('&lt;myNodes&gt;
          &lt;node&gt;
              &lt;Name&gt;Alex&lt;/Name&gt;
              &lt;Surname&gt;Red&lt;/Surname&gt;
          &lt;/node&gt;
          &lt;node&gt;
              &lt;Name&gt;Tony&lt;/Name&gt;
              &lt;Surname&gt;Stark&lt;/Surname&gt;
          &lt;/node&gt;
      &lt;/myNodes&gt;'::xml);
  </code></pre>
  
  <p>and returns correctly 2 records. The problem is when I call this function from another function:</p>
  
  <pre><code>    DECLARE myTypeVar myType[];
      SELECT * FROM xml_to_type(xmlExample) INTO myTypeVar;
  </code></pre>
  
  <p>I get this exception:</p>
  
  <pre><code>ERROR:  malformed array literal: "Alex"
  DETAIL:  Array value must start with "{" or dimension information.
  </code></pre>
  
  <p>Any idea?</p>