Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1F8E9475B33 for ; Fri, 23 Aug 2002 02:07:53 -0400 (EDT) Received: from joeconway.com (unknown [63.210.180.150]) by postgresql.org (Postfix) with ESMTP id 7CE9B4759F5 for ; Fri, 23 Aug 2002 02:07:52 -0400 (EDT) Received: from [192.168.5.2] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 3.5.9) with ESMTP-TLS id 1311383; Thu, 22 Aug 2002 22:55:04 -0700 Message-ID: <3D65D107.9070307@joeconway.com> Date: Thu, 22 Aug 2002 23:07:03 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bhuvan A Cc: pgsql-admin@postgresql.org Subject: Re: Preserving datatypes in dblink. References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/270 X-Sequence-Number: 5384 Bhuvan A wrote: > Hi, > > I am using postgresql 7.2.1. > > I am using dblink function in order to execute remote queries. I did this > by creating a view (as suggested by README.dblink). Here i found a strange > thing that the datatype of all the fields of this view is set to text, > irrespect of the source datatype. > > Here is an example. > > # \c db1 > # \d my_table > Table "my_table" > Column | Type | Modifiers > --------+--------------------------+--------------- > key | text | > value | text | > ctime | timestamp with time zone | default now() > mtime | timestamp with time zone | > > # \c db2 > # CREATE VIEW dbl_my_view AS SELECT dblink_tok(t.ptr, 0) AS key, > dblink_tok(t.ptr, 1) AS value, dblink_tok(t.ptr, 2) AS ctime, > dblink_tok(t.ptr, 3) AS mtime FROM (SELECT dblink('hostaddr=192.168.1.15 > port=5432 dbname=db1 user=my_user password=my_pass', 'select key, value, > ctime, mtime from my_table') AS ptr) t; > CREATE > # \d dbl_my_view > View "dbl_my_view" > Column | Type | Modifiers > --------+------+----------- > key | text | > value | text | > ctime | text | > mtime | text | > View definition: SELECT dblink_tok(t.ptr, 0) AS "key", dblink_tok(t.ptr, > 1) AS value, dblink_tok(t.ptr, 2) AS ctime, dblink_tok(t.ptr, 3) AS mtime > FROM (SELECT dblink('hostaddr=192.168.1.15 port=5432 dbname=db1 > user=my_user password=my_pass', 'select key, value, ctime, mtime from > my_table'::text) AS ptr) t; > That's because dblink_tok is declared to return text (it *has* to be declared to return something, so text is the best choice). Explicitly cast the columns in you view to whatever datatype is correct. I.e. (untested) CREATE VIEW dbl_my_view AS SELECT dblink_tok(t.ptr, 0) AS "key", dblink_tok(t.ptr,1) AS value, dblink_tok(t.ptr, 2)::timestamp with time zone AS ctime, dblink_tok(t.ptr, 3)::timestamp with time zone AS mtime FROM (SELECT dblink('hostaddr=192.168.1.15 port=5432 dbname=db1 user=my_user password=my_pass', 'select key, value, ctime, mtime from my_table'::text) AS ptr) t; Joe