title: Postgres Create View With Record Type Function
body: <p>I have this complex query with a user defined function <code>conta_relatos()</code> running very well as a select statement. But it doesn't work when I try to create a view with the same instructions. Postgres is telling me that the column "conta_relatos" has pseudo-type record. This function, <code>conta_relatos()</code> returns a record type variable.<br>
  <sub>Addition by Editor:</sub><br>
  The return type is a well known composite type as defined in the preceding question:<br>
  <a href="https://stackoverflow.com/questions/15030722/postgres-function-end-lop-and-return-error">Postgres Function End Loop and return Error</a></p>
  
  <p>Below is the query:</p>
  
  <pre><code>CREATE OR REPLACE VIEW "Sumario" AS 
  SELECT "Aspectos"."ID" AS "Aspecto Normativo ID", 
      "Aspectos"."Aspecto" AS "Aspecto Normativo", 
      "Fatores"."ID" AS "Fator Normativo ID", 
      "Fatores"."Fator" AS "Fator Normativo", "Diagnostico"."Vinculo_Final", 
      "Fatores_1"."ID" AS "Fator Determinativo ID", 
      "Fatores_1"."Fator" AS "Fator Determinativo", 
      "Aspectos_1"."ID" AS "Aspecto Determinativo ID", 
      "Aspectos_1"."Aspecto" AS "Aspecto Determinativo", 
      count("Itens"."ID") AS "No Itens", 
      conta_relatos("Fatores"."ID", "Fatores_1"."ID")
  FROM  
      "Diagnostico"
  JOIN ("Aspectos" "Aspectos_1"
  JOIN ("Fontes"
  JOIN "Itens" ON "Fontes"."ID" = "Itens"."Fonte"
  JOIN ("Fatores" "Fatores_1"
  JOIN ("Aspectos"
  JOIN ("Vinculos"
  JOIN "Fatores" ON "Vinculos"."Fator_Normativo" = "Fatores"."ID") ON "Aspectos"."ID" = "Fatores"."Aspecto" AND "Aspectos"."ID" = "Fatores"."Aspecto") ON "Fatores_1"."ID" = "Vinculos"."Fator_Determinativo") ON "Itens"."ID" = "Vinculos"."Item") ON "Aspectos_1"."ID" = "Fatores_1"."Aspecto") ON "Diagnostico"."ID" = "Vinculos"."Diagnostico_ID"
  GROUP BY "Aspectos"."ID", "Aspectos"."Aspecto", "Fatores"."ID", "Fatores"."Fator", "Diagnostico"."Vinculo_Final", "Fatores_1"."ID", "Fatores_1"."Fator", "Aspectos_1"."ID", "Aspectos_1"."Aspecto"
  ORDER BY "Aspectos"."ID", "Aspectos_1"."ID", "Fatores"."Fator", "Fatores_1"."Fator";
  </code></pre>