remote database queries

From: "Joe Conway" <joe(dot)conway(at)mail(dot)com>
To: <pgsql-interfaces(at)postgresql(dot)org>
Subject: remote database queries
Date: 2001-05-28 05:02:17
Message-ID: 002901c0e733$5e683590$0705a8c0@jecw2k1
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-interfaces

Hello all,

Inspired by others who have recently gotten PostgreSQL functions to return
sets, I set out to create my own. I have on more than one occasion wished I
could run a query across databases or servers (similar to a dblink in Oracle
or a linked db in MSSQL). Attached is a C file which is my attempt. It
exports two functions:

dblink(text connect_string, text sql, text field_separator)
dblink_tok(text delimited_text, text field_separator, int ord_position)

The functions are used as shown in the following example:

select
dblink_tok(t1.f1,'~',0)::int as vl_id
,dblink_tok(t1.f1,'~',1)::text as vl_guid
,dblink_tok(t1.f1,'~',2)::text as vl_pri_email
,dblink_tok(t1.f1,'~',3)::text as vl_acct_pass_phrase
,dblink_tok(t1.f1,'~',4)::text as vl_email_relname
,dblink_tok(t1.f1,'~',5)::text as vl_hwsn_relname
,dblink_tok(t1.f1,'~',6)::timestamp as vl_mod_dt
,dblink_tok(t1.f1,'~',7)::int as vl_status
from
(select dblink('host=192.168.5.150 port=5432 dbname=vsreg_001 user=postgres
password=postgres','select * from vs_lkup','~') as f1) as t1

By doing "create view vs_lkup_rm as . . ." with the above query, from a
database on another server, I can then write:
"select * from vs_lkup" and get results just as if I were on 192.168.5.150
(sort of -- see problem below).

I have one question, and one problem regarding this.

First the question: is there any way to get the dblink function to return
setof composite -- i.e. return tuples instead of scalar values? The
documentation indicates that a function can return a composite type, but my
attempts all seemed to produce only pointer values (to the tuples?)

Now the problem: as I stated above, "select * from vs_lkup" returns results
just as if I were on 192.168.5.150 -- but if I try "select * from vs_lkup
WHERE vl_id = 1" or "select * from vs_lkup WHERE vl_pri_email in
('email1(at)foo(dot)com')" I get the following error message: "ERROR: Set-valued
function called in context that cannot accept a set". Any ideas how to work
around this?

Thanks,

Joe Conway

Attachment Content-Type Size
dblink.h application/octet-stream 1.9 KB
dblink.c application/octet-stream 8.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zeugswetter Andreas SB 2001-05-28 06:59:48 AW: GiST index on data types that require compression
Previous Message Tatsuo Ishii 2001-05-28 04:36:23 Re: PQsetdbLogin bug?

Browse pgsql-interfaces by date

  From Date Subject
Next Message Alexaki Sofia 2001-05-28 08:42:19 jdbc: Batch updates-Prepared Statements
Previous Message pgsql-interfaces 2001-05-28 02:22:03 libpq++ in Apache Problem.