title: PostgreSQL: Accessing fields of record types
body: <p>I have a function that returns a record</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION atPeriod(
      IN segment segmentST,
      IN period period,
      OUT n integer,
      OUT s segmentST,
      OUT p pointST)
    RETURNS record AS
  '$libdir/Hermes', 'atPeriodSegmentSTV1'
    LANGUAGE c STABLE STRICT
    COST 1;
  </code></pre>
  
  <p>I call that function in another function</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION distance(seg1 segmentST, seg2 segmentST) RETURNS realTS AS
  $BODY$
  DECLARE 
      projseg1 record;
      projseg2 record;
  BEGIN
      SELECT atPeriod(seg1,p) INTO projseg1; 
      SELECT atPeriod(seg2,p) INTO projseg2; 
      IF (projseg1).n = 2 AND (projseg2).n = 2 THEN
          ...
      END IF;
  END;
  $BODY$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>When I execute the second function I receive the error</p>
  
  <pre><code>********** Error **********
  
  ERROR: record "projseg1" has no field "n"
  SQL state: 42703
  </code></pre>
  
  <p>If instead I call the first function in a SQL statement</p>
  
  <pre><code>WITH Rec AS (
  select 2 as seg_id, atPeriod(SegmentST(
  pointst('2012-01-01 08:00:00', 1, 2),
  pointst('2012-01-01 08:02:00', 2, 2)),
  period('2012-01-01 08:00:00', '2012-01-01 08:01:00')
  ) as seg
  )
  Select (seg).n from rec
  </code></pre>
  
  <p>I receive the value 2 and no error.</p>
  
  <p>Any hint how to solve this issue ?</p>