Specify dblink column definition list from a local existing type

后端 未结 2 764
离开以前
离开以前 2021-01-11 13:11

I am using dblink to move certain data between databases. Everything is save and sound but I am wondering if there is a more convenient way to define the column definition l

相关标签:
2条回答
  • 2021-01-11 13:33

    Try something like this :

    select (rec).* from dblink('dbname=...','select myalias from foreign_table
      myalias') t1 (rec local_type)
    

    Example (get tables stats from other database) :

    select (rec).* from dblink('dbname=foreignDb','select t1 from
      pg_stat_all_tables t1') t2 (rec pg_stat_all_tables)
    
    0 讨论(0)
  • 2021-01-11 13:59

    You might need to make sure that your types are always in sync but this should work:

    SELECT (t1::test).* 
      FROM dblink('dbname=remote', 'select * from test') AS t1;
    

    The key is that often you need parentheses to ensure that the parser knows you are dealing with tuples.

    For example this works for me:

     CREATE TABLE test (id int, test bool);
     select (t1::test).* from (select 1, true) t1;
    

    But this throws a syntax error:

     select t1::test.* from (select 1, true) t1;
    
    0 讨论(0)
提交回复
热议问题