Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5af4855

Browse files
committed
Allow remote query execution (dblink)
Joe Conway
1 parent b33c662 commit 5af4855

File tree

6 files changed

+560
-0
lines changed

6 files changed

+560
-0
lines changed

contrib/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ dbase -
3838
Converts from dbase/xbase to PostgreSQL
3939
by Ivan Baldo, lubaldo@adinet.com.uy
4040

41+
dblink -
42+
Allows remote query execution
43+
by Joe Conway, joe.conway@mail.com
44+
4145
earthdistance -
4246
Operator for computing earth distance for two points
4347
by Hal Snyder <hal@vailsys.com>

contrib/dblink/Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
subdir = contrib/dblink
2+
top_builddir = ../..
3+
include $(top_builddir)/src/Makefile.global
4+
include $(top_builddir)/src/Makefile.shlib
5+
include_srcdir = $(top_builddir)/src/include
6+
7+
8+
NAME := dblink
9+
SONAME := $(NAME)$(DLSUFFIX)
10+
11+
override CFLAGS += -I$(srcdir)
12+
override CFLAGS += -I$(include_srcdir)
13+
override CFLAGS += -I$(libpq_srcdir)
14+
override CFLAGS += $(CFLAGS_SL)
15+
16+
OBJS = $(NAME).o
17+
18+
all: $(OBJS) $(SONAME) $(NAME).sql
19+
20+
$(OBJS): $(NAME).c
21+
$(CC) -o $@ -c $(CFLAGS) $<
22+
23+
$(SONAME): $(OBJS)
24+
$(LD) -o $@ -Bshareable $(libpq) $<
25+
26+
$(NAME).sql: $(NAME).sql.in
27+
sed -e 's:MODULE_PATHNAME:$(libdir)/contrib/$(SONAME):g' < $< > $@
28+
29+
install: all installdirs
30+
$(INSTALL_DATA) README.$(NAME) $(docdir)/contrib
31+
$(INSTALL_DATA) $(NAME).sql $(datadir)/contrib
32+
$(INSTALL_SHLIB) $(SONAME) $(libdir)/contrib
33+
34+
installdirs:
35+
$(mkinstalldirs) $(docdir)/contrib $(datadir)/contrib $(libdir)/contrib
36+
37+
uninstall:
38+
rm -rf $(docdir)/contrib/README.$(NAME) $(datadir)/contrib/$(NAME).sql $(libdir)/contrib/$(SONAME)
39+
40+
clean distclean maintainer-clean:
41+
rm -f $(SONAME) *.o *.sql
42+
43+
depend dep:
44+
$(CC) -MM -MG $(CFLAGS) *.c > depend
45+
46+
ifeq (depend,$(wildcard depend))
47+
include depend
48+
endif

contrib/dblink/README.dblink

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* dblink
3+
*
4+
* Functions returning results from a remote database
5+
*
6+
* Copyright (c) Joseph Conway <joe.conway@mail.com>, 2001;
7+
*
8+
* Permission to use, copy, modify, and distribute this software and its
9+
* documentation for any purpose, without fee, and without a written agreement
10+
* is hereby granted, provided that the above copyright notice and this
11+
* paragraph and the following two paragraphs appear in all copies.
12+
*
13+
* IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
14+
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
15+
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
16+
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
17+
* POSSIBILITY OF SUCH DAMAGE.
18+
*
19+
* THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
20+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21+
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22+
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
23+
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24+
*
25+
*/
26+
27+
Version 0.2 (29 May, 2001):
28+
Function to test returning data set from remote database
29+
Tested under Linux (Red Hat 6.2 and 7.0) and PostgreSQL 7.1 and 7.2devel
30+
31+
Release Notes:
32+
33+
Version 0.2
34+
- initial release
35+
36+
Installation:
37+
Place these files in a directory called 'dblink' under 'contrib' in the PostgreSQL source tree. Then run:
38+
39+
make
40+
make install
41+
42+
You can use dblink.sql to create the functions in your database of choice, e.g.
43+
44+
psql -U postgres template1 < dblink.sql
45+
46+
installs following functions into database template1:
47+
48+
dblink() - returns a pointer to results from remote query
49+
dblink_tok() - extracts and returns individual field results
50+
51+
Documentation
52+
==================================================================
53+
Name
54+
55+
dblink -- Returns a pointer to a data set from a remote database
56+
57+
Synopsis
58+
59+
dblink(text connstr, text sql)
60+
61+
Inputs
62+
63+
connstr
64+
65+
standard libpq format connection srting,
66+
e.g. "hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd"
67+
68+
sql
69+
70+
sql statement that you wish to execute on the remote host
71+
e.g. "select * from pg_class"
72+
73+
Outputs
74+
75+
Returns setof int (pointer)
76+
77+
Example usage
78+
79+
select dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd','select f1, f2 from mytable');
80+
81+
82+
==================================================================
83+
84+
Name
85+
86+
dblink_tok -- Returns individual select field results from a dblink remote query
87+
88+
Synopsis
89+
90+
dblink_tok(int pointer, int fnumber)
91+
92+
Inputs
93+
94+
pointer
95+
96+
a pointer returned by a call to dblink()
97+
98+
fnumber
99+
100+
the ordinal position (zero based) of the field to be returned from the dblink result set
101+
102+
Outputs
103+
104+
Returns text
105+
106+
Example usage
107+
108+
select dblink_tok(t1.dblink_p,0) as f1, dblink_tok(t1.dblink_p,1) as f2
109+
from (select dblink('hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
110+
,'select f1, f2 from mytable') as dblink_p) as t1;
111+
112+
113+
==================================================================
114+
115+
NOTE: If you need to provide selection criteria in a WHERE clause, it is necessary
116+
to 'fake' a UNION, e.g.
117+
118+
select
119+
dblink_tok(t1.dblink_p,0) as f1
120+
,dblink_tok(t1.dblink_p,1) as f2
121+
from
122+
(
123+
select dblink(
124+
'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
125+
,'select f1, f2 from mytable'
126+
) as dblink_p
127+
union
128+
select null,null where false
129+
) as t1
130+
where
131+
f1 = 'mycriteria';
132+
133+
in order to work around an issue with the query optimizer. A more convenient way to approach
134+
this problem is to create a view:
135+
136+
create view myremotetable as
137+
select
138+
dblink_tok(t1.dblink_p,0) as f1
139+
,dblink_tok(t1.dblink_p,1) as f2
140+
from
141+
(
142+
select dblink(
143+
'hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswd'
144+
,'select f1, f2 from mytable'
145+
) as dblink_p
146+
union
147+
select null,null where false
148+
) as t1;
149+
150+
Then you can simply write:
151+
152+
select f1,f2 from myremotetable where f1 = 'mycriteria';
153+
154+
==================================================================
155+
156+
-- Joe Conway
157+

0 commit comments

Comments
 (0)