title: Using pgsql_fdw in a function result "ERROR: cache lookup failed for type 0"
body: <p>We want to use pgsql_fdw to select a table of remote postgresql database. When we
  select the table in a session it is okay, but when we use the foreign table in a funciton 
  it turns out "ERROR:  cache lookup failed for type 0". Does anybody know why?</p>
  
  <h2>1. base information</h2>
  
  <pre><code>skytf=&gt; \d ft_test;
         Foreign table "skytf.ft_test"
   Column |         Type          | Modifiers 
  --------+-----------------------+-----------
   id     | integer               | 
   name   | character varying(32) | 
  Server: pgsql_srv
  
  
  skytf=&gt; \des+ pgsql_srv
                                                  List of foreign servers
     Name    | Owner | Foreign-data wrapper | Access privileges | Type | Version |                Options                 
  -----------+-------+----------------------+-------------------+------+---------+----------------------------------------
   pgsql_srv | skytf | pgsql_fdw            |                   |      |         | {host=127.0.0.1,port=1923,dbname=mydb}
  (1 row)
  </code></pre>
  
  <h2>2. destination table</h2>
  
  <pre><code>mydb=&gt; \d test
               Table "mydb.test"
   Column |         Type          | Modifiers 
  --------+-----------------------+-----------
   id     | integer               | 
   name   | character varying(32) | 
  Indexes:
      "idx_test_1" btree (id)
  </code></pre>
  
  <h2>3. function</h2>
  
  <pre><code> CREATE or replace FUNCTION  func_sync_bill() RETURNS INTEGER  AS $$
      BEGIN
  
       begin
        insert into test_tf (id,name) select id,name from ft_test;
        return 1;
       end; 
      END;
    $$ LANGUAGE 'plpgsql';
  </code></pre>
  
  <h2>4. it works in a session</h2>
  
  <pre><code>skytf=&gt; create table test_tf(id integer,name varchar(32));
  CREATE TABLE
  
  skytf=&gt; insert into test_tf select * from ft_test;
  INSERT 0 1990000
  </code></pre>
  
  <h2>5. function call error</h2>
  
  <pre><code>skytf=&gt; truncate table test_tf;
  TRUNCATE TABLE
  
  skytf=&gt; select func_sync_bill();
  ERROR:  cache lookup failed for type 0
  CONTEXT:  SQL statement "insert into test_tf (id,name) select id,name from ft_test"
  PL/pgSQL function "func_sync_bill" line 5 at SQL statement
  </code></pre>
  
  <p>When I call the function func_sync_bill() which will select a foreign table, it generates the the error.</p>
  
  <p>Is this a bug of pgsql_fdw?</p>